Revision 2
Updated some classes
Handler.java | ||
---|---|---|
10 | 10 |
import chunk.FileManager; |
11 | 11 |
|
12 | 12 |
public class Handler implements Runnable { |
13 |
private Peer parentPeer; |
|
13 |
private BlockingQueue<Message> msgQueue; |
|
14 |
private ScheduledExecutorService agent; |
|
15 |
private Peer peer; |
|
14 | 16 |
private Chunk peerInformation; |
15 |
private BlockingQueue<Message> msgQueue; |
|
16 |
private ScheduledExecutorService executor; |
|
17 |
|
|
17 | 18 |
private int delayLimit; |
18 | 19 |
|
19 | 20 |
private Random random; |
20 | 21 |
|
21 |
public Handler(Peer parentPeer) {
|
|
22 |
this.parentPeer = parentPeer;
|
|
23 |
this.peerInformation = parentPeer.getPeerInformation();
|
|
22 |
public Handler(Peer peer) { |
|
23 |
this.peer = peer;
|
|
24 |
this.peerInformation = peer.getPeerInformation(); |
|
24 | 25 |
msgQueue = new LinkedBlockingQueue<>(); |
25 |
executor = Executors.newScheduledThreadPool(5);
|
|
26 |
agent = Executors.newScheduledThreadPool(5);
|
|
26 | 27 |
delayLimit = 300; |
27 | 28 |
|
28 | 29 |
this.random = new Random(); |
... | ... | |
36 | 37 |
try { |
37 | 38 |
msg = msgQueue.take(); |
38 | 39 |
if (msg == null) { |
39 |
System.out.println("Null Message Received");
|
|
40 |
System.out.println("No msg received!");
|
|
40 | 41 |
return; |
41 | 42 |
} |
42 |
|
|
43 |
System.out.println("R: " + msg.toString()); |
|
44 |
|
|
43 |
System.out.println("R:: " + msg.toString()); |
|
45 | 44 |
switch (msg.getType()) { |
46 | 45 |
case PUTCHUNK: |
47 |
Backup backup = new Backup(parentPeer, msg);
|
|
48 |
executor.execute(backup);
|
|
46 |
Backup backup = new Backup(peer, msg); |
|
47 |
agent.execute(backup);
|
|
49 | 48 |
break; |
50 | 49 |
case STORED: |
51 | 50 |
peerInformation.addChunkReplication(msg.getFileID(), msg.getChunkNo()); |
52 | 51 |
break; |
53 |
case GETCHUNK: |
|
54 |
|
|
55 |
break; |
|
56 |
case CHUNK: |
|
57 |
|
|
58 |
break; |
|
59 | 52 |
case REMOVED: |
60 |
FileManager database = parentPeer.getDatabase();
|
|
53 |
FileManager database = peer.getDatabase(); |
|
61 | 54 |
String fileID = msg.getFileID(); |
62 | 55 |
int chunkNo = msg.getChunkNo(); |
63 | 56 |
|
... | ... | |
67 | 60 |
int targetReplication = chunkInfo.getReplication(); |
68 | 61 |
|
69 | 62 |
if (perceivedReplication < targetReplication) { |
70 |
byte[] chunkData = parentPeer.loadChunk(fileID, chunkNo);
|
|
63 |
byte[] chunkData = peer.loadChunk(fileID, chunkNo); |
|
71 | 64 |
|
72 |
executor.schedule(
|
|
73 |
new RemovedChunkHandler(parentPeer, chunkInfo, chunkData),
|
|
65 |
agent.schedule(
|
|
66 |
new RemovedChunkHandler(peer, chunkInfo, chunkData), |
|
74 | 67 |
this.random.nextInt(delayLimit + 1), |
75 | 68 |
TimeUnit.MILLISECONDS |
76 | 69 |
); |
77 | 70 |
} |
78 | 71 |
break; |
79 | 72 |
case DELETE: |
80 |
Delete delete = new Delete(parentPeer, msg);
|
|
81 |
executor.execute(delete);
|
|
73 |
Delete delete = new Delete(peer, msg); |
|
74 |
agent.execute(delete);
|
|
82 | 75 |
break; |
83 | 76 |
default: |
84 | 77 |
return; |
... | ... | |
89 | 82 |
} |
90 | 83 |
} |
91 | 84 |
|
92 |
|
|
93 | 85 |
public void pushMessage(byte[] data, int length) { |
94 | 86 |
Message msgParsed = new Message(data, length); |
95 | 87 |
msgQueue.add(msgParsed); |
96 | 88 |
} |
89 |
|
|
90 |
// Getters and setters |
|
91 |
|
|
92 |
public BlockingQueue<Message> getMsgQueue() { |
|
93 |
return msgQueue; |
|
94 |
} |
|
95 |
|
|
96 |
public ScheduledExecutorService getAgent() { |
|
97 |
return agent; |
|
98 |
} |
|
99 |
|
|
100 |
public Peer getPeer() { |
|
101 |
return peer; |
|
102 |
} |
|
103 |
|
|
104 |
public Chunk getPeerInformation() { |
|
105 |
return peerInformation; |
|
106 |
} |
|
107 |
|
|
108 |
public int getDelayLimit() { |
|
109 |
return delayLimit; |
|
110 |
} |
|
111 |
|
|
112 |
public Random getRandom() { |
|
113 |
return random; |
|
114 |
} |
|
115 |
|
|
116 |
public void setMsgQueue(BlockingQueue<Message> msgQueue) { |
|
117 |
this.msgQueue = msgQueue; |
|
118 |
} |
|
119 |
|
|
120 |
public void setAgent(ScheduledExecutorService agent) { |
|
121 |
this.agent = agent; |
|
122 |
} |
|
123 |
|
|
124 |
public void setPeer(Peer peer) { |
|
125 |
this.peer = peer; |
|
126 |
} |
|
127 |
|
|
128 |
public void setPeerInformation(Chunk peerInformation) { |
|
129 |
this.peerInformation = peerInformation; |
|
130 |
} |
|
131 |
|
|
132 |
public void setDelayLimit(int delayLimit) { |
|
133 |
this.delayLimit = delayLimit; |
|
134 |
} |
|
135 |
|
|
136 |
public void setRandom(Random random) { |
|
137 |
this.random = random; |
|
138 |
} |
|
97 | 139 |
} |
Also available in: Unified diff