fuse_server.cpp

00001 
00002 /***************************************************************************
00003  *  fuse_server.tcp - network image transport server interface
00004  *
00005  *  Generated: Mon Mar 19 15:56:22 2007
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <fvutils/net/fuse_server.h>
00025 #include <fvutils/net/fuse_server_client_thread.h>
00026 
00027 #include <core/threading/thread_collector.h>
00028 #include <netcomm/utils/acceptor_thread.h>
00029 
00030 #include <algorithm>
00031 
00032 using namespace fawkes;
00033 
00034 namespace firevision {
00035 #if 0 /* just to make Emacs auto-indent happy */
00036 }
00037 #endif
00038 
00039 /** @class FuseServer <fvutils/net/fuse_server.h>
00040  * FireVision FUSE protocol server.
00041  * The FuseServer will open a StreamSocket and listen on it for incoming
00042  * connections. For each connection a client thread is started that will process
00043  * all requests issued by the client.
00044  *
00045  * @ingroup FUSE
00046  * @ingroup FireVision
00047  * @author Tim Niemueller
00048  */
00049 
00050 /** Constructor.
00051  * @param port Port to listen on for incoming connections
00052  * @param collector optional thread collector
00053  */
00054 FuseServer::FuseServer(unsigned short int port, ThreadCollector *collector)
00055   : Thread("FuseServer", Thread::OPMODE_WAITFORWAKEUP)
00056 {
00057   __thread_collector = collector;
00058 
00059   __acceptor_thread = new NetworkAcceptorThread(this, port, "FuseNetworkAcceptorThread");
00060   if (__thread_collector) {
00061     __thread_collector->add(__acceptor_thread);
00062   } else {
00063     __acceptor_thread->start();
00064   }
00065 }
00066 
00067 
00068 /** Destructor. */
00069 FuseServer::~FuseServer()
00070 {
00071   if ( __thread_collector ) {
00072     __thread_collector->remove(__acceptor_thread);
00073   } else {
00074     __acceptor_thread->cancel();
00075     __acceptor_thread->join();
00076   }
00077 
00078   for (__cit = __clients.begin(); __cit != __clients.end(); ++__cit) {
00079     if ( __thread_collector ) {
00080       // ThreadCollector::remove also stops the threads!
00081       __thread_collector->remove(*__cit);
00082     } else {
00083       (*__cit)->cancel();
00084       (*__cit)->join();
00085     }
00086     delete *__cit;
00087   }
00088   __clients.clear();
00089 
00090   delete __acceptor_thread;
00091 }
00092 
00093 
00094 void
00095 FuseServer::add_connection(StreamSocket *s) throw()
00096 {
00097   FuseServerClientThread *client = new FuseServerClientThread(this, s);
00098   if ( __thread_collector) {
00099     __thread_collector->add(client);
00100   } else {
00101     client->start();
00102   }
00103   __clients.push_back_locked(client);
00104 }
00105 
00106 
00107 /** Connection died.
00108  * @param client client whose connection died
00109  */
00110 void
00111 FuseServer::connection_died(FuseServerClientThread *client) throw()
00112 {
00113   __dead_clients.push_back_locked(client);
00114   wakeup();
00115 }
00116 
00117 
00118 void
00119 FuseServer::loop()
00120 {
00121   // Check for dead clients, cancel and join if there are any
00122   __dead_clients.lock();
00123   __clients.lock();
00124 
00125   LockList<FuseServerClientThread *>::iterator  dcit;
00126 
00127   while ( ! __dead_clients.empty() ) {
00128     dcit = __dead_clients.begin();
00129 
00130     if ( __thread_collector ) {
00131       // ThreadCollector::remove also stops the threads!
00132       __thread_collector->remove(*dcit);
00133     } else {
00134       (*dcit)->cancel();
00135       (*dcit)->join();
00136     }
00137     if ( (__cit = find(__clients.begin(), __clients.end(), *dcit)) != __clients.end() ) {
00138       __clients.erase(__cit);
00139     }
00140 
00141     FuseServerClientThread *tc = *dcit;
00142     __dead_clients.erase(dcit);
00143     delete tc;
00144   }
00145 
00146   __clients.unlock();
00147   __dead_clients.unlock();  
00148 }
00149 
00150 } // end namespace firevision

Generated on Tue Feb 22 13:32:16 2011 for Fawkes API by  doxygen 1.4.7