-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessageBuilder.java
95 lines (83 loc) · 3.02 KB
/
MessageBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* Utility class that builds the messages ready to send via the sockets in byte[] format
*/
public class MessageBuilder {
/**
* Return byte[] of PUTCHUNK message.
*/
public static byte[] getPutchunkMessage(String fileId, int chunkNo, byte[] body, int copyN)
throws IOException {
String msg = getMessage("PUTCHUNK", fileId, chunkNo, copyN);
// msg += "\r\n"; // CRLF
// msg += "\r\n"; // CRLF
ByteArrayOutputStream message = new ByteArrayOutputStream();
message.write(msg.getBytes());
message.write(0xD);
message.write(0xA);
message.write(0xD);
message.write(0xA);
message.write(body);
return message.toByteArray();
}
/**
* Return byte[] of CHUNK message.
*/
public static byte[] getChunkMessage(String fileId, int chunkNo, int copyNo, byte[] body) throws IOException {
String msg = getMessage("CHUNK", fileId, chunkNo, copyNo);
// msg += "\r\n"; // CRLF
// msg += "\r\n"; // CRLF
ByteArrayOutputStream message = new ByteArrayOutputStream();
message.write(msg.getBytes());
message.write(0xD);
message.write(0xA);
message.write(0xD);
message.write(0xA);
message.write(body);
return message.toByteArray();
}
/**
* Return byte[] of GETCHUNK message.
*/
public static byte[] getGetchunkMessage(String fileId, int chunkNo, int copyNo) throws IOException {
String msg = getMessage("GETCHUNK", fileId, chunkNo, copyNo);
// msg += "\r\n"; // CRLF
// msg += "\r\n"; // CRLF
ByteArrayOutputStream message = new ByteArrayOutputStream();
message.write(msg.getBytes());
return message.toByteArray();
}
/**
* Return byte[] of DELETE message.
*/
public static byte[] getDeleteMessage(String fileId, int chunkNo) throws IOException {
String msg = getMessage("DELETE", fileId, chunkNo);
ByteArrayOutputStream message = new ByteArrayOutputStream();
message.write(msg.getBytes());
return message.toByteArray();
}
public static byte[] getDelegateMessage(String fileId, int chunkNo, int copyNo, byte[] body) throws IOException{
String msg = getChordMessage("DELEGATE", fileId, chunkNo, copyNo);
ByteArrayOutputStream message = new ByteArrayOutputStream();
message.write(msg.getBytes());
message.write(0xD);
message.write(0xA);
message.write(0xD);
message.write(0xA);
message.write(body);
return message.toByteArray();
}
private static String getChordMessage(String msgType, String fileId, int chunkNo, int copyNo) {
String string = "CHORD " + msgType + " " + fileId + " " + chunkNo + " " + copyNo;
return string;
}
private static String getMessage(String msgType, String fileId, int chunkNo) {
String string = "PROTOCOL " + msgType + " " + fileId + " " + chunkNo;
return string;
}
private static String getMessage(String msgType, String fileId, int chunkNo, int copyN) {
String string = "PROTOCOL " + msgType + " " + fileId + " " + chunkNo + " " + copyN;
return string;
}
}