-
Notifications
You must be signed in to change notification settings - Fork 12
/
LISCommState.cs
192 lines (183 loc) · 6.07 KB
/
LISCommState.cs
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
using static UniversaLIS.UniversaLIService;
namespace UniversaLIS
{
public class LisCommState : ILISState
{
public ILISState CommState { get; set; }
public CommFacilitator comm { get; set; }
public LisCommState(CommFacilitator comm)
{
this.comm = comm;
CommState = new IdleState(this.comm);
}
public void RcvInput(string InputString)
{
switch (InputString)
{
case Constants.ACK:
RcvACK();
break;
case Constants.NAK:
RcvNAK();
break;
case Constants.ENQ:
RcvENQ();
break;
case Constants.EOT:
RcvEOT();
break;
default:
RcvData(InputString);
break;
}
}
public void RcvACK()
{
CommState.RcvACK();
if (CommState is TransEnqState)
{
// Transition to TransWaitState.
ChangeToTransWaitState();
}
if (CommState is IdleState) // In case the ACK comes back before we've transitioned to TransENQState.
{
// DEBUG: Better keep track of how often this happens, for testing purposes.
AppendToLog("RcvACK in IdleState!");
// Transition to TransENQState?
ChangeToTransENQState();
CommState.RcvACK();
// Transition to TransWaitState.
ChangeToTransWaitState();
}
if (CommState is TransWaitState && comm.CurrentMessage.FrameList.Count < comm.CurrentFrameCounter)
{
// When all frames have been sent, return to IdleState.
ChangeToIdleState();
}
}
public void RcvData(string InputString)
{
CommState.RcvData(InputString);
}
public void RcvENQ()
{
CommState.RcvENQ();
if (CommState is IdleState)
{
ChangeToRcvWaitState();
}
if (CommState is TransEnqState)
{
ChangeToIdleState();
}
}
public void RcvEOT()
{
CommState.RcvEOT();
if (CommState is RcvWaitState)
{
// Return to Idle.
ChangeToIdleState();
if (comm.OutboundMessageQueue.Count > 0)
{
// Don't make the operator wait for the timer tick.
IdleCheck();
}
}
}
public void RcvNAK()
{
CommState.RcvNAK();
if (CommState is TransWaitState && comm.NumNAK == 6)
{
ChangeToIdleState();
}
if (CommState is TransEnqState)
{
ChangeToIdleState();
}
}
public void HaveData()
{
if (CommState is IdleState)
{
CommState.HaveData();
// Change state
ChangeToTransENQState();
#if DEBUG
AppendToLog("HaveData complete.");
#endif
}
}
public void ChangeToIdleState()
{
CommState = new IdleState(comm);
comm.CurrentFrameCounter = 0;
comm.TransTimer.Reset(-1);
#if DEBUG
AppendToLog("CommState changed to IdleState!");
#endif
}
public void ChangeToTransENQState()
{
CommState = new TransEnqState(comm);
#if DEBUG
AppendToLog("CommState changed to TransENQState!");
#endif
}
public void ChangeToTransWaitState()
{
CommState = new TransWaitState(comm);
#if DEBUG
AppendToLog("CommState changed to TransWaitState!");
#endif
}
public void ChangeToRcvWaitState()
{
CommState = new RcvWaitState(comm);
#if DEBUG
AppendToLog("CommState changed to RcvWaitState!");
#endif
}
public void TransTimeout()
{
if (CommState is TransEnqState || CommState is TransWaitState)
{
// Send EOT and return to idle state.
comm.Send(Constants.EOT);
if (CommState is TransWaitState && comm.CurrentMessage.FrameList.Count > comm.CurrentFrameCounter)
{
comm.OutboundMessageQueue.Enqueue(comm.CurrentMessage);
comm.CurrentMessage = new Message(comm);
}
comm.CurrentMessage = new Message(comm);
comm.CurrentFrameCounter = 0;
ChangeToIdleState();
}
}
public void RcvTimeout()
{
if (CommState is RcvWaitState)
{
// Discard last incomplete message.
if (comm.CurrentMessage.Terminator < 'E')
{
comm.CurrentMessage = new Message(comm);
}
else
{
comm.ProcessMessage(comm.CurrentMessage);
}
// Return to idle state.
CommState = new IdleState(comm);
}
}
public void IdleCheck()
{
if (CommState is IdleState && comm.OutboundMessageQueue.Count > 0)
{
HaveData();
}
}
}
}