Files
unison/samples/main-packet.cc

98 lines
1.8 KiB
C++
Raw Normal View History

2006-11-01 13:11:30 +01:00
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2006-08-29 17:51:04 +02:00
#include "ns3/packet.h"
2006-10-02 11:23:18 +02:00
#include "ns3/header.h"
2006-08-29 17:47:17 +02:00
#include <iostream>
2006-08-29 17:55:34 +02:00
using namespace ns3;
2006-08-29 17:47:17 +02:00
2006-10-13 13:07:45 -07:00
/* A sample Header implementation
2006-08-29 17:47:17 +02:00
*/
2006-10-02 11:23:18 +02:00
class MyHeader : public Header {
2006-08-29 17:47:17 +02:00
public:
2006-11-01 13:11:30 +01:00
MyHeader ();
virtual ~MyHeader ();
2006-08-29 17:47:17 +02:00
2006-11-01 13:11:30 +01:00
void SetData (uint16_t data);
uint16_t GetData (void) const;
2006-08-29 17:47:17 +02:00
private:
2006-11-01 13:11:30 +01:00
virtual void PrintTo (std::ostream &os) const;
virtual void SerializeTo (Buffer::Iterator start) const;
virtual uint32_t DeserializeFrom (Buffer::Iterator start);
2006-11-01 13:11:30 +01:00
virtual uint32_t GetSerializedSize (void) const;
2006-08-29 17:47:17 +02:00
2006-11-01 13:11:30 +01:00
uint16_t m_data;
2006-08-29 17:47:17 +02:00
};
2006-10-02 11:23:18 +02:00
MyHeader::MyHeader ()
2006-08-29 17:47:17 +02:00
{}
2006-10-02 11:23:18 +02:00
MyHeader::~MyHeader ()
2006-08-29 17:47:17 +02:00
{}
void
2006-10-06 13:37:25 +02:00
MyHeader::PrintTo (std::ostream &os) const
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
os << "MyHeader data=" << m_data << std::endl;
2006-08-29 17:47:17 +02:00
}
2006-10-02 11:23:18 +02:00
uint32_t
2006-10-06 13:37:25 +02:00
MyHeader::GetSerializedSize (void) const
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
return 2;
2006-08-29 17:47:17 +02:00
}
void
2006-10-06 13:37:25 +02:00
MyHeader::SerializeTo (Buffer::Iterator start) const
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
// serialize in head of buffer
start.WriteHtonU16 (m_data);
2006-08-29 17:47:17 +02:00
}
uint32_t
2006-10-06 13:37:25 +02:00
MyHeader::DeserializeFrom (Buffer::Iterator start)
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
// deserialize from head of buffer
m_data = start.ReadNtohU16 ();
return GetSerializedSize ();
2006-08-29 17:47:17 +02:00
}
void
2006-10-06 13:37:25 +02:00
MyHeader::SetData (uint16_t data)
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
m_data = data;
2006-08-29 17:47:17 +02:00
}
uint16_t
2006-10-06 13:37:25 +02:00
MyHeader::GetData (void) const
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
return m_data;
2006-08-29 17:47:17 +02:00
}
/* A sample Tag implementation
*/
struct MyTag {
2006-11-01 13:11:30 +01:00
uint16_t m_streamId;
2006-08-29 17:47:17 +02:00
};
static TagRegistration<struct MyTag> g_MyTagRegistration ("ns3::MyTag", 0);
2006-08-29 17:47:17 +02:00
static void
2006-10-06 13:37:25 +02:00
Receive (Packet p)
2006-08-29 17:47:17 +02:00
{
2006-11-01 13:11:30 +01:00
MyHeader my;
p.RemoveHeader (my);
2006-11-01 13:11:30 +01:00
std::cout << "received data=" << my.GetData () << std::endl;
struct MyTag myTag;
p.PeekTag (myTag);
2006-08-29 17:47:17 +02:00
}
int main (int argc, char *argv[])
{
2006-11-01 13:11:30 +01:00
Packet p;
MyHeader my;
my.SetData (2);
std::cout << "send data=2" << std::endl;
p.AddHeader (my);
2006-11-01 13:11:30 +01:00
struct MyTag myTag;
myTag.m_streamId = 5;
p.AddTag (myTag);
Receive (p);
return 0;
2006-08-29 17:47:17 +02:00
}