00001 /*************************************************************************** 00002 * Copyright (C) 2007,2008,2009 by Rick L. Vinyard, Jr. * 00003 * rvinyard@cs.nmsu.edu * 00004 * * 00005 * This file is part of the dbus-cxx library. * 00006 * * 00007 * The dbus-cxx library is free software; you can redistribute it and/or * 00008 * modify it under the terms of the GNU General Public License * 00009 * version 3 as published by the Free Software Foundation. * 00010 * * 00011 * The dbus-cxx library is distributed in the hope that it will be * 00012 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * 00013 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU General Public License * 00017 * along with this software. If not see <http://www.gnu.org/licenses/>. * 00018 ***************************************************************************/ 00019 #ifndef __DBUSXX_DISPATCHER_H 00020 #define __DBUSXX_DISPATCHER_H 00021 00022 #include <map> 00023 #include <list> 00024 #include <set> 00025 00026 #include <sys/select.h> 00027 00028 #include <dbus/dbus.h> 00029 #include <dbus-cxx/connection.h> 00030 #include <dbus-cxx/watch.h> 00031 #include <dbus-cxx/timeout.h> 00032 00033 namespace DBus 00034 { 00035 00049 class Dispatcher 00050 { 00051 public: 00052 00053 typedef DBusCxxPointer<Dispatcher> pointer; 00054 00055 typedef DBusCxxPointer<const Dispatcher> const_pointer; 00056 00057 Dispatcher(bool is_running=true); 00058 00059 static pointer create( bool is_running=true ); 00060 00061 virtual ~Dispatcher(); 00062 00065 00066 Connection::pointer create_connection( DBusConnection* cobj = NULL, bool is_private=false ); 00067 00068 Connection::pointer create_connection( BusType type, bool is_private=false ); 00069 00070 Connection::pointer create_connection( const Connection& other ); 00071 00072 bool add_connection( Connection::pointer connection ); 00073 00075 00076 bool start(); 00077 00078 bool stop(); 00079 00080 bool is_running(); 00081 00082 const struct timeval& responsiveness(); 00083 00084 void set_responsiveness( const struct timeval& r ); 00085 00086 void set_responsiveness( time_t sec, suseconds_t usec ); 00087 00088 protected: 00089 00090 typedef std::list<Connection::pointer> Connections; 00091 Connections m_connections; 00092 00093 bool m_running; 00094 00095 pthread_t m_dispatch_thread; 00096 pthread_t m_watch_thread; 00097 00098 pthread_mutex_t m_mutex_read_watches; 00099 std::map<int, Watch::pointer> m_read_watches; 00100 std::set<int> m_enabled_read_fds; 00101 fd_set m_read_fd_set; 00102 int m_maximum_read_fd; 00103 00104 void build_read_fd_set(); 00105 00106 pthread_mutex_t m_mutex_write_watches; 00107 std::map<int, Watch::pointer> m_write_watches; 00108 std::set<int> m_enabled_write_fds; 00109 fd_set m_write_fd_set; 00110 int m_maximum_write_fd; 00111 00112 void build_write_fd_set(); 00113 00114 fd_set m_exception_fd_set; 00115 00116 pthread_mutex_t m_mutex_exception_fd_set; 00117 00118 struct timeval m_responsiveness; 00119 00127 unsigned int m_dispatch_loop_limit; 00128 00129 bool m_initiate_processing; 00130 pthread_cond_t m_cond_initiate_processing; 00131 pthread_mutex_t m_mutex_initiate_processing; 00132 00133 virtual void dispatch_thread_main(); 00134 00135 void watch_thread_main(); 00136 00143 static void* proxy_dispatch_thread_main(void*); 00144 00151 static void* proxy_watch_thread_main(void*); 00152 00153 bool on_add_watch(Watch::pointer); 00154 00155 bool on_remove_watch(Watch::pointer); 00156 00157 bool on_watch_toggled(Watch::pointer); 00158 00159 bool on_add_timeout(Timeout::pointer); 00160 00161 bool on_remove_timeout(Timeout::pointer); 00162 00163 bool on_timeout_toggled(Timeout::pointer); 00164 00165 void on_wakeup_main(Connection::pointer); 00166 00167 void on_dispatch_status_changed(DispatchStatus, Connection::pointer); 00168 }; 00169 00170 } 00171 00172 #endif