-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfectMedium.java
More file actions
94 lines (57 loc) · 2.43 KB
/
PerfectMedium.java
File metadata and controls
94 lines (57 loc) · 2.43 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
// ===================================================================
// PerfectMedium
// Scott F. H. Kaplan -- http://www.cs.amherst.edu/~sfkaplan
// September 2004
// ===================================================================
// ===================================================================
// A point-to-point medium that, like a hypothetical ``perfect wire'',
// introduces no error.
class PerfectMedium extends Medium {
// ===================================================================
// ===============================================================
// PUBLIC METHODS
// ===============================================================
// ===============================================================
// Register one of the two allowed clients as connected to an end
// of the medium.
public void register (PhysicalLayer client) {
// If there is an end of the wire available, then assign this
// client to it.
if (client1 == null) {
client1 = client;
} else if (client2 == null) {
client2 = client;
} else {
throw new RuntimeException();
}
} // register
// ===============================================================
// ===============================================================
// Allow a client to send a bit to the other client.
public void send (PhysicalLayer sender, boolean bit) {
// Determine who the receiver is. Send only if the sender is
// a known client.
PhysicalLayer receiver = null;
if (client1 == sender) {
receiver = client2;
} else if (client2 == sender) {
receiver = client1;
} else {
throw new RuntimeException();
}
// Deliver the bit to the receiver by performing an upcall to
// it.
receiver.receive(bit);
} // send
// ===============================================================
// ===============================================================
// DATA MEMBERS
// ===============================================================
// ===============================================================
// The two physical layer clients on either end of the wire.
PhysicalLayer client1;
PhysicalLayer client2;
// ===============================================================
// ===================================================================
} // class PointToPointPhysicalLayer
// ===================================================================