State Machines
Software engineers tend to think of (finite) state machines as a kind of intermediary interpreter. That is, you take a regular language and compile that into a state machine, then execute the state machine. The state machine itself is rarely visible to the developer: itâs an internal representationâoptimized, compressed, and bizarre.
However, it turns out that state machines are also valuable as first-class modeling languages for protocol handlers, such as ÃMQ clients and servers. ÃMQ makes it rather easy to design protocols, but weâve never defined a good pattern for writing those clients and servers properly.
A protocol has at least two levels:
How we represent individual messages on the wire
How messages flow between peers, and the significance of each message
Weâve seen in this chapter how to produce codecs that handle serialization. Thatâs a good start. But if we leave the second job to developers, that gives them a lot of room to interpret. As we make more ambitious protocols (file transfer + heartbeating + credit + authentication), it becomes less and less sane to try to implement clients and servers by hand.
Yes, people do this almost systematically. But the costs are high, and theyâre avoidable. In this section Iâll explain how to model protocols using state machines, and how to generate neat and solid code from those models.
My initial experience with using state machines as a software construction tool dates back to 1985 and my first real job making ...
Get ZeroMQ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.