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
|
package pl.edu.mimuw.cloudatlas.agent.modules;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import pl.edu.mimuw.cloudatlas.agent.messages.AttributesMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.CleanOldGossipsMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.GetStateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.GossipGirlMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.HejkaMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.InitiateGossipMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.NoCoTamMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.QueryMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.ResponseMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.StateMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UDUPMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateAttributesMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.UpdateQueriesMessage;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.ValueContact;
import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueTime;
import pl.edu.mimuw.cloudatlas.model.ZMI;
import pl.edu.mimuw.cloudatlas.querysigner.QuerySignerApiImplementation;
public class GossipGirl extends Module {
private long nextGossipId = 0;
private Map<Long, GossipGirlState> gossipStates;
public GossipGirl() {
super(ModuleType.GOSSIP);
gossipStates = new HashMap();
}
public void handleTyped(GossipGirlMessage message) throws InterruptedException, InvalidMessageType {
System.out.println("INFO: got GossipGirlMessage " + message.getType());
switch(message.getType()) {
case INITIATE:
initiateGossip((InitiateGossipMessage) message);
break;
case HEJKA:
receiveGossip((HejkaMessage) message);
break;
case NO_CO_TAM:
handleNoCoTam((NoCoTamMessage) message);
break;
case ATTRIBUTES:
handleAttributes((AttributesMessage) message);
break;
case QUERY:
handleQuery((QueryMessage) message);
break;
case CLEAN:
cleanOldGossips((CleanOldGossipsMessage) message);
break;
default:
throw new InvalidMessageType("This type of message cannot be handled by GossipGirl");
}
}
public void handleTyped(ResponseMessage message) throws InterruptedException, InvalidMessageType {
switch(message.getType()) {
case STATE:
System.out.println("INFO: GossipGirl got a StateMessage");
setState((StateMessage) message);
break;
default:
throw new InvalidMessageType("This type of message cannot be handled by GossipGirl");
}
}
private void initiateGossip(InitiateGossipMessage message) throws InterruptedException {
Long gossipId = nextGossipId;
nextGossipId++;
gossipStates.put(gossipId, new GossipGirlState(gossipId, message.getOurPath(), message.getTheirContact(), true));
GetStateMessage getState = new GetStateMessage("", 0, ModuleType.GOSSIP, gossipId);
System.out.println("INFO: GossipGirl sending GetStateMessage when initiating");
sendMessage(getState);
}
private void receiveGossip(HejkaMessage message) throws InterruptedException {
Long gossipId = nextGossipId;
nextGossipId++;
gossipStates.put(gossipId, new GossipGirlState(
gossipId,
message.getReceiverPath(),
new ValueContact(message.getSenderPath(), message.getSenderAddress()),
false
)
);
gossipStates.get(gossipId).handleHejka(message);
GetStateMessage getState = new GetStateMessage("", 0, ModuleType.GOSSIP, gossipId);
System.out.println("INFO: GossipGirl sending GetStateMessage when responding");
sendMessage(getState);
}
private void setState(StateMessage message) throws InterruptedException {
GossipGirlState state = gossipStates.get(message.getRequestId());
if (state != null) {
System.out.println("INFO: setting state in gossip " + Long.toString(message.getRequestId()));
state.setLastAction();
state.setState(message.getZMI(), message.getQueries());
if (state.state == GossipGirlState.State.SEND_HEJKA) {
HejkaMessage hejka = new HejkaMessage(
"",
0,
state.gossipId,
state.ourPath,
state.theirContact.getName(),
state.getZoneTimestampsToSend(),
state.getQueryTimestampsToSend()
);
UDUPMessage udupMessage = new UDUPMessage("", 0, state.theirContact, hejka);
System.out.println("INFO: GossipGirl sending HejkaMessage");
sendMessage(udupMessage);
state.sentHejka();
} else if (state.state == GossipGirlState.State.SEND_NO_CO_TAM) {
NoCoTamMessage noCoTam = new NoCoTamMessage(
"",
0,
state.gossipId,
state.theirGossipId,
state.getZoneTimestampsToSend(),
state.getQueryTimestampsToSend(),
state.hejkaSendTimestamp,
state.hejkaReceiveTimestamp
);
UDUPMessage udupMessage = new UDUPMessage("", 0, state.theirContact, noCoTam);
System.out.println("INFO: GossipGirl sending NoCoTamMessage");
sendMessage(udupMessage);
state.sentNoCoTam();
}
} else {
System.out.println("ERROR: GossipGirl got state for a nonexistent gossip");
}
}
private void handleNoCoTam(NoCoTamMessage message) throws InterruptedException {
GossipGirlState state = gossipStates.get(message.getReceiverGossipId());
if (state != null) {
System.out.println("INFO: handling NoCoTamMessage in" + Long.toString(message.getReceiverGossipId()));
state.setLastAction();
state.handleNoCoTam(message);
System.out.println("DEBUG: handled NoCoTam in GossipGirlState");
sendInfo(state);
System.out.println("DEBUG: sent info after NoCoTam");
} else {
System.out.println("ERROR: GossipGirl got state for a nonexistent gossip");
}
}
private void sendInfo(GossipGirlState state) throws InterruptedException {
System.out.println("DEBUG: about to send info");
for (ZMI zmi : state.getZMIsToSend()) {
AttributesMessage attributesMessage = new AttributesMessage("", 0, zmi.getPathName(), zmi.getAttributes(), state.theirGossipId, state.offset);
UDUPMessage udupMessage = new UDUPMessage("", 0, state.theirContact, attributesMessage);
System.out.println("INFO: GossipGirl sending AttributesMessage");
sendMessage(udupMessage);
}
for (Entry<Attribute, ValueQuery> query : state.getQueriesToSend()) {
QueryMessage queryMessage = new QueryMessage("", 0, query.getKey(), query.getValue(), state.theirGossipId, state.offset);
UDUPMessage udupMessage = new UDUPMessage("", 0, state.theirContact, queryMessage);
System.out.println("INFO: GossipGirl sending QueryMessage");
sendMessage(udupMessage);
}
state.sentInfo();
}
private void handleAttributes(AttributesMessage message) throws InterruptedException {
GossipGirlState state = gossipStates.get(message.getReceiverGossipId());
if (state != null) {
System.out.println("INFO: handling Attributes in " + Long.toString(message.getReceiverGossipId()));
state.setLastAction();
state.gotAttributes(message);
if (state.state == GossipGirlState.State.SEND_INFO || state.state == GossipGirlState.State.SEND_INFO_AND_FINISH) {
sendInfo(state);
}
UpdateAttributesMessage updateMessage = new UpdateAttributesMessage("", 0, message.getPath().toString(), state.modifyAttributes(message.getAttributes()));
System.out.println("INFO: GossipGirl sending UpdateAttributesMessage");
sendMessage(updateMessage);
if (state.state == GossipGirlState.State.FINISHED) {
gossipStates.remove(message.getReceiverGossipId());
}
} else {
System.out.println("ERROR: GossipGirl got attributes for a nonexistent gossip");
}
}
private void handleQuery(QueryMessage message) throws InterruptedException {
GossipGirlState state = gossipStates.get(message.getReceiverGossipId());
if (state != null) {
System.out.println("INFO: handling Query in " + Long.toString(message.getReceiverGossipId()));
state.setLastAction();
state.gotQuery(message);
Map<Attribute, ValueQuery> queries = new HashMap();
ValueQuery vq = message.getQuery();
queries.put(message.getName(), vq);
UpdateQueriesMessage updateMessage = new UpdateQueriesMessage("", 0, queries);
System.out.println("INFO: GossipGirl sending UpdateQueriesMessage");
sendMessage(updateMessage);
if (state.state == GossipGirlState.State.FINISHED) {
gossipStates.remove(message.getReceiverGossipId());
}
} else {
System.out.println("ERROR: GossipGirl got query for a nonexistent gossip");
}
}
private void cleanOldGossips(CleanOldGossipsMessage message) {
Iterator<Entry<Long, GossipGirlState>> iterator = gossipStates.entrySet().iterator();
while (iterator.hasNext()) {
GossipGirlState state = iterator.next().getValue();
if (state.lastAction.isLowerThan(message.getAgeThreshold()).getValue()) {
System.out.println("INFO: GossipGirl removing old gossip " + Long.toString(state.gossipId));
iterator.remove();
}
}
}
}
|