Files
unison/samples/main-packet.cc

101 lines
1.8 KiB
C++
Raw Normal View History

2006-09-05 13:18:11 +02:00
/* -*- Mode:C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:f -*- */
2006-08-29 17:51:04 +02:00
#include "ns3/packet.h"
#include "ns3/chunk.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
/* A sample Chunk implementation
*/
class MyChunk : public Chunk {
public:
2006-09-05 13:18:11 +02:00
MyChunk ();
virtual ~MyChunk ();
2006-08-29 17:47:17 +02:00
2006-09-05 13:18:11 +02:00
void setData (uint16_t data);
uint16_t getData (void) const;
2006-08-29 17:47:17 +02:00
private:
2006-09-05 13:18:11 +02:00
virtual void print (std::ostream *os) const;
virtual void addTo (Buffer *buffer) const;
virtual void peekFrom (Buffer const *buffer);
virtual void removeFrom (Buffer *buffer);
2006-08-29 17:47:17 +02:00
2006-09-05 13:18:11 +02:00
uint16_t m_data;
2006-08-29 17:47:17 +02:00
};
MyChunk::MyChunk ()
{}
MyChunk::~MyChunk ()
{}
void
MyChunk::print (std::ostream *os) const
{
2006-09-05 13:18:11 +02:00
*os << "MyChunk data=" << m_data << std::endl;
2006-08-29 17:47:17 +02:00
}
void
MyChunk::addTo (Buffer *buffer) const
2006-08-29 17:47:17 +02:00
{
2006-09-05 13:18:11 +02:00
// reserve 2 bytes at head of buffer
buffer->addAtStart (2);
Buffer::Iterator i = buffer->begin ();
// serialize in head of buffer
i.writeHtonU16 (m_data);
2006-08-29 17:47:17 +02:00
}
void
MyChunk::peekFrom (Buffer const *buffer)
2006-08-29 17:47:17 +02:00
{
2006-09-05 13:18:11 +02:00
Buffer::Iterator i = buffer->begin ();
// deserialize from head of buffer
m_data = i.readNtohU16 ();
2006-08-29 17:47:17 +02:00
}
void
MyChunk::removeFrom (Buffer *buffer)
2006-08-29 17:47:17 +02:00
{
2006-09-05 13:18:11 +02:00
// remove deserialized data
buffer->removeAtStart (2);
2006-08-29 17:47:17 +02:00
}
void
MyChunk::setData (uint16_t data)
2006-08-29 17:47:17 +02:00
{
2006-09-05 13:18:11 +02:00
m_data = data;
2006-08-29 17:47:17 +02:00
}
uint16_t
MyChunk::getData (void) const
2006-08-29 17:47:17 +02:00
{
2006-09-05 13:18:11 +02:00
return m_data;
2006-08-29 17:47:17 +02:00
}
/* A sample Tag implementation
*/
struct MyTag {
2006-09-05 13:18:11 +02:00
uint16_t m_streamId;
2006-08-29 17:47:17 +02:00
};
static void
receive (Packet p)
{
2006-09-05 13:18:11 +02:00
MyChunk my;
p.peek (&my);
p.remove (&my);
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-09-05 13:18:11 +02:00
Packet p;
MyChunk my;
my.setData (2);
std::cout << "send data=2" << std::endl;
p.add (&my);
struct MyTag myTag;
myTag.m_streamId = 5;
p.addTag (&myTag);
receive (p);
return 0;
2006-08-29 17:47:17 +02:00
}