casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Dispatcher.h
Go to the documentation of this file.
1 /*
2  * This file was originally part of the "D-Bus++ - C++ bindings for D-Bus":
3  *
4  * include/dbus-cpp/eventloop-integration.h
5  *
6  * I found the BusDispatcher to be... not so great. I was eventually able to
7  * get it to work. All I wanted to do was be able to wait until a signal to
8  * arrive. To do that, I had to use a "dispatcher". I did not want to hitch
9  * my wagon to glib or qt or ..., but only wanted a simple minimal dispatcher.
10  * Thus, despite admonitions to the contrary, I adopted the "BusDispatcher".
11  * the signal I was waiting for was delivered to my callback promptly, but
12  * I found that there was a pregnant pause between when I told the dispatcher
13  * to "leave( )" undoubtedly because of timeouts in polling. I tried to
14  * create a subclass of BusDispatcher, but because all of the _mutex_*
15  * were private to the DefaultMainLoop (what a mess). At that point,
16  * decided to implement my own, minimal, dispatcher. It is hard to believe
17  * this should be necessary, but apparently so.
18  *
19  * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
20  * Copyright (C) 2009 Associated Universities Inc.
21  *
22  *
23  * This library is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU Lesser General Public
25  * License as published by the Free Software Foundation; either
26  * version 2.1 of the License, or (at your option) any later version.
27  *
28  * This library is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31  * Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU Lesser General Public
34  * License along with this library; if not, write to the Free Software
35  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36  *
37  */
38 
39 
40 #ifndef DBUS_DISPATCHER_H_
41 #define DBUS_DISPATCHER_H_
42 
43 #if defined(DBUS_CPP)
44 #include <dbus-cpp/dbus.h>
45 #include <dbus-cpp/connection.h>
46 #else
47 #include <dbus-c++/dbus.h>
48 #include <dbus-c++/connection.h>
49 #endif
50 
51 namespace casa {
52  namespace dbus {
53 
54  class Dispatcher;
55 
56  class Timeout : public DBus::Timeout {
57  public:
58  Timeout( DBus::Timeout::Internal *, Dispatcher *);
59  ~Timeout( );
60  void toggle();
61  DBus::Slot<void, Timeout &> expired;
62 
63  private:
64  bool _enabled;
65  int _interval;
66  bool _repeat;
67 
68  double _expiration;
69 /* void *_data; */
70 
72  friend class Dispatcher;
73  };
74 
75  class Watch : public DBus::Watch {
76 
77  public:
78  Watch( DBus::Watch::Internal *, Dispatcher *);
79  ~Watch( );
80  void toggle();
81 
82  DBus::Slot<void, Watch &> ready;
83 
84  private:
85  bool _enabled;
86 
87  int _fd;
88  int _flags;
89  int _state;
90 
91 /* void *_data; */
92 
94  friend class Dispatcher;
95  };
96 
97  class Dispatcher : public DBus::Dispatcher {
98 
99  public:
100  Dispatcher();
101  ~Dispatcher();
102 
103  // pure virtual functions from Dispatcher
104  void enter();
105  void leave();
106 
107  DBus::Timeout *add_timeout(DBus::Timeout::Internal *);
108  void rem_timeout(DBus::Timeout *);
109 
110  DBus::Watch *add_watch(DBus::Watch::Internal *);
111  void rem_watch(DBus::Watch *);
112 
113  // helper function
114  void do_iteration();
115  void watch_ready(Watch &);
116  void timeout_expired(Timeout &);
117 
118  void dispatch( );
119 
120  private:
121  DBus::DefaultMutex _mutex_t;
122  std::list<Timeout*> _timeouts;
123 
124  DBus::DefaultMutex _mutex_w;
125  std::list<Watch*> _watches;
126 
127  bool _running;
128  int _leave_pipe[2];
129  friend class Timeout;
130  friend class Watch;
131  };
132  }
133 }
134 
135 #endif
void enter()
pure virtual functions from Dispatcher
Timeout(DBus::Timeout::Internal *, Dispatcher *)
DBus::Slot< void, Timeout & > expired
Definition: Dispatcher.h:61
Dispatcher * _disp
Definition: Dispatcher.h:93
Dispatcher * _disp
Definition: Dispatcher.h:71
void rem_timeout(DBus::Timeout *)
DBus::DefaultMutex _mutex_w
Definition: Dispatcher.h:124
std::list< Timeout * > _timeouts
Definition: Dispatcher.h:122
void do_iteration()
helper function
void timeout_expired(Timeout &)
DBus::DefaultMutex _mutex_t
Definition: Dispatcher.h:121
DBus::Slot< void, Watch & > ready
Definition: Dispatcher.h:82
Watch(DBus::Watch::Internal *, Dispatcher *)
DBus::Watch * add_watch(DBus::Watch::Internal *)
void watch_ready(Watch &)
DBus::Timeout * add_timeout(DBus::Timeout::Internal *)
void rem_watch(DBus::Watch *)
std::list< Watch * > _watches
Definition: Dispatcher.h:125