NetAnim: Simple Write callback
authorJohn Abraham <john.abraham@gatech.edu>
Thu, 18 Aug 2011 19:36:00 -0400
changeset 7454 e1bbc514be33
parent 7453 1626895e0ebc
child 7455 cb9c052288e2
NetAnim: Simple Write callback
src/netanim/model/animation-interface.cc
src/netanim/model/animation-interface.h
--- a/src/netanim/model/animation-interface.cc	Thu Aug 18 14:14:52 2011 -0700
+++ b/src/netanim/model/animation-interface.cc	Thu Aug 18 19:36:00 2011 -0400
@@ -55,21 +55,21 @@
 AnimationInterface::AnimationInterface ()
   : m_fHandle (STDOUT_FILENO), m_xml (false), mobilitypollinterval (Seconds(0.25)),
     usingSockets (false), mport (0), outputfilename (""),
-    OutputFileSet (false), ServerPortSet (false), gAnimUid (0)
+    OutputFileSet (false), ServerPortSet (false), gAnimUid (0), m_writeCallback (0)
 {
 }
 
 AnimationInterface::AnimationInterface (const std::string fn, bool usingXML)
   : m_fHandle (STDOUT_FILENO), m_xml (usingXML), mobilitypollinterval (Seconds(0.25)), 
     usingSockets (false), mport (0), outputfilename (fn),
-    OutputFileSet (false), ServerPortSet (false), gAnimUid (0)
+    OutputFileSet (false), ServerPortSet (false), gAnimUid (0), m_writeCallback (0)
 {
 }
 
 AnimationInterface::AnimationInterface (const uint16_t port, bool usingXML)
   : m_fHandle (STDOUT_FILENO), m_xml (usingXML), mobilitypollinterval (Seconds(0.25)), 
     usingSockets (true), mport (port), outputfilename (""),
-    OutputFileSet (false), ServerPortSet (false), gAnimUid (0)
+    OutputFileSet (false), ServerPortSet (false), gAnimUid (0), m_writeCallback (0)
 {
 }
 
@@ -87,7 +87,14 @@
 bool AnimationInterface::SetOutputFile (const std::string& fn)
 {
   if (OutputFileSet)
-    return true;
+    {
+      return true;
+    }
+  if (fn == "")
+    {
+      OutputFileSet = true;
+      return true;
+    }
   FILE* f = fopen (fn.c_str (), "w");
   if (!f)
     {
@@ -96,10 +103,21 @@
     }
   m_fHandle = fileno (f); // Set the file handle
   usingSockets = false;
+  outputfilename = fn;
   OutputFileSet = true;
   return true;
 }
 
+void AnimationInterface::SetAnimWriteCallback (AnimWriteCallback cb)
+{
+  m_writeCallback = cb;
+}
+
+void AnimationInterface::ResetAnimWriteCallback ()
+{
+  m_writeCallback = 0;
+}
+
 bool AnimationInterface::SetServerPort (uint16_t port)
 {
 #if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H)
@@ -332,6 +350,10 @@
 
 int AnimationInterface::WriteN (int h, const std::string& st)
 {
+  if (m_writeCallback)
+    {
+      m_writeCallback (st.c_str ());
+    }
   return WriteN (h, st.c_str (), st.length ());
 }
 
--- a/src/netanim/model/animation-interface.h	Thu Aug 18 14:14:52 2011 -0700
+++ b/src/netanim/model/animation-interface.h	Thu Aug 18 19:36:00 2011 -0400
@@ -148,6 +148,27 @@
    */
   void SetMobilityPollInterval (Time t);
 
+  /**
+   * \brief typedef for WriteCallBack used for listening to AnimationInterface
+   * write messages
+   * 
+   */
+  typedef void (*AnimWriteCallback) (const char * str);
+
+  /**
+   * \brief Set a callback function to listen to AnimationInterface write events
+   *
+   * \param cb Address of callback function
+   *
+   */
+  void SetAnimWriteCallback (AnimWriteCallback cb);
+
+  /**
+   * \brief Reset the write callback function
+   *
+   */
+  void ResetAnimWriteCallback ();
+
 private:
   
   int       m_fHandle;  // File handle for output (-1 if none)
@@ -218,6 +239,8 @@
   void RecalcTopoBounds (Vector v);
   std::vector < Ptr <Node> > RecalcTopoBounds ();
 
+  AnimWriteCallback m_writeCallback;
+
   // Path helper
   std::vector<std::string> GetElementsFromContext (std::string context);
   Ptr <NetDevice> GetNetDeviceFromContext (std::string context);
@@ -313,7 +336,6 @@
   uint64_t Get (void) const;
 
 private:
-
   uint64_t m_AnimUid;
 };