add p2p netdevice and channel

This commit is contained in:
Mathieu Lacage committed 2007-02-12 16:45:58 +01:00
1 parent 6c4f3dfdc0
commit 8769a67a47
5 files changed
+275

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-
//
// Copyright (c) 2006 Georgia Tech Research Corporation
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//
// Implementation of simple point-to-point channel
// George F. Riley, Georgia Tech, Spring 2007
#include "ns3/simulator.h"
#include "ns3/packet.h"
#include "p2p-channel.h"
#include "p2p-net-device.h"
#include "node.h"
namespace ns3 {
P2PChannel::P2PChannel(const Time& delay, double maxRate)
: m_nd1(0), m_nd2(0),
m_delay (delay),
m_maxRate (maxRate)
{
}
// Channels create compatible net devices
P2PNetDevice* P2PChannel::CreateNetDevice(Node &node, MacAddress address)
{
// Create a new point-to-point network device
P2PNetDevice* nd = new P2PNetDevice(node, address);
nd->Connect (this);
// Add to list of peers
if (!m_nd1) m_nd1 = nd;
else m_nd2 = nd;
return nd;
}
void P2PChannel::RemoveNetDevice(NetDevice* nd)
{
if (nd == m_nd1) m_nd1 = 0;
if (nd == m_nd2) m_nd2 = 0;
// Now if all removed, remove the channel as well
if (!m_nd1 && !m_nd2)
{
delete this;
}
}
void P2PChannel::Send(P2PNetDevice *device, Packet& p, double rate)
{ // Schedule a receive event at receiver
// First calculate time in future
double maxRate;
if (rate < m_maxRate)
{
maxRate = rate;
}
else
{
maxRate = m_maxRate;
}
Time txTime = Seconds (p.GetSize() * 8 / maxRate);
Time rxTime = m_delay + txTime;
P2PNetDevice *to, *from;
if (device == m_nd1)
{
from = m_nd1;
to = m_nd2;
}
else
{
from = m_nd2;
to = m_nd1;
}
// Schedule the receive event at receiver
Simulator::Schedule(rxTime, &P2PNetDevice::Receive, to, p);
// Schedule the link free event
Simulator::Schedule(txTime, &P2PNetDevice::TxComplete, from);
}
}//namespace ns3
+56
View File
@@ -0,0 +1,56 @@
// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-
//
// Copyright (c) 2006 Georgia Tech Research Corporation
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//
// Definition of a simple point-to-point channel
// George F. Riley, Georgia Tech, Spring 2007
#ifndef P2P_CHANNEL_H
#define P2P_CHANNEL_H
#include "ns3/nstime.h"
#include "mac-address.h"
namespace ns3 {
class P2PNetDevice;
class NetDevice;
class Node;
class Packet;
class P2PChannel {
public:
P2PChannel(const Time& delay, double maxRate /* bits/s */);
~P2PChannel();
P2PNetDevice* CreateNetDevice(Node &node, MacAddress address);
void RemoveNetDevice (NetDevice *device);
void Send (P2PNetDevice *device, Packet&p, double rate /* bits/s */);
private:
// The two endpoints of this channel
P2PNetDevice* m_nd1;
P2PNetDevice* m_nd2;
Time m_delay;
double m_maxRate;
};
}//namespace ns3
#endif
+68
View File
@@ -0,0 +1,68 @@
// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-
//
// Copyright (c) 2006 Georgia Tech Research Corporation
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//
// Implementation of a point-to-point network device
// George F. Riley, Georgia Tech, Spring 2007
#include "p2p-net-device.h"
#include "p2p-channel.h"
namespace ns3 {
P2PNetDevice::P2PNetDevice (Node &node, MacAddress const &addr)
: NetDevice (node, addr)
{}
P2PNetDevice::~P2PNetDevice()
{ // Inform channel that we are destroyed
m_channel->RemoveNetDevice(this);
}
void
P2PNetDevice::SetRate (double rate)
{
m_rate = rate;
}
void
P2PNetDevice::Connect (P2PChannel *channel)
{
m_channel = channel;
}
bool
P2PNetDevice::SendTo (Packet& p, const MacAddress&)
{
m_channel->Send (this, p, m_rate);
return true;
}
void
P2PNetDevice::Receive(Packet p)
{
ForwardUp (p);
}
void
P2PNetDevice::TxComplete (void)
{}
}//namespace ns3
+52
View File
@@ -0,0 +1,52 @@
// -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-
//
// Copyright (c) 2006 Georgia Tech Research Corporation
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//
// Definition for a Point-to-Point network device
// George F. Riley, Georgia Tech, Spring 2007
#ifndef P2P_NET_DEVICE_H
#define P2P_NET_DEVICE_H
#include "net-device.h"
namespace ns3 {
class P2PChannel;
class P2PNetDevice : public NetDevice {
public:
P2PNetDevice(Node &node, MacAddress const &addr);
virtual ~P2PNetDevice();
void SetRate (double Rate);
void Connect (P2PChannel *channel);
void Receive(Packet p);
void TxComplete (void);
private:
virtual bool SendTo (Packet& p, const MacAddress& dest);
double m_rate;
P2PChannel *m_channel;
};
}//namespace ns3
#endif /* P2P_NET_DEVICE_H */