00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <gui_utils/connection_dispatcher.h>
00025 #include <netcomm/fawkes/client.h>
00026
00027 #include <cstring>
00028 #include <cstdlib>
00029
00030 namespace fawkes {
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 ConnectionDispatcher::ConnectionDispatcher(unsigned int cid)
00042 {
00043 __cid = cid;
00044 __client = new FawkesNetworkClient();
00045 __client->register_handler(this, __cid);
00046 __client_owned = true;
00047
00048 connect_signals();
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058 ConnectionDispatcher::ConnectionDispatcher(const char *hostname,
00059 unsigned short int port,
00060 unsigned int cid)
00061 {
00062 __cid = cid;
00063 __client = new FawkesNetworkClient(hostname, port);
00064 __client->register_handler(this, __cid);
00065 __client_owned = true;
00066
00067 connect_signals();
00068 }
00069
00070
00071 ConnectionDispatcher::~ConnectionDispatcher()
00072 {
00073 set_client(NULL);
00074 }
00075
00076
00077 void
00078 ConnectionDispatcher::connect_signals()
00079 {
00080 __dispatcher_connected.connect(sigc::mem_fun(*this, &ConnectionDispatcher::on_connection_established));
00081 __dispatcher_disconnected.connect(sigc::mem_fun(*this, &ConnectionDispatcher::on_connection_died));
00082 __dispatcher_message_received.connect(sigc::mem_fun(*this, &ConnectionDispatcher::on_message_received));
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092 void
00093 ConnectionDispatcher::set_cid(unsigned int cid)
00094 {
00095 if ( __client ) {
00096 __client->deregister_handler(__cid);
00097 __client->register_handler(this, cid);
00098 }
00099 __cid = cid;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108
00109 void
00110 ConnectionDispatcher::set_client(FawkesNetworkClient *client)
00111 {
00112 if ( __client ) __client->deregister_handler(__cid);
00113 if ( __client_owned ) {
00114 delete __client;
00115 }
00116 __client_owned = false;
00117 __client = client;
00118 if ( __client ) __client->register_handler(this, __cid);
00119 }
00120
00121
00122
00123
00124
00125 FawkesNetworkClient *
00126 ConnectionDispatcher::get_client()
00127 {
00128 return __client;
00129 }
00130
00131
00132
00133
00134
00135
00136 ConnectionDispatcher::operator bool()
00137 {
00138 return (__client && __client->connected());
00139 }
00140
00141
00142
00143
00144
00145 void
00146 ConnectionDispatcher::on_connection_established()
00147 {
00148 __signal_connected.emit();
00149 }
00150
00151
00152
00153
00154
00155 void
00156 ConnectionDispatcher::on_connection_died()
00157 {
00158 __signal_disconnected.emit();
00159 }
00160
00161
00162
00163
00164
00165 void
00166 ConnectionDispatcher::on_message_received()
00167 {
00168 __queue_message_received.lock();
00169 while (! __queue_message_received.empty()) {
00170 FawkesNetworkMessage *msg = __queue_message_received.front();
00171 __signal_message_received.emit(msg);
00172 msg->unref();
00173 __queue_message_received.pop();
00174 }
00175 __queue_message_received.unlock();
00176 }
00177
00178
00179 void
00180 ConnectionDispatcher::deregistered(unsigned int id) throw()
00181 {
00182
00183 }
00184
00185
00186 void
00187 ConnectionDispatcher::inbound_received(FawkesNetworkMessage *m, unsigned int id) throw()
00188 {
00189 m->ref();
00190 __queue_message_received.push_locked(m);
00191 __dispatcher_message_received();
00192 }
00193
00194
00195 void
00196 ConnectionDispatcher::connection_died(unsigned int id) throw()
00197 {
00198 __dispatcher_disconnected();
00199 }
00200
00201
00202 void
00203 ConnectionDispatcher::connection_established(unsigned int id) throw()
00204 {
00205 __dispatcher_connected();
00206 }
00207
00208
00209
00210
00211
00212
00213
00214 sigc::signal<void, FawkesNetworkMessage *>
00215 ConnectionDispatcher::signal_message_received()
00216 {
00217 return __signal_message_received;
00218 }
00219
00220
00221
00222
00223
00224
00225 sigc::signal<void>
00226 ConnectionDispatcher::signal_connected()
00227 {
00228 return __signal_connected;
00229 }
00230
00231
00232
00233
00234
00235
00236
00237 sigc::signal<void>
00238 ConnectionDispatcher::signal_disconnected()
00239 {
00240 return __signal_disconnected;
00241 }
00242
00243 }