-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkLayer.java
More file actions
92 lines (57 loc) · 2.71 KB
/
NetworkLayer.java
File metadata and controls
92 lines (57 loc) · 2.71 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
// ===================================================================
// NetworkLayer
// Scott F. H. Kaplan -- http://www.cs.amherst.edu/~sfkaplan
// September 2004
// ===================================================================
// ===================================================================
// Currently, we use a network layer simply as a client of a data link
// layer. Thus, it exists simply to send and receive sample data and
// display the results to the user.
class NetworkLayer {
// ===================================================================
// ===============================================================
// PUBLIC METHODS
// ===============================================================
// ===============================================================
// The constructor.
public NetworkLayer (DataLinkLayer dataLinkLayer) {
// Register with the data link layer.
dataLinkLayer.register(this);
// Keep a pointer to the data link layer.
this.dataLinkLayer = dataLinkLayer;
} // NetworkLayer
// ===============================================================
// ===============================================================
// Allow a client to send a string of bytes on the medium.
public void send () {
String[] messages = { "abc",
"abd",
"The quick brown fox...",
"Does {}{} byte packing \\ work?"};
for (int i = 0; i < messages.length; i++) {
byte[] data = messages[i].getBytes();
System.out.print("Network.send() message: ");
System.out.println(messages[i]);
dataLinkLayer.send(data);
}
} // send
// ===============================================================
// ===============================================================
// Allow the data link layer to deliver an array of bytes to this
// layer.
void receive (byte[] data) {
String message = new String(data);
System.out.print("Network.receive() message: ");
System.out.println(message);
} // receive
// ===============================================================
// ===============================================================
// DATA MEMBERS
// ===============================================================
// ===============================================================
// The medium to which this layer is connected.
DataLinkLayer dataLinkLayer;
// ===============================================================
// ===================================================================
} // class NetworkLayer
// ===================================================================