casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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-c++/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 #include <dbus-c++/dbus.h>
00044 #include <dbus-c++/connection.h>
00045 
00046 namespace casa {
00047     namespace dbus {
00048 
00049         class Dispatcher;
00050 
00051         class Timeout : public DBus::Timeout {
00052             public:
00053                 Timeout( DBus::Timeout::Internal *, Dispatcher *);
00054                 ~Timeout( );
00055                 void toggle();
00056                 DBus::Slot<void, Timeout &> expired;
00057         
00058             private:
00059                 bool _enabled;
00060                 int _interval;
00061                 bool _repeat;
00062 
00063                 double _expiration;
00064 /*              void *_data; */
00065         
00066                 Dispatcher *_disp;
00067                 friend class Dispatcher;
00068         };
00069 
00070         class Watch : public DBus::Watch {
00071 
00072             public:
00073                 Watch( DBus::Watch::Internal *, Dispatcher *);
00074                 ~Watch( );
00075                 void toggle();
00076 
00077                 DBus::Slot<void, Watch &> ready;
00078 
00079             private:
00080                 bool _enabled;
00081 
00082                 int _fd;
00083                 int _flags;
00084                 int _state;
00085 
00086 /*              void *_data; */
00087 
00088                 Dispatcher *_disp;
00089                 friend class Dispatcher;
00090         };
00091 
00092                 class Dispatcher : public DBus::Dispatcher {
00093 
00094                         public:
00095                                 Dispatcher();
00096                                 ~Dispatcher();
00097 
00098                                 // pure virtual functions from Dispatcher
00099                                 void enter();
00100                                 void leave();
00101 
00102                                 DBus::Timeout *add_timeout(DBus::Timeout::Internal *);
00103                                 void rem_timeout(DBus::Timeout *);
00104 
00105                                 DBus::Watch *add_watch(DBus::Watch::Internal *);
00106                                 void rem_watch(DBus::Watch *);
00107 
00108                                 // helper function
00109                                 void do_iteration();
00110                                 void watch_ready(Watch &);
00111                                 void timeout_expired(Timeout &);
00112 
00113                                 void dispatch( );
00114 
00115                         private:
00116                                 DBus::DefaultMutex _mutex_t;
00117                                 std::list<Timeout*> _timeouts;
00118 
00119                                 DBus::DefaultMutex _mutex_w;
00120                                 std::list<Watch*> _watches;
00121 
00122                                 bool _running;
00123                                 int _leave_pipe[2];
00124                                 friend class Timeout;
00125                                 friend class Watch;
00126                 };
00127     }
00128 }
00129 
00130 #endif