Project

General

Profile

Statistics
| Revision:

root / MDBListener.java @ 1

History | View | Annotate | Download (2.88 KB)

1 1 up20150476
import java.io.File;
2
import java.io.IOException;
3
import java.net.DatagramPacket;
4
import java.net.InetAddress;
5
import java.net.MulticastSocket;
6
import java.util.Arrays;
7
8
/**
9
 * MDBListener
10
 */
11
public class MDBListener implements Runnable {
12
    private static final int PACKET_SIZE = 65000; //65KBytes, just enough space for the chunk itself
13
    private MulticastSocket socket;
14
    private InetAddress address;
15
    private int port;
16
    private int serverId;
17
18
    private InetAddress MCaddress;
19
    private int MCport;
20
21
    private Peer peer;
22
23
    MDBListener(InetAddress address, int port, int serverId, InetAddress MCaddress, int MCport, Peer peer) throws IOException {
24
        this.address = address;
25
        this.port = port;
26
        this.serverId = serverId;
27
        this.MCaddress = MCaddress;
28
        this.MCport = MCport;
29
        this.peer = peer;
30
31
        socket = new MulticastSocket(port);
32
        socket.joinGroup(this.address);
33
    }
34
35
    @Override
36
    public void run() {
37
        print("MDB-Listener started: Address-"+this.address.getHostName()+" Port-"+this.port);
38
        while (true) {
39
            byte[] buf = new byte[PACKET_SIZE];
40
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
41
            try {
42
                socket.receive(packet);
43
                parseMessage(Arrays.copyOfRange(buf, 0, packet.getLength()));
44
            } catch (IOException e) {
45
                e.printStackTrace();
46
            }
47
        }
48
    }
49
50
    private void parseMessage(byte[] data) {
51
        String message = new String(data);
52
        String[] args = message.split(" ");
53
54
        switch (args[0]) {
55
            case "PUTCHUNK":
56
                                print("received PUTCHUNK message: <SenderId> "+args[2]+" <FileId> "+args[3]+" <ChunkNo> "+args[4]+" <replicationDeg> "+args[5]);
57
                                int headerLength = 0;
58
59
                                for(int i = 0; i < 6; i++)
60
                                        headerLength += args[i].length()+1;
61
62
                                headerLength += 4;
63
                byte[] body = Arrays.copyOfRange(data, headerLength, data.length);
64
65
                String fileId = args[3];
66
                int chunkNo = Integer.parseInt(args[4]);
67
                File chunk = new File("Peer-"+this.serverId+File.separator+"Chunks"+File.separator+fileId+File.separator+chunkNo);
68
                if (Integer.parseInt(args[2]) == this.serverId) {
69
                    print("The initiator-peer can't store the chunks himself!");
70
                    return;
71
                }
72
                if(chunk.exists()){
73
                    print("Chunk already exists");
74
                    return;
75
                }
76
77
                new Thread(new PutChunkTask(args, this.MCaddress, this.MCport, this, this.peer, body)).start();
78
                print("starting PUTCHUNK protocol");
79
                break;
80
81
            default:
82
                break;
83
        }
84
    }
85
86
    public void print(String message) {
87
        System.out.println("[PEER-"+this.serverId+"-MDB] "+message);
88
    }
89
}