-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSNDContent.java
More file actions
216 lines (183 loc) · 6.34 KB
/
SNDContent.java
File metadata and controls
216 lines (183 loc) · 6.34 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
import java.net.DatagramPacket;
/*
* String Types:
* Packet Types - SND and ACK
* Packet Number - ##
* Content Types - HELLO, FETRQ, FETRP, PACIN, FLWMD
* Source Host - %%
* Destination Host - $$
* Content - (insert string here) + '\u0003'
*
*/
public class SNDContent {
String fullDataPacketString;
String packetType; // SND
int packetNumber; // ##
String contentType; // HELLO, FETRQ, FETRP, PACIN, FLWMD, PACKT
String sourceHostName;
String destinationHostName;
String content; // Job listing/Worker name string
boolean validPacket; // used for error checking
// for creating PacketContent from a received packet
public SNDContent(DatagramPacket packet)
{
validPacket = false; //set as false at start
byte[] data;
data = packet.getData();
fullDataPacketString = new String(data);
//all elements have to be valid for the entire packet to be considered valid. These methods
//also set the relevant variables within this PacketContent object at the same time.
validPacket = setPacketType(fullDataPacketString)
&& setPacketNumber(fullDataPacketString)
&& setContentType(fullDataPacketString)
&& setSourceHostName(fullDataPacketString)
&& setDestinationHostName(fullDataPacketString)
&& setPacketContent(fullDataPacketString);
if(validPacket) this.fullDataPacketString = getPacketType()
+ getPacketNumberToString()
+ getContentType()
+ getSourceHostName()
+ getDestinationHostName()
+ getPacketContent()
+ '\u0003';
}
//for creating a PacketContent manually from a String in order to send it as packet
public SNDContent(String inputString)
{
validPacket = false; //set as false at start
validPacket = setPacketType(inputString)
&& setPacketNumber(inputString)
&& setContentType(inputString)
&& setSourceHostName(inputString)
&& setDestinationHostName(inputString)
&& setPacketContent(inputString);
if(validPacket) this.fullDataPacketString = getPacketType()
+ getPacketNumberToString()
+ getContentType()
+ getSourceHostName()
+ getDestinationHostName()
+ getPacketContent()
+ '\u0003';
}
public String toString() {
return getPacketType() + getPacketNumberToString() + getContentType()
+ getSourceHostName() + getDestinationHostName() + getPacketContent() + '\u0003';
}
/*
* toDatagramPacket() can be used (a) after instantiating a PacketContent object with the variables
* we want to send, and then turning it into a DatagramPacket in order to actually send it, or (b)
* after receiving a DatagramPacket and calling createACK() in order to turn it into an ACK packet.
*
* '\u0003' is appended to the end of the fullDataPacketString. This is the character "End of Text",
* which makes sure that the receiver knows exactly when the String ends. I was getting some errors
* without this, and it doesn't matter if there are multiple '\u0003's at the end of the packet, as
* long as there is at least one.
*/
public DatagramPacket toDatagramPacket() {
DatagramPacket packet= null;
try
{
this.fullDataPacketString = toString();
byte[] data = this.fullDataPacketString.getBytes();
packet = new DatagramPacket(data, data.length);
} catch(Exception e) { e.printStackTrace(); }
return packet;
}
// SET METHODS
private boolean setPacketType(String packetString) //boolean returns true if assignment is successful
{
String packetType = packetString.substring(0,3); //gets first 3 letters of packet string
if(packetType.equals("ACK") || packetType.equals("SND"))
{
this.packetType = packetType;
return true;
}
return false;
}
//setPacketNumber establishes the packet number of the packet.
private boolean setPacketNumber(String packetString)
{
String packetNumberString = packetString.substring(3, 5);
this.packetNumber = Integer.parseInt(packetNumberString);
if(packetNumber >= 0 && packetNumber <= 15)
return true;
return false;
}
//setContentType() establishes if a packet is of type JOB/CMP/WVU/WVA.
private boolean setContentType(String packetString)
{
String contentType = packetString.substring(5, 10);
if(contentType.equals("HELLO") || contentType.contentEquals("FETRQ")
|| (contentType.equals("FETRP") || contentType.equals("PACIN")
|| (contentType.equals("FLWMD"))))
{
this.contentType = contentType;
return true;
}
return false;
}
// Both the destinationHostName and sourceHostName will have the structure
// of one letter and one number, so we only need to find substring of length 2.
private boolean setSourceHostName(String packetString)
{
sourceHostName = packetString.substring(10, 12);
return true;
}
private boolean setDestinationHostName(String packetString)
{
destinationHostName = packetString.substring(12, 14);
return true;
}
// setPacketContent() is for creating the string of information that is contained within a
// packet, which will be either a job description or a worker name.
private boolean setPacketContent(String packetString)
{
//Cutting off at EndOfText char
String content = packetString.substring(14, (packetString.indexOf('\u0003')));
this.content = content;
return true;
}
public void resetContentType(String newContentType)
{
this.contentType = newContentType;
}
public void resetPacketNumber(int newPacketNumber)
{
this.packetNumber = newPacketNumber;
// if we are resetting packet number, we know it will be a SND packet
packetType = "SND";
}
// GET METHODS
public String getPacketType()
{
return packetType;
}
public int getPacketNumber()
{
return packetNumber;
}
public String getPacketNumberToString()
{
return "" + ((packetNumber < 10) ? "0" + packetNumber : packetNumber);
}
public String getContentType()
{
return contentType;
}
public String getDestinationHostName()
{
return destinationHostName;
}
public String getSourceHostName()
{
return sourceHostName;
}
public String getPacketContent()
{
return content;
}
public boolean isValid()
{
return validPacket;
}
}