Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Wireless.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_Wireless_h_
00003 #define INCLUDED_Wireless_h_
00004 
00005 #ifdef PLATFORM_APERIOS
00006 #  include <OPENR/OObject.h>
00007 #  include <OPENR/OSubject.h>
00008 #  include <OPENR/OObserver.h>
00009 #  include <ant.h>
00010 #else
00011 #  include "IPC/Thread.h"
00012 #  include "Shared/Resource.h"
00013 #  include <stdint.h>
00014 typedef uint32_t uint32;
00015 #endif
00016 #include "Socket.h"
00017 #include "DummySocket.h"
00018 #include <list>
00019 
00020 class SocketListener;
00021 
00022 //! Tekkotsu wireless class
00023 /*!
00024  * For more information on using wireless, please read the following tutorials:
00025  * - <a href="../TekkotsuMon.html">TekkotsuMon</a>
00026  * - <a href="../Wireless.html">TCP/IP</a>
00027  *
00028  * The networking interface needs more documentation.  It also needs a
00029  * cleanup.  In the mean time, take a look at the TekkotsuMon objects
00030  * in <i>Tekkotsu</i><tt>/Behaviors/Mon</tt>.  They all listen for new
00031  * connections.  Unfortunately, at the momement there are no examples
00032  * of outgoing connections, but it should give you a pretty good idea
00033  * how to start moving.
00034  */
00035 class Wireless {
00036 public:
00037   //! Maximum number of sockets which can be created
00038   static const int WIRELESS_MAX_SOCKETS=100;
00039 
00040   //! Default number of bytes to use for receive buffers (overridden by value passed to socket())
00041   static const int WIRELESS_DEF_RECV_SIZE=1024;
00042 
00043   //! Default number of bytes to use for send buffers (overridden by value passed to socket())
00044   static const int WIRELESS_DEF_SEND_SIZE=1024;
00045 
00046   //! constructor - only one wireless object is required per Aperios process.
00047   /*! MMCombo already creates one. The (global) instance is called wireless,
00048    * and you can access it by including Wireless/Wireless.h (this file) in
00049    * your code
00050    */
00051   Wireless();
00052   ~Wireless(); //!< destructor
00053 
00054   //@{
00055   //! Creates a new socket
00056   /*! @return pointer to Socket object created
00057    * @param ttype selects between TCP and UDP
00058    * @see WIRELESS_DEF_RECV_SIZE, WIRELESS_DEF_SEND_SIZE */
00059   Socket* socket(Socket::TransportType_t ttype);
00060   /*!@param ttype selects between TCP and UDP
00061    * @param recvsize size of input buffer
00062    * @param sendsize size of output buffer
00063    */
00064   Socket* socket(Socket::TransportType_t ttype, int recvsize, int sendsize);
00065   //@}
00066 
00067   //! The socket waits for incoming connections.
00068   /*! That is, it acts like a server. If a connection is established and
00069    * later broken, it resumes waiting for new connections if the
00070    * socket's daemon flag is set.
00071    */
00072   int listen(int sock, int port);
00073 
00074   //! The socket tries to connect to a specific
00075   int connect(int sock, const char* ipaddr, int port);
00076 
00077   //! sets receiver callback for a socket
00078   void setReceiver(int sock, int (*rcvcbckfn) (char*, int) );
00079 
00080   //! sets receiver callback for a socket, this version requiring the SocketListener interface (more powerful, as this lets us tell connections apart)
00081   void setReceiver(int sock, SocketListener *listener);
00082 
00083   //! sets the socket to be a daemon (recycles on close)
00084   void setDaemon(int sock, bool val=true) { sockets[sock]->daemon=val; }
00085   //! sets the socket to be a daemon (recycles on close)
00086   bool getDaemon(int sock) { return sockets[sock]->daemon; }
00087   //! closes and destroys non server, daemon sockets
00088   void close(int sock);
00089 
00090   //@{
00091   //! utility function that you can use if you're curious about the state of the socket.
00092   /*! You shouldn't need to use it, since asking sockets for write
00093    * and read buffers does the necessary sanity checks
00094    */
00095   bool isConnected(int sock) {
00096     return sockets[sock]==NULL ? false : sockets[sock]->state==Socket::CONNECTION_CONNECTED;
00097   }
00098   bool isError(int sock) {
00099     return sockets[sock]==NULL ? false : sockets[sock]->state==Socket::CONNECTION_ERROR;
00100   }
00101 
00102   bool isReady(int sock) { return !sockets[sock]->tx; }
00103   bool hasData(int sock) { return !sockets[sock]->rx; }
00104   //@}
00105 
00106   //@{
00107   //! helper function for the function with the same name that takes a socket descriptor (int)
00108   void setReceiver(Socket &sobj, int (*rcvcbckfn) (char*, int) )
00109     { setReceiver(sobj.sock, rcvcbckfn); }
00110   void setReceiver(Socket *sobj, int (*rcvcbckfn) (char*, int) )
00111     { setReceiver(sobj->sock, rcvcbckfn); }
00112   void setReceiver(Socket &sobj, SocketListener *listener)
00113   { setReceiver(sobj.sock, listener); }
00114   void setReceiver(Socket *sobj, SocketListener *listener)
00115   { setReceiver(sobj->sock, listener); }
00116   void setDaemon(Socket &sobj, bool val=true) { setDaemon(sobj.sock, val); }
00117   void setDaemon(Socket *sobj, bool val=true) { setDaemon(sobj->sock, val); }
00118   bool getDaemon(Socket &sobj) { return getDaemon(sobj.sock); }
00119   bool getDaemon(Socket *sobj) { return getDaemon(sobj->sock); }
00120   int listen(Socket &sobj, int port) { return listen(sobj.sock, port); }
00121   int listen(Socket *sobj, int port) { return listen(sobj->sock, port); }
00122   int connect(Socket &sobj, const char* ipaddr, int port)
00123     { return connect (sobj.sock, ipaddr, port); }
00124   int connect(Socket *sobj, const char* ipaddr, int port)
00125     { return connect (sobj->sock, ipaddr, port); }
00126   void close(Socket &sobj) { close(sobj.sock); }
00127   void close(Socket *sobj) { close(sobj->sock); }
00128   unsigned int getNumInterfaces() { return 1; }
00129   uint32 getIPAddress(unsigned int idx=0);
00130   uint32 getIFAddress(const char*);
00131   //@}
00132 
00133   //@{
00134   //! function for internal and Socket use. You should not call this
00135   void receive(int sock, int (*rcvcbckfn) (char*, int) );
00136   void receive(int sock);
00137   //@}
00138 
00139   //@{
00140   //! function called by the Socket objects to actually write
00141   //! data to the network. You should not call this.
00142   void send(int sock);
00143   void blockingSend(int sock);
00144   //@}
00145 
00146 #ifdef PLATFORM_APERIOS
00147   //@{
00148   //! callback function for communicating
00149   //! with Aperios Networking Toolkit. You should not call this.
00150   void ListenCont (void* msg);
00151   void BindCont   (void* msg);
00152   void ConnectCont(void* msg);
00153   void SendCont   (void* msg);
00154   void ReceiveCont(void* msg);
00155   void CloseCont  (void* msg);
00156   //@}
00157 
00158 #else
00159   void pollSetup(); //!< on non-aperios, set up structures to be checked in pollTest()
00160   bool pollTest(struct timeval* tv); //!< on non-aperios, check to see any network communication has occurred
00161   void pollProcess(); //!< on non-aperios, process callbacks and state changes as signaled in pollTest()
00162   void wakeup(Socket * del=NULL); //!< writes @a del on #interruptCtl, breaking out of a pending pollTest() and thus giving an opportunity to change the contents of the FD sets being used;
00163 
00164   void setCallbackLock(Resource& l); //!< sets #callbackLock
00165   void clearCallbackLock(); //!< resets #callbackLock to a self-defined lock, which you can request from getCallbackLock() (there's always a callbackLock, the only question is it internally or externally instantiated)
00166   Resource& getCallbackLock() const { static Thread::Lock cl; return callbackLock==NULL ? cl : *callbackLock; } //!< returns #callbackLock
00167 #endif
00168 
00169 protected:
00170   friend class Socket; //so socket can lock as well
00171   static const int MAXCONNECTIONS = 5; //!< the maximum number of connections which can be queued when listening
00172 
00173   //@{
00174   //!private ALOKL_TODO
00175 #ifdef PLATFORM_APERIOS
00176   antStackRef ipstackRef;
00177   OID myOID;
00178 #else
00179   static Resource& getLock(); //!< returns the lock to use during @e all wireless operations (not just callbacks, this is more general)
00180   Resource* callbackLock; //!< this lock will be aquired during any callbacks which might occur during pollProcess()
00181   int interruptChk; //!< a socket, connected to #interruptCtl, which allows pollTest() to be interrupted if new sockets need to be polled
00182   int interruptCtl; //!< a socket, connected to #interruptChk, which allows pollTest() to be interrupted if new sockets need to be polled
00183   fd_set rfds; //!< a set of file descriptors which should be polled for readable data; set up by pollSetup(), watched (blocking) by pollTest(), and processed by pollProcess()
00184   fd_set wfds; //!< a set of file descriptors which should be polled for write-complete; set up by pollSetup(), watched (blocking) by pollTest(), and processed by pollProcess()
00185   fd_set efds; //!< a set of file descriptors which should be polled for errors; set up by pollSetup(), watched (blocking) by pollTest(), and processed by pollProcess()
00186   int fdsMax; //!< maximum file descriptor value in the #rfds, #wfds, #efds fd_set's
00187 #endif
00188   Socket* sockets[WIRELESS_MAX_SOCKETS];
00189   std::list<int> freeSockets;
00190   std::list<int> usedSockets;
00191   bool usedSocketsInvalidated; //!< set to true at modifcation of #usedSockets, cleared prior to callbacks so we can tell if callback invalidates active iterators
00192   //@}
00193 
00194 private:
00195   Wireless(const Wireless&); //!< don't call
00196   Wireless& operator= (const Wireless&); //!< don't call
00197 };
00198 
00199 //! the global wireless object - you'll want to make your function calls on this
00200 extern Wireless* wireless;
00201 
00202 /*! @file
00203  * @brief Interacts with the system to provide networking services
00204  * @author alokl (Creator)
00205  *
00206  * @verbinclude CMPack_license.txt
00207  */
00208 
00209 #endif // Wireless_h_DEFINED

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:53 2016 by Doxygen 1.6.3