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){};
    00031
    00032       void* getPackage(int newsize)
    00033       {
    00034          if (newsize>size)
    00035          {
    00036             cout << "NetSender::getPackage " << newsize << "\n";
    00037             if (package) free(package);
    00038             package = malloc(newsize);
    00039             size=newsize;
    00040             ASSERT(package);
    00041          }
    00042         return (package);
    00043       }
    00044
    00045       void freePackage()
    00046       {
    00047          free(package);
    00048          package = NULL;
    00049          size = 0;
    00050          sending = false;
    00051       }
    00052
    00053       void setHandler(NetHandler* h)
    00054       {
    00055          handler = h;
    00056       }
    00057
    00058       void sendDone()
    00059       {
    00060          // cout << "NetSender::sendDone() \n" << flush;
    00061          sending=false;
    00062          // cout << "Net Latency = " << SystemCall::getTimeSince (sendStartTimeStamp) << "\n" << flush;
    00063       }
    00064
    00065    public:
    00066       NetHandler* handler;
    00067       void* package;
    00068       int size;
    00069       bool sending;
    00070       unsigned long sendStartTimeStamp;
    00071       int senderNumber;
    00072 };
    00073
    00074
    00075 /**
    00076  * Template class for NetSender
    00077  */
    00078
    00079 template<class T> class NetSender : public T, public NetSenderBase
    00080 {
    00081   public:
    00082     NetSender():T()
    00083     {
    00084     }
    00085
    00086     ~NetSender()
    00087     {
    00088     }
    00089
    00090     virtual void send(void)
    00091     {
    00092
    00093       if(!sending)
    00094       {
    00095          T& data = *static_cast<T*>(this);
    00096
    00097          // Find out the size of the stream
    00098          OutBinarySize sizeStream;
    00099          sizeStream << data;
    00100
    00101          if (sizeStream.getSize() != 0)
    00102          {
    00103               // Get memory to prepare the data to send.
    00104               // At first we send the size of the data and after that the data itself
    00105               getPackage(sizeStream.getSize()+sizeof(int));
    00106
    00107               // Store size
    00108               *((int*)package) = sizeStream.getSize();
    00109
    00110               // Store data
    00111               OutBinaryMemory memory((char*)package+sizeof(int));
    00112               memory << data;
    00113
    00114               // Send all this
    00115               sendStartTimeStamp = SystemCall::getCurrentSystemTime();
    00116               sending=true;
    00117               handler->sendPackage(senderNumber,package,sizeStream.getSize()+sizeof(int));
    00118
    00119            }
    00120         }
    00121      };
    00122
    00123 };
    00124
    00125
    00126 /**
    00127  * Class NetDebugSender
    00128  * Special NetSender for MessageQueues. Clears the data in the
    00129  * MessageQueue after sending
    00130  */
    00131
    00132 class NetDebugSender : public NetSender<MessageQueue>
    00133 {
    00134   public:
    00135    virtual void send(void)
    00136    {
    00137      if (!sending)
    00138      {
    00139        MessageQueue& data = *static_cast<MessageQueue*>(this);
    00140
    00141        // Get size of the Stream
    00142        OutBinarySize sizeStream;
    00143        sizeStream << data;
    00144
    00145        int size = data.getStreamedSize();
    00146        // cout << "NetDebugSender::send() Martin-Size = " << size << " Stream-size = " << sizeStream.getSize();
    00147
    00148        size=sizeStream.getSize();
    00149
    00150
    00151        if (size != 0)
    00152        {
    00153           // Get memory
    00154           getPackage(size+sizeof(int));
    00155
    00156           // Store size
    00157           *((int*)package) = size;
    00158
    00159           // Store data
    00160           OutBinaryMemory memory((char*)package+sizeof(int));
    00161           memory << data;
    00162
    00163           // Send all this
    00164           sendStartTimeStamp = SystemCall::getCurrentSystemTime();
    00165           handler->sendPackage(senderNumber,package,size+sizeof(int));
    00166           sending=true;
    00167
    00168           clear(); // of MessageQueue.
    00169        }
    00170      }
    00171      else
    00172      {
    00173        // cout << "NetDebugSender::send() Send blocked " << SystemCall::getCurrentSystemTime() << "\n" << flush;
    00174      }
    00175    }
    00176
    00177 };
    00178
    00179
    00180
    00181 #endif