Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4ceb3e79f6f3c48106d1e071e7a638d034690fb7 (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
/*******************************************************************************
 * Copyright (c) 2007, 2013 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.core;

import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;

/**
 * Implementation of the {@code IToken} interface. Used to match commands to results and to cancel pending commands
 */
public class Token implements IToken {

    /**
     * Internal static variable used to update the Token ID for the given command
     */
    private static int cnt = 0;

    /**
     * Token ID. This is what is seen when we
     */
    private final String id;
    /**
     * Byte representation of the Token ID
     */
    private final byte[] bytes;
    /**
     * ICommandListener associated with this token
     */
    private final IChannel.ICommandListener listener;

    public Token() {
        id = null;
        bytes = null;
        listener = null;
    }

    /**
     * Constructs a token with the given ICommandListener.
     * Used when sending a command/result to the channel
     * @param listener ICommandListener
     */
    public Token(IChannel.ICommandListener listener) {
        this.listener = listener;
        id = Integer.toString(cnt++);
        int l = id.length();
        bytes = new byte[l];
        for (int i = 0; i < l; i++) bytes[i] = (byte)id.charAt(i);
    }

    /**
     * Constructs a token from an array of bytes.
     * Used when receiving a command/result from the channel
     * @param bytes
     */
    public Token(byte[] bytes) {
        this.bytes = bytes;
        listener = null;
        int l = bytes.length;
        char[] bf = new char[l];
        for (int i = 0; i < l; i++) bf[i] = (char)(bytes[i] & 0xff);
        id = new String(bf);
    }

    public boolean cancel() {
        return false;
    }

    public String getID() {
        return id;
    }

    public byte[] getBytes() {
        return bytes;
    }

    public IChannel.ICommandListener getListener() {
        return listener;
    }

    @Override
    public String toString() {
        return id;
    }
}

Back to the top