SockServ.H

Go to the documentation of this file.
00001 /*!@file Beowulf/SockServ.H A simple multi-client socket server */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // University of Southern California (USC) and the iLab at USC.         //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Beowulf/SockServ.H $
00035 // $Id: SockServ.H 5335 2005-08-17 14:27:01Z rjpeters $
00036 //
00037 
00038 #ifndef SOCKSERV_H_DEFINED
00039 #define SOCKSERV_H_DEFINED
00040 
00041 #include "Component/ModelComponent.H"
00042 #include "Component/ModelParam.H"
00043 
00044 #include <sys/time.h>
00045 #include <sys/types.h>
00046 #include <unistd.h>
00047 #include <netinet/in.h>
00048 #include <arpa/inet.h>
00049 
00050 //! max mumber of new clients that can come between two check():
00051 #define MAXNBNUCLI 128
00052 
00053 //! return values for check():
00054 #define SOCKSERV_IDLE   0
00055 #define SOCKSERV_BUG    1
00056 #define SOCKSERV_ACTIV  2
00057 
00058 //! This is a multi-socket connection server
00059 /*! This is a multi-socket connection server; it listens onto a master
00060   socket and accepts incoming connections; it then can monitor all
00061   open connections for read, write and/or error, all in a single call
00062   to check(), using a non-blocking select().
00063 
00064   Classes using SockServ should call check() to determine which
00065   connections require attention. Subsequently, they should take
00066   appropriate action on the connections that are active, using
00067   getXXXclient().  In adition to the connections that are
00068   automatically open by SockServ on accept() on the master socket,
00069   users may tell SockServ to monitor additional (already open)
00070   connections, by adding them to SockServ's monitor list using
00071   addUserClient().  By default, connections time out after CLITIMEOUT
00072   time of inactivity. This can be disabled using noTimeOut(). */
00073 class SockServ : public ModelComponent {
00074 public:
00075   //! SockServ constructor
00076   SockServ(OptionManager& mgr,
00077            const std::string& descrName = "Socket Server",
00078            const std::string& tagName = "SockServ");
00079 
00080   //! SockServ destructor
00081   ~SockServ();
00082 
00083   //! check what's going on
00084   int check(const int stimeout, const int utimeout);
00085 
00086   //! zero if no more new clients, else client fd
00087   int getNewClient();
00088 
00089   //! zero if no more new clients, else client fd
00090   int getReadClient();
00091 
00092   //! zero if no more new clients, else client fd
00093   int getWriteClient();
00094 
00095   //! zero if no more new clients, else client fd
00096   int getErrorClient();
00097 
00098   //! get IP address of a client
00099   in_addr_t getClientIP(const int clifd) const;
00100 
00101   //! get port of a client
00102   short int getClientPort(const int clifd) const;
00103 
00104   //! all wclients first, then all rclients
00105   int getRWClient();
00106 
00107   //! disconnect a client
00108   void disconnect(const int client);
00109 
00110   //! external users can also add their own new clients:
00111   bool addUserClient(void *data);
00112   void* getNewUserClient();
00113 
00114   //! what should we monitor for, for a given connection:
00115   bool monitorRead(const int client, const bool startstop = true);
00116 
00117   //! what should we monitor for, for a given connection:
00118   bool monitorWrite(const int client, const bool startstop = true);
00119 
00120   //! what should we monitor for, for a given connection:
00121   bool monitorError(const int client, const bool startstop = true);
00122 
00123   bool noTimeOut(const int client);
00124 
00125   void resetTimeOut(const int client);
00126 
00127   void addClient(const int client);
00128   void deleteClient(const int client);
00129 
00130 protected:
00131   OModelParam<short int> itsPort;       //!< My port to listen to
00132   NModelParam<std::string> itsServ;     //!< Name of service in /etc/services
00133   NModelParam<std::string> itsServType; //!< Type of service in /etc/services
00134   NModelParam<int> itsCliTout;          //!< Timeout on idel clients (in s)
00135   NModelParam<int> itsQlen;             //!< Length of listen queue
00136 
00137 private:
00138   void start1();  // get started
00139   void stop2();   // stop ourselves
00140 
00141   void init(const int servport, const int qlen);
00142   void closedown();
00143 
00144   void addNewClients();
00145   bool hasClient(const int client);
00146   void debug(const char *label);
00147 
00148   int state;
00149   int sock, port;
00150   fd_set readfds, writefds, exceptfds, rfds, wfds, efds;
00151   int newcli[FD_SETSIZE], rcli[FD_SETSIZE], wcli[FD_SETSIZE], ecli[FD_SETSIZE];
00152   void* nuclidata[MAXNBNUCLI];
00153   int nbnewcli, nbrcli, nbwcli, nbecli, nbnucli;
00154 
00155   int clitimeout[FD_SETSIZE];
00156   int cli[FD_SETSIZE];
00157   unsigned long int cliIP[FD_SETSIZE];
00158   short int cliPort[FD_SETSIZE];
00159   int nbclients;
00160 };
00161 
00162 #endif
00163 
00164 // ######################################################################
00165 /* So things look consistent in everyone's emacs... */
00166 /* Local Variables: */
00167 /* indent-tabs-mode: nil */
00168 /* End: */
Generated on Sun May 8 08:04:33 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3