snacktime wrote:
> So I'm refactoring a very ugly piece of client code that needs to
> implement some fairly complicated error correction over a line based
> tcp protocol. There are about 10 different error scenarios I have to
> detect and respond differently to. Think error correction over serial
> lines, that's actually where this was derived from and now it's
> layered over a tcp connection. Most of the scenarios involve several
> back and forth messages between client and host. Most of the
> scenarios are very similar, so if you think of it as a tree I might
> not know what scenario I am dealing with until I've gotten 2-3 levels
> deep, and at any point in the tree I need to know where I am and what
> the possible branches are I can take based on the next response from
> the host.
>
> Any suggestions on how to implement this cleanly?
>
> Chris
For clues to a possible approach, look at the HTTP client implementation
in EventMachine. Sync to SCM, and it's in version_0/lib/protocols.
You can get as crazy with this kind of thing as you want, but if your
requirements are simple you can get away with a variable that indicates
the current protocol state. Then a simple case statement will constrain
your possible paths. It's quite rare for a network protocol to be more
complicated than that. But if yours is, consider using a LALR(1)
grammar.
--
Posted via http://www.ruby-forum.com/.