Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b51f5677a1e2de8690ede97ac8990441ae1a84fe (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.services.remote;

import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.tm.internal.tcf.core.ReadOnlyMap;
import org.eclipse.tm.tcf.core.Base64;
import org.eclipse.tm.tcf.core.Command;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.services.IMemory;


public class MemoryProxy implements IMemory {

    private final IChannel channel;
    private final Map<MemoryListener,IChannel.IEventListener> listeners =
        new HashMap<MemoryListener,IChannel.IEventListener>();
    
    private static class Range implements Comparable<Range> {
        int offs;
        int size;
        int stat;
        String msg;
        
        public int compareTo(Range o) {
            if (offs < o.offs) return -1;
            if (offs > o.offs) return +1;
            return 0;
        }
    }
    
    private class MemoryErrorReport extends MemoryError implements ErrorOffset {
        
        private static final long serialVersionUID = 796525409870265390L;
        private final Range[] ranges;
        
        @SuppressWarnings("unchecked")
        MemoryErrorReport(String msg, Number addr, Object ranges) {
            super(msg);
            Collection<Map<String,Object>> c = (Collection<Map<String,Object>>)ranges;
            this.ranges = c == null ? null : new Range[c.size()];
            if (c != null) {
                int n = 0;
                BigInteger addr_bi = addr instanceof BigInteger ?
                        (BigInteger)addr : new BigInteger(addr.toString());
                for (Map<String,Object> m : c) {
                    Range r = new Range();
                    Number x = (Number)m.get("addr");
                    BigInteger y = x instanceof BigInteger ?
                            (BigInteger)x : new BigInteger(x.toString());
                    r.offs = addr_bi.subtract(y).intValue();
                    r.size = ((Number)m.get("size")).intValue();
                    r.stat = ((Number)m.get("stat")).intValue();
                    r.msg = Command.toErrorString(m.get("msg"));
                    assert r.offs >= 0;
                    assert r.size >= 0;
                    this.ranges[n++] = r;
                }
                Arrays.sort(this.ranges);
            }
        }

        public String getMessage(int offset) {
            if (ranges == null) return null;
            int l = 0;
            int h = ranges.length - 1;
            while (l <= h) {
                int n = (l + h) / 2;
                Range r = ranges[n];
                if (r.offs > offset) {
                    h = n - 1;
                }
                else if (offset >= r.offs + r.size) {
                    l = n + 1;
                }
                else {
                    return r.msg;
                }
            }
            return null;
        }

        public int getStatus(int offset) {
            if (ranges == null) return BYTE_UNKNOWN;
            int l = 0;
            int h = ranges.length - 1;
            while (l <= h) {
                int n = (l + h) / 2;
                Range r = ranges[n];
                if (r.offs > offset) {
                    h = n - 1;
                }
                else if (offset >= r.offs + r.size) {
                    l = n + 1;
                }
                else {
                    return r.stat;
                }
            }
            return BYTE_UNKNOWN;
        }
    }

    private class MemContext implements MemoryContext {

        private final Map<String,Object> props;

        MemContext(Map<String,Object> props) {
            assert props instanceof ReadOnlyMap;
            this.props = props;
        }

        public String getID() {
            return (String)props.get(PROP_ID);
        }

        public String getParentID() {
            return (String)props.get(PROP_PARENT_ID);
        }

        public int getAddressSize() {
            Number n = (Number)props.get(PROP_ADDRESS_SIZE);
            if (n == null) return 0;
            return n.intValue();
        }

        public int getProcessID() {
            Number n = (Number)props.get(PROP_PROCESS_ID);
            if (n == null) return 0;
            return n.intValue();
        }

        public boolean isBigEndian() {
            Boolean n = (Boolean)props.get(PROP_BIG_ENDIAN);
            if (n == null) return false;
            return n.booleanValue();
        }

        public Map<String, Object> getProperties() {
            return props;
        }

        public IToken fill(final Number addr, int word_size,
                byte[] value, int size, int mode, final DoneMemory done) {
            return new MemoryCommand("fill", new Object[] {
                getID(), addr, word_size, size, mode, value
            } ) {
                public void done(Exception error, Object[] args) {
                    MemoryError e = null;
                    if (error != null) {
                        e = new MemoryError(error.getMessage());
                    }
                    else {
                        assert args.length == 2;
                        e = toMemoryError(addr, args[0], args[1]);
                    }
                    done.doneMemory(token, e);
                }
            }.token;
        }

        public IToken get(final Number addr, int word_size,
                final byte[] buf, final int offs, final int size,
                int mode, final DoneMemory done) {
            return new MemoryCommand("get", new Object[] {
                    getID(), addr, word_size, size, mode
                } ) {
                    public void done(Exception error, Object[] args) {
                        MemoryError e = null;
                        if (error != null) {
                            e = new MemoryError(error.getMessage());
                        }
                        else {
                            assert args.length == 3;
                            String str = (String)args[0];
                            if (str != null) Base64.toByteArray(buf, offs, size, str.toCharArray());
                            e = toMemoryError(addr, args[1], args[2]);
                        }
                        done.doneMemory(token, e);
                    }
                }.token;
        }

        public IToken set(final Number addr, int word_size,
                byte[] buf, int offs, int size, int mode, final DoneMemory done) {
            return new MemoryCommand("set", new Object[] {
                    getID(), addr, word_size, size, mode, Base64.toBase64(buf, offs, size)
                } ) {
                    public void done(Exception error, Object[] args) {
                        MemoryError e = null;
                        if (error != null) {
                            e = new MemoryError(error.getMessage());
                        }
                        else {
                            assert args.length == 2;
                            e = toMemoryError(addr, args[0], args[1]);
                        }
                        done.doneMemory(token, e);
                    }
                }.token;
        }
        
        public String toString() {
            return "[Memory Context " + props.toString() + "]";
        }
    }

    public MemoryProxy(IChannel channel) {
        this.channel = channel;
    }

    public void addListener(final MemoryListener listener) {
        IChannel.IEventListener l = new IChannel.IEventListener() {

            public void event(String name, byte[] data) {
                try {
                    Object[] args = JSON.parseSequence(data);
                    if (name.equals("contextAdded")) {
                        assert args.length == 1;
                        listener.contextAdded(toContextArray(args[0]));
                    }
                    else if (name.equals("contextChanged")) {
                        assert args.length == 1;
                        listener.contextChanged(toContextArray(args[0]));
                    }
                    else if (name.equals("contextRemoved")) {
                        assert args.length == 1;
                        listener.contextRemoved(toStringArray(args[0]));
                    }
                    else if (name.equals("memoryChanged")) {
                        assert args.length == 2;
                        listener.memoryChanged((String)args[0],
                                toAddrArray(args[1]), toSizeArray(args[1]));
                    }
                    else {
                        throw new IOException("Memory service: unknown event: " + name);
                    }
                }
                catch (Throwable x) {
                    channel.terminate(x);
                }
            }
        };
        channel.addEventListener(this, l);
        listeners.put(listener, l);
    }

    public void removeListener(MemoryListener listener) {
        IChannel.IEventListener l = listeners.remove(listener);
        if (l != null) channel.removeEventListener(this, l);
    }

    public IToken getContext(String context_id, final DoneGetContext done) {
        return new Command(channel, this, "getContext", new Object[]{ context_id }) {
            @SuppressWarnings("unchecked")
            @Override
            public void done(Exception error, Object[] args) {
                MemContext ctx = null;
                if (error == null) {
                    assert args.length == 2;
                    error = toError(args[0]);
                    if (args[1] != null) ctx = new MemContext((Map<String,Object>)args[1]);
                }
                done.doneGetContext(token, error, ctx);
            }
        }.token;
    }
    
    public IToken getChildren(String parent_context_id, final DoneGetChildren done) {
        return new Command(channel, this, "getChildren", new Object[]{ parent_context_id }) {
            @Override
            public void done(Exception error, Object[] args) {
                String[] arr = null;
                if (error == null) {
                    assert args.length == 2;
                    error = toError(args[0]);
                    arr = toStringArray(args[1]);
                }
                done.doneGetChildren(token, error, arr);
            }
        }.token;
    }

    public String getName() {
        return NAME;
    }
    
    private abstract class MemoryCommand extends Command {
        
        MemoryCommand(String cmd, Object[] args) {
            super(channel, MemoryProxy.this, cmd, args);
        }

        @SuppressWarnings("unchecked")
        MemoryError toMemoryError(Number addr, Object data, Object ranges) {
            if (data == null) return null;
            Map<String,Object> map = (Map<String,Object>)data;
            Integer code = (Integer)map.get(ERROR_CODE);
            String cmd = getCommandString();
            if (cmd.length() > 72) cmd = cmd.substring(0, 72) + "...";
            return new MemoryErrorReport(
                    "TCF command exception:" +
                    "\nCommand: " + cmd +
                    "\nException: " + toErrorString(data) +
                    "\nError code: " + code, addr, ranges);
        }
    }

    @SuppressWarnings("unchecked")
    private MemoryContext[] toContextArray(Object o) {
        Collection<Map<String,Object>> c = (Collection<Map<String,Object>>)o;
        if (c == null) return new MemoryContext[0];
        int n = 0;
        MemoryContext[] ctx = new MemoryContext[c.size()];
        for (Iterator<Map<String,Object>> i = c.iterator(); i.hasNext();) {
            ctx[n++] = new MemContext(i.next());
        }
        return ctx;
    }

    @SuppressWarnings("unchecked")
    private long[] toSizeArray(Object o) {
        Collection<Map<String,Object>> c = (Collection<Map<String,Object>>)o;
        if (c == null) return null;
        long[] a = new long[c.size()];
        int n = 0;
        for (Map<String,Object> m : c) {
            Number sz = (Number)m.get("size");
            a[n++] = sz == null ? 0 : sz.longValue();
        }
        return a;
    }

    @SuppressWarnings("unchecked")
    private Number[] toAddrArray(Object o) {
        Collection<Map<String,Object>> c = (Collection<Map<String,Object>>)o;
        if (c == null) return null;
        Number[] a = new Number[c.size()];
        int n = 0;
        for (Map<String,Object> m : c) {
            a[n++] = (Number)m.get("addr");
        }
        return a;
    }

    @SuppressWarnings("unchecked")
    private String[] toStringArray(Object o) {
        Collection<String> c = (Collection<String>)o;
        if (c == null) return new String[0];
        return (String[])c.toArray(new String[c.size()]);
    }
}

Back to the top