views:

748

answers:

8

I've come to realize that several questions I asked in the past, such as this really boil down to a more fundamental question.

Are there any well known design patterns for network communications and by virtue of it's nature, protocol construction/parsing? A google search has not revealed much.

Note that i'm not looking for solutions for any given problem, i'm looking for documented design patterns dealing with network communications and their protocols.

EDIT:

Please, don't suggest various implementation details or discuss specific protocols unless it's tied to a design pattern. Protocol design is not the issue, it's the design patterns for creating or parsing protocols that i'm looking for, not to mention the communication patterns themselves.

EDIT2:

I find it hard to believe that nobody has come up with any common patterns for network communication. Yes, I know "it depends", but you can say that about any project, yet there are lots of patterns that cover general ideas.

A: 

I don't know about patterns, as such, but there's a few "obvious" selection points. First, do you want to use ASN.1 or not (this influences a WHOLE lot)? Second, do you want a human-readable protocol or a binary one? Third, do you want any security aspects in your protocol?

Not that answering "want to use ASN.1" will force teh answer to quite a few of the protocol design questions.

Vatine
+1  A: 

I recommend: abstract away the network protocol/s.

First decide what are the functionality, the modules and the APIs between them. Then decide what protocol is the data going to ride across the network.

Then carefully encapsulate all the network issues in their own layer so you can later apply encryption, compression, add http transport (to pass firewalls) or whatever you want to add later in a manner orthogonal to functionality.

flybywire
A: 

I don't know about design patterns, but researching existing protocols is probably a good starting point, especially "modern" ones that have been standardized.

BitTorrent is a highly popular decentralized protocol that has a number of extensions.

OpenSSH is another good candidate; it supports feature negotiation, multiple encryption types, and de/muxing channels.

VoIP protocols are good for streaming applications: RTP and H.323

Network routing protocols are good as well: BGP (and extensions), LDP, VRRP/CARP.

HUAGHAGUAH
+4  A: 

This is a pretty broad question and its treatment likely requires a fairly dense book.

I don't know of any such resource myself, but lets think this through and consider what would be the dimensions of a network communication pattern space:

connection modality: { connection-based, connection-less}

interaction modality: { synchronous, asynchronous }

conversation complexity: { command-response, dialog}

message form: { freeform-stream, semi-structured block, fully-structured block } ..?

A good place to start is to take the TCP/IP family of protocols, map them to the above space, and take a look at the implementation(s) of one or more specimens that occupy a unique position in the above protocol-characteristics pattern space. Source code of your favorite *nix os would be a good place to look.

Parser implementations would probably fall into two broad categories: {command-switched processing, finite-state-machine}.

The former is (obviously) the simpler of the two and likely the initial implementation (unless you've done this sort of thing before).

The latter is (likely) more robust, efficient (in terms of loc), and would allow for adopting changes to a protocol (if it is still subject to design change).

(The underlying (virtual) OS networking facilities (of course) also greatly influence the implementation. Take JVM, for example: NIO selection based channel processing would work quite nicely with a FSM.)

Hope that helps.

+2  A: 

I would say that the chain of responsability pattern could be usefull to send/receive data from/to the network.

You build a series of commands to send to the server from the client. Each command is processed through the chain of responsability, with data added to handle the command correctly.

On data send, the chain could look like that


Command   --> Wrap some       --> Encrypt --> Send data
to send       data around 
              the command 
              (source, extra 
              information if 
              needed)

On data receive, the chain could be similar, but the other way around

Receive Data  -->  Decrypt --> Unwrap extra data --> Execute command

You can check this article for more informations about the chain of responsability. http://www.vincehuston.org/dp/chain.html

Martin
A: 

Haven't gone through this thoroughly, but I guess this is good doc:

Also, this looks like a good book:

CuriousTiger
A: 

I had the same question and went for a FSM model.

Look up the state pattern. I adapted it a little, creating the states using a factories (look up the factory pattern), with the states fetched by id from the factories and the factory implementation used being specified at runtime, depending on the protocol version sent from the server. You can use the observer pattern (look up the observer pattern) also to have whatever message loop you are using fire events in the states if the message loop holds one observer / listener at a time. It would be relatively easy to add encryption (Decorator pattern perhaps) and so on also. It seems reasonably generic and scalable if not over-engineered. Certainly corresponds with the open closed principle and the like. Wouldn't mind knowing of other solutions though.

John
+1  A: 

found this paper http://hillside.net/plop/plop2k/proceedings/Parssinen/Parssinen.pdf

zimaj