Dispatcher.h

Go to the documentation of this file.
00001 /*
00002  *  This file was originally part of the "D-Bus++ - C++ bindings for D-Bus":
00003  *
00004  *          include/dbus-cpp/eventloop-integration.h
00005  *
00006  *  I found the BusDispatcher to be... not so great. I was eventually able to
00007  *  get it to work. All I wanted to do was be able to wait until a signal to
00008  *  arrive. To do that, I had to use a "dispatcher". I did not want to hitch
00009  *  my wagon to glib or qt or ..., but only wanted a simple minimal dispatcher.
00010  *  Thus, despite admonitions to the contrary, I adopted the "BusDispatcher".
00011  *  the signal I was waiting for was delivered to my callback promptly, but
00012  *  I found that there was a pregnant pause between when I told the dispatcher
00013  *  to "leave( )" undoubtedly because of timeouts in polling. I tried to
00014  *  create a subclass of BusDispatcher, but because all of the _mutex_*
00015  *  were private to the DefaultMainLoop (what a mess). At that point,
00016  *  decided to implement my own, minimal, dispatcher. It is hard to believe
00017  *  this should be necessary, but apparently so.
00018  *
00019  *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
00020  *  Copyright (C) 2009       Associated Universities Inc.
00021  *
00022  *
00023  *  This library is free software; you can redistribute it and/or
00024  *  modify it under the terms of the GNU Lesser General Public
00025  *  License as published by the Free Software Foundation; either
00026  *  version 2.1 of the License, or (at your option) any later version.
00027  *
00028  *  This library is distributed in the hope that it will be useful,
00029  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00030  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00031  *  Lesser General Public License for more details.
00032  *
00033  *  You should have received a copy of the GNU Lesser General Public
00034  *  License along with this library; if not, write to the Free Software
00035  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00036  *
00037  */
00038 
00039 
00040 #ifndef DBUS_DISPATCHER_H_
00041 #define DBUS_DISPATCHER_H_
00042 
00043 #if defined(DBUS_CPP)
00044 #include <dbus-cpp/dbus.h>
00045 #include <dbus-cpp/connection.h>
00046 #else
00047 #include <dbus-c++/dbus.h>
00048 #include <dbus-c++/connection.h>
00049 #endif
00050 
00051 namespace casa {
00052     namespace dbus {
00053 
00054         class Dispatcher;
00055 
00056         class Timeout : public DBus::Timeout {
00057             public:
00058                 Timeout( DBus::Timeout::Internal *, Dispatcher *);
00059                 ~Timeout( );
00060                 void toggle();
00061                 DBus::Slot<void, Timeout &> expired;
00062         
00063             private:
00064                 bool _enabled;
00065                 int _interval;
00066                 bool _repeat;
00067 
00068                 double _expiration;
00069 /*              void *_data; */
00070         
00071                 Dispatcher *_disp;
00072                 friend class Dispatcher;
00073         };
00074 
00075         class Watch : public DBus::Watch {
00076 
00077             public:
00078                 Watch( DBus::Watch::Internal *, Dispatcher *);
00079                 ~Watch( );
00080                 void toggle();
00081 
00082                 DBus::Slot<void, Watch &> ready;
00083 
00084             private:
00085                 bool _enabled;
00086 
00087                 int _fd;
00088                 int _flags;
00089                 int _state;
00090 
00091 /*              void *_data; */
00092 
00093                 Dispatcher *_disp;
00094                 friend class Dispatcher;
00095         };
00096 
00097                 class Dispatcher : public DBus::Dispatcher {
00098 
00099                         public:
00100                                 Dispatcher();
00101                                 ~Dispatcher();
00102 
00103                                 // pure virtual functions from Dispatcher
00104                                 void enter();
00105                                 void leave();
00106 
00107                                 DBus::Timeout *add_timeout(DBus::Timeout::Internal *);
00108                                 void rem_timeout(DBus::Timeout *);
00109 
00110                                 DBus::Watch *add_watch(DBus::Watch::Internal *);
00111                                 void rem_watch(DBus::Watch *);
00112 
00113                                 // helper function
00114                                 void do_iteration();
00115                                 void watch_ready(Watch &);
00116                                 void timeout_expired(Timeout &);
00117 
00118                                 void dispatch( );
00119 
00120                         private:
00121                                 DBus::DefaultMutex _mutex_t;
00122                                 std::list<Timeout*> _timeouts;
00123 
00124                                 DBus::DefaultMutex _mutex_w;
00125                                 std::list<Watch*> _watches;
00126 
00127                                 bool _running;
00128                                 int _leave_pipe[2];
00129                                 friend class Timeout;
00130                                 friend class Watch;
00131                 };
00132     }
00133 }
00134 
00135 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1