/*
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * Author:
 *         Stefan Denk <denk@ira.uka.de>
 *         Kendy Kutzner <kutzner@ira.uka.de>
 *
 * $Id: serverthread.h,v 1.4 2006/01/05 10:31:45 denk Exp $
 *
 */

#ifndef _SERVERTHREAD_H_
#define _SERVERTHREAD_H_

#include <string>
using namespace std;

#include <thread.h>

#include "watchdog.h"
#include "vdrrecording.h"

/** The listening socket.
 * This thread owns the socket listen for incoming connections from other
 * clients. As soon as one connection comes in, another thread is spawned and
 * the listening contiues.
 */

class serverthread : public cThread {
	public:
		/// ctor for serverthread
		serverthread(string transport, watchdog *wd, vdrrecordings * recording);
		/** The main loop
		 * Listens for connections. Incoming connections are accepted
		 * and a new subthread is created.
		 */
		void Action();
	private:
		/** Talk with one client.
		 * Start a new subthread after the main loop has accepted a new
		 * connection.
		 */
		void spawn_subserverthread(int connfd, watchdog * mywatchdog, vdrrecordings * recording);

		/** A thread serving one client.
		 * These threads are started by the main loop. Each
		 * csubserverthread serves one client connection. The thread
		 * terminates as soon as the socket is closed. Talks to class
		 * clientthread on the other end.
		 */
		class csubserverthread : public cThread{
			friend class serverthread;
			public:
				/** Create the thread.
				 * Start to serve the client.
				 * \param connfd The socket connected to the client.
				 */
				csubserverthread(int connfd, watchdog * _wd, vdrrecordings * recording);
				/** Serve the client.
				 * Talks to the client. Should close the socket
				 * if it wants to return.
				 */
				void Action();
			private:
				///\bug doc me
				int _connfd;
				watchdog * _watchdog;
				/// a pointer to the vdrrecordings class
				vdrrecordings * _recording;
				
		};
		/// the port this server will listen on
		string _transport;
		/// a pointer to the speed watchdog, to be passed on to the subserverthread(s)
		watchdog * _wd;
		/// a pointer to the vdrrecordings class
		vdrrecordings * _recording;




};


#endif // _SERVERTHREAD_H_
