-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
169 lines (143 loc) · 5.26 KB
/
Copy pathEncoder.java
File metadata and controls
169 lines (143 loc) · 5.26 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
import java.io.UnsupportedEncodingException;
/**
* This class Encodes a Message object into bytes
*/
/**
* INFO0010 - Introduction to Computer Networking @Uliège
* Second part of the assignment
* Academic year 2021-2022
* @author Tristan Catteeuw - s161627 | Brieuc Jamoulle - s151977
*/
public class Encoder {
private byte[] intToByteArray(int packetId) {
return new byte[] {
(byte)(packetId>> 8),
(byte) packetId};
}
public byte[] encodeConnack(boolean sp, Integer returnCode) {
byte[] message = new byte[4];
// Always the same fixed header
Integer firstByte = 32;
Integer secondByte = 2;
message[0] = firstByte.byteValue();
message[1] = secondByte.byteValue();
Integer spValue = 0;
if (sp) {
spValue = 1;
}
message[2] = spValue.byteValue();
// can be 0 to 5
message[3] = returnCode.byteValue();
return message;
}
public byte[] encodeSubAck(String[] topics, QoS[] qoss, Integer byteIdentifier, Integer returnCode) {
byte[] message = new byte[4 + topics.length];
Integer firstByte = 144;
Integer secondByte = 2 + topics.length;
message[0] = firstByte.byteValue();
message[1] = secondByte.byteValue();
byte[] byteIdentifierByte = intToByteArray(byteIdentifier);
if (byteIdentifierByte.length == 2)
{
message[2] = byteIdentifierByte[0];
message[3] = byteIdentifierByte[1];
}
else if (byteIdentifierByte.length == 1){
Integer mlb = 0;
message[2] = mlb.byteValue();
message[3] = byteIdentifierByte[0];
} else{
System.out.println("The byte identifier couldn't be resolved to a bytearray of length 2!");
}
for(int i =0; i<topics.length; i++){
message[4 + i] = returnCode.byteValue();
}
return message;
}
public byte[] encodeUnsubAck(Integer packetIdentifier) {
byte[] message = new byte[4];
Integer firstByte = 160;
Integer secondByte = 2;
message[0] = firstByte.byteValue();
message[1] = secondByte.byteValue();
byte[] packet = intToByteArray(packetIdentifier);
if ( packet.length == 2)
{
message[2] = packet[0];
message[3] = packet[1];
}
else if ( packet.length == 1){
Integer mlb = 0;
message[2] = mlb.byteValue();
message[3] = packet[0];
} else{
System.out.println("The byte identifier couldn't be resolved to a bytearray of length 2!");
}
return message;
}
public byte[] encodePublish(String topic, byte[] payload, boolean retain, Integer packetIdentifier){
byte[] message = new byte[2 + 2 + topic.length() + 2 + payload.length];
Integer firstByte;
Integer RemainingLength = 2 + topic.length() + 2 + payload.length;
if (retain){
firstByte = 49;
}else{
firstByte = 48;
}
message[0] = firstByte.byteValue();
message[1] = RemainingLength.byteValue();
Integer topicLength = topic.length();
byte[] packet = intToByteArray(topicLength);
if ( packet.length == 2)
{
message[2] = packet[0];
message[3] = packet[1];
}
else if (packet.length == 1){
Integer mlb = 0;
message[2] = mlb.byteValue();
message[3] = packet[0];
} else{
System.out.println("The byte identifier couldn't be resolved to a bytearray of length 2!");
}
byte[] topicByte = new byte[0];
try{
topicByte = topic.getBytes("UTF8");
} catch(UnsupportedEncodingException e){
System.out.println("The byte identifier couldn't be resolved to a bytearray of length 2!");
}
for(int i =0; i< topicByte.length; i++){
message[4+i] = topicByte[i];
}
packet = intToByteArray(packetIdentifier);
if ( packet.length == 2)
{
message[4 + topicByte.length] = packet[0];
message[5 + topicByte.length] = packet[1];
}
else if ( packet.length == 1){
Integer mlb = 0;
message[4 + topicByte.length] = mlb.byteValue();
message[5 + topicByte.length] = packet[0];
} else{
System.out.println("The byte identifier couldn't be resolved to a bytearray of length 2!");
}
for(int i = 0; i< payload.length; i++){
message[6 + topicByte.length + i] = payload[i];
}
return message;
}
/**
* This function encodes in byte the PINGRESP message.
*
* @return The byte array
*/
public byte[] encodePingResp() {
byte[] encodedMessage = new byte[2];
Integer firstByte = 208;
Integer secondByte = 0;
encodedMessage[0] = firstByte.byteValue();
encodedMessage[1] = secondByte.byteValue();
return encodedMessage;
}
}