Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 421c79753086d64667114d44677f62f33f6777b2 (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
/*******************************************************************************
 * 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.debug.test.services;

import java.util.Map;

import org.eclipse.tcf.debug.test.util.ICache;
import org.eclipse.tcf.debug.test.util.TokenCache;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IRegisters;
import org.eclipse.tcf.services.IRegisters.RegistersContext;
import org.eclipse.tcf.services.IRunControl;
import org.eclipse.tcf.services.IRunControl.RunControlContext;

/**
 * 
 */
public class RegistersCM extends AbstractCacheManager implements IRunControl.RunControlListener, IRegisters.RegistersListener  {
    private IRegisters fService;
    private IRunControl fRunControl;
    private final ResetMap fRunControlStateResetMap = new ResetMap();
    private final ResetMap fRegistersMap = new ResetMap();
    
    public RegistersCM(IChannel channel, IRegisters service, IRunControl runControl) {
        super(channel);
        fService = service;
        fRunControl = runControl;
        fRunControl.addListener(this);
    }

    @Override
    public void dispose() {
        fRunControl.removeListener(this);
        super.dispose();
    }

    public ICache<String[]> getChildren(final String id) {
        class MyCache extends TokenCache<String[]> implements IRegisters.DoneGetChildren {
            MyCache() { super(fChannel); }
            @Override
            protected IToken retrieveToken() {
                return fService.getChildren(id, this);
            }
            
            public void doneGetChildren(IToken token, Exception error, String[] context_ids) {
                fRegistersMap.addValid(id, this);
                set(token, context_ids, error);
            }
        };

        return mapCache(new IdKey<MyCache>(MyCache.class, id) {
            @Override MyCache createCache() { return new MyCache(); }
        });
    }

    public ICache<RegistersContext> getContext(final String id) {
        class MyCache extends TokenCache<RegistersContext> implements IRegisters.DoneGetContext {
            MyCache() { super(fChannel); }
            @Override
            protected IToken retrieveToken() {
                return fService.getContext(id, this);
            }
            
            public void doneGetContext(IToken token, Exception error, RegistersContext context) {
                fRegistersMap.addValid(id, this);
                set(token, context, error);
            }
        };

        return mapCache(new IdKey<MyCache>(MyCache.class, id) {
            @Override MyCache createCache() { return new MyCache(); }
        });        
    }
    
    public ICache<byte[]> getContextValue(final RegistersContext context) {
        class MyCache extends TokenCache<byte[]> implements IRegisters.DoneGet {
            MyCache() { super(fChannel); }
            @Override
            protected IToken retrieveToken() {
                return context.get(this);
            }
            
            public void doneGet(IToken token, Exception error, byte[] value) {
                fRegistersMap.addValid(context.getID(), this);
                set(token, value, error);
            }
        };

        return mapCache(new IdKey<MyCache>(MyCache.class, context.getID()) {
            @Override MyCache createCache() { return new MyCache(); }
        });                
    }
    
    protected abstract static class ContextCommandKey<V> extends IdKey<V> {
        Object fClientKey;
        
        ContextCommandKey(Class<V> cacheClass, String id, Object clientKey) {
            super(cacheClass, id);
            fClientKey = clientKey;
        }
        
        @Override
        public boolean equals(Object obj) {
            if (super.equals(obj) && obj instanceof ContextCommandKey<?>) {
                return ((ContextCommandKey<?>)obj).fClientKey.equals(fClientKey);
            } 
            return false;        
        }
        
        @Override
        public int hashCode() {
            return super.hashCode() + fClientKey.hashCode();
        }
    }

    
    public ICache<Object> setContextValue(final RegistersContext context, Object clientKey, final byte[] value) {
        class MyCache extends TokenCache<Object> implements IRegisters.DoneSet {
            MyCache() { super(fChannel); }
            @Override
            protected IToken retrieveToken() {
                return context.set(value, this);
            }
            
            public void doneSet(IToken token, Exception error) {
                fRegistersMap.addValid(context.getID(), this);
                set(token, null, error);
            }
        };

        return mapCache(new ContextCommandKey<MyCache>(MyCache.class, context.getID(), clientKey) {
            @Override MyCache createCache() { return new MyCache(); }
        });        
    }
    
    public void contextChanged() {
        fRegistersMap.resetAll();     
    }
    
    public void registerChanged(String id) {
        fRegistersMap.reset(id);
    }

    public void contextAdded(RunControlContext[] contexts) {
    }

    public void contextRemoved(String[] context_ids) {
        for (String id : context_ids) {
            fRunControlStateResetMap.reset(id);
        }
    }

    public void contextChanged(RunControlContext[] contexts) {
        for (RunControlContext context : contexts) {
            fRunControlStateResetMap.reset(context.getID());
        }
    }
    
    public void contextSuspended(String context, String pc, String reason, Map<String, Object> params) {
        fRunControlStateResetMap.reset(context);
    }

    public void contextResumed(String context) {
        fRunControlStateResetMap.reset(context);
    }

    public void containerSuspended(String context, String pc, String reason, Map<String, Object> params,
        String[] suspended_ids) 
    {
        for (String id : suspended_ids) {
            fRunControlStateResetMap.reset(id);
        }
    }

    public void containerResumed(String[] context_ids) {
        for (String id : context_ids) {
            fRunControlStateResetMap.reset(id);
        }
    }

    public void contextException(String context, String msg) {
        fRunControlStateResetMap.reset(context);
    }
}

Back to the top