Project

General

Profile

Statistics
| Revision:

root / src / channels / McChannel.java

History | View | Annotate | Download (2.75 KB)

1
package channels;
2

    
3

    
4
import peer.Peer;
5
import utils.Chunk;
6
import utils.Utils;
7
import static utils.Utils.BUFFER_SIZE;
8
import static utils.Utils.chunkReceived;
9

    
10
import java.io.File;
11
import java.io.FileInputStream;
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
14
import java.net.DatagramPacket;
15
import java.util.Arrays;
16
import java.util.Random;
17

    
18
public class McChannel implements Runnable{
19

    
20

    
21
    public void run() {
22
        while (true) {
23
            byte[] buf = new byte[BUFFER_SIZE];
24

    
25
            DatagramPacket receiveDatagram = new DatagramPacket(buf, buf.length);
26

    
27
            try {
28
                Utils.mcSocket.receive(receiveDatagram);
29
                String receivedMessage = new String(buf, 0, buf.length).trim();
30
                String[] receivedArgs = receivedMessage.split("\\s+");
31

    
32
                switch (receivedArgs[0]) {
33
                    case "GETCHUNK":
34
                        if (!receivedArgs[2].equals((Integer.toString(Peer.getSenderId())))) { //if not init peer send chunk message to other peers
35
                            Utils.chunkReceived = false;
36
                            sendChunk(receivedArgs);
37
                        }
38
                        break;
39
                    case "STORED":
40
                        //System.out.println("STORED <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>");
41
                        break;
42
                }
43

    
44
            } catch (IOException e) {
45
                System.out.println(e.getMessage());
46

    
47
            } catch (InterruptedException e) {
48
                e.printStackTrace();
49
            }
50
        }
51
    }
52

    
53
    private void sendChunk(String[] receivedArgs) throws IOException, InterruptedException {
54
        String chunkyNo = receivedArgs[4];
55
        String cleanChunkNo = chunkyNo.substring(0, chunkyNo.length() - 4);
56

    
57
        File toCheck = new File("storage/Backup/Peer" + Peer.getSenderId() + "/" + receivedArgs[3] + "/" + cleanChunkNo);
58
        if(toCheck.exists() && !toCheck.isDirectory()){ //go get the chunk in memory
59
            FileInputStream stream = new FileInputStream(toCheck);
60
            byte[] buffer = new byte[64000];
61
            int sizeRead = stream.read(buffer,0,64000);
62
            stream.close();
63
            byte[] realBuffer = Arrays.copyOf(buffer,sizeRead);
64

    
65
            Chunk toSend = new Chunk(realBuffer,"CHUNK",receivedArgs[3], cleanChunkNo);
66
            sendTheChunkMdr(toSend);
67
        }
68
    }
69
    private void sendTheChunkMdr(Chunk toSend) throws InterruptedException, IOException {
70
        int delay = new Random().nextInt(400); // waits before sending chunk message
71
        Thread.sleep(delay);
72
        if(!Utils.chunkReceived){
73
            System.out.println("McChannel: " + toSend.getHeaderMsg());
74
            MdrChannel.sendMessage(toSend.getBuffer());
75
        }
76
    }
77

    
78

    
79

    
80
}