[svn-commits] dlee: branch dlee/stasis-app r384286 - /team/dlee/stasis-app/res/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Mar 28 12:36:20 CDT 2013


Author: dlee
Date: Thu Mar 28 12:36:16 2013
New Revision: 384286

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=384286
Log:
Margins

Modified:
    team/dlee/stasis-app/res/res_stasis_websocket.c

Modified: team/dlee/stasis-app/res/res_stasis_websocket.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/stasis-app/res/res_stasis_websocket.c?view=diff&rev=384286&r1=384285&r2=384286
==============================================================================
--- team/dlee/stasis-app/res/res_stasis_websocket.c (original)
+++ team/dlee/stasis-app/res/res_stasis_websocket.c Thu Mar 28 12:36:16 2013
@@ -44,14 +44,18 @@
 /*! WebSocket protocol for Stasis */
 static const char * const ws_protocol = "stasis";
 
-/*! Number of buckets for the Stasis application hash table. Remember to keep it a prime number! */
+/*! Number of buckets for the Stasis application hash table. Remember to keep it
+ *  a prime number!
+ */
 #define APPS_NUM_BUCKETS 7
 
 struct websocket_app {
 	char *name;
 };
 
-static struct ast_json *build_err(int id, const char *msg_format, ...) __attribute__((format(printf, 2, 3)));
+static struct ast_json *build_err(int id, const char *msg_format, ...)
+	__attribute__((format(printf, 2, 3)));
+
 struct ast_json *build_err(int id, const char *msg_format, ...)
 {
 	va_list args;
@@ -67,7 +71,8 @@
 	return err;
 }
 
-static struct ast_json *build_success(int id, const char *msg_format, ...) __attribute__((format(printf, 2, 3)));
+static struct ast_json *build_success(int id, const char *msg_format, ...)
+	__attribute__((format(printf, 2, 3)));
 struct ast_json *build_success(int id, const char *msg_format, ...)
 {
 	va_list args;
@@ -90,7 +95,8 @@
  * \return 0 on success.
  * \return -1 on error.
  */
-static int websocket_write_json(struct ast_websocket *session, struct ast_json *message)
+static int websocket_write_json(struct ast_websocket *session,
+				struct ast_json *message)
 {
 	RAII_VAR(char *, str, ast_json_dump_string(message), ast_free);
 
@@ -99,7 +105,8 @@
 		return -1;
 	}
 
-	return ast_websocket_write(session, AST_WEBSOCKET_OPCODE_TEXT, str, strlen(str));
+	return ast_websocket_write(session, AST_WEBSOCKET_OPCODE_TEXT, str,
+				   strlen(str));
 }
 
 /*! Hash function for websocket_app */
@@ -136,21 +143,25 @@
 	struct ao2_container *websocket_apps;
 };
 
-static void app_handler(void *data, const char *app_name, struct ast_json *message)
+static void app_handler(void *data, const char *app_name,
+			struct ast_json *message)
 {
 	struct stasis_ws_session_info *session = data;
 	int res;
 
-	res = ast_json_object_set(message, "application", ast_json_string_create(app_name));
+	res = ast_json_object_set(message, "application",
+				  ast_json_string_create(app_name));
 	ast_assert(res == 0);
 
 	websocket_write_json(session->ws_session, message);
 }
 
 
-static struct ast_json *websocket_process_req(struct stasis_ws_session_info *session, struct ast_json *req)
-{
-	const char *command = ast_json_string_get(ast_json_object_get(req, "command"));
+static struct ast_json *websocket_process_req(
+	struct stasis_ws_session_info *session, struct ast_json *req)
+{
+	const char *command =
+		ast_json_string_get(ast_json_object_get(req, "command"));
 	struct ast_json *id_json = ast_json_object_get(req, "id");
 	intmax_t id;
 
@@ -168,7 +179,8 @@
 	}
 
 	if (strcmp("ActivateApplication", command) == 0) {
-		const char *app_name = ast_json_string_get(ast_json_object_get(req, "name"));
+		const char *app_name =
+			ast_json_string_get(ast_json_object_get(req, "name"));
 		RAII_VAR(struct websocket_app *, app, NULL, ao2_cleanup);
 		if (ast_strlen_zero(app_name)) {
 			return build_err(id, "Missing application name");
@@ -176,7 +188,9 @@
 
 		app = ao2_find(session->websocket_apps, app_name, OBJ_KEY);
 		if (app) {
-			return build_err(id, "Application '%s' already registered", app_name);
+			return build_err(
+				id, "Application '%s' already registered",
+				app_name);
 		}
 
 		app = ao2_alloc(sizeof(*app), app_dtor);
@@ -191,14 +205,17 @@
 
 		return build_success(id, "Application activated");
 	} else if (strcmp("DeactivateApplication", command) == 0) {
-		const char *app_name = ast_json_string_get(ast_json_object_get(req, "name"));
+		const char *app_name =
+			ast_json_string_get(ast_json_object_get(req, "name"));
 		RAII_VAR(struct websocket_app *, app, NULL, ao2_cleanup);
 		if (ast_strlen_zero(app_name)) {
 			return build_err(id, "Missing application name");
 		}
-		app = ao2_find(session->websocket_apps, app_name, OBJ_KEY | OBJ_UNLINK);
+		app = ao2_find(session->websocket_apps, app_name,
+			       OBJ_KEY | OBJ_UNLINK);
 		if (app == NULL) {
-			return build_err(id, "Application '%s' not activated", app_name);
+			return build_err(id, "Application '%s' not activated",
+					 app_name);
 		}
 
 		stasis_app_unregister(app_name);
@@ -209,7 +226,9 @@
 	}
 }
 
-static void websocket_callback(struct ast_websocket *session, struct ast_variable *parameters, struct ast_variable *headers)
+static void websocket_callback(struct ast_websocket *session,
+			       struct ast_variable *parameters,
+			       struct ast_variable *headers)
 {
 	struct stasis_ws_session_info stasis_session = {};
         struct ao2_iterator i;
@@ -219,11 +238,13 @@
 	ast_debug(3, "Stasis web socket connection\n");
 
 	if (ast_websocket_set_nonblock(session) != 0) {
-		ast_log(LOG_ERROR, "Stasis web socket failed to set nonblock; closing\n");
+		ast_log(LOG_ERROR,
+			"Stasis web socket failed to set nonblock; closing\n");
 		goto end;
 	}
 
-	stasis_session.websocket_apps = ao2_container_alloc(APPS_NUM_BUCKETS, hash_app, compare_app);
+	stasis_session.websocket_apps =
+		ao2_container_alloc(APPS_NUM_BUCKETS, hash_app, compare_app);
 	stasis_session.ws_session = session;
 
 	while ((res = ast_wait_for_input(ast_websocket_fd(session), -1)) > 0) {
@@ -231,9 +252,12 @@
 		uint64_t payload_len;
 		enum ast_websocket_opcode opcode;
 		int fragmented;
-
-		if (ast_websocket_read(session, &payload, &payload_len, &opcode, &fragmented)) {
-			ast_log(LOG_ERROR, "Stasis WebSocket read error; closing\n");
+		int read = ast_websocket_read(session, &payload, &payload_len,
+					      &opcode, &fragmented);
+
+		if (read) {
+			ast_log(LOG_ERROR,
+				"Stasis WebSocket read error; closing\n");
 			break;
 		}
 
@@ -246,15 +270,21 @@
 
 			if (req == NULL) {
 				/* Parse error */
-				RAII_VAR(char *, payloadz, ast_strndup(payload, payload_len), ast_free);
-				resp = ast_json_pack("{ s: { s: o, s: i, s: i, s: s } }",
-						     "error",
-						     "message", ast_json_stringf("Error parsing message: %s", err.text),
-						     "line", err.line,
-						     "column", err.column,
-						     "request", payloadz ? payloadz : "(null)");
+				RAII_VAR(char *, payloadz, NULL, ast_free);
+				payloadz = ast_strndup(payload, payload_len);
+				resp = ast_json_pack(
+					"{ s: { s: o, s: i, s: i, s: s } }",
+					"error",
+					"message",ast_json_stringf(
+						"Error parsing message: %s",
+						err.text),
+					"line", err.line,
+					"column", err.column,
+					"request",
+					payloadz ? payloadz : "(null)");
 			} else {
-				resp = websocket_process_req(&stasis_session, req);
+				resp = websocket_process_req(&stasis_session,
+							     req);
 			}
 
 			if (resp) {




More information about the svn-commits mailing list