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

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:39 2016 by Doxygen 1.6.3