-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGnutellaActor.java
More file actions
85 lines (69 loc) · 1.99 KB
/
GnutellaActor.java
File metadata and controls
85 lines (69 loc) · 1.99 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
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
package com.example;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import scala.concurrent.duration.Duration;
public class GnutellaActor extends UntypedActor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
public static Props props() {
return Props.create(GnutellaActor.class);
}
public static class TestStability {
public boolean next = true;
public TestStability(boolean next) {
this.next = next;
}
}
public static class StartMessage {
protected final ActorRef initPeer;
protected final int id;
public StartMessage(ActorRef ip, int id) {
this.initPeer = ip;
this.id = id;
}
public ActorRef getInitPeer() {
return initPeer;
}
public int getID() {
return id;
}
}
public static class GnutellaPingMessage {
/* to implement */
}
public static class GnutellaPongMessage {
/* to implement */
}
protected void insertPeer(int pID, ActorRef peer) {
if (!peerMap.containsKey(pID)) {
peerMap.put(pID, peer);
log.info("node {} add {}", myID, pID);
}
}
protected Map<Integer, ActorRef> peerMap = new HashMap<>();
protected int myID = 0;
public static final int ITTL = 0;
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof StartMessage) {
/* to implement */
} else if (message instanceof GnutellaPingMessage) {
/* to implement */
} else if (message instanceof GnutellaPongMessage) {
/* to implement */
} else if (message instanceof TestStability) {
String peers = "";
for (int p : peerMap.keySet()) {
peers += " " + p;
}
log.info("node {} -> {}", myID, peers);
} else {
unhandled(message);
}
}
}