[asterisk-commits] kmoore: branch kmoore/stasis-channel_events-take2 r384915 - /team/kmoore/stas...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Apr 8 09:54:55 CDT 2013


Author: kmoore
Date: Mon Apr  8 09:54:51 2013
New Revision: 384915

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=384915
Log:
Reduce code duplication

Modified:
    team/kmoore/stasis-channel_events-take2/res/res_stasis_websocket.c

Modified: team/kmoore/stasis-channel_events-take2/res/res_stasis_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-channel_events-take2/res/res_stasis_websocket.c?view=diff&rev=384915&r1=384914&r2=384915
==============================================================================
--- team/kmoore/stasis-channel_events-take2/res/res_stasis_websocket.c (original)
+++ team/kmoore/stasis-channel_events-take2/res/res_stasis_websocket.c Mon Apr  8 09:54:51 2013
@@ -52,10 +52,6 @@
  */
 #define APPS_NUM_BUCKETS 7
 
-struct websocket_app {
-	char *name;
-};
-
 /*!
  * \internal
  * \brief Helper to write a JSON object to a WebSocket.
@@ -78,35 +74,6 @@
 				   strlen(str));
 }
 
-/*! Hash function for websocket_app */
-static int hash_app(const void *obj, const int flags)
-{
-	const struct websocket_app *app = obj;
-	const char *name = flags & OBJ_KEY ? obj : app->name;
-
-	return ast_str_hash(name);
-}
-
-/*! Comparison function for websocket_app */
-static int compare_app(void *lhs, void *rhs, int flags)
-{
-	const struct websocket_app *lhs_app = lhs;
-	const struct websocket_app *rhs_app = rhs;
-	const char *rhs_name = flags & OBJ_KEY ? rhs : rhs_app->name;
-
-	if (strcmp(lhs_app->name, rhs_name) == 0) {
-		return CMP_MATCH;
-	} else {
-		return 0;
-	}
-}
-
-static void app_dtor(void *obj)
-{
-	struct websocket_app *app = obj;
-	ast_free(app->name);
-}
-
 struct stasis_ws_session_info {
 	struct ast_websocket *ws_session;
 	struct ao2_container *websocket_apps;
@@ -130,7 +97,7 @@
 
 	session->ws_session = ws_session;
 	session->websocket_apps =
-		ao2_container_alloc(APPS_NUM_BUCKETS, hash_app, compare_app);
+		ast_str_container_alloc(APPS_NUM_BUCKETS);
 
 	if (!session->websocket_apps) {
 		return NULL;
@@ -152,12 +119,12 @@
 static void session_shutdown(struct stasis_ws_session_info *session)
 {
         struct ao2_iterator i;
-	struct websocket_app *app;
+	char *app;
 	SCOPED_AO2LOCK(lock, session);
 
 	i = ao2_iterator_init(session->websocket_apps, 0);
 	while ((app = ao2_iterator_next(&i))) {
-		stasis_app_unregister(app->name);
+		stasis_app_unregister(app);
 		ao2_cleanup(app);
 	}
 	ao2_iterator_destroy(&i);
@@ -210,14 +177,16 @@
 		return -1;
 	}
 	while ((app_name = strsep(&apps, ","))) {
-		RAII_VAR(struct websocket_app *, app, NULL, ao2_cleanup);
-
-		app = ao2_alloc(sizeof(*app), app_dtor);
+		RAII_VAR(char *, app, NULL, ao2_cleanup);
+
+		app = ao2_alloc(strlen(app_name) + 1, NULL);
 		if (!app) {
 			websocket_write_json(session->ws_session, oom_json);
 			return -1;
 		}
-		app->name = ast_strdup(app_name);
+
+		/* safe strcpy */
+		strcpy(app, app_name);
 		ao2_link(session->websocket_apps, app);
 
 		stasis_app_register(app_name, app_handler, session);




More information about the asterisk-commits mailing list