Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 231c39e02737743863ff8cb126e93f0ae0e38d12 (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
/*******************************************************************************
 * 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.debug.ui.model;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IMemory;
import org.eclipse.tm.tcf.services.IRunControl;
import org.eclipse.tm.tcf.util.TCFDataCache;


/**
 * This class is used to maintain a dynamic list of both executable contexts and memory spaces
 * that are children of a given parent context. The job is slightly complicated by necessity
 * to merge results from two independent services.
 */
public class TCFChildrenExecContext extends TCFChildren {

    private final TCFChildren mem_children;
    private final TCFChildren run_children;

    TCFChildrenExecContext(final TCFNode node) {
        super(node);
        mem_children = new TCFChildren(node) {
            @Override
            protected boolean startDataRetrieval() {
                IMemory mem = node.model.getLaunch().getService(IMemory.class);
                if (mem == null) {
                    set(null, null, new HashMap<String,TCFNode>());
                    return true;
                }
                assert command == null;
                command = mem.getChildren(node.id, new IMemory.DoneGetChildren() {
                    public void doneGetChildren(IToken token, Exception error, String[] contexts) {
                        Map<String,TCFNode> data = null;
                        if (command == token && error == null) {
                            int cnt = 0;
                            data = new HashMap<String,TCFNode>();
                            for (String id : contexts) {
                                TCFNode n = node.model.getNode(id);
                                if (n == null) n = new TCFNodeExecContext(node, id);
                                ((TCFNodeExecContext)n).setMemSeqNo(cnt++);
                                assert n.parent == node;
                                data.put(id, n);
                            }
                        }
                        set(token, error, data);
                    }
                });
                return false;
            }
        };
        run_children = new TCFChildren(node) {
            @Override
            protected boolean startDataRetrieval() {
                IRunControl run = node.model.getLaunch().getService(IRunControl.class);
                if (run == null) {
                    set(null, null, new HashMap<String,TCFNode>());
                    return true;
                }
                assert command == null;
                command = run.getChildren(node.id, new IRunControl.DoneGetChildren() {
                    public void doneGetChildren(IToken token, Exception error, String[] contexts) {
                        Map<String,TCFNode> data = null;
                        if (command == token && error == null) {
                            int cnt = 0;
                            data = new HashMap<String,TCFNode>();
                            for (String id : contexts) {
                                TCFNode n = node.model.getNode(id);
                                if (n == null) n = new TCFNodeExecContext(node, id);
                                ((TCFNodeExecContext)n).setExeSeqNo(cnt++);
                                assert n.parent == node;
                                data.put(id, n);
                            }
                        }
                        set(token, error, data);
                    }
                });
                return false;
            }
        };
    }

    @Override
    protected boolean startDataRetrieval() {
        TCFDataCache<?> pending = null;
        if (!mem_children.validate()) pending = mem_children;
        if (!run_children.validate()) pending = run_children;
        if (pending != null) {
            pending.wait(this);
            return false;
        }
        Throwable error = mem_children.getError();
        if (error == null) error = run_children.getError();
        Map<String,TCFNode> data = new HashMap<String,TCFNode>();
        Map<String,TCFNode> m1 = mem_children.getData();
        Map<String,TCFNode> m2 = run_children.getData();
        if (m1 != null) data.putAll(m1);
        if (m2 != null) data.putAll(m2);
        set(null, error, data);
        return true;
    }

    void onContextAdded(IRunControl.RunControlContext context) {
        // To preserve children order need to reset children list.
        reset();
        run_children.reset();
        mem_children.reset();
        assert !node.isDisposed();
        String id = context.getID();
        TCFNodeExecContext n = (TCFNodeExecContext)node.model.getNode(id);
        if (n == null) {
            n = new TCFNodeExecContext(node, id);
            n.postContextAddedDelta();
            add(n);
        }
        else {
            n.postAllChangedDelta();
        }
        run_children.add(n);
        n.setRunContext(context);
    }

    void onContextAdded(IMemory.MemoryContext context) {
        // To preserve children order need to reset children list.
        reset();
        run_children.reset();
        mem_children.reset();
        assert !node.isDisposed();
        String id = context.getID();
        TCFNodeExecContext n = (TCFNodeExecContext)node.model.getNode(id);
        if (n == null) {
            n = new TCFNodeExecContext(node, id);
            n.postContextAddedDelta();
            add(n);
        }
        else {
            n.postAllChangedDelta();
        }
        mem_children.add(n);
        n.setMemoryContext(context);
    }

    void onMemoryMapChanged() {
        for (TCFNode n : getNodes()) ((TCFNodeExecContext)n).onMemoryMapChanged();
    }
}

Back to the top