forked from john-bai/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
107 lines (86 loc) · 2.56 KB
/
Copy pathState.java
File metadata and controls
107 lines (86 loc) · 2.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
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package designpatterns;
/**
*
* @author jtherrell
*/
abstract class TCPState {
public void transmit(TCPConnection connection, TCPOctetStream stream) {}
public void activeOpen(TCPConnection connection) {}
public void passiveOpen(TCPConnection connection) {}
public void close(TCPConnection connection) {}
public void synchronize(TCPConnection connection) {}
public void acknowledge(TCPConnection connection) {}
public void send(TCPConnection connection) {}
public void changeState(TCPConnection connection, TCPState state) {
connection.changeState(state);
}
}
class TCPClosed extends TCPState {
private static TCPState s_instance;
private TCPClosed() {}
public static TCPState instance() {
if (s_instance == null) {
s_instance = new TCPClosed();
}
return s_instance;
}
@Override
public void activeOpen(TCPConnection connection) {
changeState(connection, TCPEstablished.instance());
}
@Override
public void passiveOpen(TCPConnection connection) {
changeState(connection, TCPListen.instance());
}
}
class TCPEstablished extends TCPState {
private static TCPState s_instance;
private TCPEstablished(){}
public static TCPState instance() {
if (s_instance == null) {
s_instance = new TCPEstablished();
}
return s_instance;
}
@Override
public void close(TCPConnection connection) {
changeState(connection, TCPListen.instance());
}
@Override
public void transmit(TCPConnection connection, TCPOctetStream stream) {
connection.processOctet(stream);
}
}
class TCPListen extends TCPState {
private static TCPState s_instance;
private TCPListen(){}
public static TCPState instance() {
if (s_instance == null) {
s_instance = new TCPListen();
}
return s_instance;
}
@Override
public void send(TCPConnection connection) {
// send SYN, receive SNY, ACK, etc.
changeState(connection, TCPEstablished.instance());
}
}
class TCPConnection{
private TCPState state;
public TCPConnection() { state = TCPClosed.instance(); }
public void activeOpen() { state.activeOpen(this); }
public void passiveOpen() { state.passiveOpen(this); }
public void close() { state.close(this); }
public void send() { state.send(this); }
public void acknowledge() { state.acknowledge(this); }
public void syncronize() { state.synchronize(this); }
public TCPState state() { return state; }
public void processOctet(TCPOctetStream stream) {}
protected void changeState(TCPState state) { this.state = state; }
}
class TCPOctetStream{}