-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRCDataLinkLayer.java
More file actions
399 lines (298 loc) · 11.7 KB
/
CRCDataLinkLayer.java
File metadata and controls
399 lines (298 loc) · 11.7 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.math.BigInteger;
import java.util.Arrays;
// =============================================================================
/**
* A data link layer where blocks of data entering the network get a short check
* value attached, based on the remainder of a polynomial division of their
* contents; on retrieval the calculation is repeated, and corrective action can
* be taken against presumed data corruption if the check values do not match.
**/
public class CRCDataLinkLayer extends DataLinkLayer {
// =============================================================================
// =========================================================================
/**
* The constructor. Make a new CRC data link layer.
*
* @param physicalLayer
* The physical layer through which this data link layer should
* communicate.
**/
public CRCDataLinkLayer(PhysicalLayer physicalLayer) {
// Initialize the layer.
initialize(physicalLayer);
} // ParityDataLinkLayer
// =========================================================================
// =========================================================================
/**
* Accept a buffer of data to send. Send it as divided into multiple frames
* of a fixed, maximum size. Add a parity bit for error checking to each
* frame. Call the physical layer to actually send each frame.
*
* @param data
* An array of bytes to be framed and transmitted.
**/
public void send(byte[] data) {
// Calculate the number of frames needed to transmit this data.
int numberFrames = (int) Math
.ceil((double) data.length / _maxFrameSize);
// Construct each frame and send it.
for (int frameNumber = 0; frameNumber < numberFrames; frameNumber++) {
int beginIndex = _maxFrameSize * frameNumber;
int endIndex = _maxFrameSize * (frameNumber + 1);
if (endIndex > data.length) {
endIndex = data.length;
}
byte[] frame = constructFrame(data, beginIndex, endIndex);
physicalLayer.send(frame);
}
} // send (byte[] data)
// =========================================================================
// =========================================================================
/**
* Create a single frame to be transmitted.
*
* @param data
* The original buffer of data from which to extract a frame's
* worth.
* @param begin
* The starting index from the original data buffer.
* @param end
* The ending index from the original frame buffer.
* @return A byte array that contains an entirely constructed frame.
**/
private byte[] constructFrame(byte[] data, int begin, int end) {
// Allocate an array of bytes large enough to hold the largest possible
// frame (tags and parity byte included).
byte[] framedData = new byte[(_maxFrameSize * 2) + _generator.length()
- 1];
// Begin with the start tag.
int frameIndex = 0;
framedData[frameIndex++] = _startTag;
// Add each byte of original data.
for (int dataIndex = begin; dataIndex < end; dataIndex++) {
// If the current data byte is itself a metadata tag, then preceed
// it with an escape tag.
byte currentByte = data[dataIndex];
if ((currentByte == _startTag) || (currentByte == _stopTag)
|| (currentByte == _escapeTag)) {
framedData[frameIndex++] = _escapeTag;
}
// Add the data byte itself.
framedData[frameIndex++] = currentByte;
}
// Calculate the remainder and append it.
BitVector r = new BitVector(data,begin,end);
BitVector rem = calculateRemainder(r,true);
byte[] byteRemainder = rem.toByteArray();
for (int i = 0; i < byteRemainder.length; i++) {
framedData[frameIndex++] = byteRemainder[i];
}
// End with a stop tag.
framedData[frameIndex++] = _stopTag;
// Copy the complete frame into a buffer of the exact desired
// size.
byte[] finalFrame = new byte[frameIndex];
for (int i = 0; i < frameIndex; i++) {
finalFrame[i] = framedData[i];
}
//System.out.println("Calculated message with remainder: " + Arrays.toString(finalFrame));
return finalFrame;
} // constructFrame (byte[] data, int begin, int end)
// =========================================================================
// ==========================================================================
public BitVector calculateRemainder(BitVector dividend, boolean z) {
BitVector remainder = new BitVector();
int size = dividend.length();
// Append 0's
// System.out.println("This is the dividend length:" + size);
for (int i = size; i < size + _generator.length() - 1; i++) {
dividend.setBit(i, false);
}
// System.out.println("Appended 0's");
size = dividend.length();
for (int i = 0; i < size; i++) {
remainder.setBit(i, dividend.getBit(i));
}
//print ("starting remainder", remainder);
//Print the dividend
//print("dividend", dividend);
//Print the generator
//print("generator", _generator);
//System.out.println();
int currentIndex = 0;
while (size-currentIndex>= _generator.length()) {
//System.out.println("This is the dividend length: " + remainder.length());
//print("dividend", dividend);
//System.out.println("Stuck in remainder while loop?");
for (int i = 0; i < _generator.length(); i++) {
boolean bit = remainder.getBit(i+currentIndex)^ _generator.getBit(i);
/*System.out.print("This is the XORed bit: ");
if (bit) {
System.out.print("1");
} else {
System.out.print("0");
}*/
remainder.setBit(i+currentIndex,bit);
//System.out.println();
}
//print("current remainder", remainder);
while ((currentIndex != size - 1) && ((remainder.getBit(currentIndex) == false))) {
//System.out.println("This is the bit at current: " + remainder.getBit(currentIndex));
currentIndex++;
}
//System.out.println("This is the current index: " + currentIndex);
}
//Remove extra 0's at the front
//print("remainder before removing 0's", remainder);
int zeroCount = 1;
int index = 0;
byte[] rem = remainder.toByteArray();
//System.out.println("Byte: " + Arrays.toString(rem));
while (index < rem.length -1 && rem[index + 1]==0) {
zeroCount++;
index++;
}
byte[] zero = new byte[1];
zero[0] = 0;
if (zeroCount == rem.length) return new BitVector(zero, 0,1);
int start = zeroCount*8;
BitVector newRemainder = new BitVector();
int index2 = 0;
for (int i = start; i < remainder.length(); i ++) {
newRemainder.setBit(index2++, remainder.getBit(i));
}
//print("remainder after removing 0's", remainder);
//print("remainder done", newRemainder);
//System.out.println(Arrays.toString(newRemainder.toByteArray()));
return newRemainder;
}
// =========================================================================
/**
* Determine whether the buffered data forms a complete frame.
*
* @return Whether a complete buffer has arrived.
**/
protected boolean receivedCompleteFrame() {
// Any frame with less than two bytes cannot be complete, since even the
// empty frame contains a start and a stop tag.
if (bufferIndex < 2) {
return false;
}
// A frame is complete iff the byte received is an non-escaped stop tag.
return ((incomingBuffer[bufferIndex - 1] == _stopTag) && (incomingBuffer[bufferIndex - 2] != _escapeTag));
} // receivedCompleteFrame
// =========================================================================
// =========================================================================
/**
* Remove the framing metadata and return the original data.
*
* @return The data carried in this frame; <tt>null</tt> if the data was not
* successfully received.
**/
protected byte[] processFrame() {
// Allocate sufficient space to hold the original data, which
// does not need space for the start/stop tags.
byte[] originalData = new byte[bufferIndex - 2];
// Check the start tag.
int frameIndex = 0;
if (incomingBuffer[frameIndex++] != _startTag) {
System.err.println("ParityDLL: Missing start tag!");
return null;
}
// Loop through the frame, extracting the bytes. Look ahead to find the
// stop tag (making sure it is not escaped), because the byte before
// that is the parity byte.
int originalIndex = 0;
while ((incomingBuffer[frameIndex + 1] != _stopTag)
|| (incomingBuffer[frameIndex] == _escapeTag)) {
// If the next original byte is escape-tagged, then skip
// the tag so that only the real data is extracted.
if (incomingBuffer[frameIndex] == _escapeTag) {
frameIndex++;
}
// Copy the original byte.
originalData[originalIndex++] = incomingBuffer[frameIndex++];
}
originalData[originalIndex++] = incomingBuffer[frameIndex++];
// Allocate a space that is only as large as the original
// message and then copy the original data into it.
byte[] finalData = new byte[originalIndex];
for (int i = 0; i < originalIndex; i++) {
finalData[i] = originalData[i];
}
//System.out.println("Received message with remainder: " + Arrays.toString(finalData));
BitVector messageWithRemainder = new BitVector(finalData,0,finalData.length);
BitVector finalMessage = new BitVector();
//Get rid of extra ending 0
for (int i = 0; i < messageWithRemainder.length();i++) {
finalMessage.setBit(i, messageWithRemainder.getBit(i));
}
//print("final message", finalMessage);
// Calculate the remainder of the extracted data and make sure
// it is 0. If it's not, return null.
BitVector remainder = calculateRemainder(messageWithRemainder,false);
boolean allZeroes = true;
for (int i = 0; i < remainder.length(); i++) {
if (remainder.getBit(i) != false) {
allZeroes = false;
break;
}
}
//System.out.println(allZeroes);
if (!allZeroes) {
System.err.print("CRCDLL message: ");
for (int i = 0; i < finalData.length; i++) {
System.err.print((char) finalData[i]);
}
System.err.println(" <= Remainder not 0!");
finalData = null;
return finalData;
}
//print("remainder", remainder);
//System.out.println("THIS IS THE FINAL DATA" + Arrays.toString(finalData));
byte[] finalByteMessage = new byte[finalData.length-2];
for (int i = 0; i < finalByteMessage.length; i++) {
finalByteMessage[i] = finalData[i];
}
return finalByteMessage;
} // processFrame
// =========================================================================
//======================================================================
//Print BitVectors
public static void print (String s, BitVector b) {
System.out.println("This is the " + s);
for (int i = 0; i < b.length(); i++) {
if(b.getBit(i)) {
System.out.print(1);
} else {
System.out.print(0);
}
if ((i+1) % 4 == 0)
System.out.print(' ');
} System.out.println();
}
// =========================================================================
// DATA MEMBERS
/**
* The tag that marks the beginning of a frame.
**/
final byte _startTag = (byte) '{';
/**
* The tag that marks the end of a frame.
**/
final byte _stopTag = (byte) '}';
/**
* The tag that marks the following byte as data (and not metadata).
**/
final byte _escapeTag = (byte) '\\';
/**
* The maximum number of data (not metadata) bytes in a frame.
**/
final int _maxFrameSize = 8;
byte[] _generatorByteArray = { (byte) 0xA6, (byte) 0xBC };
private BitVector _generator = new BitVector(_generatorByteArray, 0,
_generatorByteArray.length);
// =========================================================================
// =============================================================================
} // class CRCDataLinkLayer
// =============================================================================