root / MCListener.java @ 1
History | View | Annotate | Download (3.94 KB)
1 |
import java.io.IOException; |
---|---|
2 |
import java.io.File; |
3 |
import java.net.DatagramPacket; |
4 |
import java.net.InetAddress; |
5 |
import java.net.MulticastSocket; |
6 |
import java.util.Arrays; |
7 |
|
8 |
/**
|
9 |
* MCListener
|
10 |
*/
|
11 |
public class MCListener implements Runnable { |
12 |
private static final int PACKET_SIZE = 256; |
13 |
|
14 |
private MulticastSocket socket; |
15 |
private InetAddress address; |
16 |
private int port; |
17 |
private int serverId; |
18 |
|
19 |
private Peer peer;
|
20 |
|
21 |
MCListener(InetAddress address, int port, int serverId, Peer peer) throws IOException { |
22 |
this.address = address;
|
23 |
this.port = port;
|
24 |
this.serverId = serverId;
|
25 |
this.peer = peer;
|
26 |
socket = new MulticastSocket(port); |
27 |
socket.joinGroup(this.address);
|
28 |
} |
29 |
|
30 |
@Override
|
31 |
public void run() { |
32 |
print("MC-Listener started: Address-"+this.address.getHostName()+" Port-"+this.port); |
33 |
while (true) { |
34 |
byte[] buf = new byte[PACKET_SIZE]; |
35 |
DatagramPacket packet = new DatagramPacket(buf, buf.length); |
36 |
try {
|
37 |
socket.receive(packet); |
38 |
parseMessage(Arrays.copyOfRange(buf, 0, packet.getLength())); |
39 |
} catch (IOException e) { |
40 |
e.printStackTrace(); |
41 |
} |
42 |
} |
43 |
} |
44 |
|
45 |
private void parseMessage(byte[] data) { |
46 |
String message = new String(data); |
47 |
String[] args = message.split(" "); |
48 |
|
49 |
switch (args[0]) { |
50 |
//STORED <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
|
51 |
case "STORED": |
52 |
String fileId = args[3]; |
53 |
String version = args[1]; |
54 |
int chunkNo = Integer.parseInt(args[4]); |
55 |
new Thread(new StoredTask(this.peer, fileId, version, chunkNo, this)).start(); |
56 |
print("starting "+args[0]+" protocol"); |
57 |
break;
|
58 |
//GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
|
59 |
case "GETCHUNK": |
60 |
print("received GETCHUNK message");
|
61 |
if (Integer.parseInt(args[2]) == this.peer.getSenderId() || |
62 |
!this.peer.chunksMap.containsKey(args[3]) || |
63 |
!this.peer.chunksMap.get(args[3]).getChunksMap().containsKey(Integer.parseInt(args[4]))) { |
64 |
return;
|
65 |
} |
66 |
new Thread(new GetchunkTask(this.peer, args, this)).start(); |
67 |
break;
|
68 |
//DELETE <Version> <SenderId> <FileId> <CRLF><CRLF>
|
69 |
case "DELETE": |
70 |
print("received DELETE message");
|
71 |
if (!this.peer.chunksMap.containsKey(args[3])) { |
72 |
return;
|
73 |
} |
74 |
this.peer.chunksMap.remove(args[3]); //remove ficheiro da hashmap |
75 |
this.peer.repDegMap.remove(args[3]); |
76 |
DeleteFile fileToDelete = new DeleteFile("Peer-" + this.peer.getSenderId() + File.separator + "Chunks" + File.separator + args[3], args[3]); |
77 |
this.peer.deleteMap.put(args[3], fileToDelete); |
78 |
for(File chunk : fileToDelete.getFile().listFiles()){ |
79 |
this.peer.removeStorage(chunk.length());
|
80 |
if(!chunk.delete())
|
81 |
print("Error deleting chunk");
|
82 |
} |
83 |
if(!fileToDelete.getFile().delete())
|
84 |
print("Error deleting chunks directory");
|
85 |
|
86 |
break;
|
87 |
//REMOVED <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
|
88 |
case "REMOVED": |
89 |
print("received REMOVED message");
|
90 |
print("SenderID: "+this.peer.getSenderId()); |
91 |
print("args[2]: "+Integer.parseInt(args[2])); |
92 |
if (this.peer.getSenderId() == Integer.parseInt(args[2]) || !this.peer.chunksMap.containsKey(args[3]) || !this.peer.chunksMap.get(args[3]).getChunksMap().containsKey(Integer.parseInt(args[4]))) |
93 |
return;
|
94 |
new Thread(new RemovedTask(this.peer, args, this)).start(); |
95 |
break;
|
96 |
|
97 |
default:
|
98 |
break;
|
99 |
} |
100 |
} |
101 |
|
102 |
public void print(String message) { |
103 |
System.out.println("[PEER-"+this.serverId+"-MC] "+message); |
104 |
} |
105 |
} |