Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Platform/Aperios1.3.2/NetSender.h

Go to the documentation of this file.
00001 /** 
00002 * @file  Platform/Aperios1.3.2/NetSender.h
00003 *
00004 * @author <A href=mailto:robocup@m-wachter.de>Michael Wachter</A>
00005 */
00006 
00007 #ifndef _NetSender_H_
00008 #define _NetSender_H_
00009 
00010 /**
00011  * Base class for NetSenders. Has the template-independent parts.
00012  */
00013 
00014 class NetSenderBase
00015 {
00016    public:
00017       NetSenderBase() 
00018       {
00019          size = 0;
00020          senderNumber = 0;
00021          package = NULL;
00022          sending = false;
00023       } 
00024 
00025       ~NetSenderBase()
00026       {
00027         freePackage();
00028       }
00029 
00030       virtual void send(void){};  /**< called within the process to send data */
00031             
00032       /**
00033        * gets more memory for the data to send. To prevent memory-fragmentation on the robot
00034      * this is only called for sizes bigger than the actual size
00035      * @param newsize The size of the buffer to get
00036      */
00037       void* getPackage(int newsize)
00038       {
00039          if (newsize>size)
00040          {
00041             cout << "NetSender::getPackage " << newsize << "\n";
00042             if (package) free(package);
00043             package = malloc(newsize);
00044             size=newsize;
00045             ASSERT(package);
00046          }
00047         return (package);
00048       }
00049 
00050       /**
00051      * Frees the Memoy
00052      */
00053 
00054       void freePackage()
00055       {
00056          free(package);
00057          package = NULL;
00058          size = 0;
00059          sending = false;
00060       }
00061 
00062     /**
00063      * Sets the handler which is call after "streaming" the data 
00064      * into the memory to actually send the data
00065      */
00066       void setHandler(NetHandler* h)
00067       {
00068          handler = h;
00069       }
00070 
00071       /**
00072      * Called by the handler after sucessfully sending the data
00073      */
00074       void sendDone()
00075       {
00076          // cout << "NetSender::sendDone() \n" << flush;
00077          sending=false;
00078          // cout << "Net Latency = " << SystemCall::getTimeSince (sendStartTimeStamp) << "\n" << flush;
00079       }
00080       
00081    public:
00082       NetHandler* handler;                /**< the associated NetHandler (a TCPHandler or an UDPHandler) */
00083       void* package;                      /**< pointer to the buffer */
00084       int size;                           /**< actual size of the buffer */
00085       bool sending;                       /**< true while sending the data */
00086       unsigned long sendStartTimeStamp;   /**< time when send startet (hack for some dalay-measurements) */
00087       int senderNumber;                   /**< number of the sender if there are more than one associated to the same NetHandler */
00088 
00089 };
00090 
00091 
00092 /**
00093  * @class NetSender
00094  * 
00095  * Template class for NetSender. Used for sending data of typ T.
00096  */
00097 
00098 template<class T> class NetSender : public T, public NetSenderBase
00099 {
00100   public:
00101     NetSender():T() 
00102     { 
00103     }
00104 
00105     ~NetSender() 
00106     { 
00107     }
00108     
00109   /**
00110     * Called somewhere within the process to send data
00111     */
00112     virtual void send(void)
00113     {
00114       
00115       if(!sending)
00116       {
00117          T& data = *static_cast<T*>(this);
00118 
00119          // Find out the size of the stream
00120          OutBinarySize sizeStream;
00121          sizeStream << data;
00122 
00123          if (sizeStream.getSize() != 0)
00124          {
00125               // Get memory to prepare the data to send.
00126               // At first we send the size of the data and after that the data itself
00127               getPackage(sizeStream.getSize()+sizeof(int));
00128 
00129               // Store size
00130               *((int*)package) = sizeStream.getSize();
00131                        
00132               // Store data
00133               OutBinaryMemory memory((char*)package+sizeof(int));
00134               memory << data;
00135           
00136               // Send all this
00137               sendStartTimeStamp = SystemCall::getCurrentSystemTime();
00138               sending=true;
00139               handler->sendPackage(senderNumber,package,sizeStream.getSize()+sizeof(int));
00140               
00141            }
00142         }
00143      };
00144       
00145 };
00146 
00147 
00148 /**
00149  * @class NetDebugSender
00150  * Special NetSender for MessageQueues. Clears the data in the 
00151  * MessageQueue after sending.
00152  */
00153 
00154 class NetDebugSender : public NetSender<MessageQueue>
00155 {
00156   public:
00157    virtual void send(void)
00158    {
00159      if (!sending)
00160      {
00161        MessageQueue& data = *static_cast<MessageQueue*>(this);
00162 
00163        // Get size of the Stream
00164 
00165        int size ;
00166        OutBinarySize sizeStream;
00167        sizeStream << data;
00168 
00169        // int size = data.getStreamedSize();
00170        // cout << "NetDebugSender::send() Martin-Size = " << size << " Stream-size = " << sizeStream.getSize();
00171 
00172        size=sizeStream.getSize();
00173        
00174 
00175        if (size != 0)
00176        {
00177           // Get memory
00178           getPackage(size+sizeof(int));
00179 
00180           // Store size
00181           *((int*)package) = size;
00182                  
00183           // Store data
00184           OutBinaryMemory memory((char*)package+sizeof(int));
00185           memory << data;
00186     
00187           // Send all this
00188           sendStartTimeStamp = SystemCall::getCurrentSystemTime();
00189           handler->sendPackage(senderNumber,package,size+sizeof(int));
00190           sending=true;
00191 
00192           clear(); // of MessageQueue.
00193        }
00194      }
00195      else
00196      {
00197        // cout << "NetDebugSender::send() Send blocked " << SystemCall::getCurrentSystemTime() << "\n" << flush;
00198      }
00199    }
00200 
00201 };
00202 
00203 
00204 
00205 #endif 
00206 
00207 /*
00208  *
00209  * Changelog :
00210  *
00211  * $Log: NetSender.h,v $
00212  * Revision 1.3  2004/09/14 13:54:21  wachter
00213  * no message
00214  *
00215  * Revision 1.2  2004/09/12 20:10:47  wachter
00216  * Documentation-fixes
00217  *
00218  * Revision 1.1.1.1  2004/05/22 17:23:27  cvsadm
00219  * created new repository GT2004_WM
00220  *
00221  * Revision 1.9  2004/01/21 17:33:08  wachter
00222  * UDP Team-communication now working with packets<1400 bytes.
00223  * Not activated at the moment.
00224  *
00225  * Revision 1.8  2004/01/20 14:21:41  wachter
00226  * - Added sender-number to NetSender
00227  * - worked on with Team-Communication
00228  *
00229  * Revision 1.7  2004/01/19 17:09:31  wachter
00230  * Bugfix
00231  *
00232  * Revision 1.6  2004/01/16 16:23:58  wachter
00233  * Bugfixes
00234  *
00235  * Revision 1.5  2004/01/09 13:59:23  wachter
00236  * Removed problems if TCPHandler is disconnected while sending.
00237  *
00238  * Revision 1.4  2004/01/06 10:01:14  wachter
00239  * Added comments
00240  *
00241  * Revision 1.3  2004/01/03 18:57:50  wachter
00242  * Debug-communication working now
00243  *
00244  * Revision 1.2  2004/01/03 16:18:25  wachter
00245  * debug-communication mostly working now
00246  *
00247  * Revision 1.1  2003/12/21 19:27:03  wachter
00248  * Added classes for Sender/Receiver over TCP and UDP.
00249  * ( PLEASE DO NOT USE THIS NOW ! )
00250  *
00251  *
00252  */

Generated on Thu Sep 23 19:57:34 2004 for GT2004 by doxygen 1.3.6