FFL
1.0
Finfly Foundation Library
|
00001 /* $Id: server.h 2708 2011-12-24 14:02:14Z roma $ */ 00002 #ifndef _FFL_CSSC_SERVER_H_ 00003 #define _FFL_CSSC_SERVER_H_ 00004 00005 #include <signal.h> 00006 #include <queue> 00007 #include <os.h> 00008 #include "../../ipc/socket.h" 00009 #include "../logbook.h" 00010 #include "../request.h" 00011 #include "../tag.h" 00012 00013 namespace ffl 00014 { 00015 00016 namespace cssc 00017 { 00018 00024 namespace multithreaded 00025 { 00026 00030 class server 00031 { 00032 public: 00033 00040 class user 00041 { 00042 }; 00043 00044 class processor; 00045 00046 class client; 00047 00049 struct signal 00050 { 00051 00059 static long Timeout; 00060 00061 enum tag 00062 { 00063 NONE, 00064 READY, 00065 CLOSE, 00066 PERFORM, 00067 CANCEL, 00068 }; 00069 00070 unsigned char Tag; 00071 00072 client* Client; 00073 00075 signal() throw(){} 00076 00081 signal(tag _t, client* _c = 0) throw():Tag(_t), Client(_c){} 00082 00086 signal(const signal& _s) throw():Tag(_s.Tag), Client(_s.Client){} 00087 00088 }; 00089 00091 class client:public ipc::socket 00092 { 00093 public: 00094 00096 enum state 00097 { 00098 QUEUED = 1, 00099 PROCESSING, 00100 COMPLETED, 00101 } State; 00102 00104 std::auto_ptr<user> User; 00105 00111 processor* Processor; 00112 00113 signal::tag (processor::*Action)(client* _c) throw(...); 00114 00120 client(server& _server, ipc::socket& _listener) throw(...):Server(_server), ipc::socket(_server.Environment, _listener), Processor(0), State(COMPLETED) 00121 { 00122 Server.Clients.push_back(this); 00123 } 00124 00126 ~client() 00127 { 00128 Server.Clients.remove(this); 00129 } 00130 00131 void write(const cssc::failure& _x, long _t) throw(...); 00132 00133 private: 00134 00136 server& Server; 00137 00138 }; 00139 00141 class processor:public os_thread 00142 { 00143 public: 00144 00145 void enqueue(signal::tag _t) throw(...); 00146 00147 void enqueue(signal::tag _t, client* _c) throw(...); 00148 00149 void dequeue(signal& _s) throw(...); 00150 00155 std::auto_ptr<ipc::socket> Processor; 00156 00161 std::auto_ptr<ipc::socket> Dispatcher; 00162 00169 int QueueSize; 00170 00172 static const long IOTimeout; 00173 00174 signal::tag do_authorize(client* _c) throw(...); 00175 00189 signal::tag do_request(client* _c) throw(...); 00190 00191 processor(server& _server, const char* _log) throw(...); 00192 00193 bool inactive() throw(){return getstate() != 0;} 00194 00195 void cancel() throw(){} 00196 00203 logbook Log; 00204 00210 static processor* self() throw(){return Self;} 00211 00212 protected: 00213 00214 server& Server; 00215 00216 int run() throw(); 00217 00218 virtual void idle() throw(){Log.timestamp("idle");} 00219 00220 virtual user* authorize(const char* _name, const char* _pass) throw(...){throw cssc::failure("SERV", "0001","Authorization failed"); return 0;} 00221 00231 virtual void process(user& _u, request& _r) throw(...){} 00232 00244 virtual void trace_exception(const user& _u) throw(){} 00245 00265 void checkpoint() throw(...); 00266 00267 private: 00268 00276 static __declspec(thread) processor* Self; 00277 00283 client* Client; 00284 00285 }; 00286 00297 server 00298 ( 00299 size_t _max_users, 00300 size_t _max_processors, 00301 const char* _log_name, 00302 long _idle_timeout = 5000 00303 ) throw():MaxClients(_max_users), MaxProcessors(_max_processors), Log(_log_name), CountProcessors(0), 00304 IdleTimeout(_idle_timeout) 00305 {} 00306 00323 void run(const char* _host, const char* _service) throw(...); 00324 00334 void cancel() throw(); 00335 00336 protected: 00337 00354 virtual void idle() throw(...); 00355 00366 virtual void trace_request(const user& _u) throw(){} 00367 00372 virtual void just_started() throw(){} 00373 00378 virtual void just_ready() throw(){} 00379 00385 virtual void just_canceled() throw(){} 00386 00396 virtual processor* scale(const char* _log_name) throw(...) 00397 { 00398 return new processor(*this, _log_name); 00399 } 00400 00401 logbook Log; 00402 00403 private: 00404 00405 ipc::socket::environment Environment; 00406 00407 size_t MaxClients; 00408 00409 size_t MaxProcessors; 00410 00411 size_t CountProcessors; 00412 00413 long IdleTimeout; 00414 00415 std::list<processor*> Processors; 00416 00417 std::list<ipc::socket*> Listeners; // Список слушающих сокетов. 00418 00419 std::list<client*> Clients; 00420 00428 void initialize_listeners(const char* _n, const char* _s) throw(...); 00429 00430 volatile sig_atomic_t Cancel; 00431 00432 void dispatch(client* _c) throw(...); 00433 00440 void sweep_out_inactive_processors() throw(); 00441 00442 void dismiss(client* _c) throw(); 00443 00444 }; 00445 00446 } 00447 00448 } 00449 00450 } 00451 00452 #endif