26.3. Implementing Sequential Logging Servers by Andy Oram, Greg Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Implementing Sequential Logging Servers

This section demonstrates the implementation of logging servers that feature sequential concurrency models—i.e., all processing is performed in a single thread. We cover both iterative and reactive implementations of sequential logging servers.

An Iterative Logging Server

Iterative servers process all log records from each client before handling any log records from the next client. Since there is no need to spawn or synchronize threads, we use the Null_Mutex facade to parameterize the Iterative_Logging_Server subclass template, as follows:

	template <typename ACCEPTOR>
	   class Iterative_Logging_Server :
	  virtual Logging_Server<ACCEPTOR, Null_Mutex> {
	public:
	  typedef Logging_Server<ACCEPTOR, Null_Mutex>::HANDLER HANDLER;
	  Iterative_Logging_Server (int argc, char *argv[]);

	protected:
	  virtual void open (void);
	  virtual void wait_for_multiple_events (void) {};
	  virtual void handle_connections (void);
	  virtual void handle_data
	    (typename ACCEPTOR::PEER_STREAM *stream = 0);
	  HANDLER log_handler_;

	  // One log file shared by all clients.
	  std::ofstream logfile_;
	};

Implementing this version of our server is straightforward. The open( ) method decorates the behavior of the method from the Logging_Server base class by opening an output file before delegating to the parent's open( ), as follows:

 template <typename ACCEPTOR> void Interative_Logging_Server<ACCEPTOR>::open (void) { logfile_.open (filename_.c_str ()); if (!logfile_.good ()) throw std::runtime_error; // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required