Wexus2 0.20
wexus2.src/wexus/HTTPServer.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_HTTPSERVER_H__
00009 #define __INCLUDED_WEXUS_HTTPSERVER_H__
00010 
00011 #include <QString>
00012 
00013 #include <wexus/TR1.h>
00014 #include <wexus/Exception.h>
00015 
00016 namespace wexus
00017 {
00018   class HTTPParams;
00019   class HTTPServer;
00020 
00021   class HTTPHandler;  //fwd
00022 }
00023 
00024 /**
00025  * Options that can be passed to the HTTPServer upon bootup.
00026  *
00027  * @author Aleksander Demko
00028  */
00029 class wexus::HTTPParams
00030 {
00031   public:
00032     /// constructor
00033     HTTPParams(void);
00034 
00035     /// port to listen to, 8080 by default.
00036     int port(void) const { return dm_port; }
00037 
00038     int numThreads(void) const { return dm_numthreads; }
00039 
00040     void setPort(int p);
00041 
00042     void setNumThreads(int t);
00043 
00044     /**
00045      * Sets the handeler that will receive
00046      * all the http events. By default, it
00047      * is null, for no handler.
00048      *
00049      * @author Aleksander Demko
00050      */ 
00051     void setHandler(HTTPHandler *handler);
00052 
00053     HTTPHandler * handler(void) const { return dm_handler; }
00054 
00055   private:
00056     int dm_port;
00057     int dm_numthreads;
00058     HTTPHandler *dm_handler;
00059 };
00060 
00061 /**
00062  * Base class/interface for all HTTP servers.
00063  *
00064  * @author Aleksander Demko
00065  */ 
00066 class wexus::HTTPServer
00067 {
00068   public:
00069     class Exception : public wexus::Exception
00070     {
00071       public:
00072         /// no log message constructor... is this version needed?
00073         //Exception(void);
00074         /// log message constructor
00075         Exception(const QString &logmsg);
00076 
00077         virtual ~Exception() throw ();
00078 
00079         const QString & logMessage(void) const { return dm_logmsg; }
00080 
00081       protected:
00082         QString dm_logmsg;
00083     };
00084     class PortInUseException : public Exception
00085     {
00086       public:
00087         PortInUseException(int port);
00088     };
00089 
00090   public:
00091     /// destructor - will stop the server is needed
00092     virtual ~HTTPServer();
00093 
00094     /**
00095      * Create a new HTTPServer implementation, based on some decendant.
00096      * This is a factory method. In the future, this could take a string
00097      * or perhaps be moved to a whole factory class system.
00098      *
00099      * Exceptions might be thrown.
00100      *
00101      * @author Aleksander Demko
00102      */ 
00103     static std::shared_ptr<wexus::HTTPServer> factoryNew(const wexus::HTTPParams &params);
00104 
00105     /// is the web server currently running?
00106     virtual bool isRunning(void) const = 0;
00107     /// starts the web server
00108     virtual void start(void) = 0;
00109     /// notifies the web server to stop. you should still do a wait()
00110     /// this function may or may not block
00111     virtual void quit(void) = 0;
00112     /// waits until the server finishes
00113     virtual void wait(void) = 0;
00114 
00115   protected:
00116     HTTPServer(void);
00117 };
00118 
00119 #endif
00120 
 All Classes Namespaces Functions Variables Enumerations Enumerator