How to read a FSM diagram
How do i take this diagram and translate it into a useable program. I'm not really sure how to read this diagram. If someone could just kind of walk me through, 开发者_Python百科maybe show an example of code and how it relates to the diagram, that be great.
Thanks!
Circles with text inside are the states. Text describes what the state is.
Dashed arrow points to starting state.
Outgoing arrows determine where this state could change. Beside of the arrow is the text divided by the line into upper part and lower part. Lower part is actions that should take place when arrow transition is executed. Upper part is conditions. When they are true - this transition is executed (and so lower part).
Lambda symbol means you should do nothing except changing current state when transition takes place.
So lower parts have coarse corresponding to your functions. And upper parts are the points where you should wait for conditions - polling or async waiting for pending I/O packets, whatever.
Here is some pseudo-code similar to C (I've written it just here so do not assume it works or even compiles):
enum State { WaitFor0Call, WaitForAck0, WaitForCall1, WaitForAck1 }
int main() {
State state = WaitFor0Call;
while (1) {
switch (state) {
case WaitFor0Call:
if (rdt_rcv(rcvpkt)) continue;
if (rdt_send(data)) {
state = WaitForAck0;
sndpkt = make_pkt(0, data, checksum);
udt_send(sndpkt);
start_timer();
}
break;
case WaitForAck0:
// ...similar code...
break;
case WaitForCall1:
// ...similar code...
break;
case WaitForAck1:
// ...similar code...
break;
}
}
}
You should also take into account that receive and send functions could be blocking, so code if (rdt_rcv(rcvpkt)) whatever;
is technically incorrect as you don't check for rdt_send
until it returns control. So FSM communicates only logical flow, not technical aspects of how it should be organized, thread management etc. And my code doesn't show this aspects also because it could be decently complicated depending on your needs and because you didn't give enough details to make informed advices on these sort of things :)
My only guess is that you would have some sort of bi-directed stream (for input and output respectively) and conditions would be like if (there_is_ready_for_consuming_packet_in_the_input_queue) continue;
and if (data_was_put_to_outgoing_stream_successfully) ...;
精彩评论