-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGossipThread.java
More file actions
59 lines (53 loc) · 1.92 KB
/
GossipThread.java
File metadata and controls
59 lines (53 loc) · 1.92 KB
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
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
public class GossipThread extends Thread {
private static Logger log = Logger.getLogger(GossipThread.class);
public void run() {
while (true) {
VectorClock.NumElems = ServerStateManager.getSnapshot().size();
log.info(ServerStateManager.getSnapshot());
try {
Thread.sleep(5000 * WebServer.delayModifier);
} catch (InterruptedException e) {
}
ServerStateManager.purgeDead();
JSONgossipMessage j = new JSONgossipMessage();
j.snapshot = ServerStateManager.getSnapshot();
j.myState = ServerStateManager.getMyState();
j.tweets = BackendDataStorage.getInstance().getTemp();
for (Integer i : j.snapshot.keySet()) {
log.info("Gossiping to: " + i);
if (ServerStateManager.getID() != i.intValue()) {
HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("s", WebServer.g
.toJson(j, JSONgossipMessage.class)));
UrlEncodedFormEntity entity = null;
try {
entity = new UrlEncodedFormEntity(formparams, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
HttpPost httppost = new HttpPost("http://"
+ j.snapshot.get(i).ip + ":"
+ j.snapshot.get(i).port + "/data/gossip");
httppost.setEntity(entity);
try {
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
}
}
}
}