Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a7a07eda765bb6c3911c05b5f94adaacb313e022 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*******************************************************************************
 * Copyright (c) 2007 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 com.windriver.debug.tcf.core.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.Launch;

import com.windriver.debug.tcf.core.TCFCore;
import com.windriver.debug.tcf.core.launch.TCFLaunchDelegate;
import com.windriver.tcf.api.protocol.IChannel;
import com.windriver.tcf.api.protocol.IPeer;
import com.windriver.tcf.api.protocol.IService;
import com.windriver.tcf.api.protocol.IToken;
import com.windriver.tcf.api.protocol.Protocol;
import com.windriver.tcf.api.services.IProcesses;
import com.windriver.tcf.api.services.IProcesses.ProcessContext;

public class TCFLaunch extends Launch {

    public interface Listener {

        public void onConnected(TCFLaunch launch);	

        public void onDisconnected(TCFLaunch launch);

    }

    public interface TerminateListener {

        public boolean canTerminate();

        public boolean isTerminated();

        public void terminate(Runnable done);
    }

    private static final Collection<Listener> listeners = new ArrayList<Listener>();

    private IChannel channel;
    private Throwable error;
    private TerminateListener terminate_listener;
    private TCFBreakpointsStatus breakpoints_status;
    private String mode;
    private boolean connecting;
    private ProcessContext process;

    public TCFLaunch(ILaunchConfiguration launchConfiguration, String mode) {
        super(launchConfiguration, mode, null);
    }

    private void onConnected() {
        try {
            final Runnable done = new Runnable() {
                public void run() {
                    connecting = false;
                    for (Listener l : listeners) l.onConnected(TCFLaunch.this);
                    fireChanged();
                }
            };
            if (mode.equals(ILaunchManager.DEBUG_MODE)) {
                TCFCore.getBreakpointsModel().downloadBreakpoints(channel, new Runnable() {
                    public void run() {
                        if (channel.getState() != IChannel.STATE_OPEN) return;
                        breakpoints_status = new TCFBreakpointsStatus(TCFLaunch.this);
                        downloadApplication(done);
                    }
                });
            }
            else {
                downloadApplication(done);
            }
        }
        catch (Exception x) {
            channel.terminate(x);
        }
    }
    
    @SuppressWarnings("unchecked")
    protected void downloadApplication(final Runnable done) {
        try {
            ILaunchConfiguration cfg = getLaunchConfiguration();
            final String file = cfg.getAttribute(TCFLaunchDelegate.ATTR_PROGRAM_FILE, "");
            if (file.length() == 0) {
                Protocol.invokeLater(done);
                return;
            }
            final String dir = cfg.getAttribute(TCFLaunchDelegate.ATTR_WORKING_DIRECTORY, "");
            final String args = cfg.getAttribute(TCFLaunchDelegate.ATTR_PROGRAM_ARGUMENTS, "");
            final Map<String,String> env = cfg.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map)null);
            final boolean append = cfg.getAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true);
            final boolean attach = mode.equals(ILaunchManager.DEBUG_MODE);
            final IProcesses ps = channel.getRemoteService(IProcesses.class);
            if (ps == null) throw new Exception("Target does not provide Processes service");
            IProcesses.DoneGetEnvironment done_env = new IProcesses.DoneGetEnvironment() {
                public void doneGetEnvironment(IToken token, Exception error, Map<String,String> def) {
                    if (error != null) {
                        channel.terminate(error);
                        return;
                    }
                    Map<String,String> vars = new HashMap<String,String>();
                    if (append) vars.putAll(def);
                    if (env != null) vars.putAll(env);
                    ps.start(dir, file, toArgsArray(args), vars, attach, new IProcesses.DoneStart() {
                        public void doneStart(IToken token, Exception error, ProcessContext process) {
                            if (error != null) {
                                channel.terminate(error);
                                return;
                            }
                            TCFLaunch.this.process = process;
                            Protocol.invokeLater(done);
                        }
                    });
                }
            };
            if (append) ps.getEnvironment(done_env);
            else done_env.doneGetEnvironment(null, null, null);
        }
        catch (Exception x) {
            channel.terminate(x);
        }
    }
    
    private String[] toArgsArray(String cmd) {
        int i = 0;
        int l = cmd.length();
        List<String> arr = new ArrayList<String>();
        for (;;) {
            while (i < l && cmd.charAt(i) == ' ') i++;
            if (i >= l) break;
            String s = null;
            if (cmd.charAt(i) == '"') {
                i++;
                StringBuffer bf = new StringBuffer();
                while (i < l) {
                    char ch = cmd.charAt(i++);
                    if (ch == '"') break;
                    if (ch == '\\' && i < l) ch = cmd.charAt(i++);
                    bf.append(ch);
                }
                s = bf.toString();
            }
            else {
                int i0 = i;
                while (i < l && cmd.charAt(i) != ' ') i++;
                s = cmd.substring(i0, i);
            }
            arr.add(s);
        }
        return arr.toArray(new String[arr.size()]);
    }
    
    private void onDisconnected(Throwable error) {
        this.error = error;
        breakpoints_status = null;
        connecting = false;
        for (Iterator<Listener> i = listeners.iterator(); i.hasNext();) {
            i.next().onDisconnected(this);
        }
        if (DebugPlugin.getDefault() != null) fireTerminate();
    }

    /*--------------------------------------------------------------------------------------------*/

    public Throwable getError() {
        return error;
    }
    
    public void setError(Throwable x) {
        if (error != null) return;
        error = x;
        if (channel != null && channel.getState() == IChannel.STATE_OPEN) {
            channel.terminate(x);
        }
        fireChanged();
    }
    
    public TCFBreakpointsStatus getBreakpointsStatus() {
        return breakpoints_status;
    }

    public static void addListener(Listener listener) {
        assert Protocol.isDispatchThread();
        listeners.add(listener);
    }

    public static void removeListener(Listener listener) {
        assert Protocol.isDispatchThread();
        listeners.remove(listener);
    }

    public IChannel getChannel() {
        assert Protocol.isDispatchThread();
        if (channel == null || channel.getState() != IChannel.STATE_OPEN) return null;
        return channel;
    }
    
    public IProcesses.ProcessContext getProcessContext() {
        return process;
    }
    
    public boolean isConnecting() {
        return connecting;
    }

    public IPeer getPeer() {
        assert Protocol.isDispatchThread();
        return channel.getRemotePeer();
    }

    public <V extends IService> V getService(Class<V> cls) {
        assert Protocol.isDispatchThread();
        return channel.getRemoteService(cls);
    }

    public boolean canTerminate() {
        final boolean res[] = new boolean[1];
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                if (terminate_listener == null) res[0] = false;
                else res[0] = terminate_listener.canTerminate();
            }
        });
        return res[0];
    }

    public boolean isTerminated() {
        final boolean res[] = new boolean[1];
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                if (channel == null || channel.getState() == IChannel.STATE_CLOSED) res[0] = true;
                else if (terminate_listener == null) res[0] = false;
                else res[0] = terminate_listener.isTerminated();
            }
        });
        return res[0];
    }

    public void terminate() {
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                if (terminate_listener == null) return;
                terminate_listener.terminate(new Runnable() {
                    public void run() {
                        fireTerminate();
                    }
                });
            }
        });
    }

    public void terminate(Runnable done) {
        if (terminate_listener == null) done.run();
        else terminate_listener.terminate(done);
    }

    public boolean canDisconnect() {
        final boolean res[] = new boolean[1];
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                res[0] = channel != null && channel.getState() != IChannel.STATE_CLOSED;
            }
        });
        return res[0];
    }

    public boolean isDisconnected() {
        final boolean res[] = new boolean[1];
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                res[0] = channel == null || channel.getState() == IChannel.STATE_CLOSED;
            }
        });
        return res[0];
    }
    
    public void disconnect() throws DebugException {
        Protocol.invokeAndWait(new Runnable() {
            public void run() {
                if (channel != null && channel.getState() != IChannel.STATE_CLOSED) {
                    channel.close();
                }
                fireTerminate();
            }
        });
    }
    
    public void launchTCF(String mode, IPeer peer, TerminateListener terminate_listener) throws DebugException {
        assert Protocol.isDispatchThread();
        this.mode = mode;
        this.terminate_listener = terminate_listener;
        connecting = true;
        channel = peer.openChannel();
        channel.addChannelListener(new IChannel.IChannelListener() {

            public void onChannelOpened() {
                onConnected();
            }

            public void congestionLevel(int level) {
            }

            public void onChannelClosed(Throwable error) {
                channel.removeChannelListener(this);
                onDisconnected(error);
            }

        });
        assert channel.getState() == IChannel.STATE_OPENNING; 
    }
}

Back to the top