Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

Wireless.h

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

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:50 2005 by Doxygen 1.4.4