root / src / channels / MdbChannel.java @ 1
History | View | Annotate | Download (3.47 KB)
1 | 1 | up20140508 | package channels; |
---|---|---|---|
2 | |||
3 | import peer.Peer; |
||
4 | import utils.FileController; |
||
5 | import utils.Utils; |
||
6 | |||
7 | import java.io.File; |
||
8 | import java.io.IOException; |
||
9 | import java.net.DatagramPacket; |
||
10 | import java.util.ArrayList; |
||
11 | import java.util.Random; |
||
12 | |||
13 | public class MdbChannel implements Runnable{ |
||
14 | public void run() { |
||
15 | while(true){ |
||
16 | byte[] buf = new byte[65000]; //header + body |
||
17 | |||
18 | DatagramPacket receivedPacket = new DatagramPacket(buf,buf.length); |
||
19 | |||
20 | try {
|
||
21 | //received chunck
|
||
22 | Utils.mdbSocket.receive(receivedPacket); |
||
23 | String received = new String(receivedPacket.getData(),0,receivedPacket.getLength()); |
||
24 | String[] receivedArray = received.split("\\r\\n\\r\\n"); //separate header from body |
||
25 | int offsetOfBody = receivedArray[0].length() + 4; |
||
26 | |||
27 | String receivedChunkData = received.substring(offsetOfBody, received.length());
|
||
28 | |||
29 | int size = received.length() - offsetOfBody;
|
||
30 | byte[] toRet = new byte[size]; |
||
31 | |||
32 | for(int i = offsetOfBody; i < received.length(); i++) { |
||
33 | toRet[i - offsetOfBody] = receivedPacket.getData()[i]; |
||
34 | } |
||
35 | |||
36 | FileController receivedFile = new FileController(receivedArray, toRet);
|
||
37 | |||
38 | |||
39 | //addToChunkDatabase
|
||
40 | if(!Utils.storedChunks.containsKey(receivedFile.getHashedFileId() + ":" + receivedFile.getChunkNo())){ |
||
41 | Utils.storedChunks.put(receivedFile.getHashedFileId() + ":" + receivedFile.getChunkNo(),new ArrayList<Integer>()); |
||
42 | Utils.storedChunks.get(receivedFile.getHashedFileId() + ":" + receivedFile.getChunkNo()).add(Integer.parseInt(receivedFile.getReplicationDeg())); |
||
43 | } |
||
44 | |||
45 | //saveFileIfSentFromAnotherPeer
|
||
46 | if(! (receivedFile.getSenderId() == Peer.getSenderId())) {
|
||
47 | |||
48 | //System.out.println("MDB: Received CMD - " + receivedFile.getStringInformation());
|
||
49 | if(!checkIfFileExists(receivedFile.getHashedFileId() + receivedFile.getChunkNo()))
|
||
50 | receivedFile.saveToDisk(Peer.getSenderId(),receivedFile.getChunkNo()); |
||
51 | try {
|
||
52 | Thread.sleep(new Random().nextInt(400)); |
||
53 | } catch (InterruptedException e) { |
||
54 | e.printStackTrace(); |
||
55 | } |
||
56 | sendStoredMessage(receivedFile); |
||
57 | } |
||
58 | } |
||
59 | catch(IOException e){ |
||
60 | e.getCause(); |
||
61 | } |
||
62 | |||
63 | |||
64 | } |
||
65 | |||
66 | |||
67 | } |
||
68 | |||
69 | private void sendStoredMessage(FileController receivedFile) throws IOException { |
||
70 | String message = ("STORED " + receivedFile.getVersion() + " " + |
||
71 | Peer.getSenderId() + " " + receivedFile.getHashedFileId()
|
||
72 | + " " + receivedFile.getChunkNo() + Utils.crlf);
|
||
73 | byte[] buf = new byte[256]; |
||
74 | buf = message.getBytes(); |
||
75 | |||
76 | DatagramPacket packet = new DatagramPacket(buf,buf.length, Peer.getMcAddress(),Peer.getMcPort()); |
||
77 | Utils.mcSocket.send(packet); |
||
78 | } |
||
79 | |||
80 | public static void sendMessage(byte[] buf) throws IOException{ |
||
81 | DatagramPacket packet = new DatagramPacket(buf,buf.length, Peer.getMdbAddress(),Peer.getMdbPort()); |
||
82 | Utils.mdbSocket.send(packet); |
||
83 | } |
||
84 | |||
85 | private boolean checkIfFileExists(String string) { |
||
86 | File f = new File("src/test/" + Peer.getSenderId() + "/" + string); |
||
87 | if(f.exists() && !f.isDirectory())
|
||
88 | return true; |
||
89 | return false; |
||
90 | } |
||
91 | } |