-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicalLayer.java
More file actions
66 lines (55 loc) · 1.56 KB
/
PhysicalLayer.java
File metadata and controls
66 lines (55 loc) · 1.56 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
import java.io.IOException;
public class PhysicalLayer
{
static ClientSocketManager medium = new ClientSocketManager();
// Constructor - connect to local ip address using server port
public PhysicalLayer()
{
try {
medium.connect("0", PhysicalLayerServer.PL_PORT); // connects to local IP
} catch (IOException e) {
System.out.println("Physical layer: Could not connect to Physical Layer Server");
e.printStackTrace();
}
}
public void close() throws IOException
{
medium.close();
}
public void transmit(String frame)
{
try {
medium.write(frame);
System.out.println("Physical layer: transmitted frame >"+BitString.displayFrame(frame)+"<");
} catch (IOException e) {
System.out.println("Physical layer: IO Exception on transmitting frame");
e.printStackTrace();
}
}
// returns a null if no frame available
// at the physical layer
public String pollReceive()
{
String frame = null;
try {
if(medium.poll()) frame = medium.read();
if(frame != null) System.out.println("Physical layer: received frame >"+BitString.displayFrame(frame)+"<");
} catch (IOException e) {
System.out.println("Physical layer: IO Exception on receiving frame");
e.printStackTrace();
}
return(frame);
}
public String receive()
{
String frame = null;
try {
frame = medium.read();
System.out.println("Physical layer: received frame >"+BitString.displayFrame(frame)+"<");
} catch (IOException e) {
System.out.println("Physical layer: IO Exception on receiving frame");
e.printStackTrace();
}
return(frame);
}
}