Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2005,2006,2007 INRIA |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include "ccnx-net-device-face.h" |
| 23 | |
| 24 | #include "ns3/ccnx-l3-protocol.h" |
| 25 | #include "ns3/net-device.h" |
| 26 | #include "ns3/log.h" |
| 27 | #include "ns3/packet.h" |
| 28 | #include "ns3/node.h" |
| 29 | #include "ns3/pointer.h" |
| 30 | |
| 31 | NS_LOG_COMPONENT_DEFINE ("CcnxNetDeviceFace"); |
| 32 | |
| 33 | namespace ns3 { |
| 34 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 35 | /** |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 36 | * By default, Ccnx face are created in the "down" state. Before |
| 37 | * becoming useable, the user must invoke SetUp on the face |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 38 | */ |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 39 | CcnxNetDeviceFace::CcnxNetDeviceFace (const Ptr<NetDevice> &netDevice) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 40 | { |
| 41 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 42 | |
| 43 | m_netDevice = netDevice; |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | CcnxNetDeviceFace::~CcnxNetDeviceFace () |
| 47 | { |
| 48 | NS_LOG_FUNCTION_NOARGS (); |
| 49 | } |
| 50 | |
| 51 | CcnxNetDeviceFace::CcnxNetDeviceFace (const CcnxNetDeviceFace &) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | CcnxNetDeviceFace& CcnxNetDeviceFace::operator= (const CcnxNetDeviceFace &) |
| 56 | { |
| 57 | return *this; |
| 58 | } |
| 59 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 60 | Ptr<NetDevice> |
| 61 | CcnxNetDeviceFace::GetNetDevice () const |
| 62 | { |
| 63 | return m_netDevice; |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | CcnxNetDeviceFace::RegisterProtocolHandler (ProtocolHandler handler) |
| 68 | { |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 69 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 70 | NS_ASSERT_MSG (m_netDevice != 0, "CcnxNetDeviceFace needs to be assigned NetDevice first"); |
| 71 | |
| 72 | m_protocolHandler = handler; |
| 73 | |
| 74 | m_node->RegisterProtocolHandler (MakeCallback (&CcnxNetDeviceFace::ReceiveFromNetDevice, this), |
| 75 | CcnxL3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, true/*promiscuous mode*/); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | CcnxNetDeviceFace::Send (Ptr<Packet> packet) |
| 80 | { |
| 81 | NS_ASSERT_MSG (packet->GetSize () <= m_netDevice->GetMtu (), |
| 82 | "Packet size " << packet->GetSize () << " exceeds device MTU " |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 83 | << m_netDevice->GetMtu () |
| 84 | << " for Ccnx; fragmentation not supported"); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 85 | |
| 86 | NS_LOG_FUNCTION (*packet); |
| 87 | if (!IsUp ()) |
| 88 | { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | m_netDevice->Send (packet, m_netDevice->GetBroadcast (), |
Alexander Afanasyev | a5bbe0e | 2011-11-22 17:28:39 -0800 | [diff] [blame^] | 93 | CcnxL3Protocol::ETHERNET_FRAME_TYPE); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // callback |
| 97 | void |
| 98 | CcnxNetDeviceFace::ReceiveFromNetDevice (Ptr<NetDevice> device, |
| 99 | Ptr<const Packet> p, |
| 100 | uint16_t protocol, |
| 101 | const Address &from, |
| 102 | const Address &to, |
| 103 | NetDevice::PacketType packetType) |
| 104 | { |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 105 | m_protocolHandler (Ptr<CcnxFace>(this), p); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 109 | std::ostream& |
| 110 | CcnxNetDeviceFace::Print (std::ostream& os) const |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 111 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 112 | os << "dev=net(" << GetId () << ")"; |
| 113 | return os; |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | }; // namespace ns3 |
| 117 | |