00001 /* 00002 * Scale Stack 00003 * 00004 * Copyright 2010 Eric Day 00005 * 00006 * Licensed under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 00024 #ifndef SCALESTACK_EVENT_HANDLER_H 00025 #define SCALESTACK_EVENT_HANDLER_H 00026 00027 #include <memory> 00028 #include <stdint.h> 00029 #include <unistd.h> 00030 00031 #include <scalestack/event/handler_service.h> 00032 00033 namespace scalestack 00034 { 00035 namespace kernel 00036 { 00037 class module; 00038 } /* namespace kernel */ 00039 00040 namespace event 00041 { 00042 00043 class handler_provider; 00044 00049 class handler 00050 { 00051 public: 00052 00053 handler(handler_service& handler_service); 00054 00055 virtual ~handler(); 00056 00064 void start(void); 00065 00070 virtual void started(void); 00071 00079 virtual void shutdown(void); 00080 00085 void stop(void); 00086 00095 void set_timer(uint64_t milliseconds); 00096 00100 virtual void timer_expired(void); 00101 00108 void set_file_descriptor(int file_descriptor); 00109 00114 virtual void close_file_descriptor(int file_descriptor); 00115 00119 void watch_read(void); 00120 00124 virtual void read_ready(int file_descriptor); 00125 00129 void watch_write(void); 00130 00134 virtual void write_ready(int file_descriptor); 00135 00141 void run_now(void); 00142 00146 virtual void run(void); 00147 00148 protected: 00149 00150 kernel::module& _module; 00151 00152 private: 00153 00157 handler(const handler&); 00158 00162 handler& operator=(const handler&); 00163 00169 void _start(void); 00170 00175 void _stop(void); 00176 00177 handler_service& _handler_service; 00178 std::auto_ptr<handler_provider> _handler_provider; 00179 00180 friend class handler_provider; 00181 friend class handler_service; 00182 }; 00183 00188 class handler_provider 00189 { 00190 public: 00191 00192 handler_provider(kernel::module& module, handler* handler); 00193 00194 virtual ~handler_provider(); 00195 00199 virtual void start(void) = 0; 00200 00204 virtual void stop(void) = 0; 00205 00209 virtual void set_timer(uint64_t milliseconds) = 0; 00210 00214 virtual void set_file_descriptor(int file_descriptor) = 0; 00215 00219 virtual void watch_read(void) = 0; 00220 00224 virtual void watch_write(void) = 0; 00225 00229 virtual void run_now(void) = 0; 00230 00231 protected: 00232 00236 void _stop(void); 00237 00238 kernel::module& _module; 00239 handler* _handler; 00240 00241 private: 00242 00246 handler_provider(const handler_provider&); 00247 00251 handler_provider& operator=(const handler_provider&); 00252 }; 00253 00254 /* 00255 * Public handler methods. 00256 */ 00257 00258 inline handler::handler(handler_service& handler_service): 00259 _module(handler_service.get_module()), 00260 _handler_service(handler_service), 00261 _handler_provider(handler_service._add_handler(this)) 00262 { 00263 } 00264 00265 inline handler::~handler() 00266 { 00267 } 00268 00269 inline void handler::start(void) 00270 { 00271 _handler_service._start_handler(this); 00272 } 00273 00274 inline void handler::started(void) 00275 { 00276 } 00277 00278 inline void handler::shutdown(void) 00279 { 00280 stop(); 00281 } 00282 00283 inline void handler::stop(void) 00284 { 00285 _handler_provider->stop(); 00286 } 00287 00288 inline void handler::set_timer(uint64_t milliseconds) 00289 { 00290 _handler_provider->set_timer(milliseconds); 00291 } 00292 00293 inline void handler::timer_expired(void) 00294 { 00295 } 00296 00297 inline void handler::set_file_descriptor(int file_descriptor) 00298 { 00299 _handler_provider->set_file_descriptor(file_descriptor); 00300 } 00301 00302 inline void handler::close_file_descriptor(int file_descriptor) 00303 { 00304 close(file_descriptor); 00305 } 00306 00307 inline void handler::watch_read(void) 00308 { 00309 _handler_provider->watch_read(); 00310 } 00311 00312 inline void handler::read_ready(int) 00313 { 00314 } 00315 00316 inline void handler::watch_write(void) 00317 { 00318 _handler_provider->watch_write(); 00319 } 00320 00321 inline void handler::write_ready(int) 00322 { 00323 } 00324 00325 inline void handler::run_now(void) 00326 { 00327 _handler_provider->run_now(); 00328 } 00329 00330 inline void handler::run(void) 00331 { 00332 } 00333 00334 /* 00335 * Private handler methods. 00336 */ 00337 00338 inline void handler::_start(void) 00339 { 00340 _handler_provider->start(); 00341 } 00342 00343 inline void handler::_stop(void) 00344 { 00345 _handler_service._stop_handler(this); 00346 } 00347 00348 /* 00349 * Public handler_provider methods. 00350 */ 00351 00352 inline handler_provider::handler_provider(kernel::module& module, 00353 handler* handler): 00354 _module(module), 00355 _handler(handler) 00356 { 00357 } 00358 00359 inline handler_provider::~handler_provider() 00360 { 00361 } 00362 00363 /* 00364 * Protected handler_provider methods. 00365 */ 00366 00367 inline void handler_provider::_stop(void) 00368 { 00369 _handler->_stop(); 00370 } 00371 00372 } /* namespace event */ 00373 } /* namespace scalestack */ 00374 00375 #endif /* SCALESTACK_EVENT_HANDLER_H */
1.6.3