-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammingDataLinkLayer.java
More file actions
394 lines (300 loc) · 11.7 KB
/
HammingDataLinkLayer.java
File metadata and controls
394 lines (300 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
import java.util.ArrayList;
import java.util.Arrays;
// =============================================================================
/**
* A data link layer that colorless green ideas sleep furiously
**/
public class HammingDataLinkLayer extends DataLinkLayer {
// =============================================================================
// =========================================================================
/**
* The constructor. Make a new parity-checking data link layer.
*
* @param physicalLayer
* The physical layer through which this data link layer should
* communicate.
**/
public HammingDataLinkLayer(PhysicalLayer physicalLayer) {
// Initialize the layer.
initialize(physicalLayer);
} // HammingDataLinkLayer
// =========================================================================
// =========================================================================
/**
* 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) + 3];
// Begin with the start tag.
int frameIndex = 0;
framedData[frameIndex++] = _startTag;
// Hamming-fy the data
BitVector plainMessage = new BitVector(data,begin,end);
//print("plain message: ", plainMessage);
BitVector hammingData = createHamming(plainMessage);
//Pad with 0's
data = hammingData.toByteArray();
//print("Hamming Data: ", hammingData);
//System.out.println(Arrays.toString(data));
// Add each byte of original data.
for (int dataIndex = 0; dataIndex < data.length; 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;
}
// 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("Final Frame sent: " + Arrays.toString(finalFrame));
return finalFrame;
} // constructFrame (byte[] data, int begin, int end)
// =========================================================================
public static BitVector createHamming(BitVector data) {
BitVector hamming = new BitVector();
// Make space for parity bits at power of 2 indices by placing 0 bit
int dataIndex = 0;
int hammingIndex = 1;
while (dataIndex < data.length()) {
if (isPowerOf2(hammingIndex)) {
hamming.setBit(hammingIndex++, false);
} else {
hamming.setBit(hammingIndex - 1, data.getBit(dataIndex++));
hammingIndex++;
}
}
//print("Hamming after spaces: ", hamming);
hammingIndex = 1;
while (hammingIndex < hamming.length()) {
if (isPowerOf2(hammingIndex)) {
//System.out.println("Parity at " + hammingIndex + " is " + (calculateParity(hammingIndex - 1, hamming)));
hamming.setBit(hammingIndex - 1, calculateParity(hammingIndex - 1, hamming));
}
hammingIndex++;
}
return hamming;
}
public static boolean calculateParity(int index, BitVector data) {
boolean parity = data.getBit(index);
for (int i = index; i < data.length(); i++) {
if (((i + 1) & (index + 1)) != 0) {
//System.out.println("Checking " + (i+1) + " and " + (index+1 + " gets " + (parity ^ data.getBit(i))));
parity = parity ^ data.getBit(i);
}
}
//System.out.println("Calculated parity at " + (index+1) + ": " + parity);
return parity;
}
public static ArrayList<Integer> verifyHamming(BitVector data) {
ArrayList<Integer> wrongIndices = new ArrayList<Integer>();
int checkIndex = 1;
while (checkIndex < data.length()) {
if (isPowerOf2(checkIndex)) {
boolean parity = calculateParity(checkIndex-1,data);
if (parity != data.getBit(checkIndex-1)) {
//Note that you are adding indices starting from 1
wrongIndices.add(new Integer(checkIndex));
}
}
checkIndex++;
}
//System.out.println(wrongIndices.toString());
return wrongIndices;
}
public static BitVector correctError (BitVector data, ArrayList<Integer> wrongIndices) {
// System.out.println("Begin correction.");
// System.out.println(wrongIndices.toString());
int sum = 0;
for (int i = 0; i < wrongIndices.size(); i++) {
sum += wrongIndices.get(i);
}
// System.out.println("This is the sum: " + sum);
if (sum!=0) data.setBit(sum - 1, !data.getBit(sum - 1));
return data;
}
public static boolean isPowerOf2(int hammingIndex) {
return 2 * hammingIndex == (hammingIndex ^ (hammingIndex - 1)) + 1;
}
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();
}
// =========================================================================
/**
* 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
// hamming 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("Final Data received: " + Arrays.toString(finalData));
// Verify that the hamming message is correct. Store any indices whose
// parity is wrong. Find the wrong index and correct it.
BitVector hammingMessage = new BitVector(finalData,0,finalData.length);
//print("Received hamming message: ", hammingMessage);
//Deconstruct the hamming data into the message before correction
BitVector finalWrongMessage = new BitVector();
int hammingIndex1 = 1;
int finalMessageIndex1 = 0;
while (hammingIndex1 < hammingMessage.length() + 1) {
if (isPowerOf2(hammingIndex1)) {
hammingIndex1++;
} else {
finalWrongMessage.setBit(finalMessageIndex1++,hammingMessage.getBit(hammingIndex1-1));
hammingIndex1++;
}
}
byte[] wrongMessage = finalWrongMessage.toByteArray();
ArrayList<Integer> wrongIndices = verifyHamming(hammingMessage);
BitVector correctMessage = correctError(hammingMessage,wrongIndices);
//System.out.println("Corrected message " + Arrays.toString(correctMessage.toByteArray()));
//Deconstruct the hamming data into the message after correction
BitVector finalMessage = new BitVector();
int hammingIndex = 1;
int finalMessageIndex = 0;
while (hammingIndex < correctMessage.length() + 1) {
if (isPowerOf2(hammingIndex)) {
hammingIndex++;
} else {
finalMessage.setBit(finalMessageIndex++,correctMessage.getBit(hammingIndex-1));
hammingIndex++;
}
}
finalData = finalMessage.toByteArray();
if (wrongIndices.size() != 0) {
//used to be finaldata
System.err.print("ParityDLL message: ");
for (int i = 0; i < wrongMessage.length; i++) {
System.err.print((char) wrongMessage[i]);
}
System.err.println(" <= Parity mismatch! Wrong indices " + wrongIndices.toString());
}
//print("final message with hamming: ", correctMessage);
//print("final message w/0 hamming: ", finalMessage);
//System.out.println("FINAL FINAL messgae: " + Arrays.toString(finalData));
return finalData;
} // processFrame
// =========================================================================
// =========================================================================
// 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;
// =========================================================================
// =============================================================================
} // class HammingDataLinkLayer
// =============================================================================