Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 94ab622ff1e4de6ea080523fe70d7843fdfe0141 (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
/*******************************************************************************
 * 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.tcf.core;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.tm.internal.tcf.core.TransportManager;
import org.eclipse.tm.internal.tcf.services.local.LocatorService;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IPeer;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.ILocator;
import org.eclipse.tm.tcf.services.ILocator.LocatorListener;

/**
 * Abstract implementation of IPeer interface.
 */
public class AbstractPeer implements IPeer {

    private final Map<String, String> ro_attrs;
    private final Map<String, String> rw_attrs;

    private long last_heart_beat_time;

    public AbstractPeer(Map<String,String> attrs) {
        assert Protocol.isDispatchThread();
        if (attrs != null) {
            rw_attrs = new HashMap<String, String>(attrs);
        }
        else {
            rw_attrs = new HashMap<String, String>();
        }
        ro_attrs = Collections.unmodifiableMap(rw_attrs);
        assert getID() != null;
        LocatorService.addPeer(this);
    }

    void onChannelTerminated() {
        // A channel to this peer was terminated:
        // not delaying next heart beat helps client to recover much faster.
        last_heart_beat_time = 0;
    }

    public void updateAttributes(Map<String,String> attrs) {
        boolean equ = true;
        assert attrs.get(ATTR_ID).equals(rw_attrs.get(ATTR_ID));
        for (Iterator<String> i = rw_attrs.keySet().iterator(); i.hasNext();) {
            String key = i.next();
            if (!rw_attrs.get(key).equals(attrs.get(key))) {
                equ = false;
                break;
            }
        }
        for (Iterator<String> i = attrs.keySet().iterator(); i.hasNext();) {
            String key = i.next();
            if (!attrs.get(key).equals(rw_attrs.get(key))) {
                equ = false;
                break;
            }
        }
        long time = System.currentTimeMillis();
        if (!equ) {
            rw_attrs.clear();
            rw_attrs.putAll(attrs);
            for (LocatorListener l : LocatorService.getListeners()) {
                try {
                    l.peerChanged(this);
                }
                catch (Throwable x) {
                    Protocol.log("Unhandled exception in Locator listener", x);
                }
            }
            try {
                Object[] args = { rw_attrs };
                Protocol.sendEvent(ILocator.NAME, "peerChanged", JSON.toJSONSequence(args));
            }
            catch (IOException x) {
                Protocol.log("Locator: failed to send 'peerChanged' event", x);
            }
            last_heart_beat_time = time;
        }
        else if (last_heart_beat_time + ILocator.DATA_RETENTION_PERIOD / 4 < time) {
            for (LocatorListener l : LocatorService.getListeners()) {
                try {
                    l.peerHeartBeat(attrs.get(ATTR_ID));
                }
                catch (Throwable x) {
                    Protocol.log("Unhandled exception in Locator listener", x);
                }
            }
            try {
                Object[] args = { rw_attrs.get(ATTR_ID) };
                Protocol.sendEvent(ILocator.NAME, "peerHeartBeat", JSON.toJSONSequence(args));
            }
            catch (IOException x) {
                Protocol.log("Locator: failed to send 'peerHeartBeat' event", x);
            }
            last_heart_beat_time = time;
        }
    }

    public void sendPeerAddedEvent() {
        for (LocatorListener l : LocatorService.getListeners()) {
            try {
                l.peerAdded(this);
            }
            catch (Throwable x) {
                Protocol.log("Unhandled exception in Locator listener", x);
            }
        }
        try {
            Object[] args = { rw_attrs };
            Protocol.sendEvent(ILocator.NAME, "peerAdded", JSON.toJSONSequence(args));
        }
        catch (IOException x) {
            Protocol.log("Locator: failed to send 'peerAdded' event", x);
        }
        last_heart_beat_time = System.currentTimeMillis();
    }

    public void sendPeerRemovedEvent() {
        for (LocatorListener l : LocatorService.getListeners()) {
            try {
                l.peerRemoved(rw_attrs.get(ATTR_ID));
            }
            catch (Throwable x) {
                Protocol.log("Unhandled exception in Locator listener", x);
            }
        }
        try {
            Object[] args = { rw_attrs.get(ATTR_ID) };
            Protocol.sendEvent(ILocator.NAME, "peerRemoved", JSON.toJSONSequence(args));
        }
        catch (IOException x) {
            Protocol.log("Locator: failed to send 'peerRemoved' event", x);
        }
    }

    public void dispose() {
        assert Protocol.isDispatchThread();
        TransportManager.peerDisposed(this);
        LocatorService.removePeer(this);
    }

    public Map<String,String> getAttributes() {
        assert Protocol.isDispatchThread();
        return ro_attrs;
    }

    public String getID() {
        assert Protocol.isDispatchThread();
        return ro_attrs.get(ATTR_ID);
    }

    public String getName() {
        assert Protocol.isDispatchThread();
        return ro_attrs.get(ATTR_NAME);
    }

    public String getOSName() {
        assert Protocol.isDispatchThread();
        return ro_attrs.get(ATTR_OS_NAME);
    }

    public String getTransportName() {
        assert Protocol.isDispatchThread();
        return ro_attrs.get(ATTR_TRANSPORT_NAME);
    }

    public IChannel openChannel() {
        return TransportManager.openChannel(this);
    }
}

Back to the top