Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bf73c19382ad49fe87a154100a6a3ca4fdb6cdb2 (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
/*******************************************************************************
 * Copyright (c) 2007, 2011 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.debug.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.IBreakpoints;


public class TCFBreakpointsStatus {

    private final IBreakpoints service;
    private final Map<String,Map<String,Object>> breakpoints = new HashMap<String,Map<String,Object>>();
    private final Map<String,Map<String,Object>> status = new HashMap<String,Map<String,Object>>();
    private final Set<ITCFBreakpointListener> listeners = new HashSet<ITCFBreakpointListener>();

    private static final Map<String,Object> status_not_supported = new HashMap<String,Object>();

    static {
        status_not_supported.put(IBreakpoints.STATUS_ERROR, "Not supported");
    }

    TCFBreakpointsStatus(TCFLaunch launch) {
        assert Protocol.isDispatchThread();
        service = launch.getChannel().getRemoteService(IBreakpoints.class);
        if (service != null) {
            final IBreakpoints.BreakpointsListener listener = new IBreakpoints.BreakpointsListener() {

                public void breakpointStatusChanged(String id, Map<String,Object> m) {
                    assert Protocol.isDispatchThread();
                    if (status.get(id) == null) return;
                    status.put(id, m);
                    for (Iterator<ITCFBreakpointListener> i = listeners.iterator(); i.hasNext();) {
                        i.next().breakpointStatusChanged(id);
                    }
                }

                public void contextAdded(Map<String,Object>[] bps) {
                    for (Map<String,Object> bp : bps) {
                        String id = (String)bp.get(IBreakpoints.PROP_ID);
                        breakpoints.put(id, bp);
                        if (status.get(id) != null) continue;
                        status.put(id, new HashMap<String,Object>());
                        for (Iterator<ITCFBreakpointListener> i = listeners.iterator(); i.hasNext();) {
                            i.next().breakpointStatusChanged(id);
                        }
                    }
                }

                public void contextChanged(Map<String,Object>[] bps) {
                    for (Map<String,Object> bp : bps) {
                        String id = (String)bp.get(IBreakpoints.PROP_ID);
                        breakpoints.put(id, bp);
                        if (!status.containsKey(id)) continue;
                        for (Iterator<ITCFBreakpointListener> i = listeners.iterator(); i.hasNext();) {
                            i.next().breakpointChanged(id);
                        }
                    }
                }

                public void contextRemoved(String[] ids) {
                    for (String id : ids) {
                        breakpoints.remove(id);
                        if (!status.containsKey(id)) continue;
                        for (Iterator<ITCFBreakpointListener> i = listeners.iterator(); i.hasNext();) {
                            i.next().breakpointRemoved(id);
                        }
                        status.remove(id);
                    }
                }
            };
            service.addListener(listener);

            // query foreign breakpoints
            service.getIDs(new IBreakpoints.DoneGetIDs() {
                @SuppressWarnings("unchecked")
                public void doneGetIDs(IToken token, Exception error, String[] ids) {
                    if (error != null) return;
                    for (final String id : ids) {
                        service.getProperties(id, new IBreakpoints.DoneGetProperties() {
                            public void doneGetProperties(IToken token, Exception error, Map<String, Object> props) {
                                if (error == null) {
                                    listener.contextAdded((Map<String, Object>[]) new Map[] { props });
                                    service.getStatus(id, new IBreakpoints.DoneGetStatus() {
                                        public void doneGetStatus(IToken token, Exception error, Map<String, Object> status) {
                                            if (error == null) listener.breakpointStatusChanged(id, status);
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            });
        }
    }

    public Set<String> getStatusIDs() {
        return status.keySet();
    }

    public Map<String,Object> getStatus(String id) {
        assert id != null;
        assert Protocol.isDispatchThread();
        if (service == null) return status_not_supported;
        return status.get(id);
    }

    public Map<String,Object> getProperties(String id) {
        assert id != null;
        assert Protocol.isDispatchThread();
        return breakpoints.get(id);
    }

    public Map<String,Object> getStatus(IBreakpoint bp) {
        try {
            String id = TCFBreakpointsModel.getBreakpointsModel().getBreakpointID(bp);
            if (id == null) return status_not_supported;
            return getStatus(id);
        }
        catch (CoreException e) {
            return status_not_supported;
        }
    }

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

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

Back to the top