Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 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 | * |
Ilya Moiseenko | 1cf6b0a | 2011-08-29 13:02:34 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "ccnx-local-face.h" |
| 24 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 25 | #include "ns3/log.h" |
| 26 | #include "ns3/packet.h" |
| 27 | #include "ns3/node.h" |
| 28 | #include "ns3/pointer.h" |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 29 | #include "ns3/assert.h" |
| 30 | |
| 31 | #include "ns3/ccnx-header-helper.h" |
| 32 | #include "ccnx-interest-header.h" |
| 33 | #include "ccnx-content-object-header.h" |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 34 | |
| 35 | NS_LOG_COMPONENT_DEFINE ("CcnxLocalFace"); |
| 36 | |
Ilya Moiseenko | 1cf6b0a | 2011-08-29 13:02:34 -0700 | [diff] [blame] | 37 | namespace ns3 |
| 38 | { |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 40 | // NS_OBJECT_ENSURE_REGISTERED (CcnxLocalFace); |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 42 | // TypeId |
| 43 | // CcnxLocalFace::GetTypeId (void) |
| 44 | // { |
| 45 | // static TypeId tid = TypeId ("ns3::CcnxLocalFace") |
| 46 | // .SetGroupName ("Ccnx") |
| 47 | // .SetParent<CcnxFace> () |
| 48 | // ; |
| 49 | // return tid; |
| 50 | // } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 52 | CcnxLocalFace::CcnxLocalFace () |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 53 | : m_onInterest (0) |
| 54 | , m_onContentObject (0) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 55 | { |
| 56 | NS_LOG_FUNCTION (this); |
Ilya Moiseenko | acac1ea | 2011-10-28 13:16:53 -0700 | [diff] [blame] | 57 | m_isLocal = true; |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | CcnxLocalFace::~CcnxLocalFace () |
| 61 | { |
| 62 | NS_LOG_FUNCTION_NOARGS (); |
| 63 | } |
| 64 | |
| 65 | void |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 66 | CcnxLocalFace::RegisterProtocolHandler (ProtocolHandler handler) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 67 | { |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 68 | m_protocolHandler = handler; |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 71 | void |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 72 | CcnxLocalFace::SetInterestHandler (InterestHandler onInterest) |
| 73 | { |
| 74 | m_onInterest = onInterest; |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | CcnxLocalFace::SetContentObjectHandler (ContentObjectHandler onContentObject) |
| 79 | { |
| 80 | m_onContentObject = onContentObject; |
| 81 | } |
Ilya Moiseenko | acac1ea | 2011-10-28 13:16:53 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 83 | void |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 84 | CcnxLocalFace::Send (Ptr<Packet> p) |
| 85 | { |
Ilya Moiseenko | acac1ea | 2011-10-28 13:16:53 -0700 | [diff] [blame] | 86 | NS_LOG_FUNCTION("Local face send"); |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 87 | NS_LOG_FUNCTION (*p); |
| 88 | if (!IsUp ()) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 93 | try |
| 94 | { |
| 95 | CcnxHeaderHelper::Type type = CcnxHeaderHelper::GetCcnxHeaderType (p); |
| 96 | switch (type) |
| 97 | { |
| 98 | case CcnxHeaderHelper::INTEREST: |
| 99 | if (!m_onInterest.IsNull ()) |
| 100 | { |
| 101 | Ptr<CcnxInterestHeader> header = Create<CcnxInterestHeader> (); |
| 102 | p->RemoveHeader (*header); |
| 103 | m_onInterest (header); |
| 104 | } |
| 105 | break; |
| 106 | case CcnxHeaderHelper::CONTENT_OBJECT: |
| 107 | if (!m_onContentObject.IsNull ()) |
| 108 | { |
| 109 | static CcnxContentObjectTail tail; |
| 110 | Ptr<CcnxContentObjectHeader> header = Create<CcnxContentObjectHeader> (); |
| 111 | p->RemoveHeader (*header); |
| 112 | p->RemoveTrailer (tail); |
| 113 | m_onContentObject (header, p/*payload*/); |
| 114 | } |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | catch (CcnxUnknownHeaderException) |
| 119 | { |
| 120 | NS_LOG_ERROR ("Unknown header type"); |
| 121 | } |
Ilya Moiseenko | 1cf6b0a | 2011-08-29 13:02:34 -0700 | [diff] [blame] | 122 | } |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 124 | // propagate interest down to ccnx stack |
| 125 | void |
| 126 | CcnxLocalFace::ReceiveFromApplication (Ptr<Packet> p) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 127 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 128 | m_protocolHandler (Ptr<CcnxFace>(this), p); |
| 129 | } |
| 130 | |
| 131 | std::ostream& CcnxLocalFace::Print (std::ostream& os) const |
| 132 | { |
| 133 | os << "dev=local(" << GetId() << ")"; |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 134 | return os; |
| 135 | } |
| 136 | |
| 137 | }; // namespace ns3 |
| 138 | |