-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataMsg.java
More file actions
236 lines (185 loc) · 7.38 KB
/
Copy pathDataMsg.java
File metadata and controls
236 lines (185 loc) · 7.38 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import java.io.UnsupportedEncodingException;
public class DataMsg {
private String msgLen;
private String msgType;
private int dataLen = Constants.TYPE_OF_DATA_MSG;
private byte[] lgth = null;
private byte[] payload = null;
private byte[] type = null;
public DataMsg() { }
public DataMsg(String msgType){
try {
if (msgType == Constants.CHOKE_DATA_MESSAGE || msgType == Constants.UNCHOKE_DATA_MESSAGE
|| msgType == Constants.NOTINTERESTED_DATA_MESSAGE
|| msgType == Constants.INTERESTED_DATA_MESSAGE)
{
this.setMsgLen(1);
this.setMsgType(msgType);
this.payload = null;
}
else
throw new Exception("DataMsg :: Wrong constructor selected");
} catch (Exception e) {
peerProcess.printLog(e.toString());
}
}
public DataMsg(String Type, byte[] Payload)
{
try
{
if (Payload != null)
{
this.setMsgLen(Payload.length + 1);
if (this.lgth.length > Constants.SIZE_OF_DATA_MSG)
throw new Exception("length of data message is too large.");
this.setPayload(Payload);
}
else
{
if (Type == Constants.CHOKE_DATA_MESSAGE || Type == Constants.UNCHOKE_DATA_MESSAGE
|| Type == Constants.INTERESTED_DATA_MESSAGE
|| Type == Constants.NOTINTERESTED_DATA_MESSAGE)
{
this.setMsgLen(1);
this.payload = null;
}
else
throw new Exception("DataMsg :: Pay load should not be null");
}
this.setMsgType(Type);
if (this.getMessageType().length > Constants.TYPE_OF_DATA_MSG)
throw new Exception("DataMsg :: Type of data message length is very large.");
} catch (Exception e) {
peerProcess.printLog(e.toString());
}
}
public static int byteArrayToInt(byte[] inputArray) {
int output = 0;
for (int i = 0; i < 4; i++)
{
int shift = (3 - i) * 8;
output = output + (inputArray[i + 0] & 0x000000FF) << shift;
}
return output;
}
public static byte[] intToByteArray(int input)
{
byte[] output = new byte[4];
for (int i = 0; i < 4; i++)
{
int offset = (output.length - 1 - i) * 8;
output[i] = (byte) ((input >>> offset) & 0xFF);
}
return output;
}
public String getMessageTypeString() {
return msgType;
}
public byte[] getMessageLength() {
return lgth;
}
public byte[] getMessageType() {
return type;
}
public byte[] getPayload() {
return payload;
}
public void setPayload(byte[] payload) {
this.payload = payload;
}
public static byte[] encodeMessage(DataMsg msg)
{
byte[] msgStream = null;
int msgType;
try
{
msgType =Integer.parseInt(msg.getMessageTypeString());
if (msg.getMessageLength().length > Constants.SIZE_OF_DATA_MSG)
throw new Exception("DataMsg :: Invalid message length.");
else if (msgType < 0 || msgType > 7)
throw new Exception("DataMsg :: Invalid message type.");
else if (msg.getMessageType() == null)
throw new Exception("DataMsg :: Invalid message type.");
else if (msg.getMessageLength() == null)
throw new Exception("DataMsg :: Invalid message length.");
if (msg.getPayload()!= null) {
msgStream = new byte[Constants.SIZE_OF_DATA_MSG + Constants.TYPE_OF_DATA_MSG + msg.getPayload().length];
System.arraycopy(msg.getMessageLength(), 0, msgStream, 0, msg.getMessageLength().length);
System.arraycopy(msg.getMessageType(), 0, msgStream, Constants.SIZE_OF_DATA_MSG, Constants.TYPE_OF_DATA_MSG);
System.arraycopy(msg.getPayload(), 0, msgStream, Constants.SIZE_OF_DATA_MSG + Constants.TYPE_OF_DATA_MSG, msg.getPayload().length);
} else {
msgStream = new byte[Constants.SIZE_OF_DATA_MSG + Constants.TYPE_OF_DATA_MSG];
System.arraycopy(msg.getMessageLength(), 0, msgStream, 0, msg.getMessageLength().length);
System.arraycopy(msg.getMessageType(), 0, msgStream, Constants.SIZE_OF_DATA_MSG, Constants.TYPE_OF_DATA_MSG);
}
}
catch (Exception e)
{
peerProcess.printLog(e.toString());
msgStream = null;
}
return msgStream;
}
public void setMsgLen(byte[] len) {
Integer l = byteArrayToInt(len);
this.msgLen = l.toString();
this.lgth = len;
this.dataLen = l;
}
public void setMsgLen(int messageLength) {
this.dataLen = messageLength;
this.msgLen = ((Integer)messageLength).toString();
this.lgth = intToByteArray(messageLength);
}
public void setMsgType(byte[] type) {
try {
this.msgType = new String(type, Constants.NAME_OF_MESSAGE_CHAR_SET);
this.type = type;
} catch (UnsupportedEncodingException e) {
peerProcess.printLog(e.toString());
}
}
public void setMsgType(String msgType) {
try {
this.msgType = msgType.trim();
this.type = this.msgType.getBytes(Constants.NAME_OF_MESSAGE_CHAR_SET);
} catch (UnsupportedEncodingException e) {
peerProcess.printLog(e.toString());
}
}
public static DataMsg decodeMessage(byte[] Message) {
DataMsg msg = new DataMsg();
byte[] msgLength = new byte[Constants.SIZE_OF_DATA_MSG];
byte[] msgType = new byte[Constants.TYPE_OF_DATA_MSG];
byte[] payLoad = null;
int len;
try
{
if (Message == null)
throw new Exception("DataMsg :: Invalid data.");
else if (Message.length < Constants.SIZE_OF_DATA_MSG + Constants.TYPE_OF_DATA_MSG)
throw new Exception("DataMsg :: Byte array length is too small...");
System.arraycopy(Message, 0, msgLength, 0, Constants.SIZE_OF_DATA_MSG);
System.arraycopy(Message, Constants.SIZE_OF_DATA_MSG, msgType, 0, Constants.TYPE_OF_DATA_MSG);
msg.setMsgLen(msgLength);
msg.setMsgType(msgType);
len = byteArrayToInt(msgLength);
if (len > 1)
{
payLoad = new byte[len-1];
System.arraycopy(Message, Constants.SIZE_OF_DATA_MSG + Constants.TYPE_OF_DATA_MSG, payLoad, 0, Message.length - Constants.SIZE_OF_DATA_MSG - Constants.TYPE_OF_DATA_MSG);
msg.setPayload(payLoad);
}
payLoad = null;
}
catch (Exception e)
{
peerProcess.printLog("DataMsg :: "+e.toString());
msg = null;
}
return msg;
}
public int getMessageLengthInt() {
return this.dataLen;
}
}