<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][16747] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=16747">16747</a></dd>
<dt>Author</dt> <dd>sathieu</dd>
<dt>Date</dt> <dd>2010-02-24 05:57:45 -0600 (Wed, 24 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Skinny: Milestone 0 : no-op module (listeners and base skinny parsing)
Based on mod_event_socket.
- create listeners
- parse sccp header (length, reserved, type)
- don't listen to reloadxml event, as we don't reload listeners
- listen to heartbeat event to expire listeners</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc">freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c (16746 => 16747)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-02-24 11:57:37 UTC (rev 16746)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-02-24 11:57:45 UTC (rev 16747)
</span><span class="lines">@@ -59,10 +59,10 @@
</span><span class="cx"> } GFLAGS;
</span><span class="cx">
</span><span class="cx"> static struct {
</span><del>- /* prefs */
</del><ins>+        /* prefs */
</ins><span class="cx">         int debug;
</span><span class="cx">         char *ip;
</span><del>-        int port;
</del><ins>+        unsigned int port;
</ins><span class="cx">         char *dialplan;
</span><span class="cx">         char *codec_string;
</span><span class="cx">         char *codec_order[SWITCH_MAX_CODECS];
</span><span class="lines">@@ -72,11 +72,13 @@
</span><span class="cx">         int codec_rates_last;
</span><span class="cx">         int keep_alive;
</span><span class="cx">         char *date_format;
</span><del>- /* data */
-        switch_event_node_t *node;
</del><ins>+        /* data */
+        switch_event_node_t *heartbeat_node;
</ins><span class="cx">         unsigned int flags;
</span><span class="cx">         int calls;
</span><span class="cx">         switch_mutex_t *mutex;
</span><ins>+        switch_mutex_t *listener_mutex;        
+        int listener_threads;
</ins><span class="cx"> } globals;
</span><span class="cx">
</span><span class="cx"> struct private_object {
</span><span class="lines">@@ -101,6 +103,63 @@
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_rates_string, globals.codec_rates_string);
</span><span class="cx"> SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_date_format, globals.date_format);
</span><span class="cx">
</span><ins>+/*****************************************************************************/
+/* SKINNY TYPES */
+/*****************************************************************************/
+#define SKINNY_MESSAGE_FIELD_SIZE 4 /* 4-bytes field */
+#define SKINNY_MESSAGE_HEADERSIZE 12 /* three 4-bytes fields */
+#define SKINNY_MESSAGE_MAXSIZE 1000
+
+union skinny_data {
+        void *raw;
+};
+
+struct skinny_message {
+        int length;
+        int reserved;
+        int type;
+        union skinny_data data;
+};
+typedef struct skinny_message skinny_message_t;
+
+/*****************************************************************************/
+/* LISTENERS TYPES */
+/*****************************************************************************/
+
+typedef enum {
+        LFLAG_RUNNING = (1 << 0),
+} event_flag_t;
+
+struct listener {
+        switch_socket_t *sock;
+        switch_memory_pool_t *pool;
+        switch_core_session_t *session;
+        switch_thread_rwlock_t *rwlock;
+        switch_sockaddr_t *sa;
+        char remote_ip[50];
+        switch_mutex_t *flag_mutex;
+        uint32_t flags;
+        switch_port_t remote_port;
+        uint32_t id;
+        time_t expire_time;
+        struct listener *next;
+};
+
+typedef struct listener listener_t;
+
+typedef switch_status_t (*skinny_listener_callback_func_t) (listener_t *listener);
+
+static struct {
+        switch_socket_t *sock;
+        switch_mutex_t *sock_mutex;
+        listener_t *listeners;
+        uint8_t ready;
+} listen_list;
+
+/*****************************************************************************/
+/* CHANNEL FUNCTIONS */
+/*****************************************************************************/
+
</ins><span class="cx"> static switch_status_t channel_on_init(switch_core_session_t *session);
</span><span class="cx"> static switch_status_t channel_on_hangup(switch_core_session_t *session);
</span><span class="cx"> static switch_status_t channel_on_destroy(switch_core_session_t *session);
</span><span class="lines">@@ -259,7 +318,7 @@
</span><span class="cx">                 switch_clear_flag_locked(tech_pvt, TFLAG_IO);
</span><span class="cx">                 switch_clear_flag_locked(tech_pvt, TFLAG_VOICE);
</span><span class="cx">                 switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
</span><del>-                //switch_thread_cond_signal(tech_pvt->cond);
</del><ins>+                //switch_thread_cond_sigpnal(tech_pvt->cond);
</ins><span class="cx">                 break;
</span><span class="cx">         case SWITCH_SIG_BREAK:
</span><span class="cx">                 switch_set_flag_locked(tech_pvt, TFLAG_BREAK);
</span><span class="lines">@@ -505,6 +564,429 @@
</span><span class="cx">         /*.receive_event */ channel_receive_event
</span><span class="cx"> };
</span><span class="cx">
</span><ins>+/*****************************************************************************/
+/* SKINNY FUNCTIONS */
+/*****************************************************************************/
+
+static switch_status_t skinny_read_packet(listener_t *listener, skinny_message_t **req, uint32_t timeout)
+{
+        skinny_message_t *request;
+ uint32_t elapsed = 0;
+        time_t start = 0;
+        switch_size_t mlen, bytes = 0;
+        char mbuf[SKINNY_MESSAGE_MAXSIZE] = "";
+        char *ptr;
+        switch_status_t status = SWITCH_STATUS_SUCCESS;
+
+        request = switch_core_alloc(module_pool, SKINNY_MESSAGE_MAXSIZE);
+
+        if (!request) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to allocate memory.\n");
+                return SWITCH_STATUS_MEMERR;
+        }
+        
+        if (!running) {
+                return SWITCH_STATUS_FALSE;
+        }
+
+        start = switch_epoch_time_now(NULL);
+        ptr = mbuf;
+
+        while (listener->sock && running) {
+                uint8_t do_sleep = 1;
+                if(bytes < SKINNY_MESSAGE_FIELD_SIZE) {
+                        /* We have nothing yet, get length header field */
+                        mlen = SKINNY_MESSAGE_FIELD_SIZE - bytes;
+                } else {
+                        /* We now know the message size */
+                        mlen = request->length + 2*SKINNY_MESSAGE_FIELD_SIZE - bytes;
+                }
+
+                status = switch_socket_recv(listener->sock, ptr, &mlen);
+                
+                if (!running || (!SWITCH_STATUS_IS_BREAK(status) && status != SWITCH_STATUS_SUCCESS)) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket break.\n");
+                        return SWITCH_STATUS_FALSE;
+                }
+                
+                if(mlen) {
+                        bytes += mlen;
+                        
+                        if(bytes >= SKINNY_MESSAGE_FIELD_SIZE) {
+                                do_sleep = 0;
+                                ptr += mlen;
+                                memcpy(request, mbuf, bytes);
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
+                                        "Got request: length=%d,reserved=%d,type=%d\n",
+                                        request->length,request->reserved,request->type);
+                                if(request->length < SKINNY_MESSAGE_FIELD_SIZE) {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+                                                "Skinny client sent invalid data. Length should be greated than 4 but got %d.\n",
+                                                request->length);
+                                        return SWITCH_STATUS_FALSE;
+                                }
+                                if(bytes >= request->length + 2*SKINNY_MESSAGE_FIELD_SIZE) {
+                                        /* Message body */
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
+                                                "Got complete request: length=%d,reserved=%d,type=%d,data=%d\n",
+                                                request->length,request->reserved,request->type,request->data.as_char);
+                                        *req = request;
+                                        return SWITCH_STATUS_SUCCESS;
+                                }
+                        }
+                }
+                if (timeout) {
+                        elapsed = (uint32_t) (switch_epoch_time_now(NULL) - start);
+                        if (elapsed >= timeout) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket timed out.\n");
+                                switch_clear_flag_locked(listener, LFLAG_RUNNING);
+                                return SWITCH_STATUS_FALSE;
+                        }
+                }
+        }
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t skinny_parse_request(listener_t *listener, skinny_message_t *request, skinny_message_t **rep)
+{
+ skinny_message_t *reply;
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Parsing request.\n");
+        reply = NULL;
+        switch(request->type) {
+                /* TODO */
+                default:
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
+                                "Unknown request type: %d.\n", request->type);
+        }
+        *rep = reply;
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t skinny_send_reply(listener_t *listener, skinny_message_t *reply)
+{
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Sending reply.\n");
+        //TODO switch_socket_send(listener->sock, buf, &len);
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t skinny_free_message(skinny_message_t *message)
+{
+        if(message) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Freeing message\n");
+                /* TODO */
+        }
+        return SWITCH_STATUS_SUCCESS;
+}
+
+/*****************************************************************************/
+/* LISTENER FUNCTIONS */
+/*****************************************************************************/
+
+static void add_listener(listener_t *listener)
+{
+        switch_mutex_lock(globals.listener_mutex);
+        listener->next = listen_list.listeners;
+        listen_list.listeners = listener;
+        switch_mutex_unlock(globals.listener_mutex);
+}
+
+static void remove_listener(listener_t *listener)
+{
+        listener_t *l, *last = NULL;
+
+        switch_mutex_lock(globals.listener_mutex);
+        for (l = listen_list.listeners; l; l = l->next) {
+                if (l == listener) {
+                        if (last) {
+                                last->next = l->next;
+                        } else {
+                                listen_list.listeners = l->next;
+                        }
+                }
+                last = l;
+        }
+        switch_mutex_unlock(globals.listener_mutex);
+}
+
+
+static void walk_listeners(skinny_listener_callback_func_t callback)
+{
+        listener_t *l;
+
+        switch_mutex_lock(globals.listener_mutex);
+        for (l = listen_list.listeners; l; l = l->next) {
+                callback(l);
+        }
+        switch_mutex_unlock(globals.listener_mutex);
+
+}
+
+static void flush_listener(listener_t *listener, switch_bool_t flush_log, switch_bool_t flush_events)
+{
+
+        /* TODO */
+}
+
+static switch_status_t expire_listener(listener_t *listener)
+{
+        if (!listener->expire_time) {
+                listener->expire_time = switch_epoch_time_now(NULL);
+        }
+
+        if (switch_thread_rwlock_trywrlock(listener->rwlock) != SWITCH_STATUS_SUCCESS) {
+                return SWITCH_STATUS_FALSE;
+        }
+
+        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(listener->session), SWITCH_LOG_CRIT, "Stateful Listener %u has expired\n", listener->id);
+
+        flush_listener(listener, SWITCH_TRUE, SWITCH_TRUE);
+
+        switch_thread_rwlock_unlock(listener->rwlock);
+        switch_core_destroy_memory_pool(&listener->pool);
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static void close_socket(switch_socket_t **sock)
+{
+        switch_mutex_lock(listen_list.sock_mutex);
+        if (*sock) {
+                switch_socket_shutdown(*sock, SWITCH_SHUTDOWN_READWRITE);
+                switch_socket_close(*sock);
+                *sock = NULL;
+        }
+        switch_mutex_unlock(listen_list.sock_mutex);
+}
+
+static switch_status_t kill_listener(listener_t *listener)
+{
+        switch_clear_flag(listener, LFLAG_RUNNING);
+        close_socket(&listener->sock);
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj)
+{
+        listener_t *listener = (listener_t *) obj;
+        switch_status_t status;
+        switch_core_session_t *session = NULL;
+        switch_channel_t *channel = NULL;
+        skinny_message_t *request = NULL;
+        skinny_message_t *reply = NULL;
+
+        switch_mutex_lock(globals.listener_mutex);
+        globals.listener_threads++;
+        switch_mutex_unlock(globals.listener_mutex);
+        
+        switch_assert(listener != NULL);
+        
+        if ((session = listener->session)) {
+                if (switch_core_session_read_lock(session) != SWITCH_STATUS_SUCCESS) {
+                        goto done;
+                }
+        }
+
+        switch_socket_opt_set(listener->sock, SWITCH_SO_TCP_NODELAY, TRUE);
+        switch_socket_opt_set(listener->sock, SWITCH_SO_NONBLOCK, TRUE);
+
+        if (globals.debug > 0) {
+                if (zstr(listener->remote_ip)) {
+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Connection Open\n");
+                } else {
+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Connection Open from %s:%d\n", listener->remote_ip, listener->remote_port);
+                }
+        }
+
+        switch_socket_opt_set(listener->sock, SWITCH_SO_NONBLOCK, TRUE);
+        switch_set_flag_locked(listener, LFLAG_RUNNING);
+        add_listener(listener);
+
+
+        while (running && switch_test_flag(listener, LFLAG_RUNNING) && listen_list.ready) {
+                status = skinny_read_packet(listener, &request, 30);
+
+                if (status != SWITCH_STATUS_SUCCESS) {
+                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Socket Error!\n");
+                        switch_clear_flag_locked(listener, LFLAG_RUNNING);
+                        break;
+                }
+
+                if (!request) {
+                        continue;
+                }
+
+                if (skinny_parse_request(listener, request, &reply) != SWITCH_STATUS_SUCCESS) {
+                        switch_clear_flag_locked(listener, LFLAG_RUNNING);
+                        break;
+                }
+
+                skinny_free_message(request);
+
+                if (reply != NULL) {
+                        skinny_send_reply(listener, reply);
+                }
+                skinny_free_message(reply);
+        }
+
+ done:
+        
+        skinny_free_message(request);
+        skinny_free_message(reply);
+        
+        remove_listener(listener);
+
+        if (globals.debug > 0) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Session complete, waiting for children\n");
+        }
+
+        switch_thread_rwlock_wrlock(listener->rwlock);
+        flush_listener(listener, SWITCH_TRUE, SWITCH_TRUE);
+
+        if (listener->session) {
+                channel = switch_core_session_get_channel(listener->session);
+        }
+        
+        if (listener->sock) {
+                close_socket(&listener->sock);
+        }
+
+        switch_thread_rwlock_unlock(listener->rwlock);
+
+        if (globals.debug > 0) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Connection Closed\n");
+        }
+
+        if (listener->session) {
+                switch_channel_clear_flag(switch_core_session_get_channel(listener->session), CF_CONTROLLED);
+                //TODO switch_clear_flag_locked(listener, LFLAG_SESSION);
+                switch_core_session_rwunlock(listener->session);
+        } else if (listener->pool) {
+                switch_memory_pool_t *pool = listener->pool;
+                switch_core_destroy_memory_pool(&pool);
+        }
+
+        switch_mutex_lock(globals.listener_mutex);
+        globals.listener_threads--;
+        switch_mutex_unlock(globals.listener_mutex);
+
+        return NULL;
+}
+
+/* Create a thread for the socket and launch it */
+static void launch_listener_thread(listener_t *listener)
+{
+        switch_thread_t *thread;
+        switch_threadattr_t *thd_attr = NULL;
+
+        switch_threadattr_create(&thd_attr, listener->pool);
+        switch_threadattr_detach_set(thd_attr, 1);
+        switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
+        switch_thread_create(&thread, thd_attr, listener_run, listener, listener->pool);
+}
+
+int skinny_socket_create_and_bind()
+{
+        switch_status_t rv;
+        switch_sockaddr_t *sa;
+        switch_socket_t *inbound_socket = NULL;
+        listener_t *listener;
+        switch_memory_pool_t *pool = NULL, *listener_pool = NULL;
+        uint32_t errs = 0;
+
+        if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n");
+                return SWITCH_STATUS_TERM;
+        }
+
+        while(running) {
+                rv = switch_sockaddr_info_get(&sa, globals.ip, SWITCH_INET, globals.port, 0, pool);
+                if (rv)
+                        goto fail;
+                rv = switch_socket_create(&listen_list.sock, switch_sockaddr_get_family(sa), SOCK_STREAM, SWITCH_PROTO_TCP, pool);
+                if (rv)
+                        goto sock_fail;
+                rv = switch_socket_opt_set(listen_list.sock, SWITCH_SO_REUSEADDR, 1);
+                if (rv)
+                        goto sock_fail;
+                rv = switch_socket_bind(listen_list.sock, sa);
+                if (rv)
+                        goto sock_fail;
+                rv = switch_socket_listen(listen_list.sock, 5);
+                if (rv)
+                        goto sock_fail;
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Socket up listening on %s:%u\n", globals.ip, globals.port);
+
+                break;
+         sock_fail:
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error! Could not listen on %s:%u\n", globals.ip, globals.port);
+                switch_yield(100000);
+        }
+
+        listen_list.ready = 1;
+
+        while(running) {
+
+                if (switch_core_new_memory_pool(&listener_pool) != SWITCH_STATUS_SUCCESS) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n");
+                        goto fail;
+                }
+
+                if ((rv = switch_socket_accept(&inbound_socket, listen_list.sock, listener_pool))) {
+                        if (!running) {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Shutting Down\n");
+                                goto end;
+                        } else {
+                                /* I wish we could use strerror_r here but its not defined everywhere =/ */
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error [%s]\n", strerror(errno));
+                                if (++errs > 100) {
+                                        goto end;
+                                }
+                        }
+                } else {
+                        errs = 0;
+                }
+
+                
+                if (!(listener = switch_core_alloc(listener_pool, sizeof(*listener)))) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error\n");
+                        break;
+                }
+
+                switch_thread_rwlock_create(&listener->rwlock, listener_pool);
+
+                listener->sock = inbound_socket;
+                listener->pool = listener_pool;
+                listener_pool = NULL;
+
+                switch_mutex_init(&listener->flag_mutex, SWITCH_MUTEX_NESTED, listener->pool);
+
+                switch_socket_addr_get(&listener->sa, SWITCH_TRUE, listener->sock);
+                switch_get_addr(listener->remote_ip, sizeof(listener->remote_ip), listener->sa);
+                listener->remote_port = switch_sockaddr_get_port(listener->sa);
+                launch_listener_thread(listener);
+
+        }
+
+ end:
+
+        close_socket(&listen_list.sock);
+        
+        if (pool) {
+                switch_core_destroy_memory_pool(&pool);
+        }
+
+        if (listener_pool) {
+                switch_core_destroy_memory_pool(&listener_pool);
+        }
+
+
+ fail:
+        return SWITCH_STATUS_TERM;
+}
+
+/*****************************************************************************/
+/* MODULE FUNCTIONS */
+/*****************************************************************************/
</ins><span class="cx"> static switch_status_t load_skinny_config(void)
</span><span class="cx"> {
</span><span class="cx">         char *cf = "skinny.conf";
</span><span class="lines">@@ -562,10 +1044,8 @@
</span><span class="cx">
</span><span class="cx"> static void event_handler(switch_event_t *event)
</span><span class="cx"> {
</span><del>-        if (event->event_id == SWITCH_EVENT_RELOADXML) {
-                if (load_skinny_config() != SWITCH_STATUS_SUCCESS) {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to reload config file\n");
-                }
</del><ins>+        if (event->event_id == SWITCH_EVENT_HEARTBEAT) {
+                walk_listeners(expire_listener);
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -578,6 +1058,16 @@
</span><span class="cx">
</span><span class="cx">         load_skinny_config();
</span><span class="cx">
</span><ins>+        switch_mutex_init(&globals.listener_mutex, SWITCH_MUTEX_NESTED, pool);
+
+        memset(&listen_list, 0, sizeof(listen_list));
+        switch_mutex_init(&listen_list.sock_mutex, SWITCH_MUTEX_NESTED, pool);
+
+        if ((switch_event_bind_removable(modname, SWITCH_EVENT_HEARTBEAT, NULL, event_handler, NULL, &globals.heartbeat_node) != SWITCH_STATUS_SUCCESS)) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind our heartbeat handler!\n");
+                /* Not such severe to prevent loading */
+        }
+
</ins><span class="cx">         /* connect my internal structure to the blank pointer passed to me */
</span><span class="cx">         *module_interface = switch_loadable_module_create_module_interface(pool, modname);
</span><span class="cx">         skinny_endpoint_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_ENDPOINT_INTERFACE);
</span><span class="lines">@@ -585,37 +1075,36 @@
</span><span class="cx">         skinny_endpoint_interface->io_routines = &skinny_io_routines;
</span><span class="cx">         skinny_endpoint_interface->state_handler = &skinny_state_handlers;
</span><span class="cx">
</span><del>-        if ((switch_event_bind_removable(modname, SWITCH_EVENT_RELOADXML, NULL, event_handler, NULL, &globals.node) != SWITCH_STATUS_SUCCESS)) {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind our reloadxml handler!\n");
-                /* Not such severe to prevent loading */
-        }
</del><span class="cx">
</span><del>-
</del><span class="cx">         /* indicate that the module should continue to be loaded */
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> SWITCH_MODULE_RUNTIME_FUNCTION(mod_skinny_runtime)
</span><span class="cx"> {
</span><del>- /* TODO: create listeners */
-        return SWITCH_STATUS_TERM;
</del><ins>+        return skinny_socket_create_and_bind();
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_skinny_shutdown)
</span><span class="cx"> {
</span><del>-        switch_event_unbind(&globals.node);
</del><ins>+        int sanity = 0;
</ins><span class="cx">
</span><del>-        int x = 0;
</del><ins>+        switch_event_unbind(&globals.heartbeat_node);
</ins><span class="cx">
</span><del>-        running = -1;
</del><ins>+        running = 0;
</ins><span class="cx">
</span><del>-        while (running) {
-                if (x++ > 100) {
</del><ins>+        walk_listeners(kill_listener);
+
+        close_socket(&listen_list.sock);
+
+        while (globals.listener_threads) {
+                switch_yield(100000);
+                walk_listeners(kill_listener);
+                if (++sanity >= 200) {
</ins><span class="cx">                         break;
</span><span class="cx">                 }
</span><del>-                switch_yield(20000);
</del><span class="cx">         }
</span><del>-        
</del><ins>+
</ins><span class="cx">         /* Free dynamically allocated strings */
</span><span class="cx">         switch_safe_free(globals.ip);
</span><span class="cx">         switch_safe_free(globals.dialplan);
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>