Project

General

Profile

Statistics
| Revision:

root / GetchunkTask.java @ 1

History | View | Annotate | Download (3 KB)

1
import java.io.File;
2
import java.io.FileInputStream;
3
import java.io.IOException;
4
import java.net.DatagramPacket;
5
import java.net.DatagramSocket;
6
import java.rmi.NotBoundException;
7
import java.rmi.registry.LocateRegistry;
8
import java.rmi.registry.Registry;
9
import java.util.Random;
10

    
11
/**
12
 * GetchunkTask
13
 */
14
public class GetchunkTask implements Runnable {
15
    private static final String CRLF = Integer.toHexString(0xD) + Integer.toHexString(0xA);
16

    
17
    private Peer peer;
18
    private String fileId;
19
    private int chunkNo;
20
    private MCListener mc;
21

    
22
    private String version;
23

    
24
    private String senderId;
25

    
26
    // GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
27
    GetchunkTask(Peer peer, String[] args, MCListener mc) {
28
        this.peer = peer;
29
        this.fileId = args[3];
30
        this.version = args[1];
31
        this.senderId = args[2];
32
        this.chunkNo = Integer.parseInt(args[4]);
33
        this.mc = mc;
34

    
35
        this.mc.print("starting GETCHUNK protocol: [fileId] " + fileId + "[chunkNo] " + chunkNo);
36
    }
37

    
38
    @Override
39
    public void run() {
40
        File file = new File("Peer-" + this.peer.getSenderId() + File.separator + "Chunks" + File.separator + fileId
41
                + File.separator + chunkNo);
42
        try {
43
            FileInputStream stream = new FileInputStream(file);
44
            byte[] chunk = new byte[(int) file.length()];
45
            stream.read(chunk);
46
            stream.close();
47
            Random random = new Random();
48
            Thread.sleep(random.nextInt(401)); // [0-400] ms
49

    
50
            if (this.peer.chunksMap.get(fileId).getChunksMap().get(chunkNo).getRecentlyRequested()) {
51
                this.mc.print("Chunk was sent by another peer already");
52
                return;
53
            }
54

    
55
            //Protocol enhancement - 
56
            if (this.version.equals("2.0")) {
57
                Registry registry = LocateRegistry.getRegistry();
58
                TestAppRemote stub = (TestAppRemote) registry.lookup(this.senderId);
59
                stub.chunk(this.fileId, chunk);
60
            } else {
61
                // CHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF><Body>
62
                String message = "CHUNK " + this.version + " " + this.peer.getSenderId() + " " + fileId + " "
63
                        + chunkNo + " " + CRLF + CRLF;
64

    
65
                byte[] packetData = new byte[message.getBytes().length + chunk.length];
66
                System.arraycopy(message.getBytes(), 0, packetData, 0, message.getBytes().length);
67
                System.arraycopy(chunk, 0, packetData, message.getBytes().length, chunk.length);
68

    
69
                DatagramSocket socket = new DatagramSocket();
70
                DatagramPacket packet = new DatagramPacket(packetData, packetData.length, this.peer.getMDRaddress(),
71
                        this.peer.getMDRport());
72
                socket.send(packet);
73
                socket.close();
74
            }
75

    
76
        } catch (IOException | InterruptedException | NotBoundException e) {
77
                        e.printStackTrace();
78
                }
79
        this.mc.print("GETCHUNK protocol finished");
80
    }    
81
}