Author: dlee
Date: Fri Aug 16 09:34:44 2013
New Revision: 396839

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=396839
Log:
Addressed review feedback.

* Removed bogus comment
* Properly shut down/unsubscribe stasis apps
* Check for empty application names (instead of just NULL)

Modified:
    team/dlee/ASTERISK-21969/res/res_stasis.c
    team/dlee/ASTERISK-21969/res/stasis/app.c
    team/dlee/ASTERISK-21969/res/stasis/app.h

Modified: team/dlee/ASTERISK-21969/res/res_stasis.c
URL: 
http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-21969/res/res_stasis.c?view=diff&rev=396839&r1=396838&r2=396839
==============================================================================
--- team/dlee/ASTERISK-21969/res/res_stasis.c (original)
+++ team/dlee/ASTERISK-21969/res/res_stasis.c Fri Aug 16 09:34:44 2013
@@ -144,6 +144,30 @@
        } else {
                return 0;
        }
+}
+
+static int cleanup_cb(void *obj, void *arg, int flags)
+{
+       struct app *app = obj;
+
+       if (!app_is_finished(app)) {
+               return 0;
+       }
+
+       ast_verb(1, "Shutting down application '%s'\n", app_name(app));
+       app_shutdown(app);
+
+       return CMP_MATCH;
+
+}
+
+/*!
+ * \brief Clean up any old apps that we don't need any more.
+ */
+static void cleanup(void)
+{
+       ao2_callback(apps_registry, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK,
+               cleanup_cb, NULL);
 }
 
 struct stasis_app_control *stasis_app_control_create(struct ast_channel *chan)
@@ -445,6 +469,11 @@
                return res;
        }
 
+       /* There's an off chance that app is ready for cleanup. Go ahead
+        * and clean up, just in case
+        */
+       cleanup();
+
        return res;
 }
 
@@ -465,29 +494,6 @@
 
        app_send(app, message);
        return 0;
-}
-
-static int cleanup_cb(void *obj, void *arg, int flags)
-{
-       struct app *app = obj;
-
-       if (!app_is_finished(app)) {
-               return 0;
-       }
-
-       ast_verb(1, "Cleaning up application '%s'\n", app_name(app));
-
-       return CMP_MATCH;
-
-}
-
-/*!
- * \brief Clean up any old apps that we don't need any more.
- */
-static void cleanup(void)
-{
-       ao2_callback(apps_registry, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK,
-               cleanup_cb, NULL);
 }
 
 int stasis_app_register(const char *app_name, stasis_app_cb handler, void 
*data)

Modified: team/dlee/ASTERISK-21969/res/stasis/app.c
URL: 
http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-21969/res/stasis/app.c?view=diff&rev=396839&r1=396838&r2=396839
==============================================================================
--- team/dlee/ASTERISK-21969/res/stasis/app.c (original)
+++ team/dlee/ASTERISK-21969/res/stasis/app.c Fri Aug 16 09:34:44 2013
@@ -87,7 +87,7 @@
 {
        RAII_VAR(struct app_forwards *, forwards, NULL, ao2_cleanup);
 
-       if (!app || !id) {
+       if (!app || ast_strlen_zero(id)) {
                return NULL;
        }
 
@@ -204,9 +204,15 @@
 {
        struct app *app = obj;
 
+       ast_verb(1, "Destroying Stasis app %s\n", app->name);
+
+       ast_assert(app->router == NULL);
+       ast_assert(app->bridge_merge_sub == NULL);
+
        ao2_cleanup(app->topic);
        app->topic = NULL;
-
+       ao2_cleanup(app->forwards);
+       app->forwards = NULL;
        ao2_cleanup(app->data);
        app->data = NULL;
 }
@@ -216,6 +222,10 @@
 {
        struct app *app = data;
        RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
+
+       if (stasis_subscription_final_message(sub, message)) {
+               ao2_cleanup(app);
+       }
 
        /* By default, send any message that has a JSON representation */
        json = stasis_message_to_json(message);
@@ -441,6 +451,10 @@
        struct ast_bridge_merge_message *merge;
        RAII_VAR(struct app_forwards *, forwards, NULL, ao2_cleanup);
 
+       if (stasis_subscription_final_message(sub, message)) {
+               ao2_cleanup(app);
+       }
+
        if (stasis_message_type(message) != ast_bridge_merge_message_type()) {
                return;
        }
@@ -495,6 +509,8 @@
        if (!app->bridge_merge_sub) {
                return NULL;
        }
+       /* Subscription holds a reference */
+       ao2_ref(app, +1);
 
        app->router = stasis_message_router_create(app->topic);
        if (!app->router) {
@@ -513,6 +529,8 @@
        if (res != 0) {
                return NULL;
        }
+       /* Router holds a reference */
+       ao2_ref(app, +1);
 
        strncpy(app->name, name, size - sizeof(*app));
        app->handler = handler;
@@ -562,6 +580,18 @@
        app->data = NULL;
 }
 
+void app_shutdown(struct app *app)
+{
+       SCOPED_AO2LOCK(lock, app);
+
+       ast_assert(app_is_finished(app));
+
+       stasis_message_router_unsubscribe(app->router);
+       app->router = NULL;
+       stasis_unsubscribe(app->bridge_merge_sub);
+       app->bridge_merge_sub = NULL;
+}
+
 int app_is_active(struct app *app)
 {
        SCOPED_AO2LOCK(lock, app);

Modified: team/dlee/ASTERISK-21969/res/stasis/app.h
URL: 
http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-21969/res/stasis/app.h?view=diff&rev=396839&r1=396838&r2=396839
==============================================================================
--- team/dlee/ASTERISK-21969/res/stasis/app.h (original)
+++ team/dlee/ASTERISK-21969/res/stasis/app.h Fri Aug 16 09:34:44 2013
@@ -46,6 +46,15 @@
  * \return \c NULL on error.
  */
 struct app *app_create(const char *name, stasis_app_cb handler, void *data);
+
+/*!
+ * \brief Tears down an application.
+ *
+ * It should be finished before calling this.
+ *
+ * \param app Application to unsubscribe.
+ */
+void app_shutdown(struct app *app);
 
 /*!
  * \brief Deactivates an application.
@@ -108,9 +117,6 @@
 /*!
  * \brief Subscribes an application to a channel.
  *
- * The returned object is AO2 managed, and should be ao2_cleanup()'ed to kill
- * the subscriptions.
- *
  * \param app Application.
  * \param chan Channel to subscribe to.
  * \return 0 on success.


--
_____________________________________________________________________
-- Bandwidth and Colocation Provided by http://www.api-digital.com --

svn-commits mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/svn-commits

Reply via email to