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
|
/**
* Copyright (c) 2006 Ecliptical Software 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:
* Ecliptical Software Inc. - initial API and implementation
*/
package org.eclipse.ecf.pubsub.model.impl;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.eclipse.ecf.core.ISharedObjectContext;
import org.eclipse.ecf.core.ReplicaSharedObjectDescription;
import org.eclipse.ecf.core.SharedObjectInitException;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.example.pubsub.SerializationUtil;
import org.eclipse.ecf.pubsub.IPublishedService;
import org.eclipse.ecf.pubsub.impl.SubscribeMessage;
import org.eclipse.ecf.pubsub.impl.UnsubscribeMessage;
import org.eclipse.ecf.pubsub.model.IMasterModel;
public class LocalAgent extends AgentBase implements IPublishedService, IMasterModel {
protected Collection subscriptions;
private final Object subscriptionMutex = new Object();
protected void initializeData(Object data) throws SharedObjectInitException {
this.data = data;
}
public synchronized void update(Object data) throws IOException {
config.getContext().sendMessage(null, SerializationUtil.serialize(data));
}
public Map getProperties() {
return Collections.EMPTY_MAP;
}
public void subscribe(ID containerID, ID requestorID) {
synchronized (subscriptionMutex) {
if (subscriptions == null)
subscriptions = new HashSet();
ISharedObjectContext ctx = config.getContext();
try {
if (subscriptions.add(containerID)) {
ctx.sendCreate(containerID, createRemoteAgentDescription(requestorID));
} else {
SubscribeMessage msg = new SubscribeMessage(requestorID);
ctx.sendMessage(containerID, SerializationUtil.serialize(msg));
}
} catch (IOException e) {
// TODO Log me!
e.printStackTrace();
}
}
}
protected void received(ID containerID, Object data) {
if (!(data instanceof byte[]))
return;
Object msg;
try {
msg = SerializationUtil.deserialize((byte[]) data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
if (!(msg instanceof UnsubscribeMessage))
return;
synchronized (subscriptionMutex) {
if (subscriptions != null)
subscriptions.remove(containerID);
}
}
protected void disconnected(ID containerID) {
synchronized (subscriptionMutex) {
if (subscriptions != null)
subscriptions.remove(containerID);
}
}
protected void deactivated() {
if (isConnected())
try {
config.getContext().sendDispose(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected ReplicaSharedObjectDescription createRemoteAgentDescription(ID requestorID) {
Map props = new HashMap(3);
try {
props.put(INITIAL_DATA_KEY, SerializationUtil.serialize(data));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
props.put(MODEL_UPDATER_KEY, updaterID);
props.put(REQUESTOR_ID, requestorID);
return new ReplicaSharedObjectDescription(RemoteAgent.class, config.getSharedObjectID(), config.getHomeContainerID(), props);
}
}
|