Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8fd6869c97ef051b6f68e5e90e5cb28ba477a776 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.internal.tcf.services.local;

import java.math.BigDecimal;
import java.util.Map;

import org.eclipse.tm.internal.tcf.core.Token;
import org.eclipse.tm.tcf.core.Command;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IErrorReport;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.IDiagnostics;


public class DiagnosticsService implements IDiagnostics {

    private final IChannel channel;

    private class CommandServer implements IChannel.ICommandServer {

        public void command(IToken token, String name, byte[] data) {
            try {
                command(token, name, JSON.parseSequence(data));
            }
            catch (Throwable x) {
                channel.terminate(x);
            }
        }

        private void command(IToken token, String name, Object[] args) throws Exception {
            if (name.equals("echo")) {
                if (args.length != 1) throw new Exception("Invalid number of arguments");
                String s = (String)args[0];
                channel.sendResult(token, JSON.toJSONSequence(new Object[]{ s }));
            }
            else if (name.equals("echoFP")) {
                if (args.length != 1) throw new Exception("Invalid number of arguments");
                Number n = (Number)args[0];
                channel.sendResult(token, JSON.toJSONSequence(new Object[]{ n }));
            }
            else if (name.equals("echoERR")) {
                if (args.length != 1) throw new Exception("Invalid number of arguments");
                @SuppressWarnings("unchecked")
                Map<String,Object> err = (Map<String,Object>)args[0];
                channel.sendResult(token, JSON.toJSONSequence(new Object[]{ err, Command.toErrorString(err) }));
            }
            else if (name.equals("getTestList")) {
                if (args.length != 0) throw new Exception("Invalid number of arguments");
                channel.sendResult(token, JSON.toJSONSequence(new Object[]{ null, new String[0] }));
            }
            else {
                channel.rejectCommand(token);
            }
        }
    }

    public DiagnosticsService(IChannel channel) {
        this.channel = channel;
        channel.addCommandServer(this, new CommandServer());
    }

    public String getName() {
        return NAME;
    }

    public IToken echo(final String s, final DoneEcho done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneEcho(token, null, s);
            }
        });
        return token;
    }

    public IToken echoFP(final BigDecimal n, final DoneEchoFP done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneEchoFP(token, null, n);
            }
        });
        return token;
    }

    public IToken echoERR(final Throwable err, final DoneEchoERR done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                if (err instanceof IErrorReport) {
                    done.doneEchoERR(token, null, err, Command.toErrorString(((IErrorReport)err).getAttributes()));
                }
                else {
                    done.doneEchoERR(token, null, err, err.getMessage());
                }
            }
        });
        return token;
    }

    public IToken getTestList(final DoneGetTestList done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneGetTestList(token, null, new String[0]);
            }
        });
        return token;
    }

    public IToken runTest(final String s, final DoneRunTest done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneRunTest(token, new Exception("Test suite not found: " + s), null);
            }
        });
        return token;
    }

    public IToken cancelTest(String context_id, final DoneCancelTest done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneCancelTest(token, null);
            }
        });
        return token;
    }

    public IToken getSymbol(String context_id, String symbol_name, final DoneGetSymbol done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneGetSymbol(token, new Exception("Invalid context"), null);
            }
        });
        return token;
    }

    public IToken createTestStreams(int inp_buf_size, int out_buf_size, final DoneCreateTestStreams done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneCreateTestStreams(token, new Exception("Not implemented"), null, null);
            }
        });
        return token;
    }

    public IToken disposeTestStream(String id, final DoneDisposeTestStream done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneDisposeTestStream(token, new Exception("Invalid context"));
            }
        });
        return token;
    }

    public IToken not_implemented_command(final DoneNotImplementedCommand done) {
        final IToken token = new Token();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                done.doneNotImplementedCommand(token, new Exception("Not implemented"));
            }
        });
        return token;
    }
}

Back to the top