Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 1 | /** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 5 | * |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 6 | * GNU 3.0 license, See the LICENSE file for more information |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #ifndef NDN_WIRE_H |
| 12 | #define NDN_WIRE_H |
| 13 | |
| 14 | #include "ns3/buffer.h" |
| 15 | |
| 16 | #include "ns3/ndn-common.h" |
| 17 | #include "ns3/ndn-name.h" |
| 18 | #include "ns3/ndn-interest.h" |
| 19 | #include "ns3/ndn-content-object.h" |
| 20 | |
| 21 | NDN_NAMESPACE_BEGIN |
| 22 | |
| 23 | struct Wire |
| 24 | { |
| 25 | enum |
| 26 | { |
| 27 | WIRE_FORMAT_DEFAULT = -2, |
| 28 | WIRE_FORMAT_AUTODETECT = -1, |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 30 | WIRE_FORMAT_NDNSIM = 0, |
| 31 | WIRE_FORMAT_CCNB = 1 |
| 32 | }; |
| 33 | |
| 34 | static Ptr<Packet> |
| 35 | FromInterest (Ptr<const Interest> interest, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
| 36 | |
| 37 | static Ptr<Interest> |
| 38 | ToInterest (Ptr<Packet> packet, int8_t type = WIRE_FORMAT_AUTODETECT); |
| 39 | |
| 40 | static Ptr<Packet> |
| 41 | FromData (Ptr<const ContentObject> data, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
| 42 | |
| 43 | static Ptr<ContentObject> |
| 44 | ToData (Ptr<Packet> packet, int8_t type = WIRE_FORMAT_AUTODETECT); |
| 45 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 46 | |
| 47 | // Helper methods for Python |
| 48 | static std::string |
| 49 | FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
| 50 | |
| 51 | static Ptr<Interest> |
| 52 | ToInterestStr (const std::string &wire, int8_t type = WIRE_FORMAT_AUTODETECT); |
| 53 | |
| 54 | static std::string |
| 55 | FromDataStr (Ptr<const ContentObject> data, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
| 56 | |
| 57 | static Ptr<ContentObject> |
| 58 | ToDataStr (const std::string &wire, int8_t type = WIRE_FORMAT_AUTODETECT); |
| 59 | |
| 60 | // /* |
| 61 | // * @brief Get size of buffer to fit wire-formatted name object |
| 62 | // */ |
| 63 | // static uint32_t |
| 64 | // FromNameSize (Ptr<const Name> name, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
| 65 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @brief Convert name to wire format |
| 68 | */ |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 69 | static std::string |
| 70 | FromName (Ptr<const Name> name, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * @brief Convert name from wire format |
| 74 | */ |
| 75 | static Ptr<Name> |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 76 | ToName (const std::string &wire, int8_t wireFormat = WIRE_FORMAT_DEFAULT); |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Alexander Afanasyev | 76b1157 | 2013-07-16 21:49:50 -0700 | [diff] [blame] | 79 | inline std::string |
| 80 | PacketToBuffer (Ptr<const Packet> pkt) |
| 81 | { |
| 82 | std::string buffer; |
| 83 | buffer.resize (pkt->GetSize ()); |
| 84 | pkt->CopyData (reinterpret_cast<uint8_t*> (&buffer[0]), buffer.size ()); |
| 85 | |
| 86 | return buffer; |
| 87 | } |
| 88 | |
| 89 | inline Ptr<Packet> |
| 90 | BufferToPacket (const std::string &buffer) |
| 91 | { |
| 92 | return Create<Packet> (reinterpret_cast<const uint8_t*> (&buffer[0]), buffer.size ()); |
| 93 | } |
| 94 | |
| 95 | |
Alexander Afanasyev | 7b923f3 | 2013-07-16 13:23:04 -0700 | [diff] [blame] | 96 | NDN_NAMESPACE_END |
| 97 | |
| 98 | #endif // NDN_WIRE_H |