Changeset 6027 for branches/newapi

Show
Ignore:
Timestamp:
01/11/08 07:59:37 (11 months ago)
Author:
erijo
Message:

Updated simple plugin to react on events and shutdown licq after 5 secs

Location:
branches/newapi/simple/src
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branches/newapi/simple/src/plugin.cpp

    r5922 r6027  
    4848Licq::Plugin* licqCreatePlugin(const Licq::Plugin::Setup& setup) 
    4949{ 
    50   return new SimplePlugin(*setup.log); 
     50  return new SimplePlugin(setup); 
    5151} 
    5252 
  • branches/newapi/simple/src/simpleplugin.cpp

    r5788 r6027  
    1818 */ 
    1919 
     20#include "licq/interface/daemon.h" 
    2021#include "simpleplugin.h" 
    2122 
    22 SimplePlugin::SimplePlugin(Licq::Log& log) 
    23   : myLog(log) 
     23SimplePlugin::SimplePlugin(const Licq::Plugin::Setup& setup) 
     24  : myLog(*setup.log), 
     25    mySetup(setup), 
     26    myEventDispatcher(myEventLoop, *mySetup.eventQueue, Licq::Daemon::getInstance()) 
    2427{ 
    2528  myLog.debug("Constructing SimplePlugin"); 
     
    3437{ 
    3538  myLog.debug(boost::format("onTimer(%1%)") % timer); 
     39  static int counter = 0; 
     40  if (++counter == 10) 
     41  { 
     42    Licq::Event::Ptr exit = Licq::Event::create("daemon.exit"); 
     43    Licq::Daemon::getInstance()->sendEventToDaemon(exit); 
     44  } 
    3645} 
    3746 
     
    4150} 
    4251 
     52bool SimplePlugin::onEvent(Licq::Event::Ptr event) 
     53{ 
     54  myLog.debug(boost::format("onEvent(%1%)") % event->getName()); 
     55  if (event->getName() == "plugin.stop") 
     56  { 
     57    myEventLoop.exit(0); 
     58    Licq::Event::Ptr stopped = Licq::Event::create("plugin.stopped"); 
     59    stopped->setProperty("id", mySetup.id); 
     60    Licq::Daemon::getInstance()->sendEventToDaemon(stopped);     
     61    return false; 
     62  } 
     63  return true; 
     64} 
     65 
    4366int SimplePlugin::run() 
    4467{ 
    45   Licq::EventLoop eventLoop; 
    46   eventLoop.addTimer(20, this); 
     68  myEventDispatcher.setDefaultCallback(&SimplePlugin::onEvent, this); 
     69  myEventLoop.addTimer(500, this); 
    4770 
    4871  myLog.debug("Starting SimplePlugin"); 
    49   return eventLoop.run(); 
     72  return myEventLoop.run(); 
    5073} 
  • branches/newapi/simple/src/simpleplugin.h

    r5914 r6027  
    2121#define SIMPLEPLUGIN_H 
    2222 
     23#include "licq/event/eventdispatcher.h" 
    2324#include "licq/event/eventloop.h" 
    2425#include "licq/interface/plugin.h" 
     
    3031{ 
    3132public: 
    32   SimplePlugin(Licq::Log& log); 
     33  SimplePlugin(const Licq::Plugin::Setup& setup); 
    3334  ~SimplePlugin(); 
    3435 
     36  bool onEvent(Licq::Event::Ptr event); 
     37 
     38  // From Licq::EventLoop::Handler 
    3539  void onTimer(int timer); 
    3640  void onDescriptor(int fd, Licq::EventLoop::NotifierType type); 
     
    4246private: 
    4347  Licq::Log& myLog; 
     48  const Licq::Plugin::Setup& mySetup; 
     49 
     50  Licq::EventLoop myEventLoop; 
     51  Licq::EventDispatcher myEventDispatcher; 
    4452}; 
    4553