title: Designing a class architecture for network messages
body: <p>I have client/server applications and a very simple protocol for communication. More precisely, it's a set of commands a server sends and a set of requests a client can make.</p>
  
  <p>The idea is as follows:</p>
  
  <p>When a command is made by the server, a client has to execute it.
  When a request is made, server checks permissions and if everything is ok it grants the request.</p>
  
  <p>The application is written in C++ and I got slightly stuck designing the architecture for this sort of communication protocol. Assuming that command/request is a tab-delimited string with first parameter being the name of the message, I was planning to make a <code>MessageManager</code> class which would store all messages in a hash-table and retrieve them when necessary. The problem here is this:</p>
  
  <pre><code>typedef std::vector&lt; std::string &gt; ArgArray;
  class Request : public Message
  {
  public:
     Request( const char *name ) : Message( name ) { }
  #ifdef CLIENT
     /** problem here **/
     virtual void make( ... ) = 0;
  #elif defined SERVER
     virtual void grant( const Client &amp;c, const ArgArray &amp;params ) const = 0;
  protected:
     virtual void checkPermissions( const Client &amp;c, const ArgArray &amp;params ) const = 0;
  #endif
  };
  </code></pre>
  
  <p>Because different messages can take different arguments to be constructed I can't really create a complete interface. For example, some messages might need a simple string to be constructed, whereas others might need some numeric data. This complicates things and makes the design a bit untidy... I.e. I have to get round the problem by ommitting <code>make()</code> from interface definition and simply add different <code>make()</code> for each Request I make. Also, if I wish to store pointers to different Requests in one container I cannot use <code>dynamic_cast</code> because <code>Request</code> is not a polymorphic type. An obvious (untidy) solution would use <code>make( int n, ... )</code> definition and use <code>stdarg.h</code> to extract different arguments but I consider this to be unsafe and confusing for the programmer.</p>
  
  <p>There is obviously a design flaw in my idea. I already have a solution in mind but I am just wondering, how would people at SO tackle this problem? What sort of Object architecture would you use? Is there a simpler approach that could solve this problem? There aren't any specific requirements to this except to keep it as simple as possible and to keep the actual protocol as it is (tab-delimited strings with first parameter indicating which message it is).</p>