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 | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 35 | // NS_OBJECT_ENSURE_REGISTERED (CcnxNetDeviceFace); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 37 | // TypeId |
| 38 | // CcnxNetDeviceFace::GetTypeId () |
| 39 | // { |
| 40 | // static TypeId tid = TypeId ("ns3::CcnxNetDeviceFace") |
| 41 | // .SetGroupName ("Ccnx") |
| 42 | // .SetParent<CcnxFace> () |
| 43 | // ; |
| 44 | // return tid; |
| 45 | // } |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 48 | * By default, Ccnx face are created in the "down" state. Before |
| 49 | * becoming useable, the user must invoke SetUp on the face |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 50 | */ |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 51 | CcnxNetDeviceFace::CcnxNetDeviceFace (const Ptr<NetDevice> &netDevice) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 52 | { |
| 53 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 54 | |
| 55 | m_netDevice = netDevice; |
Ilya Moiseenko | 57959f9 | 2011-10-28 13:15:58 -0700 | [diff] [blame] | 56 | m_isLocal = false; |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | CcnxNetDeviceFace::~CcnxNetDeviceFace () |
| 60 | { |
| 61 | NS_LOG_FUNCTION_NOARGS (); |
| 62 | } |
| 63 | |
| 64 | CcnxNetDeviceFace::CcnxNetDeviceFace (const CcnxNetDeviceFace &) |
| 65 | { |
Ilya Moiseenko | 57959f9 | 2011-10-28 13:15:58 -0700 | [diff] [blame] | 66 | m_isLocal = false; |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | CcnxNetDeviceFace& CcnxNetDeviceFace::operator= (const CcnxNetDeviceFace &) |
| 70 | { |
| 71 | return *this; |
| 72 | } |
| 73 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 74 | Ptr<NetDevice> |
| 75 | CcnxNetDeviceFace::GetNetDevice () const |
| 76 | { |
| 77 | return m_netDevice; |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | CcnxNetDeviceFace::RegisterProtocolHandler (ProtocolHandler handler) |
| 82 | { |
Ilya Moiseenko | 25f7d4d | 2011-09-29 18:41:06 -0700 | [diff] [blame] | 83 | NS_LOG_FUNCTION(this); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 84 | NS_ASSERT_MSG (m_netDevice != 0, "CcnxNetDeviceFace needs to be assigned NetDevice first"); |
| 85 | |
| 86 | m_protocolHandler = handler; |
| 87 | |
| 88 | m_node->RegisterProtocolHandler (MakeCallback (&CcnxNetDeviceFace::ReceiveFromNetDevice, this), |
| 89 | CcnxL3Protocol::ETHERNET_FRAME_TYPE, m_netDevice, true/*promiscuous mode*/); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | CcnxNetDeviceFace::Send (Ptr<Packet> packet) |
| 94 | { |
| 95 | NS_ASSERT_MSG (packet->GetSize () <= m_netDevice->GetMtu (), |
| 96 | "Packet size " << packet->GetSize () << " exceeds device MTU " |
| 97 | << m_netDevice->GetMtu () |
| 98 | << " for Ccnx; fragmentation not supported"); |
| 99 | |
| 100 | NS_LOG_FUNCTION (*packet); |
| 101 | if (!IsUp ()) |
| 102 | { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | m_netDevice->Send (packet, m_netDevice->GetBroadcast (), |
| 107 | CcnxL3Protocol::ETHERNET_FRAME_TYPE); |
| 108 | } |
| 109 | |
| 110 | // callback |
| 111 | void |
| 112 | CcnxNetDeviceFace::ReceiveFromNetDevice (Ptr<NetDevice> device, |
| 113 | Ptr<const Packet> p, |
| 114 | uint16_t protocol, |
| 115 | const Address &from, |
| 116 | const Address &to, |
| 117 | NetDevice::PacketType packetType) |
| 118 | { |
Alexander Afanasyev | 0ab833e | 2011-08-18 15:49:13 -0700 | [diff] [blame] | 119 | m_protocolHandler (Ptr<CcnxFace>(this), p); |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 123 | std::ostream& |
| 124 | CcnxNetDeviceFace::Print (std::ostream& os) const |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 125 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 126 | os << "dev=net(" << GetId () << ")"; |
| 127 | return os; |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | }; // namespace ns3 |
| 131 | |