00001 /** 00002 * @file Platform/Aperios1.3.2/NetReceiver.h 00003 * 00004 * receiver-class for communication over wireless lan. 00005 * 00006 * @author <A href=mailto:robocup@m-wachter.de>Michael Wachter</A> 00007 */ 00008 00009 #ifndef _NetReceiver_H_ 00010 #define _NetReceiver_H_ 00011 00012 /** 00013 * Template-Independent part of the NetReceiver 00014 * handles memory-acclocation. 00015 */ 00016 00017 class NetReceiverBase 00018 { 00019 public: 00020 NetReceiverBase() 00021 { 00022 package = NULL; 00023 receivedNewPackage = false; 00024 size=0; 00025 } 00026 00027 ~NetReceiverBase() 00028 { 00029 freePackage(); 00030 } 00031 00032 virtual void onReceive() = 0; /**< is called by the associated handler when receiving data. */ 00033 00034 bool receivedNew() 00035 { 00036 return (receivedNewPackage); 00037 } 00038 00039 /** 00040 * gets memory for the NetHandler to store the data 00041 * the NetReceiver then calls the streaming-operator 00042 * for this data. 00043 */ 00044 void* getPackage(int newsize) 00045 { 00046 if (newsize>size) 00047 { 00048 cout << "NetReceiver::getPackage " << newsize << "\n" << flush; 00049 if (package) free(package); 00050 package = malloc(newsize); 00051 size=newsize; 00052 ASSERT(package); 00053 } 00054 return (package); 00055 } 00056 00057 /** 00058 * frees the memory allocated by getPackage 00059 */ 00060 void freePackage() 00061 { 00062 free(package); 00063 package = NULL; 00064 size=0; 00065 receivedNewPackage = false; 00066 } 00067 00068 public: 00069 bool receivedNewPackage; /**< true if new data is aviable */ 00070 void* package; /**< pointer to the data */ 00071 int size; /**< size of the data */ 00072 }; 00073 00074 00075 /** 00076 * Template-Class to read a package from network and call the 00077 * streaming-operator for it. 00078 */ 00079 00080 template<class T> class NetReceiver : public T, public NetReceiverBase 00081 { 00082 public: 00083 virtual void onReceive() 00084 { 00085 // cout << "NetReceiver::onReceive() : received " << size << " bytes"; 00086 00087 InBinaryMemory stream(package,size); 00088 T& data = *static_cast<T*>(this); 00089 stream >> data; 00090 receivedNewPackage = true; 00091 } 00092 }; 00093 00094 #endif 00095 00096 /* 00097 * 00098 * Changelog : 00099 * 00100 * $Log: NetReceiver.h,v $ 00101 * Revision 1.2 2004/09/12 20:10:47 wachter 00102 * Documentation-fixes 00103 * 00104 * Revision 1.1.1.1 2004/05/22 17:23:26 cvsadm 00105 * created new repository GT2004_WM 00106 * 00107 * Revision 1.8 2004/05/14 14:12:08 wachter 00108 * - Added communication support for 5 robots 00109 * - rewrote parts of team-communication to be faster and more stable 00110 * 00111 * Revision 1.7 2004/01/21 17:33:08 wachter 00112 * UDP Team-communication now working with packets<1400 bytes. 00113 * Not activated at the moment. 00114 * 00115 * Revision 1.6 2004/01/19 17:09:31 wachter 00116 * Bugfix 00117 * 00118 * Revision 1.5 2004/01/16 16:23:58 wachter 00119 * Bugfixes 00120 * 00121 * Revision 1.4 2004/01/06 10:01:14 wachter 00122 * Added comments 00123 * 00124 * Revision 1.3 2004/01/03 18:57:50 wachter 00125 * Debug-communication working now 00126 * 00127 * Revision 1.2 2004/01/03 16:18:25 wachter 00128 * debug-communication mostly working now 00129 * 00130 * Revision 1.1 2003/12/21 19:27:03 wachter 00131 * Added classes for Sender/Receiver over TCP and UDP. 00132 * ( PLEASE DO NOT USE THIS NOW ! ) 00133 * 00134 * 00135 */