-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondaryHDLCDataLink.java
More file actions
326 lines (280 loc) · 10.3 KB
/
SecondaryHDLCDataLink.java
File metadata and controls
326 lines (280 loc) · 10.3 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
import java.io.IOException;
import java.util.ArrayList;
// Data Link Layer Entity for Secondary Station
// Uses the HDLC protocol for communication over a multipoint link
// Assumptions
// Normal Response Mode operation over multi-point link (simulated using PhysicalLayer class over Sockets)
// Use 3-bit sequence numbers
// Not Supported:
// FSC checking
// Bit stuffing (frames are transmitted as strings)
// Frames implemented:
// Command Frames:
// NRM:
// DISC:
// Response Frames:
// UA:
// Command/Response Frames:
// I: maximum length of data field is 64 bytes.
// RR:
public class SecondaryHDLCDataLink
{
// Private instance variables
private PhysicalLayer physicalLayer; // for sending/receiving frames
private int stationAdr; // Station address - not used for the primary station
// Data for multiple connections in the case of the primary station
// For the secondary station, used values at index 0
private int vs;
private int vr;
private int rhsWindow; // right hand side of window.
private int windowSize; // transmit window size. reception window size is 1.
private ArrayList<String> frameBuffer;
// Constructor
public SecondaryHDLCDataLink(int adr)
{
physicalLayer = new PhysicalLayer();
stationAdr = adr;
vs = 0;
vr = 0;
windowSize = 4; //
frameBuffer = new ArrayList<String>();
rhsWindow = vs+windowSize; // seq # < rhsWindow
}
public void close() throws IOException
{
physicalLayer.close();
}
/*----------------------------------------------------------
* Connection Service
*-----------------------------------------------------------*/
// This is a confirmed service, i.e. the return value reflects results from the confirmation
public Result dlConnectIndication()
{ // Receive NRM command frame
Result.ResultCode cd = Result.ResultCode.SrvSucessful;
int adr = 0;
String retStr = null;
// Wait for UA response frame
String frame = getFrame(true); // true - wait for frame
adr = BitString.bitStringToInt(frame.substring(HdlcDefs.ADR_START,HdlcDefs.ADR_END));
// Check if frame is U-frame
String type = frame.substring(HdlcDefs.TYPE_START, HdlcDefs.TYPE_END);
if(type.equals(HdlcDefs.U_FRAME) == false)
{
cd = Result.ResultCode.UnexpectedFrameReceived;
retStr = type;
}
else
{
String uframe = frame.substring(HdlcDefs.M1_START, HdlcDefs.M1_END) +
frame.substring(HdlcDefs.M2_START, HdlcDefs.M2_END);
if(uframe.equals(HdlcDefs.SNRM)==false)
{
cd = Result.ResultCode.UnexpectedUFrameReceived;
retStr = uframe;
}
else System.out.println("Data Link Layer: received SNRM frame >"+BitString.displayFrame(frame)+"<");
}
return(new Result(cd, adr, retStr));
}
public Result dlConnectResponse()
{
Result.ResultCode cd = Result.ResultCode.SrvSucessful;
// Check if room for additional connection
String frame = HdlcDefs.FLAG+BitString.intToBitString(stationAdr,HdlcDefs.ADR_SIZE_BITS)+
HdlcDefs.U_FRAME+
HdlcDefs.UA_M1+HdlcDefs.P1+HdlcDefs.UA_M2+
HdlcDefs.FLAG;
System.out.println("Data Link Layer: prepared UA frame >"+BitString.displayFrame(frame)+"<");
physicalLayer.transmit(frame);
vs=0;
vr=0;
return(new Result(cd, stationAdr, null));
}
/*----------------------------------------------------------
* Disconnect service - non-confirmed service
*-----------------------------------------------------------*/
public Result dlDisconnectIndication()
{ // Disconnection to secondary.
Result.ResultCode cd = Result.ResultCode.SrvSucessful;
int adr = 0;
String retStr = null;
// Wait for DISC frame
String frame = getFrame(true); // true - wait for frame
adr = BitString.bitStringToInt(frame.substring(HdlcDefs.ADR_START,HdlcDefs.ADR_END));
// Check if frame is U-frame
String type = frame.substring(HdlcDefs.TYPE_START, HdlcDefs.TYPE_END);
if(type.equals(HdlcDefs.U_FRAME) == false)
{
cd = Result.ResultCode.UnexpectedFrameReceived;
retStr = type;
}
else
{
String uframe = frame.substring(HdlcDefs.M1_START, HdlcDefs.M1_END) +
frame.substring(HdlcDefs.M2_START, HdlcDefs.M2_END);
if(uframe.equals(HdlcDefs.DISC)==false)
{
cd = Result.ResultCode.UnexpectedUFrameReceived;
retStr = uframe;
}
else System.out.println("Data Link Layer: received DISC frame >"+BitString.displayFrame(frame)+"<");
}
return(new Result(cd, adr, retStr));
}
/*----------------------------------------------------------
* Data service - non-confirmed service
*-----------------------------------------------------------*/
public Result dlDataRequest(String sdu)
{
String frame; // For building frames
Result.ResultCode cd = Result.ResultCode.SrvSucessful;
// Wait for poll - need an RR with P bit - 1
/*Completer cette partie*/
do {
frame = getRRFrame(true);
} while(frame.charAt(HdlcDefs.PF_IX) == '0'); //if it's not a poll
// Send the Service Data Unit
// After each transmission, check for an ACK (RR)
// sliding window
// Reception will be go back-N
String [] dataArr = BitString.splitString(sdu, HdlcDefs.MAX_DATA_SIZE_BYTES);
// Convert the strings into bitstrings
for(int ix=0 ; ix < dataArr.length; ix++)
dataArr[ix] = BitString.stringToBitString(dataArr[ix]);
int ackFrames;
int nr;
String bitFrame;
int i = 0;
// Loop to transmit frames
// Keep looping until dataArr and frameBuffer frames have all been processed or transmitted
while (i < dataArr.length || frameBuffer.size() > 0)
{
// if window is not closed and not all data has been transmitted
if(vs != rhsWindow && i < dataArr.length)
{
// Add frame to the buffer and increment the sequence number
frameBuffer.add(bitFrame = dataArr[i]);
vs = ++vs % HdlcDefs.SNUM_SIZE_COUNT;
// transmit the frame
makeIFrame(bitFrame, i % HdlcDefs.SNUM_SIZE_COUNT, i == dataArr.length - 1);
// increment
i++;
displayDataXchngState("Data Link Layer: prepared and buffered I frame >" + BitString.displayFrame(bitFrame) + "<");
}
// Check for RR
frame = getRRFrame(false); // just poll
if ((frame != null) && (frame.charAt(HdlcDefs.PF_IX) == '0')) // have an ACK frame
{
// extract acknowledgement number
nr = BitString.bitStringToInt(frame.substring(HdlcDefs.NR_START, HdlcDefs.NR_END));
// calculate number of acknowledged frames
ackFrames = checkNr(nr, rhsWindow, windowSize);
// update right hand side based on the number of acknowledged frames
rhsWindow = (rhsWindow + ackFrames) % HdlcDefs.SNUM_SIZE_COUNT;
// remove transmitted frames from buffer
for (int j = 0; j < ackFrames; j++)
frameBuffer.remove(0);
displayDataXchngState("received an RR frame (ack) >" + BitString.displayFrame(frame) + "<");
}
}
return(new Result(cd, 0, null));
}
/*------------------------------------------------------------------------
* Helper Methods
*------------------------------------------------------------------------*/
// Determines the number of frames acknowledged from the
// acknowledge number nr.
// Parameters
// nr - received ack number - indicates next expected
// sequence number (nr can equal lhs - window is closed)
// rhs - right hand side of window - seq number to the
// right of the last valid number that can be used
// sz - size of the window
private int checkNr(int nr, int rhs, int sz)
{
int lhs;
if ((rhs - sz) >= 0) {
// left hand side = right hand side - size of the window
lhs = rhs - sz;
return ((nr <= rhs) && (nr >= lhs)) ?
nr - lhs:
0;
}
// left hand side = right hand side + the windows size
lhs = rhs + sz;
return ((nr <= rhs) || (nr >= lhs)) ?
((nr + sz) % HdlcDefs.SNUM_SIZE_COUNT) - rhs :
0;
}
private void makeIFrame(String info, int frameNumber, boolean isFinal)
{
String finalBit = isFinal? "1" : "0";
// Build the adress and control fields
String address = BitString.intToBitString(stationAdr, HdlcDefs.ADR_SIZE_BITS);
String control = HdlcDefs.I_FRAME + BitString.intToBitString(frameNumber, 3) + finalBit + BitString.intToBitString(vr, 3);
// Build the frame
String frame = HdlcDefs.FLAG + address + control + info + HdlcDefs.FLAG;
// Transmit the frame
physicalLayer.transmit(frame);
}
// Helper method to get an RR-frame
// If wait is true then wait until a frame
// arrives (call getframe(true).
// If false, return null if no frame
// is available from the physical layer (call getframe(false)
// or frame received is not an RR frame.
private String getRRFrame(boolean wait)
{
String frame;
do {
frame = getFrame(wait);
// check frame
if (frame != null) {
String type = frame.substring(HdlcDefs.TYPE_START, HdlcDefs.TYPE_END);
if (type.equals(HdlcDefs.S_FRAME)) {
String sframe = frame.substring(HdlcDefs.S_START, HdlcDefs.S_END);
// if not RR
if (!sframe.equals(HdlcDefs.RR_SS)) {
frame = null;
}
}
// if yes, frame = null
}
} while(wait && frame == null);
return frame;
}
// For displaying the status of variables used
// in exchanging data between stations.
private void displayDataXchngState(String msg)
{
int lhs; // left hand side of the window
//compute lhs
if( (rhsWindow-windowSize) >= 0) lhs = rhsWindow - windowSize;
else lhs = rhsWindow - windowSize + HdlcDefs.SNUM_SIZE_COUNT;
System.out.println("Data Link Layer: Station "+stationAdr+": "+msg);
System.out.println(" v(s) = "+vs+", v(r) = "+vr+
", Window: lhs="+lhs+" rhs="+rhsWindow+
", Number frames buffered = "+frameBuffer.size());
}
// Waits for reception of frame
// If wait is true, then wait for a frame to arrive,
// otherwise just poll physical layer for a frame.
// Returns null if no frame is received.
private String getFrame(boolean wait)
{
// Only frames with this stations address is processed - others are ignored
String frame = null;
do
{
if(wait) frame = physicalLayer.receive(); // block on receive.
else frame = physicalLayer.pollReceive(); // get frame from physical layer
if(frame != null)
{
int adr = BitString.bitStringToInt(frame.substring(HdlcDefs.ADR_START, HdlcDefs.ADR_END));
if(adr != stationAdr) frame = null; // ignore strings for other destinations
}
} while(frame == null && wait);
//if(frame != null) System.out.println("Data Link Layer: Received frame >"+BitString.displayFrame(frame)+"<");
return(frame);
}
}