00001 /*!@file Beowulf/TCPcliServ.H A client/server to receive/send TCPmessage */ 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/TCPcliServ.H $ 00035 // $Id: TCPcliServ.H 4663 2005-06-23 17:47:28Z rjpeters $ 00036 // 00037 00038 #ifndef TCPCLISERV_H_DEFINED 00039 #define TCPCLISERV_H_DEFINED 00040 00041 #include "Beowulf/TCPmessage.H" 00042 #include "Util/Timer.H" 00043 #include <deque> 00044 #include <map> 00045 #include <netinet/in.h> 00046 #include <pthread.h> 00047 #include <unistd.h> 00048 00049 //! A client/server to receive/send TCPmessage 00050 /*! This class defines a simple client/server that handles one TCP 00051 connection and can send/receive TCPmessages over it. Messages to 00052 be sent or being received are queued; the actual transmission 00053 happens in check() which should be called often, typically under 00054 the control of a TCPcommunicator object that uses a SockServ object 00055 to know when our TCPcliServ needs attention. 00056 00057 this class uses a mutex to allow check() to be called from one thread 00058 while send() and receive() may simultaneously be called from another 00059 thread. 00060 */ 00061 class TCPcliServ { 00062 public: 00063 //! Base constructor. Must call init() later 00064 TCPcliServ(); 00065 00066 //! (re)-initialization from a connected socket 00067 /*! my_ipaddr and my_port should be my IP address and 00068 port. cli_ipaddr and cli_port should be the IP address and port of 00069 the client on the other end of connected_fd. For now, they are 00070 only used to determine whether we can optimize transfers by using 00071 shared memory in case the client's IP address matches ours. inqlen 00072 and outqlen are the maximum incoming and outgoing queue length, or 00073 zero for unlimited. indlast and outdlast specify the drop policies 00074 for ths input and output queues; if true, the last message (most 00075 recent) coming into the queue will be dropped if the queue is 00076 full, otherwise the oldest message already in the queue will be 00077 dropped. */ 00078 void init(const int connected_fd, const in_addr_t my_ipaddr, 00079 const short int my_port, const in_addr_t cli_ipaddr, 00080 const short int cli_port, const int inqlen = 0, 00081 const int outqlen = 0, const bool indlast = false, 00082 const bool outdlast = false, const bool disableShm = false); 00083 00084 //! Constructor and initialization from a connected socket 00085 TCPcliServ(const int connected_fd, const in_addr_t my_ipaddr, 00086 const short int my_port, const in_addr_t cli_ipaddr, 00087 const short int cli_port, const int inqlen = 0, 00088 const int outqlen = 0, const bool indlast = false, 00089 const bool outdlast = false, const bool disableShm = false); 00090 00091 //! Destructor 00092 ~TCPcliServ(); 00093 00094 //! Send a message 00095 void send(const TCPmessage& msg); 00096 00097 //! See if we have received a message (returns false if no new message) 00098 bool receive(TCPmessage& msg); 00099 00100 //! Do we have any received messages in our queue 00101 /*! Returns the number of messages that have been fully received 00102 (not those in progress) and are waiting in the queue. Use 00103 receive() to get the messages out of the incoming queue. */ 00104 int nbReceived(); 00105 00106 //! Check for new incoming message and do transfers 00107 /*! Check for new incoming message, continue reading/writing current 00108 messages, or start writing out a new message from the write queue 00109 (thread-safe). This must be called often for data to flow. */ 00110 int check(); 00111 00112 //! Terminate the connection 00113 int disconnect(); 00114 00115 private: 00116 void storeReceivedMessage(); // Store a received incoming message 00117 void queueIncomingMessage(TCPmessage& imsg); 00118 int reset(); // reset everything 00119 00120 int state; // Internal state 00121 int fd; // Socket used to talk to peer 00122 std::deque<TCPmessage> inmsg; // Incoming messages 00123 std::deque<TCPmessage> oumsg; // Outgoing messages 00124 TCPmessage im; // Currently incoming message 00125 TCPmessage om; // Currently outgoing message 00126 uint inmsgqlen, outmsgqlen; // in/out message queue length or zero 00127 bool indroplast, outdroplast; // queue drop policies 00128 pthread_mutex_t mutin; // Mutex for access to incoming message queue 00129 pthread_mutex_t mutou; // Mutex for access to outgoing message queue 00130 00131 in_addr_t myIP, cliIP; 00132 short int myPort, cliPort; 00133 bool useShm; 00134 std::map<int, void *> shmmap; //!< map associating shmid to address 00135 int shmcount; //!< counter used to generate unique shm keys 00136 }; 00137 00138 #endif 00139 00140 // ###################################################################### 00141 /* So things look consistent in everyone's emacs... */ 00142 /* Local Variables: */ 00143 /* indent-tabs-mode: nil */ 00144 /* End: */