]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Allow the event encoding strategy to be configurable; choices are string or binary
authorAndrew Thompson <andrew@hijacked.us>
Mon, 26 Jan 2009 23:31:40 +0000 (23:31 +0000)
committerAndrew Thompson <andrew@hijacked.us>
Mon, 26 Jan 2009 23:31:40 +0000 (23:31 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11495 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/mod/event_handlers/mod_erlang_event/ei_helpers.c
src/mod/event_handlers/mod_erlang_event/erlang_event.conf.xml
src/mod/event_handlers/mod_erlang_event/handle_msg.c
src/mod/event_handlers/mod_erlang_event/mod_erlang_event.c
src/mod/event_handlers/mod_erlang_event/mod_erlang_event.h

index 4f314c9733acad661384a42f14431631d874c68e..67dd41bcc483fba1ca8f4d8495346d4fa23d5281 100644 (file)
  * */
 
 #define put8(s,n) do { \
-         (s)[0] = (char)((n) & 0xff); \
-         (s) += 1; \
+       (s)[0] = (char)((n) & 0xff); \
+       (s) += 1; \
 } while (0)
 
 #define put32be(s,n) do {  \
-         (s)[0] = ((n) >>  24) & 0xff; \
-         (s)[1] = ((n) >>  16) & 0xff; \
-         (s)[2] = ((n) >>  8) & 0xff;  \
-         (s)[3] = (n) & 0xff; \
-         (s) += 4; \
+       (s)[0] = ((n) >>  24) & 0xff; \
+       (s)[1] = ((n) >>  16) & 0xff; \
+       (s)[2] = ((n) >>  8) & 0xff;  \
+       (s)[3] = (n) & 0xff; \
+       (s) += 4; \
 } while (0)
 
+
 void ei_link(listener_t *listener, erlang_pid *from, erlang_pid *to) {
        char msgbuf[2048];
        char *s;
@@ -92,21 +93,21 @@ void ei_encode_switch_event_headers(ei_x_buff *ebuf, switch_event_t *event)
        ei_x_encode_list_header(ebuf, i+1);
 
        if (uuid) {
-               ei_x_encode_string(ebuf, switch_event_get_header(event, "unique-id"));
+               _ei_x_encode_string(ebuf, switch_event_get_header(event, "unique-id"));
        } else {
                ei_x_encode_atom(ebuf, "undefined");
        }
 
        for (hp = event->headers; hp; hp = hp->next) {
                ei_x_encode_tuple_header(ebuf, 2);
-               ei_x_encode_string(ebuf, hp->name);
-               ei_x_encode_string(ebuf, hp->value);
+               _ei_x_encode_string(ebuf, hp->name);
+               _ei_x_encode_string(ebuf, hp->value);
        }
 
        if (event->body) {
                ei_x_encode_tuple_header(ebuf, 2);
-               ei_x_encode_string(ebuf, "body");
-               ei_x_encode_string(ebuf, event->body);
+               _ei_x_encode_string(ebuf, "body");
+               _ei_x_encode_string(ebuf, event->body);
        }
 
        ei_x_encode_empty_list(ebuf);
index 66d30f2853683f48fe4e44b6386898866cb89c76..d71084ed3fbb184f5625c7df6c799fb2acd46880 100644 (file)
@@ -3,7 +3,10 @@
     <param name="listen-ip" value="127.0.0.1"/>
     <param name="listen-port" value="8031"/>
     <param name="cookie" value="ClueCon"/>
-    <param name="shortname" value="true"/>
-    <!--<param name="apply-inbound-acl" value="lan"/>-->
+               <param name="shortname" value="true"/>
+               <!-- in additon to cookie, optionally restrict by ACL -->
+               <!--<param name="apply-inbound-acl" value="lan"/>-->
+               <!-- alternative is "binary" -->
+               <!--<param name="encoding" value="string"/>--> 
   </settings>
 </configuration>
index 41034b1a59135048ef6c89078e7c1a69b0846ddf..f5c1cf3b07a844386e050ec8848f278aef2a285e 100644 (file)
@@ -101,8 +101,8 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
                        else
                                ei_x_encode_atom(&ebuf, "bgerror");
 
-                       ei_x_encode_string(&ebuf, acs->uuid_str);
-                       ei_x_encode_string(&ebuf, reply);
+                       _ei_x_encode_string(&ebuf, acs->uuid_str);
+                       _ei_x_encode_string(&ebuf, reply);
 
                        switch_mutex_lock(acs->listener->sock_mutex);
                        ei_send(acs->listener->sockfd, &acs->pid, ebuf.buff, ebuf.index);
@@ -129,7 +129,7 @@ static void *SWITCH_THREAD_FUNC api_exec(switch_thread_t *thread, void *obj)
                        ei_x_encode_atom(&rbuf, "error");
                }
                
-               ei_x_encode_string(&rbuf, reply);
+               _ei_x_encode_string(&rbuf, reply);
 
 
                switch_mutex_lock(acs->listener->sock_mutex);
@@ -360,7 +360,7 @@ static switch_status_t handle_msg_bgapi(listener_t *listener, erlang_msg *msg, i
                
                ei_x_encode_tuple_header(rbuf, 2);
                ei_x_encode_atom(rbuf, "ok");
-               ei_x_encode_string(rbuf, acs->uuid_str);
+               _ei_x_encode_string(rbuf, acs->uuid_str);
        }
        return SWITCH_STATUS_SUCCESS;
 }
index db1852fcbf6cbbcde7b4a49315dbd26b518075a8..1bd5b4ee6bd16af9d9ce9a4d88ee9acb9d65c1d2 100644 (file)
@@ -380,10 +380,10 @@ static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, c
        ei_x_encode_tuple_header(&buf, 7);
        ei_x_encode_atom(&buf, "fetch");
        ei_x_encode_atom(&buf, sectionstr);
-       ei_x_encode_string(&buf, tag_name ? tag_name : "undefined");
-       ei_x_encode_string(&buf, key_name ? key_name : "undefined");
-       ei_x_encode_string(&buf, key_value ? key_value : "undefined");
-       ei_x_encode_string(&buf, uuid_str);
+       _ei_x_encode_string(&buf, tag_name ? tag_name : "undefined");
+       _ei_x_encode_string(&buf, key_name ? key_name : "undefined");
+       _ei_x_encode_string(&buf, key_value ? key_value : "undefined");
+       _ei_x_encode_string(&buf, uuid_str);
        ei_encode_switch_event_headers(&buf, params);
 
        /*switch_core_hash_insert(ptr->reply_hash, uuid_str, );*/
@@ -840,6 +840,7 @@ static int config(void)
        memset(&prefs, 0, sizeof(prefs));
 
        prefs.shortname = SWITCH_TRUE;
+       prefs.encoding = ERLANG_STRING;
 
        if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf);
@@ -859,6 +860,14 @@ static int config(void)
                                        set_pref_nodename(val);
                                } else if (!strcmp(var, "shortname")) {
                                                prefs.shortname = switch_true(val);
+                               } else if (!strcmp(var, "encoding")) {
+                                       if (!strcasecmp(val, "string")) {
+                                               prefs.encoding = ERLANG_STRING;
+                                       } else if (!strcasecmp(val, "binary")) {
+                                               prefs.encoding = ERLANG_BINARY;
+                                       } else {
+                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid encoding strategy '%s' specified\n", val);
+                                       }
                                } else if (!strcasecmp(var, "apply-inbound-acl")) {
                                        if (prefs.acl_count < MAX_ACL) {
                                                prefs.acl[prefs.acl_count++] = strdup(val);
index 4dbaf1ecf1302543c974783bca4cd2105276fc22..4cf99f943aa7b8f4d26ab4e7bd379aff6e0e8d85 100644 (file)
@@ -44,6 +44,11 @@ typedef enum {
        ERLANG_REG_PROCESS
 } process_type;
 
+typedef enum {
+       ERLANG_STRING = 0,
+       ERLANG_BINARY
+} erlang_encoding_t;
+
 struct erlang_process {
        process_type type;
        char *reg_name;
@@ -163,6 +168,7 @@ struct prefs_struct {
        char *acl[MAX_ACL];
        uint32_t acl_count;
        uint32_t id;
+       erlang_encoding_t encoding;
 };
 typedef struct prefs_struct prefs_t;
 
@@ -197,6 +203,16 @@ int ei_compare_pids(erlang_pid *pid1, erlang_pid *pid2);
 switch_status_t initialise_ei(struct ei_cnode_s *ec);
 #define ei_encode_switch_event(_b, _e) ei_encode_switch_event_tag(_b, _e, "event")
 
+/* crazy macro for toggling encoding type */
+#define _ei_x_encode_string(buf, string) switch (prefs.encoding) { \
+       case ERLANG_BINARY: \
+               ei_x_encode_binary(buf, string, strlen(string)); \
+               break; \
+       default: \
+               ei_x_encode_string(buf, string); \
+               break; \
+}
+
 /* mod_erlang_event.c */
 session_elem_t* attach_call_to_registered_process(listener_t* listener, char* reg_name, switch_core_session_t *session);
 session_elem_t* attach_call_to_spawned_process(listener_t* listener, char *module, char *function, switch_core_session_t *session);