Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

NetworkCommPort.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_NetworkCommPort_h_
00003 #define INCLUDED_NetworkCommPort_h_
00004 
00005 #include "local/CommPort.h"
00006 #include "Wireless/netstream.h"
00007 #include "Shared/Config.h"
00008 #include "Shared/TimeET.h"
00009 
00010 //! description of NetworkCommPort
00011 /*! You probably want to use UDP if you're sending high-frequency, full-state updates, and
00012 *  use TCP if you're sending low-frequency or partial-state updates. */
00013 class NetworkCommPort : public CommPort, public virtual plist::PrimitiveListener {
00014 public:
00015   explicit NetworkCommPort(const std::string& name)
00016     : CommPort(autoRegisterNetworkCommPort,name),
00017     host(), port(), transport(Config::TCP,Config::transport_names), server(false), verbose(false), block(false),
00018     sbuf(), curaddr(), curtrans(Config::TCP), opened(0), lastAttempt(0L)
00019   {
00020     sbuf.setEcho(verbose);
00021     addEntry("Host",host,"Hostname to connect to, or interface to listen on (blank for INADDR_ANY)");
00022     addEntry("Port",port,"Port number to connect to or listen on");
00023     addEntry("Transport",transport,"Transport protocol to use");
00024     addEntry("Server",server,"If true, should listen for incoming connections instead of making an outgoing one.");
00025     addEntry("Verbose",verbose,"If true, all traffic will be echoed to the terminal (handy for debugging plain-text protocols)");
00026     addEntry("Block",block,"If true, will block until connection is (re)established on initial open or after losing the connection.");
00027     verbose.addPrimitiveListener(this);
00028   }
00029   
00030   //! destructor, checks that #sbuf has already been closed
00031   virtual ~NetworkCommPort() {
00032     if(opened>0)
00033       connectionError("Connection still open in NetworkCommPort destructor",true);
00034   }
00035   
00036   virtual std::string getClassName() const { return autoRegisterNetworkCommPort; }
00037   
00038   virtual streambuf& getReadStreambuf() { return sbuf; }
00039   virtual streambuf& getWriteStreambuf() { return sbuf; }
00040   virtual bool isWriteable() { sbuf.update_status(); if(!sbuf.is_open() && opened>0) doOpen(false); return sbuf.is_open(); }
00041   virtual bool isReadable() { sbuf.update_status(); if(!sbuf.is_open() && opened>0) doOpen(false); return sbuf.is_open(); }
00042   
00043   //! activates the #sbuf based on the current configuration settings
00044   virtual bool open();
00045   
00046   //! closes #sbuf
00047   virtual bool close();
00048   
00049   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00050   
00051   plist::Primitive<std::string> host;
00052   plist::Primitive<unsigned short> port;
00053   plist::NamedEnumeration<Config::transports> transport;
00054   plist::Primitive<bool> server;
00055   plist::Primitive<bool> verbose;
00056   plist::Primitive<bool> block;
00057   
00058 protected:
00059   //! Displays message on stderr and if @a fatal is set, calls closeFD()
00060   virtual void connectionError(const std::string& msg, bool fatal) {
00061     std::cerr << msg << std::endl;
00062     if(fatal && sbuf.is_open())
00063       close();
00064   }
00065   
00066   //! attempts to make a connection, checking that the previous attempt wasn't too recent
00067   virtual bool doOpen(bool dispError);
00068   
00069   basic_netbuf<std::ios::char_type> sbuf;
00070   IPaddr curaddr;
00071   Config::transports curtrans;
00072   unsigned int opened;
00073   TimeET lastAttempt;
00074   
00075   //! holds the class name, set via registration with the CommPort registry
00076   static const std::string autoRegisterNetworkCommPort;
00077 };
00078 
00079 /*! @file
00080  * @brief 
00081  * @author Ethan Tira-Thompson (ejt) (Creator)
00082  *
00083  * $Author: ejt $
00084  * $Name: tekkotsu-4_0 $
00085  * $Revision: 1.4 $
00086  * $State: Exp $
00087  * $Date: 2007/10/12 16:44:48 $
00088  */
00089 
00090 #endif

Tekkotsu Hardware Abstraction Layer 4.0
Generated Thu Nov 22 01:00:53 2007 by Doxygen 1.5.4