Changeset 4881 for branches/erijo-dev

Show
Ignore:
Timestamp:
04/01/07 21:44:55 (20 months ago)
Author:
erijo
Message:

Add an eventloop class and make the daemon use it.

Location:
branches/erijo-dev/licq
Files:
3 added
5 modified

Legend:

Unmodified
Added
Removed
  • branches/erijo-dev/licq/CMakeLists.txt

    r4853 r4881  
    66if (NOT CMAKE_BUILD_TYPE) 
    77  set(CMAKE_BUILD_TYPE Debug CACHE STRING 
    8     "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Maintainer." 
     8    "Choose the type of build, options are: " 
     9    "None Debug Release RelWithDebInfo MinSizeRel Maintainer." 
    910    FORCE) 
    1011endif (NOT CMAKE_BUILD_TYPE) 
     
    5556 
    5657# Generate config.h 
    57 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) 
     58configure_file( 
     59  ${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake 
     60  ${CMAKE_CURRENT_BINARY_DIR}/config.h 
     61) 
    5862 
    5963add_subdirectory(src) 
  • branches/erijo-dev/licq/licq/interface/daemon.h

    r4876 r4881  
    2121#define LICQ_IDAEMON_H 
    2222 
     23#include "licq/interface/event.h" 
     24 
    2325#include <string> 
    2426 
     
    3537 
    3638  virtual void* getService(const std::string& name) const = 0; 
     39  virtual void pushEvent(IEventPtr& event) = 0; 
    3740}; 
    3841 
  • branches/erijo-dev/licq/src/CMakeLists.txt

    r4876 r4881  
    22  daemon.cpp 
    33  event.cpp 
     4  eventloop.cpp 
    45  eventqueue.cpp 
    56  logfile.cpp 
  • branches/erijo-dev/licq/src/daemon.cpp

    r4876 r4881  
    2020#include "daemon.h" 
    2121 
    22 void Licq::TDaemon::onEvent(IEventPtr& event) 
     22#include <sys/time.h> 
     23#include <time.h> 
     24 
     25void Licq::TDaemon::onEvent(const IEventPtr& event) 
    2326{ 
     27  Log.info("TDaemon::onEvent(%s)", event->getEventName().c_str()); 
    2428  TEventHandlers::iterator handler = EventHandlers.find(event->getEventName()); 
    2529  if (handler != EventHandlers.end()) 
     
    3034} 
    3135 
    32 void Licq::TDaemon::onPluginCreate(IEventPtr& event) 
     36void Licq::TDaemon::onPluginCreate(const IEventPtr& event) 
    3337{ 
    3438  std::string name; 
    3539  if (event->getProperty("name", &name)) 
    3640  { 
    37     // something 
     41    Log.info("Creating plugin %s", name.c_str()); 
     42  } 
     43} 
     44 
     45void Licq::TDaemon::onTimeout(int timer) 
     46{ 
     47  static int count = 0; 
     48  timeval now; 
     49  ::gettimeofday(&now, NULL); 
     50  Log.info("Timeout at %ld:%03ld for timer %d", now.tv_sec, now.tv_usec/1000, timer); 
     51  if (count++ > 10) 
     52    EventLoop.exit(0); 
     53 
     54  if (count % 2) 
     55  { 
     56    IEventPtr event = IEvent::create(0, "plugin:create"); 
     57    event->setProperty("name", "testplugin"); 
     58    pushEvent(event); 
     59 
     60    event = IEvent::create(0, "invalid:event"); 
     61    pushEvent(event); 
     62  } 
     63 
     64  if (count == 5) 
     65    Pipe.write("TESTING", sizeof("TESTING") - 1); 
     66} 
     67 
     68void Licq::TDaemon::onFileDescriptor(int fd) 
     69{ 
     70  if (fd == Pipe.getFileDescriptor()) 
     71  { 
     72    char buf; 
     73    Pipe.read(&buf, 1); 
     74    Log.info("Read %c from pipe", buf); 
    3875  } 
    3976} 
    4077 
    4178Licq::TDaemon::TDaemon() 
    42   : PluginRepository(NULL) 
     79  : PluginRepository(NULL), EventLoop(&EventQueue, this) 
    4380{ 
    4481  // Setup event handlers 
     
    68105  Log.debug("Done rebuilding plugin cache."); 
    69106 
    70   return 0; 
     107  EventLoop.addTimer(1000, this); 
     108  EventLoop.addTimer(500, this); 
     109 
     110  EventLoop.addFileDescriptor(Pipe.getFileDescriptor(), 
     111                              TEventLoop::ACTION_READ_FD, this); 
     112  return EventLoop.run(); 
    71113} 
    72114 
    73 void* Licq::TDaemon::getService(const std::string& name) const 
     115void* Licq::TDaemon::getService(const std::string& /* name */) const 
    74116{ 
    75117  return NULL; 
    76118} 
     119 
     120void Licq::TDaemon::pushEvent(IEventPtr& event) 
     121{ 
     122  EventQueue.push(event); 
     123} 
  • branches/erijo-dev/licq/src/daemon.h

    r4876 r4881  
    2222 
    2323#include "licq/interface/daemon.h" 
     24#include "licq/eventloop.h" 
    2425 
     26#include "eventqueue.h" 
    2527#include "logfile.h" 
    2628#include "plugin/pluginrepository.h" 
     29#include "utils/pipe.h" 
    2730 
    2831namespace Licq 
    2932{ 
    3033 
    31 class TDaemon : public IDaemon 
     34class TDaemon : public IDaemon, 
     35                public IEventHandler, 
     36                public ITimeoutHandler, 
     37                public IFileDescriptorHandler 
    3238{ 
    3339private: 
     
    3541  TLogFile Log; 
    3642 
    37   typedef void (TDaemon::*FnEventHandler)(IEventPtr& event); 
     43  TEventQueue EventQueue; 
     44  TEventLoop EventLoop; 
     45  TPipe Pipe; 
     46 
     47  typedef void (TDaemon::*FnEventHandler)(const IEventPtr& event); 
    3848  typedef std::map<std::string, FnEventHandler> TEventHandlers; 
    3949  TEventHandlers EventHandlers; 
    4050 
    41   // Main event dispatcher 
    42   void onEvent(IEventPtr& event); 
     51  // From IEventHandler 
     52  void onEvent(const IEventPtr& event); 
    4353 
    4454  // Event handlers 
    45   void onPluginCreate(IEventPtr& event); 
     55  void onPluginCreate(const IEventPtr& event); 
     56 
     57  // From ITimeoutHandler 
     58  void onTimeout(int timer); 
     59 
     60  // From IFileDescriptorHandler 
     61  void onFileDescriptor(int fd); 
    4662 
    4763public: 
     
    5369  // From IDaemon 
    5470  void* getService(const std::string& name) const; 
     71  void pushEvent(IEventPtr& event); 
    5572}; 
    5673