/home/aherms/Code/git/awds-routing/src/awds/BasePacket.h

00001 #ifndef _BASEPACKET_H__
00002 #define _BASEPACKET_H__
00003 
00004 #include <cassert>
00005 
00006 #include <gea/Handle.h>
00007 #include <awds/NodeId.h>
00008 #include <gea/UdpHandle.h>
00009 
00010 namespace awds {
00011     
00016     enum PacketType {
00017         PacketTypeBeacon  = 0, 
00018         PacketTypeFlood   = 1, 
00019         PacketTypeUnicast = 2, 
00020         PacketTypeForward = 3  
00021     };
00022 
00029     class BasePacket {
00030     
00031     public:
00032     
00033         static const int MaxSize = 0x1000; 
00034         char buffer[MaxSize]; 
00035         size_t size;          
00040         int refcount;         
00041     
00042     
00043         NodeId dest;
00044 
00045         BasePacket() : size(0), refcount(1) {
00046             buffer[0] = 0;
00047         }
00048 
00049         int send(gea::Handle* h) { 
00050         
00051             return h->write(buffer, size);
00052         
00053         }
00054         int receive(gea::Handle* h) { return (size = h->read(buffer, MaxSize)) ; }
00055     
00058         int ref()   { return ++refcount; }
00059         
00067         int unref() { 
00068             int ret; 
00069             assert(refcount > 0);
00070             --refcount; 
00071             ret = refcount;
00072             if (refcount == 0) 
00073                 delete this;
00074             return ret;
00075         }
00076     
00077         PacketType getType() const {
00078             return static_cast<PacketType>(buffer[0] & 0x03);
00079         }
00080         
00081         void setType(PacketType pt) {
00082             buffer[0] = (buffer[0] & ~0x03) | static_cast<char>(pt);
00083         }
00084 
00085         void setDest(const NodeId& dest) {
00086             this->dest = dest;
00087         }
00088         
00089     };
00090 }
00091 
00092 
00093 
00094 
00095 #endif //_BASEPACKET_H__
00096 /* This stuff is for emacs
00097  * Local variables:
00098  * mode:c++
00099  * c-basic-offset: 4
00100  * End:
00101  */

Generated on Tue Dec 11 17:58:47 2007 for AWDS by  doxygen 1.5.3-20071008