root / src / message / MessageHeader.java @ 2
History | View | Annotate | Download (4.13 KB)
1 |
package message; |
---|---|
2 |
|
3 |
public class MessageHeader { |
4 |
|
5 |
public static final String CRLF = "\r\n\r\n"; |
6 |
public static final int MESSAGE_HEADER_MAX_SIZE = 1000; |
7 |
|
8 |
private String messageType, version, fileId; |
9 |
private int senderId, chunkNo, replicaDeg; |
10 |
|
11 |
public MessageHeader(String rawHeader) { |
12 |
System.out.println("Messageheader: " + rawHeader); |
13 |
String[] sArray = rawHeader.trim().split(" "); |
14 |
this.messageType = sArray[0]; |
15 |
this.version = sArray[1]; |
16 |
this.senderId = Integer.parseInt(sArray[2]); |
17 |
this.fileId = sArray[3]; |
18 |
System.out.println("fileid: " + fileId); |
19 |
if (this.messageType.equals("PUTCHUNK") || this.messageType.equals("STORED") |
20 |
|| this.messageType.equals("GETCHUNK") || this.messageType.equals("REMOVED") |
21 |
|| this.messageType.equals("CHUNK")) { |
22 |
String cn = sArray[4].replaceAll("[^\\d]", ""); |
23 |
this.chunkNo = Integer.parseInt(cn); |
24 |
} |
25 |
if (this.messageType.equals("PUTCHUNK")) { |
26 |
String rep = sArray[5].replaceAll("[^\\d]", ""); |
27 |
this.replicaDeg = Integer.parseInt(rep); |
28 |
} |
29 |
} |
30 |
|
31 |
public MessageHeader(String messageType, String version, int senderId, String fileId, int chunkNo) |
32 |
throws InvalidHeaderParameters {
|
33 |
this.messageType = messageType;
|
34 |
if (this.messageType.equals("PUTCHUNK")) { |
35 |
throw new InvalidHeaderParameters(); |
36 |
} |
37 |
this.version = version;
|
38 |
this.senderId = senderId;
|
39 |
this.fileId = fileId;
|
40 |
this.chunkNo = chunkNo;
|
41 |
} |
42 |
|
43 |
public MessageHeader(String messageType, String version, int senderId, String fileId, int chunkNo, int replicaDeg) { |
44 |
this.messageType = messageType;
|
45 |
this.version = version;
|
46 |
this.senderId = senderId;
|
47 |
this.fileId = fileId;
|
48 |
this.chunkNo = chunkNo;
|
49 |
this.replicaDeg = replicaDeg;
|
50 |
} |
51 |
|
52 |
public MessageHeader(String messageType, String version, int senderId, String fileId) |
53 |
throws InvalidHeaderParameters {
|
54 |
this.messageType = messageType;
|
55 |
if (!this.messageType.equals("DELETE")) { |
56 |
throw new InvalidHeaderParameters(); |
57 |
} |
58 |
this.version = version;
|
59 |
this.senderId = senderId;
|
60 |
this.fileId = fileId;
|
61 |
} |
62 |
|
63 |
public String getMessageType() { |
64 |
return this.messageType; |
65 |
} |
66 |
|
67 |
public String getVersion() { |
68 |
return this.version; |
69 |
} |
70 |
|
71 |
public int getSenderId() { |
72 |
return this.senderId; |
73 |
} |
74 |
|
75 |
public String getFileId() { |
76 |
return this.fileId; |
77 |
} |
78 |
|
79 |
public int getChunkNo() { |
80 |
return this.chunkNo; |
81 |
} |
82 |
|
83 |
public int getReplicaDeg() { |
84 |
return this.replicaDeg; |
85 |
} |
86 |
|
87 |
@Override
|
88 |
public String toString() { |
89 |
switch (messageType) {
|
90 |
case "GETCHUNK": |
91 |
return this.messageType + " " + this.version + " " + Integer.toString(this.senderId) + " " + this.fileId |
92 |
+ " " + Integer.toString(this.chunkNo) + CRLF; |
93 |
case "PUTCHUNK": |
94 |
return this.messageType + " " + this.version + " " + Integer.toString(this.senderId) + " " + this.fileId |
95 |
+ " " + Integer.toString(this.chunkNo) + " " + Integer.toString(this.replicaDeg) + CRLF; |
96 |
case "STORED": |
97 |
return this.messageType + " " + this.version + " " + Integer.toString(this.senderId) + " " + this.fileId |
98 |
+ " " + Integer.toString(this.chunkNo) + CRLF; |
99 |
case "CHUNK": |
100 |
return this.messageType + " " + this.version + " " + Integer.toString(this.senderId) + " " + this.fileId |
101 |
+ " " + Integer.toString(this.chunkNo) + CRLF; |
102 |
case "DELETE": |
103 |
return this.messageType + " " + this.version + " " + Integer.toString(this.senderId) + " " + this.fileId |
104 |
+ CRLF; |
105 |
case "REMOVED": |
106 |
return this.messageType + " " + this.version + " " + Integer.toString(this.senderId) + " " + this.fileId |
107 |
+ " " + Integer.toString(this.chunkNo) + CRLF; |
108 |
default:
|
109 |
return ""; |
110 |
|
111 |
} |
112 |
} |
113 |
} |