[ekiga/ds-opal-refactoring] Opal: Use a static create method to create Opal::Call DynamicObject.



commit 39fcd6764111dea4b83af93ed5a05d93086e289b
Author: Damien Sandras <dsandras seconix com>
Date:   Sat Apr 11 13:20:26 2015 +0200

    Opal: Use a static create method to create Opal::Call DynamicObject.
    
    This simplifies the code and makes sure we have a shared_ptr to the
    object in time.

 lib/engine/components/opal/opal-call.cpp           |   30 ++++++++-----
 lib/engine/components/opal/opal-call.h             |   44 ++++++++++++--------
 .../components/opal/process/opal-endpoint.cpp      |   22 ++--------
 lib/engine/framework/dynamic-object.h              |   42 +++++++++++--------
 lib/engine/protocol/call.h                         |    6 ---
 5 files changed, 75 insertions(+), 69 deletions(-)
---
diff --git a/lib/engine/components/opal/opal-call.cpp b/lib/engine/components/opal/opal-call.cpp
index 7dcecc7..ad6ac49 100644
--- a/lib/engine/components/opal/opal-call.cpp
+++ b/lib/engine/components/opal/opal-call.cpp
@@ -71,6 +71,14 @@ strip_special_chars (std::string& str, char* special_chars, bool start)
 }
 
 
+boost::shared_ptr<Opal::Call>
+Opal::Call::create (EndPoint& _manager,
+                    const std::string & uri)
+{
+  return boost::shared_ptr<Opal::Call> (new Opal::Call (_manager, uri));
+}
+
+
 Opal::Call::Call (Opal::EndPoint& _manager,
                  const std::string& uri)
   : OpalCall (_manager), Ekiga::Call (), remote_uri (uri),
@@ -211,9 +219,9 @@ Opal::Call::toggle_stream_pause (StreamType type)
       stream->SetPaused (!paused);
 
       if (paused)
-       Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_resumed), get_shared_ptr (), 
stream_name, type));
+       Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_resumed), this->shared_from_this (), 
stream_name, type));
       else
-       Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_paused), get_shared_ptr (), stream_name, 
type));
+       Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_paused), this->shared_from_this (), 
stream_name, type));
     }
   }
 }
@@ -462,7 +470,7 @@ Opal::Call::OnEstablished (OpalConnection & connection)
     remove_action ("reject");
 
     parse_info (connection);
-    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (established), get_shared_ptr ()));
+    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (established), this->shared_from_this ()));
   }
 
   if (PIsDescendant(&connection, OpalRTPConnection)) {
@@ -604,9 +612,9 @@ Opal::Call::OnCleared ()
     }
 
     if (IsEstablished () || is_outgoing ())
-      Ekiga::Runtime::run_in_main (boost::bind (boost::ref (cleared), get_shared_ptr (), reason));
+      Ekiga::Runtime::run_in_main (boost::bind (boost::ref (cleared), this->shared_from_this (), reason));
     else
-      Ekiga::Runtime::run_in_main (boost::bind (boost::ref (missed), get_shared_ptr ()));
+      Ekiga::Runtime::run_in_main (boost::bind (boost::ref (missed), this->shared_from_this ()));
 }
 
 
@@ -638,7 +646,7 @@ Opal::Call::OnSetUp (OpalConnection & connection)
 
   OpalCall::OnSetUp (connection);
   Ekiga::Runtime::run_in_main (boost::bind (boost::ref (setup),
-                                            get_shared_ptr ()));
+                                            this->shared_from_this ()));
 
   return true;
 }
@@ -648,7 +656,7 @@ PBoolean
 Opal::Call::OnAlerting (OpalConnection & connection)
 {
   if (!PIsDescendant(&connection, OpalPCSSConnection))
-    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (ringing), get_shared_ptr ()));
+    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (ringing), this->shared_from_this ()));
 
   return OpalCall::OnAlerting (connection);
 }
@@ -660,9 +668,9 @@ Opal::Call::OnHold (OpalConnection & /*connection*/,
                     bool on_hold)
 {
   if (on_hold)
-    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (held), get_shared_ptr ()));
+    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (held), this->shared_from_this ()));
   else
-    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (retrieved), get_shared_ptr ()));
+    Ekiga::Runtime::run_in_main (boost::bind (boost::ref (retrieved), this->shared_from_this ()));
 }
 
 
@@ -677,7 +685,7 @@ Opal::Call::OnOpenMediaStream (OpalMediaStream & stream)
   std::transform (stream_name.begin (), stream_name.end (), stream_name.begin (), (int (*) (int)) toupper);
   is_transmitting = !stream.IsSource ();
 
-  Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_opened), get_shared_ptr (), stream_name, 
type, is_transmitting));
+  Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_opened), this->shared_from_this (), 
stream_name, type, is_transmitting));
 
   if (type == Ekiga::Call::Video)
     add_action (Ekiga::ActionPtr (new Ekiga::Action ("transmit-video", _("Transmit Video"),
@@ -696,7 +704,7 @@ Opal::Call::OnClosedMediaStream (OpalMediaStream & stream)
   std::transform (stream_name.begin (), stream_name.end (), stream_name.begin (), (int (*) (int)) toupper);
   is_transmitting = !stream.IsSource ();
 
-  Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_closed), get_shared_ptr (), stream_name, 
type, is_transmitting));
+  Ekiga::Runtime::run_in_main (boost::bind (boost::ref (stream_closed), this->shared_from_this (), 
stream_name, type, is_transmitting));
 }
 
 
diff --git a/lib/engine/components/opal/opal-call.h b/lib/engine/components/opal/opal-call.h
index 48d112a..9fcd23e 100644
--- a/lib/engine/components/opal/opal-call.h
+++ b/lib/engine/components/opal/opal-call.h
@@ -61,22 +61,33 @@ namespace Opal {
       public Ekiga::Call
   {
 
-public:
-
+private:
     Call (EndPoint& _manager,
           const std::string & uri);
+
+public:
     ~Call ();
 
     /*
+     * This method will create and return an shared_ptr to the object.
+     *
+     * Classical constructors should not be used as we need to have
+     * a shared_ptr to the object as soon as possible to be able to
+     * use shared_from_this from the beginning.
+     */
+    static boost::shared_ptr<Call> create (EndPoint& _manager,
+                                           const std::string & uri);
+
+    /*
      * Call Management
      */
 
     /** Hang up the call
-    */
+     */
     void hang_up ();
 
     /** Answer an incoming call
-    */
+     */
     void answer ();
 
     /** Transfer the call.
@@ -90,7 +101,7 @@ public:
     bool transfer (std::string uri);
 
     /** Put the call on hold or retrieve it
-    */
+     */
     void toggle_hold ();
 
     /** Toggle stream transmission (if any)
@@ -161,7 +172,7 @@ public:
 public:
 
     /* Implementation of inherited methods
-    */
+     */
     bool is_outgoing () const;
 
     const RTCPStatistics & get_statistics ();
@@ -202,18 +213,18 @@ private:
     void parse_info (OpalConnection & connection);
 
     PSafePtr<OpalConnection> get_remote_connection ()
-    {
-      PSafePtr<OpalConnection> connection;
-      for (PSafePtr<OpalConnection> iterConn (connectionsActive, PSafeReference); iterConn != NULL; 
++iterConn) {
-        if (PSafePtrCast<OpalConnection, OpalPCSSConnection> (iterConn) == NULL) {
-          connection = iterConn;
-          if (!connection.SetSafetyMode(PSafeReadWrite))
-            connection.SetNULL();
-          break;
+      {
+        PSafePtr<OpalConnection> connection;
+        for (PSafePtr<OpalConnection> iterConn (connectionsActive, PSafeReference); iterConn != NULL; 
++iterConn) {
+          if (PSafePtrCast<OpalConnection, OpalPCSSConnection> (iterConn) == NULL) {
+            connection = iterConn;
+            if (!connection.SetSafetyMode(PSafeReadWrite))
+              connection.SetNULL();
+            break;
+          }
         }
+        return connection;
       }
-      return connection;
-    }
 
     /*
      *
@@ -236,7 +247,6 @@ private:
     bool outgoing;
 
 private:
-
     PTime start_time;
     RTCPStatistics statistics;
     bool auto_answer;
diff --git a/lib/engine/components/opal/process/opal-endpoint.cpp 
b/lib/engine/components/opal/process/opal-endpoint.cpp
index daf696c..2e9d564 100644
--- a/lib/engine/components/opal/process/opal-endpoint.cpp
+++ b/lib/engine/components/opal/process/opal-endpoint.cpp
@@ -127,6 +127,7 @@ Opal::EndPoint::EndPoint (Ekiga::ServiceCore& _core) : core(_core)
   SetUDPPorts (5000, 5100);
   SetTCPPorts (30000, 30100);
   SetRtpIpPorts (5000, 5100);
+  SetSignalingTimeout (1500);  // Useless to wait 10 seconds for a connection
 
   forward_on_no_answer = false;
   forward_on_busy = false;
@@ -548,26 +549,11 @@ void Opal::EndPoint::GetVideoOptions (Opal::EndPoint::VideoOptions & options) co
 OpalCall *Opal::EndPoint::CreateCall (void *uri)
 {
   boost::shared_ptr<Ekiga::CallCore> call_core = core.get<Ekiga::CallCore> ("call-core");
-  Opal::Call* _call = 0;
+  boost::shared_ptr<Opal::Call> call = Opal::Call::create (*this, uri ? (const char*) uri : std::string ());
 
-  if (uri != 0)
-    _call = new Opal::Call (*this, (const char *) uri);
-  else
-    _call = new Opal::Call (*this, "");
-
-  /* We want to hold a shared_ptr to the call object
-   * before the call object has a chance to emit any
-   * signal, otherwise shared_from_this might fail.
-   */
-  boost::shared_ptr<Opal::Call> call(_call);
-
-  /* We could pass a const reference to the shared_ptr, but then
-   * we would not be sure that the shared_ptr still exist when
-   * Opal returns the _call instance to its internal system.
-   */
   Ekiga::Runtime::run_in_main (boost::bind (&Ekiga::CallCore::add_call, call_core, call));
 
-  return _call;
+  return call.get ();
 }
 
 
@@ -584,7 +570,7 @@ Opal::EndPoint::DestroyCall (OpalCall *__call)
   Opal::Call *_call = dynamic_cast<Opal::Call *>(__call);
   boost::shared_ptr<Ekiga::CallCore> call_core = core.get<Ekiga::CallCore> ("call-core");
   if (_call)
-    Ekiga::Runtime::run_in_main (boost::bind (static_cast<void 
(Opal::EndPoint::*)(boost::shared_ptr<Ekiga::Call>)>(&Opal::EndPoint::DestroyCall), this, 
_call->get_shared_ptr ()));
+    Ekiga::Runtime::run_in_main (boost::bind (static_cast<void 
(Opal::EndPoint::*)(boost::shared_ptr<Ekiga::Call>)>(&Opal::EndPoint::DestroyCall), this, 
_call->shared_from_this ()));
 }
 
 
diff --git a/lib/engine/framework/dynamic-object.h b/lib/engine/framework/dynamic-object.h
index fdc99e8..ea60337 100644
--- a/lib/engine/framework/dynamic-object.h
+++ b/lib/engine/framework/dynamic-object.h
@@ -47,29 +47,37 @@ namespace Ekiga
 
   template<typename ObjectType>
   class DynamicObject : public virtual boost::enable_shared_from_this<ObjectType>
-  {
+    {
   public:
 
-    virtual ~DynamicObject () { }
+      /*
+       * Classical constructors should not be used as we need to have
+       * a shared_ptr to the object as soon as possible to be able to
+       * use shared_from_this from the beginning.
+       *
+       * The following method should be implemeted in the child class
+       * in order to create the object:
+       *
+       * static boost::shared_ptr<ChildObject> create ();
+       *
+       */
 
-    boost::shared_ptr<ObjectType> get_shared_ptr () { return this->shared_from_this (); };
+      /**
+       * Signals on that object
+       */
 
-    /**
-     * Signals on that object
-     */
+      /** This signal is emitted when the object has been updated.
+       */
+      boost::signals2::signal<void(boost::shared_ptr<ObjectType>)> updated;
 
-    /** This signal is emitted when the object has been updated.
-     */
-    boost::signals2::signal<void(boost::shared_ptr<ObjectType>)> updated;
 
+      /** This signal is emitted when the object has been removed.
+       */
+      boost::signals2::signal<void(boost::shared_ptr<ObjectType>)> removed;
 
-    /** This signal is emitted when the object has been removed.
-     */
-    boost::signals2::signal<void(boost::shared_ptr<ObjectType>)> removed;
-
-    /** This chain allows the object to present forms to the user
-     */
-    ChainOfResponsibility<FormRequestPtr> questions;
-  };
+      /** This chain allows the object to present forms to the user
+       */
+      ChainOfResponsibility<FormRequestPtr> questions;
+    };
 };
 #endif
diff --git a/lib/engine/protocol/call.h b/lib/engine/protocol/call.h
index 517fa46..8201c71 100644
--- a/lib/engine/protocol/call.h
+++ b/lib/engine/protocol/call.h
@@ -65,12 +65,6 @@ namespace Ekiga
 
   public:
 
-      Call ()
-        {
-        }
-
-      virtual ~Call () { };
-
       enum StreamType { Audio, Video };
 
       /*


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]