-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackets.java
More file actions
140 lines (129 loc) · 3.98 KB
/
Copy pathPackets.java
File metadata and controls
140 lines (129 loc) · 3.98 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
// Michelle Emamdie
// CNT 4007C - Network Fundamentals
// Programming Assignment 2 - RDT 3.0
/**
* Class: Packets
*
* Functions:
* Constructor - Initialize the properties
* createPacket(String) - Creates a new packet with the content passed to it
* generateChecksum(String) - Counts the sum of the ascii values in the content string
* validateAck(String) - Determines if the acknowledgement received is valid
* validate() - Validates the checksum to make sure it has not changed from what it should be
* parse(String) - Take the string and set the properties for a packet
* generateMessage() - Takes the packet and turns it into as string so that it can be sent to the network
* Properties:
* int sequenceNum - alternating 0 or 1 for each packet
* int packetID - the current number of packets
* int checkSum - sum of the ascii character values of the string
* String content - The data that will be part of the message
* boolean last - True when the last packet is being sent
*/
class Packets {
/**
* sequenceNum - alternating 0 or 1 for each packet
* packetID - the current number of packets
* checkSum - sum of the ascii character values of the string
*/
int sequenceNum;
private int packetID;
int checkSum;
/**
* The data that will be part of the message
*/
String content;
/**
* True when the last packet is being sent
*/
boolean last;
/**
* Constructor to initialize the properties
*/
public Packets() {
this.sequenceNum = 1;
this.packetID = 0;
this.last = false;
}
/**
* Creates a new packet with the content passed to it
* @param content The data that the packet should hold
*/
public void createPacket(String content) {
this.content = content;
if (this.sequenceNum == 0) {
this.sequenceNum = 1;
}
else {
this.sequenceNum = 0;
}
this.checkSum = generateChecksum(this.content);
this.packetID++;
}
/**
* Counts the sum of the ascii values in the content string
* @param content The data part of the string
* @return the total sum
*/
private int generateChecksum(String content) {
int ascii = 0;
int sum = 0;
for (int i = 0; i < content.length() ; i++ ) {
ascii = (int) content.charAt(i);
sum += ascii;
if (ascii == 46) {
last = true;
}
}
return sum;
}
/**
* Determines if the acknowledgement received is valid
* @param ack The acknowledgement to check
* @return True if the acknowledgement is valid
*/
public boolean validateAck(String ack) {
return ack.equals("ACK" + Integer.toString(sequenceNum));
}
/**
* Validates the checksum to make sure it has not changed from what it should be
* @return The acknowledgement based on the validity of the checksum
*/
public String validate() {
Integer newcs = generateChecksum(content);
if (newcs.equals(checkSum)) {
return "ACK" + Integer.toString(sequenceNum);
}
else {
if (sequenceNum == 0) {
sequenceNum = 1;
}
else {
sequenceNum = 0;
}
return "ACK" + Integer.toString(sequenceNum);
}
}
/**
* Take the string and set the properties for a packet
* @param input The message sent to the caller
*/
public void parse(String input) {
String[] split = input.split("\\s+");
sequenceNum = Integer.parseInt(split[0]);
packetID = Integer.parseInt(split[1]);
checkSum = Integer.parseInt(split[2]);
if (split.length == 4) {
content = split[3];
}
else {
content = System.getProperty("line.separator");
}
}
/**
* Takes the packet and turns it into as string so that it can be sent to the network
* @return String to be sent to the network
*/
public String generateMessage() {
return sequenceNum + " " + packetID + " " + checkSum + " " + content;
}
}