PyNDN: Initial changes of PyNDN emulation in ndnSIM

The biggest problem so far, which I cannot solve at all, is exception
handling.  NDN.cxx throws many exceptions when something bad happens,
with are not handled by python bindings

Refs #1008 (http://redmine.named-data.net/issues/1008)
Refs #1011 (http://redmine.named-data.net/issues/1011)
diff --git a/model/ndn-interest.h b/model/ndn-interest.h
index d0959a9..16e8fc3 100644
--- a/model/ndn-interest.h
+++ b/model/ndn-interest.h
@@ -229,6 +229,13 @@
   mutable Ptr<const Packet> m_wire;
 };
 
+inline std::ostream &
+operator << (std::ostream &os, const Interest &i)
+{
+  i.Print (os);
+  return os;
+}
+
 inline Ptr<const Packet>
 Interest::GetWire () const
 {
diff --git a/model/wire/ndn-wire.cc b/model/wire/ndn-wire.cc
index 6b50340..6296aa6 100644
--- a/model/wire/ndn-wire.cc
+++ b/model/wire/ndn-wire.cc
@@ -1,10 +1,10 @@
 /** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/* 
+/*
  * Copyright (c) 2013, Regents of the University of California
  *                     Alexander Afanasyev
- * 
+ *
  * GNU 3.0 license, See the LICENSE file for more information
- * 
+ *
  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
  */
 
@@ -25,7 +25,7 @@
 NDN_NAMESPACE_BEGIN
 
 static
-GlobalValue g_wireFormat ("ndn::WireFormat", 
+GlobalValue g_wireFormat ("ndn::WireFormat",
                           "Default wire format for ndnSIM.  ndnSIM will be accepting packets "
                           "in any supported packet formats, but if encoding requested, it will "
                           "use default wire format to encode (0 for ndnSIM (default), 1 for CCNb)",
@@ -51,7 +51,7 @@
 {
   if (wireFormat == WIRE_FORMAT_DEFAULT)
     wireFormat = GetWireFormat ();
-    
+
   if (wireFormat == WIRE_FORMAT_NDNSIM)
     return wire::ndnSIM::Interest::ToWire (interest);
   else if (wireFormat == WIRE_FORMAT_CCNB)
@@ -173,33 +173,97 @@
 }
 
 
-uint32_t
-Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
+std::string
+Wire::FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
 {
-  if (wireFormat == WIRE_FORMAT_DEFAULT)
-    wireFormat = GetWireFormat ();
+  Ptr<Packet> pkt = FromInterest (interest, wireFormat);
+  std::string wire;
+  wire.resize (pkt->GetSize ());
+  pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
 
-  if (wireFormat == WIRE_FORMAT_NDNSIM)
-    return wire::NdnSim::SerializedSizeName (*name);
-  else if (wireFormat == WIRE_FORMAT_CCNB)
-    return wire::Ccnb::SerializedSizeName (*name);
-  else
-    {
-      NS_FATAL_ERROR ("Unsupported format requested");
-      return 0;
-    }
+  return wire;
 }
-  
-void
-Wire::FromName (Buffer::Iterator start, Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
+
+Ptr<Interest>
+Wire::ToInterestStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
 {
+  Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
+  return ToInterest (pkt, type);
+}
+
+std::string
+Wire::FromDataStr (Ptr<const ContentObject> data, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
+{
+  Ptr<Packet> pkt = FromData (data, wireFormat);
+  std::string wire;
+  wire.resize (pkt->GetSize ());
+  pkt->CopyData (reinterpret_cast<uint8_t*> (&wire[0]), wire.size ());
+
+  return wire;
+}
+
+Ptr<ContentObject>
+Wire::ToDataStr (const std::string &wire, int8_t type/* = WIRE_FORMAT_AUTODETECT*/)
+{
+  Ptr<Packet> pkt = Create<Packet> (reinterpret_cast<const uint8_t*> (&wire[0]), wire.size ());
+  return ToData (pkt, type);
+}
+
+// uint32_t
+// Wire::FromNameSize (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
+// {
+//   if (wireFormat == WIRE_FORMAT_DEFAULT)
+//     wireFormat = GetWireFormat ();
+
+//   if (wireFormat == WIRE_FORMAT_NDNSIM)
+//     {
+//       std::cout << wire::NdnSim::SerializedSizeName (*name) << std::endl;
+//       return wire::NdnSim::SerializedSizeName (*name);
+//     }
+//   else if (wireFormat == WIRE_FORMAT_CCNB)
+//     return wire::Ccnb::SerializedSizeName (*name);
+//   else
+//     {
+//       NS_FATAL_ERROR ("Unsupported format requested");
+//       return 0;
+//     }
+// }
+
+std::string
+Wire::FromName (Ptr<const Name> name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
+{
+
   if (wireFormat == WIRE_FORMAT_DEFAULT)
     wireFormat = GetWireFormat ();
 
   if (wireFormat == WIRE_FORMAT_NDNSIM)
-    wire::NdnSim::SerializedSizeName (*name);
+    {
+      Buffer buf;
+      buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
+      Buffer::Iterator i = buf.Begin ();
+
+      wire::NdnSim::SerializeName (i, *name);
+
+      std::string wire;
+      wire.resize (buf.GetSize ());
+      buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
+
+      return wire;
+    }
   else if (wireFormat == WIRE_FORMAT_CCNB)
-    wire::Ccnb::SerializedSizeName (*name);
+    {
+      Buffer buf;
+      buf.AddAtStart (wire::NdnSim::SerializedSizeName (*name));
+      Buffer::Iterator i = buf.Begin ();
+
+      wire::Ccnb::SerializeName (i, *name);
+
+      std::string wire;
+      wire.resize (buf.GetSize ());
+      buf.CopyData (reinterpret_cast<uint8_t *>(&wire[0]), wire.size ());
+
+      return wire;
+    }
   else
     {
       NS_FATAL_ERROR ("Unsupported format requested");
@@ -207,17 +271,25 @@
 }
 
 Ptr<Name>
-Wire::ToName (Buffer::Iterator start, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
+Wire::ToName (const std::string &name, int8_t wireFormat/* = WIRE_FORMAT_DEFAULT*/)
 {
+  Buffer buf;
+  buf.AddAtStart (name.size ());
+  Buffer::Iterator i = buf.Begin ();
+  i.Write (reinterpret_cast<const uint8_t *> (&name[0]), name.size ());
+
+  i = buf.Begin ();
+
   if (wireFormat == WIRE_FORMAT_DEFAULT)
     wireFormat = GetWireFormat ();
 
   if (wireFormat == WIRE_FORMAT_NDNSIM)
-    return wire::NdnSim::DeserializeName (start);
+    {
+      return wire::NdnSim::DeserializeName (i);
+    }
   else if (wireFormat == WIRE_FORMAT_CCNB)
     {
-      return wire::Ccnb::DeserializeName (start);
-      return 0;
+      return wire::Ccnb::DeserializeName (i);
     }
   else
     {
diff --git a/model/wire/ndn-wire.h b/model/wire/ndn-wire.h
index e83c217..768ccea 100644
--- a/model/wire/ndn-wire.h
+++ b/model/wire/ndn-wire.h
@@ -1,10 +1,10 @@
 /** -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/* 
+/*
  * Copyright (c) 2013, Regents of the University of California
  *                     Alexander Afanasyev
- * 
+ *
  * GNU 3.0 license, See the LICENSE file for more information
- * 
+ *
  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
  */
 
@@ -26,7 +26,7 @@
     {
       WIRE_FORMAT_DEFAULT = -2,
       WIRE_FORMAT_AUTODETECT = -1,
-    
+
       WIRE_FORMAT_NDNSIM = 0,
       WIRE_FORMAT_CCNB = 1
     };
@@ -43,25 +43,56 @@
   static Ptr<ContentObject>
   ToData (Ptr<Packet> packet, int8_t type = WIRE_FORMAT_AUTODETECT);
 
-  /*
-   * @brief Get size of buffer to fit wire-formatted name object
-   */
-  static uint32_t
-  FromNameSize (Ptr<const Name> name, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
-  
+
+  // Helper methods for Python
+  static std::string
+  FromInterestStr (Ptr<const Interest> interest, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
+
+  static Ptr<Interest>
+  ToInterestStr (const std::string &wire, int8_t type = WIRE_FORMAT_AUTODETECT);
+
+  static std::string
+  FromDataStr (Ptr<const ContentObject> data, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
+
+  static Ptr<ContentObject>
+  ToDataStr (const std::string &wire, int8_t type = WIRE_FORMAT_AUTODETECT);
+
+  // /*
+  //  * @brief Get size of buffer to fit wire-formatted name object
+  //  */
+  // static uint32_t
+  // FromNameSize (Ptr<const Name> name, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
+
   /**
    * @brief Convert name to wire format
    */
-  static void
-  FromName (Buffer::Iterator start, Ptr<const Name> name, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
+  static std::string
+  FromName (Ptr<const Name> name, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
 
   /**
    * @brief Convert name from wire format
    */
   static Ptr<Name>
-  ToName (Buffer::Iterator start, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
+  ToName (const std::string &wire, int8_t wireFormat = WIRE_FORMAT_DEFAULT);
 };
 
+inline std::string
+PacketToBuffer (Ptr<const Packet> pkt)
+{
+  std::string buffer;
+  buffer.resize (pkt->GetSize ());
+  pkt->CopyData (reinterpret_cast<uint8_t*> (&buffer[0]), buffer.size ());
+
+  return buffer;
+}
+
+inline Ptr<Packet>
+BufferToPacket (const std::string &buffer)
+{
+  return Create<Packet> (reinterpret_cast<const uint8_t*> (&buffer[0]), buffer.size ());
+}
+
+
 NDN_NAMESPACE_END
 
 #endif // NDN_WIRE_H