Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fae411ab5967d3ccd878c37ca8bfc1e26ed31fdd (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
/*******************************************************************************
 * Copyright (c) 2014 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.tcf.internal.debug.ui.model;

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

import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchesListener;
import org.eclipse.debug.core.Launch;
import org.eclipse.tcf.internal.debug.model.TCFLaunch;
import org.eclipse.tcf.internal.debug.ui.Activator;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchListener;
import org.eclipse.ui.PlatformUI;

/**
 * TCFModelManager listens debug launch manager and creates TCF debug models when necessary.
 */
public class TCFModelManager {

    private static final Map<TCFLaunch,TCFModel> sync_model_map =
            Collections.synchronizedMap(new HashMap<TCFLaunch,TCFModel>());

    public interface ModelManagerListener {
        public void onConnected(TCFLaunch launch, TCFModel model);
        public void onDisconnected(TCFLaunch launch, TCFModel model);
    }

    private final Map<TCFLaunch,TCFModel> models = new HashMap<TCFLaunch,TCFModel>();
    private final List<ModelManagerListener> listeners = new ArrayList<ModelManagerListener>();

    private final TCFLaunch.LaunchListener tcf_launch_listener = new TCFLaunch.LaunchListener() {

        public void onCreated(TCFLaunch launch) {
            assert Protocol.isDispatchThread();
            assert models.get(launch) == null;
            TCFModel model = new TCFModel(launch);
            models.put(launch, model);
            sync_model_map.put(launch, model);
        }

        public void onConnected(TCFLaunch launch) {
            assert Protocol.isDispatchThread();
            TCFModel model = models.get(launch);
            if (model != null) model.onConnected();
            for (ModelManagerListener l : listeners) {
                try {
                    l.onConnected(launch, model);
                }
                catch (Throwable x) {
                    Activator.log(x);
                }
            }
        }

        public void onDisconnected(TCFLaunch launch) {
            assert Protocol.isDispatchThread();
            TCFModel model = models.get(launch);
            if (model != null) model.onDisconnected();
            for (ModelManagerListener l : listeners) {
                try {
                    l.onDisconnected(launch, model);
                }
                catch (Throwable x) {
                    Activator.log(x);
                }
            }
        }

        public void onProcessOutput(TCFLaunch launch, String process_id, int stream_id, byte[] data) {
            assert Protocol.isDispatchThread();
            TCFModel model = models.get(launch);
            if (model != null) model.onProcessOutput(process_id, stream_id, data);
        }

        public void onProcessStreamError(TCFLaunch launch, String process_id,
                int stream_id, Exception error, int lost_size) {
            assert Protocol.isDispatchThread();
            TCFModel model = models.get(launch);
            if (model != null) model.onProcessStreamError(process_id, stream_id, error, lost_size);
        }
    };

    private final ILaunchesListener debug_launch_listener = new ILaunchesListener() {

        public void launchesAdded(final ILaunch[] launches) {
        }

        public void launchesChanged(final ILaunch[] launches) {
            Protocol.invokeAndWait(new Runnable() {
                public void run() {
                    for (ILaunch launch : launches) {
                        TCFModel model = models.get(launch);
                        if (model != null) model.launchChanged();
                    }
                }
            });
        }

        public void launchesRemoved(final ILaunch[] launches) {
            Protocol.invokeAndWait(new Runnable() {
                public void run() {
                    for (ILaunch launch : launches) {
                        TCFModel model = models.remove(launch);
                        if (model != null) {
                            sync_model_map.remove(launch);
                            model.dispose();
                        }
                    }
                }
            });
        }
    };

    private final IWorkbenchListener workbench_listener = new IWorkbenchListener() {

        @Override
        public boolean preShutdown(IWorkbench workbench, boolean forced) {
            for (ILaunch launch : DebugPlugin.getDefault().getLaunchManager().getLaunches()) {
                if (launch instanceof TCFLaunch) {
                    try {
                        ((TCFLaunch)launch).disconnect();
                        DebugPlugin.getDefault().getLaunchManager().removeLaunch(launch);
                    }
                    catch (Exception x) {
                        Activator.log("Cannot disconnect TCF launch", x);
                    }
                }
            }
            TCFMemoryBlockRetrieval.onWorkbenchShutdown();
            return true;
        }

        @Override
        public void postShutdown(IWorkbench workbench) {
        }
    };

    public TCFModelManager() {
        assert Protocol.isDispatchThread();
        try {
            PlatformUI.getWorkbench().addWorkbenchListener(workbench_listener);
        } catch (IllegalStateException e) {
            // In headless environments the plug-in load can be still triggered.
            // Should not trigger an "Unhandled exception in TCF event dispatch thread"
        }
        DebugPlugin.getDefault().getLaunchManager().addLaunchListener(debug_launch_listener);
        TCFLaunch.addListener(tcf_launch_listener);
    }

    public void dispose() {
        assert Protocol.isDispatchThread();
        try {
            PlatformUI.getWorkbench().removeWorkbenchListener(workbench_listener);
        } catch (IllegalStateException e) {
            // In headless environments the plug-in load can be still triggered.
            // Should not trigger an "Unhandled exception in TCF event dispatch thread"
        }
        DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(debug_launch_listener);
        TCFLaunch.removeListener(tcf_launch_listener);
        for (Iterator<TCFModel> i = models.values().iterator(); i.hasNext();) {
            TCFModel model = i.next();
            sync_model_map.remove(model.getLaunch());
            model.dispose();
            i.remove();
        }
        assert models.isEmpty();
    }

    public void addListener(ModelManagerListener l) {
        listeners.add(l);
    }

    public void removeListener(ModelManagerListener l) {
        listeners.remove(l);
    }

    public Collection<TCFModel> getModels() {
        assert Protocol.isDispatchThread();
        return models.values();
    }

    public TCFModel getModel(TCFLaunch launch) {
        assert Protocol.isDispatchThread();
        return models.get(launch);
    }

    public TCFNodeLaunch getRootNode(TCFLaunch launch) {
        TCFModel model = getModel(launch);
        if (model == null) return null;
        return model.getRootNode();
    }

    public static TCFModelManager getModelManager() {
        return Activator.getModelManager();
    }

    /**
     * Synchronized and thread-safe method to map a launch to TCFModel.
     */
    public static TCFModel getModelSync(Launch launch) {
        if (launch instanceof TCFLaunch) return sync_model_map.get((TCFLaunch)launch);
        return null;
    }

    /**
     * Synchronized and thread-safe method to map a launch to TCFNodeLaunch.
     */
    public static TCFNodeLaunch getRootNodeSync(Launch launch) {
        if (launch instanceof TCFLaunch) {
            TCFModel model = sync_model_map.get((TCFLaunch)launch);
            if (model != null) return model.getRootNode();
        }
        return null;
    }
}

Back to the top