packet.cpp

00001 
00002 #include "packet.hpp"
00003 
00004 const t_uint Packet::m_DEFAULT_SIZE_IN_BYTES = 512;
00005 const double Packet::m_DEFAULT_DATA_RATE = 1e6;
00006 const t_uint Packet::m_DEFAULT_DESTINATION = 0;
00007 
00008 t_ulong Packet::m_nextUniqueId = 1;
00009 
00010 Packet::Packet()
00011    : m_dataRate(m_DEFAULT_DATA_RATE),
00012    m_txPower(0.0), m_doMaxTxPower(false), m_hasError(false),
00013    m_destination(m_DEFAULT_DESTINATION), m_uniqueId(0)
00014 {
00015 
00016 }
00017 
00018 Packet::~Packet()
00019 {
00020 
00021 }
00022 
00023 Packet::Packet(const Packet& rhs)
00024    : m_dataRate(rhs.m_dataRate), m_txPower(rhs.m_txPower), 
00025    m_hasError(rhs.m_hasError),
00026    m_destination(rhs.m_destination), m_uniqueId(rhs.m_uniqueId)
00027 {
00028    // Create a deep copy of the packet's data
00029    if(rhs.m_data.begin() != rhs.m_data.end()) {
00030       DataTypeMap::const_iterator p;
00031       for(p = rhs.m_data.begin(); p != rhs.m_data.end(); ++p) {
00032          m_data[p->first] = PacketData::create(*(p->second));
00033       }
00034    }
00035 }
00036 
00037 PacketPtr Packet::clone() const
00038 {
00039    PacketPtr p(new Packet(*this));
00040    return p;
00041 }
00042 
00043 void Packet::addData(Packet::DataTypes dataType, const PacketData& data)
00044 {
00045    PacketDataPtr deepCopy = PacketData::create(data);
00046    m_data[dataType] = deepCopy;
00047 }
00048 
00049 PacketDataPtr Packet::getData(Packet::DataTypes dataType)
00050 {
00051    DataTypeMap::iterator dataIterator = m_data.find(dataType);
00052    bool dataFound = (dataIterator != m_data.end());
00053 
00054    PacketDataPtr dataPtr;
00055    if(dataFound) {
00056       dataPtr = dataIterator->second;
00057    }
00058 
00059    return dataPtr;
00060 
00061 }
00062 
00063 bool Packet::removeData(Packet::DataTypes dataType)
00064 {
00065    int numRemoved = m_data.erase(dataType);
00066    bool wasSuccessful = (numRemoved > 0);
00067    return wasSuccessful;
00068 }
00069 
00070 bool Packet::hasUpperLayerData(Packet::DataTypes dataType)
00071 {
00072 
00073    bool hasUpperData = false;
00074 
00075    if(dataType == Packet::DataTypes_Transport ||
00076          dataType == Packet::DataTypes_Network ||
00077          dataType == Packet::DataTypes_Link ||
00078          dataType == Packet::DataTypes_Physical) {
00079       bool hasApplicationData = 
00080          (getData(Packet::DataTypes_Application).get() != 0);
00081       hasUpperData |= hasApplicationData;
00082    }
00083 
00084    if(!hasUpperData && (dataType == Packet::DataTypes_Network ||
00085          dataType == Packet::DataTypes_Link ||
00086          dataType == Packet::DataTypes_Physical)) {
00087       bool hasTransportData = 
00088          (getData(Packet::DataTypes_Transport).get() != 0);
00089       hasUpperData |= hasTransportData;
00090    }
00091 
00092    if(!hasUpperData && (dataType == Packet::DataTypes_Link ||
00093          dataType == Packet::DataTypes_Physical)) {
00094       bool hasNetworkData = 
00095          (getData(Packet::DataTypes_Network).get() != 0);
00096       hasUpperData |= hasNetworkData;
00097    }
00098 
00099    if(!hasUpperData && (dataType == Packet::DataTypes_Physical)) {
00100       bool hasLinkData = 
00101          (getData(Packet::DataTypes_Link).get() != 0);
00102       hasUpperData |= hasLinkData;
00103    }
00104 
00105    return hasUpperData;
00106 
00107 }
00108 
00109 SimTime Packet::getDuration() const
00110 {
00111    double seconds = getSizeInBits() / getDataRate();
00112    SimTime packetDuration(seconds);
00113    return packetDuration;
00114 }
00115 
00116 

Generated on Tue Dec 12 17:04:38 2006 for rfidsim by  doxygen 1.4.7