views:

328

answers:

4

Say you are writing an application that must implement the HTTP protocol. Protocols are quite complex and may allow several commands depending on which stage of a transaction they are in.

As an example, look at SMTP. An SMTP server must throw an error if the "data" command is sent before receiving "rcpt" and "mail".

My question is: what is the best way to handle protocols such as this in code? Are there any design patterns related to this?

Edit: This question relates to the theory behind implementing protocols. I'm aware that using a library is the best approach in practise.

+8  A: 

State Machines

To my mind, a state machine is the easiest way to model and handle protocols. A state would be reached by several transitions relating to valid commands received. Each state would then allow only a certain subset of commands.

State machines are used in compiler construction for lexical analysis of a program. I see the problem of protocol implementation as a special case of this.

fluffels
+1  A: 

The best ways to handle protocols like this is to use a library. Almost every computer language used on earth has preexisting, well tested libraries to handle http and smtp.

Unless you want to develop the protocol handler as an exercise, this is sound advice. These libraries are generic and tend to get well tested. Take a look at the Python standard library for an example of this phenomenon.
ConcernedOfTunbridgeWells
Thanks. My question related more to how to implement them as a theoretical question. Should I need to use them in practise I'll definitely use a library.
fluffels
A: 

@fluffels@

Zed Shaw (author of Mongrel) agrees with you; he uses Ragel.

Hank Gay
A: 

I agree with a28, the best way is to either:

  • Use a library which implements the protocol server
  • Write your application as an extension to an existing server (e.g. web server extension via IIS, Apache APIs etc, Sendmail Milter etc) OR
  • Modify an existing server to make RPC calls to your application as it receives requests.

Writing your own implementation of the protocol is likely to result in a buggy implementation with interoperability problems.

An interesting tool to do this is twisted which is python-specific but rather clever and includes implementations of numerous existing protocols (HTTP, SMTP, IRC etc).

MarkR