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

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

import org.eclipse.tm.internal.tcf.core.RemotePeer;
import org.eclipse.tm.internal.tcf.services.local.LocatorService;
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.
 * Objects of this class are stored in Locator service peer table.
 * The class implements sending notification events to Locator listeners.
 * See TransientPeer for IPeer objects that are not stored in the Locator table.
 */
public class AbstractPeer extends TransientPeer {

    private long last_heart_beat_time;

    public AbstractPeer(Map<String,String> attrs) {
        super(attrs);
        assert Protocol.isDispatchThread();
        String id = getID();
        assert id != null;
        Map<String,IPeer> peers = LocatorService.getLocator().getPeers();
        if (peers.get(id) instanceof RemotePeer) {
            ((RemotePeer)peers.get(id)).dispose();
        }
        assert peers.get(id) == null;
        peers.put(id, this);
        sendPeerAddedEvent();
    }

    public void dispose() {
        assert Protocol.isDispatchThread();
        String id = getID();
        assert id != null;
        Map<String,IPeer> peers = LocatorService.getLocator().getPeers();
        assert peers.get(id) == this;
        peers.remove(id);
        sendPeerRemovedEvent();
    }

    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;
        }
    }

    private 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();
    }

    private 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);
        }
    }
}

Back to the top