Savarese Software Research Corporation
EventLoop.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2008 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WISP_SERVICE_EVENT_LOOP_H
00023 #define __SSRC_WISP_SERVICE_EVENT_LOOP_H
00024 
00025 #include <ssrc/wisp/service/EventHandler.h>
00026 
00027 __BEGIN_NS_SSRC_WISP_SERVICE
00028 
00029 struct EventLoopState;
00030 
00034 class EventLoop {
00035 public:
00036 
00037   static const bool Once    = true;
00038   static const bool Persist = false;
00039 
00040   typedef int EventIO;
00041 
00042   static const EventIO None;
00043   static const EventIO Read;
00044   static const EventIO Write;
00045   static const EventIO Error;
00046   static const EventIO Hangup;
00047   /*
00048   static const EventIO Signal;
00049   static const EventIO Timeout;
00050   */
00051   explicit EventLoop() SSRC_DECL_THROW(std::runtime_error);
00052 
00053   ~EventLoop();
00054 
00055   void add_handler(EventHandler & handler,
00056                    const int events,
00057                    const TimeValue & timeout = InfiniteTimeValue,
00058                    const bool once = Persist)
00059     SSRC_DECL_THROW(std::runtime_error);
00060 
00061   void remove_handler(EventHandler & handler);
00062 
00063   unsigned int count_handlers();
00064 
00065   unsigned int count_io_handlers();
00066 
00067   bool running();
00068 
00069   void start();
00070 
00071   void stop();
00072 
00073 private:
00074   // Only allow EventLoop.cc to see implementation details.
00075   // This speeds up client compilation times and avoids potential
00076   // conflicts arising from our placing system headers in custom
00077   // namespaces (e.g., Linux:: for <sys/epoll.h>).  This comes
00078   // at the expense of awkwardness, requiring client code to
00079   // specify events as int instead of the native type,
00080   // and inability to inline accessor functions.
00081   EventLoopState *_state;
00082 };
00083 
00084 class EventInfo {
00085   friend void EventLoop::start();
00086 
00087   //int _fd;
00088   bool _timeout;
00089   int _io_events;
00090   EventLoop & _loop;
00091   TimeValue _now;
00092 
00093 public:
00094 
00095   explicit EventInfo(EventLoop & loop,
00096                      //const int fd = -1,
00097                      const int io_events = EventLoop::None,
00098                      const bool timeout = false) :
00099   /* _fd(fd),*/ _timeout(timeout), _io_events(io_events), _loop(loop), _now()
00100   { }
00101 
00102   int io_events() const {
00103     return _io_events;
00104   }
00105 
00106   /*
00107     int descriptor() const {
00108     return _fd;
00109     }
00110   */
00111   EventLoop & event_loop() const {
00112     return _loop;
00113   }
00114   /*
00115     bool signal_event() const {
00116     return (_event & 0);
00117     }
00118   */
00119 
00124   const TimeValue & now() const {
00125     return _now;
00126   }
00127 
00128   bool timeout_event() const {
00129     return _timeout;
00130   }
00131 
00132   bool error_event() const {
00133     return (_io_events & EventLoop::Error);
00134   }
00135 
00136   bool hangup_event() const {
00137     return (_io_events & EventLoop::Hangup);
00138   }
00139 
00140   bool read_event() const {
00141     return (_io_events & EventLoop::Read);
00142   }
00143 
00144   bool write_event() const {
00145     return (_io_events & EventLoop::Write);
00146   }
00147 };
00148 
00149 __END_NS_SSRC_WISP_SERVICE
00150 
00151 #endif

Savarese Software Research Corporation
Copyright © 2006-2010 Savarese Software Research Corporation. All rights reserved.