// double timeStamp, string senderId, double x, double y, int type, long id void OvnisPacket::ReadHeader(Ptr<Packet> packet) { try { this->packet = packet; // read size of header t = (uint8_t*)(((malloc(sizeof(int) * sizeof (uint8_t))))); int size = packet->CopyData(t, sizeof(int)); tg = TagBuffer(t, t + size); packetSize = tg.ReadU32(); // cout << "Read packet " << packetSize << endl; // read header t = (uint8_t*)(((malloc(packetSize * sizeof (uint8_t))))); size = packet->CopyData(t, packetSize); tg = TagBuffer(t, t + size); packetSize = tg.ReadU32(); timeStamp = tg.ReadDouble(); position.x = tg.ReadDouble(); position.y = tg.ReadDouble(); uint32_t s_size = tg.ReadU32(); senderId.clear(); char *s = (char*)(((malloc(s_size * sizeof (char))))); tg.Read((uint8_t*)(((s))), s_size); senderId.assign(s, s_size); type = tg.ReadU32(); // cout << "timeStamp: " << timeStamp << ", x: " << x << ", y: "<< y << ", sender: " << senderId << ", type: " << type << endl; free(s); } catch (exception & e) { throw e; } }
TagBuffer ByteTagList::Add (TypeId tid, uint32_t bufferSize, int32_t start, int32_t end) { NS_LOG_FUNCTION (this << tid << bufferSize << start << end); uint32_t spaceNeeded = m_used + bufferSize + 4 + 4 + 4 + 4; NS_ASSERT (m_used <= spaceNeeded); if (m_data == 0) { m_data = Allocate (spaceNeeded); m_used = 0; } else if (m_data->size < spaceNeeded || (m_data->count != 1 && m_data->dirty != m_used)) { struct ByteTagListData *newData = Allocate (spaceNeeded); std::memcpy (&newData->data, &m_data->data, m_used); Deallocate (m_data); m_data = newData; } TagBuffer tag = TagBuffer (&m_data->data[m_used], &m_data->data[spaceNeeded]); tag.WriteU32 (tid.GetUid ()); tag.WriteU32 (bufferSize); tag.WriteU32 (start); tag.WriteU32 (end); m_used = spaceNeeded; m_data->dirty = m_used; return tag; }
OvnisPacket::OvnisPacket(Ptr<Packet> packet) : tg(TagBuffer(0,0)) { try { ReadHeader(packet); } catch (exception & e) { throw e; } }
struct ByteTagList::Iterator::Item ByteTagList::Iterator::Next (void) { NS_ASSERT (HasNext ()); struct Item item = Item (TagBuffer (m_current+16, m_end)); item.tid.SetUid (m_nextTid); item.size = m_nextSize; item.start = std::max (m_nextStart, m_offsetStart); item.end = std::min (m_nextEnd, m_offsetEnd); m_current += 4 + 4 + 4 + 4 + item.size; item.buf.TrimAtEnd (m_end - m_current); PrepareForNext (); return item; }
void ByteTagList::Iterator::PrepareForNext (void) { NS_LOG_FUNCTION (this); while (m_current < m_end) { TagBuffer buf = TagBuffer (m_current, m_end); m_nextTid = buf.ReadU32 (); m_nextSize = buf.ReadU32 (); m_nextStart = buf.ReadU32 (); m_nextEnd = buf.ReadU32 (); if (m_nextStart >= m_offsetEnd || m_nextEnd <= m_offsetStart) { m_current += 4 + 4 + 4 + 4 + m_nextSize; } else { break; } } }
OvnisPacket::OvnisPacket() : tg(TagBuffer(0,0)) { }