<!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][16762] </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=16762">16762</a></dd>
<dt>Author</dt> <dd>sathieu</dd>
<dt>Date</dt> <dd>2010-02-24 05:59:49 -0600 (Wed, 24 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Skinny: moving to SQL</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnymod_skinnyc">freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c</a></li>
<li><a href="#freeswitchtrunksrcmodendpointsmod_skinnytestskinnypl">freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl</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 (16761 => 16762)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-02-24 11:59:41 UTC (rev 16761)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/mod_skinny.c        2010-02-24 11:59:49 UTC (rev 16762)
</span><span class="lines">@@ -111,14 +111,16 @@
</span><span class="cx"> static char lines_sql[] =
</span><span class="cx">         "CREATE TABLE skinny_lines (\n"
</span><span class="cx">         " device_name VARCHAR(16),\n"
</span><ins>+        " line_position INTEGER,\n"
</ins><span class="cx">         " line_name VARCHAR(24),\n"
</span><span class="cx">         " line_shortname VARCHAR(40),\n"
</span><span class="cx">         " line_displayname VARCHAR(44)\n"
</span><span class="cx">         ");\n";
</span><span class="cx">
</span><del>-static char speed_sql[] =
</del><ins>+static char speeddials_sql[] =
</ins><span class="cx">         "CREATE TABLE skinny_speeddials (\n"
</span><span class="cx">         " device_name VARCHAR(16),\n"
</span><ins>+        " speed_position INTEGER,\n"
</ins><span class="cx">         " speed_number VARCHAR(24),\n"
</span><span class="cx">         " speed_displayname VARCHAR(40)\n"
</span><span class="cx">         ");\n";
</span><span class="lines">@@ -268,45 +270,6 @@
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> /* SKINNY TYPES */
</span><span class="cx"> /*****************************************************************************/
</span><del>-
-#define SKINNY_MAX_LINES 10
-struct skinny_line {
-        struct skinny_device_t *device;
-        char name[24];
-        char shortname[40];
-        char displayname[44];
-};
-typedef struct skinny_line skinny_line_t;
-
-#define SKINNY_MAX_SPEEDDIALS 20
-struct skinny_speeddial {
-        struct skinny_device_t *device;
-        char line[24];
-        char label[40];
-};
-typedef struct skinny_speeddial skinny_speeddial_t;
-
-struct skinny_device {
-        char deviceName[16];
-        uint32_t userId;
-        uint32_t instance;
-        struct in_addr ip;
-        uint32_t deviceType;
-        uint32_t maxStreams;
-
-        uint16_t port;
-
-        char *codec_string;
-        char *codec_order[SWITCH_MAX_CODECS];
-        int codec_order_last;
-
-        skinny_line_t line[SKINNY_MAX_LINES];
-        int line_last;
-        skinny_speeddial_t speeddial[SKINNY_MAX_SPEEDDIALS];
-        int speeddial_last;
-};
-typedef struct skinny_device skinny_device_t;
-
</del><span class="cx"> typedef switch_status_t (*skinny_command_t) (char **argv, int argc, switch_stream_handle_t *stream);
</span><span class="cx">
</span><span class="cx"> enum skinny_codecs {
</span><span class="lines">@@ -356,7 +319,7 @@
</span><span class="cx">
</span><span class="cx"> struct listener {
</span><span class="cx">         skinny_profile_t *profile;
</span><del>-        skinny_device_t *device; /* TODO -> SQL */
</del><ins>+        char device_name[16];
</ins><span class="cx">
</span><span class="cx">         switch_socket_t *sock;
</span><span class="cx">         switch_memory_pool_t *pool;
</span><span class="lines">@@ -403,6 +366,34 @@
</span><span class="cx"> static switch_status_t keepalive_listener(listener_t *listener, void *pvt);
</span><span class="cx">
</span><span class="cx"> /*****************************************************************************/
</span><ins>+/* PROFILES FUNCTIONS */
+/*****************************************************************************/
+static switch_status_t dump_profile(const skinny_profile_t *profile, switch_stream_handle_t *stream)
+{
+        const char *line = "=================================================================================================";
+        switch_assert(profile);
+        stream->write_function(stream, "%s\n", line);
+        stream->write_function(stream, "Name \t%s\n", profile->name);
+        stream->write_function(stream, "Domain Name \t%s\n", profile->domain);
+        stream->write_function(stream, "IP \t%s\n", profile->ip);
+        stream->write_function(stream, "Port \t%d\n", profile->port);
+        stream->write_function(stream, "Dialplan \t%s\n", profile->dialplan);
+        stream->write_function(stream, "Keep-Alive \t%d\n", profile->keep_alive);
+        stream->write_function(stream, "Date-Format \t%s\n", profile->date_format);
+        stream->write_function(stream, "DBName \t%s\n", profile->dbname ? profile->dbname : switch_str_nil(profile->odbc_dsn));
+        stream->write_function(stream, "Listener-Threads \t%d\n", profile->listener_threads);
+        stream->write_function(stream, "%s\n", line);
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+
+static skinny_profile_t *get_profile(const char *profile_name)
+{
+        return (skinny_profile_t *) switch_core_hash_find(globals.profile_hash, profile_name);
+}
+
+/*****************************************************************************/
</ins><span class="cx"> /* SQL FUNCTIONS */
</span><span class="cx"> /*****************************************************************************/
</span><span class="cx"> static void skinny_execute_sql(skinny_profile_t *profile, char *sql, switch_mutex_t *mutex)
</span><span class="lines">@@ -427,6 +418,7 @@
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
</span><span class="cx">                         goto end;
</span><span class="cx">                 }
</span><ins>+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SQL: %s\n", sql);
</ins><span class="cx">                 switch_core_db_persistant_execute(db, sql, 1);
</span><span class="cx">                 switch_core_db_close(db);
</span><span class="cx">         }
</span><span class="lines">@@ -456,6 +448,7 @@
</span><span class="cx">                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening DB %s\n", profile->dbname);
</span><span class="cx">                         goto end;
</span><span class="cx">                 }
</span><ins>+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SQL: %s\n", sql);
</ins><span class="cx">                 switch_core_db_exec(db, sql, callback, pdata, &errmsg);
</span><span class="cx">
</span><span class="cx">                 if (errmsg) {
</span><span class="lines">@@ -1018,23 +1011,46 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+static int skinny_device_event_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        switch_event_t *event = (switch_event_t *) pArg;
+
+        char *device_name = argv[0];
+        char *user_id = argv[1];
+        char *instance = argv[2];
+        char *ip = argv[3];
+        char *device_type = argv[4];
+        char *max_streams = argv[5];
+        char *port = argv[6];
+        char *codec_string = argv[7];
+
+        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Skinny-Device-Name", device_name);
+        switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-User-Id", "%s", user_id);
+        switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Instance", "%s", instance);
+        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Skinny-IP", ip);
+        switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Device-Type", "%s", device_type);
+        switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Max-Streams", "%s", max_streams);
+        switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Port", "%s", port);
+        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Skinny-Codecs", codec_string);
+
+        return 0;
+}
+
</ins><span class="cx"> static switch_status_t skinny_device_event(listener_t *listener, switch_event_t **ev, switch_event_types_t event_id, const char *subclass_name)
</span><span class="cx"> {
</span><span class="cx">         switch_event_t *event = NULL;
</span><del>-        skinny_device_t *device;
</del><ins>+        char *sql;
+        skinny_profile_t *profile;
+        assert(listener->profile);
+        profile = listener->profile;
+
</ins><span class="cx">         switch_event_create_subclass(&event, event_id, subclass_name);
</span><span class="cx">         switch_assert(event);
</span><del>-        if(listener->device) {
-                device = listener->device;
-                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Skinny-Device-Name", switch_str_nil(device->deviceName));
-                switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-User-Id", "%d", device->userId);
-                switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Instance", "%d", device->instance);
-                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Skinny-IP", inet_ntoa(device->ip));
-                switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Device-Type", "%d", device->deviceType);
-                switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Max-Streams", "%d", device->maxStreams);
-                switch_event_add_header( event, SWITCH_STACK_BOTTOM, "Skinny-Port", "%d", device->port);
-                switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Skinny-Codecs", device->codec_string);
</del><ins>+        if ((sql = switch_mprintf("select * from skinny_devices where device_name='%s'", listener->device_name))) {
+                skinny_execute_sql_callback(profile, profile->listener_mutex, sql, skinny_device_event_callback, event);
+                switch_safe_free(sql);
</ins><span class="cx">         }
</span><ins>+
</ins><span class="cx">         *ev = event;
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="lines">@@ -1044,20 +1060,15 @@
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><span class="cx">         skinny_message_t *message;
</span><del>-        skinny_device_t *device;
</del><span class="cx">         skinny_profile_t *profile;
</span><span class="cx">         switch_event_t *event = NULL;
</span><span class="cx">         switch_event_t *params = NULL;
</span><span class="cx">         switch_xml_t xroot, xdomain, xgroup, xuser, xskinny, xlines, xline, xspeeddials, xspeeddial;
</span><ins>+        char *sql;
</ins><span class="cx">         assert(listener->profile);
</span><span class="cx">         profile = listener->profile;
</span><span class="cx">
</span><del>-        skinny_execute_sql(profile, "select * from skinny_devices", profile->listener_mutex);
-        skinny_execute_sql_callback(profile, profile->listener_mutex,
-                "select * from skinny_devices", NULL, profile);
-
-
-        if(listener->device) {
</del><ins>+        if(!zstr(listener->device_name)) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
</span><span class="cx">                         "A device is already registred on this listener.\n");
</span><span class="cx">                 message = switch_core_alloc(listener->pool, 12+sizeof(message->data.reg_rej));
</span><span class="lines">@@ -1067,65 +1078,87 @@
</span><span class="cx">                 skinny_send_reply(listener, message);
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><del>-        /* Initialize device */
-        device = switch_core_alloc(listener->pool, sizeof(skinny_device_t));
-        memcpy(device->deviceName, request->data.reg.deviceName, 16);
-        device->userId = request->data.reg.userId;
-        device->instance = request->data.reg.instance;
-        device->ip = request->data.reg.ip;
-        device->deviceType = request->data.reg.deviceType;
-        device->maxStreams = request->data.reg.maxStreams;
-        device->codec_string = realloc(device->codec_string, 1);
-        device->codec_string[0] = '\0';
-        device->line_last = 0;
</del><span class="cx">
</span><span class="cx">         /* Check directory */
</span><span class="cx">         skinny_device_event(listener, &params, SWITCH_EVENT_REQUEST_PARAMS, SWITCH_EVENT_SUBCLASS_ANY);
</span><span class="cx">         switch_event_add_header_string(params, SWITCH_STACK_BOTTOM, "action", "skinny-auth");
</span><span class="cx">
</span><del>-        if (switch_xml_locate_user("id", device->deviceName, profile->domain, "", &xroot, &xdomain, &xuser, &xgroup, params) != SWITCH_STATUS_SUCCESS) {
</del><ins>+        if (switch_xml_locate_user("id", request->data.reg.deviceName, profile->domain, "", &xroot, &xdomain, &xuser, &xgroup, params) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Can't find device [%s@%s]\n"
</span><span class="cx">                                          "You must define a domain called '%s' in your directory and add a user with id=\"%s\".\n"
</span><del>-                                         , device->deviceName, profile->domain, profile->domain, device->deviceName);
</del><ins>+                                         , request->data.reg.deviceName, profile->domain, profile->domain, request->data.reg.deviceName);
</ins><span class="cx">                 message = switch_core_alloc(listener->pool, 12+sizeof(message->data.reg_rej));
</span><span class="cx">                 message->type = REGISTER_REJ_MESSAGE;
</span><span class="cx">                 message->length = 4 + sizeof(message->data.reg_rej);
</span><span class="cx">                 strcpy(message->data.reg_rej.error, "Device not found");
</span><span class="cx">                 skinny_send_reply(listener, message);
</span><del>-                return SWITCH_STATUS_FALSE;
</del><ins>+                status = SWITCH_STATUS_FALSE;
</ins><span class="cx">                 goto end;
</span><span class="cx">         }
</span><ins>+
+        if ((sql = switch_mprintf(
+                        "INSERT INTO skinny_devices "
+                                "(device_name, user_id, instance, ip, device_type, max_streams, codec_string) "
+                                "VALUES ('%s','%d','%d', '%s', '%d', '%d', '%s')",
+                        request->data.reg.deviceName,
+                        request->data.reg.userId,
+                        request->data.reg.instance,
+                        inet_ntoa(request->data.reg.ip),
+                        request->data.reg.deviceType,
+                        request->data.reg.maxStreams,
+                        "" /* codec_string */
+                        ))) {
+                skinny_execute_sql(profile, sql, profile->listener_mutex);
+                switch_safe_free(sql);
+        }
+
+
+        strcpy(listener->device_name, request->data.reg.deviceName);
+
</ins><span class="cx">         xskinny = switch_xml_child(xuser, "skinny");
</span><span class="cx">         if (xskinny) {
</span><span class="cx">                 xlines = switch_xml_child(xskinny, "lines");
</span><span class="cx">                 if (xlines) {
</span><span class="cx">                         for (xline = switch_xml_child(xlines, "line"); xline; xline = xline->next) {
</span><del>-                                //TODO const char *position = switch_xml_attr_soft(xline, "position");
</del><ins>+                                const char *position = switch_xml_attr_soft(xline, "position");
</ins><span class="cx">                                 const char *name = switch_xml_attr_soft(xline, "name");
</span><span class="cx">                                 const char *shortname = switch_xml_attr_soft(xline, "shortname");
</span><span class="cx">                                 const char *displayname = switch_xml_attr_soft(xline, "displayname");
</span><del>-                                //TODO device->line[device->line_last].device = *device;
-                                strcpy(device->line[device->line_last].name, name);
-                                strcpy(device->line[device->line_last].shortname, shortname);
-                                strcpy(device->line[device->line_last].displayname, displayname);
-                                device->line_last++;
</del><ins>+                                if ((sql = switch_mprintf(
+                                                "INSERT INTO skinny_lines "
+                                                        "(device_name, line_position, line_name, line_shortname, line_displayname) "
+                                                        "VALUES('%s', '%s', '%s', '%s', '%s')",
+                                                request->data.reg.deviceName,
+                                                position,
+                                                name,
+                                                shortname,
+                                                displayname))) {
+                                        skinny_execute_sql(profile, sql, profile->listener_mutex);
+                                        switch_safe_free(sql);
+                                }
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">                 xspeeddials = switch_xml_child(xskinny, "speed-dials");
</span><span class="cx">                 if (xspeeddials) {
</span><span class="cx">                         for (xspeeddial = switch_xml_child(xspeeddials, "speed-dial"); xspeeddial; xspeeddial = xspeeddial->next) {
</span><del>-                                //TODO const char *position = switch_xml_attr_soft(xspeeddial, "position");
</del><ins>+                                const char *position = switch_xml_attr_soft(xspeeddial, "position");
</ins><span class="cx">                                 const char *line = switch_xml_attr_soft(xspeeddial, "line");
</span><span class="cx">                                 const char *label = switch_xml_attr_soft(xspeeddial, "label");
</span><del>-                                //TODO device->speeddial[device->speeddial_last].device = *device;
-                                strcpy(device->speeddial[device->speeddial_last].line, line);
-                                strcpy(device->speeddial[device->speeddial_last].label, label);
-                                device->speeddial_last++;
</del><ins>+                                if ((sql = switch_mprintf(
+                                                "INSERT INTO skinny_speeddials "
+                                                        "(device_name, speed_position, speed_line, speed_label) "
+                                                        "VALUES('%s', '%s', '%s', '%s')",
+                                                request->data.reg.deviceName,
+                                                position,
+                                                line,
+                                                label))) {
+                                        skinny_execute_sql(profile, sql, profile->listener_mutex);
+                                        switch_safe_free(sql);
+                                }
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        listener->device = device;
</del><span class="cx">         status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">
</span><span class="cx">         /* Reply with RegisterAckMessage */
</span><span class="lines">@@ -1159,9 +1192,11 @@
</span><span class="cx">
</span><span class="cx"> static switch_status_t skinny_handle_capabilities_response(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><del>-        skinny_device_t *device = listener->device;
</del><span class="cx">         uint32_t i = 0;
</span><span class="cx">         uint32_t n = 0;
</span><ins>+        char *codec_order[SWITCH_MAX_CODECS];
+        char *codec_string;
+        
</ins><span class="cx">         size_t string_len, string_pos, pos;
</span><span class="cx">
</span><span class="cx">         n = request->data.cap_res.count;
</span><span class="lines">@@ -1171,60 +1206,91 @@
</span><span class="cx">         string_len = -1;
</span><span class="cx">         for (i = 0; i < n; i++) {
</span><span class="cx">                 char *codec = skinny_codec2string(request->data.cap_res.caps[i].codec);
</span><del>-                device->codec_order[i] = codec;
</del><ins>+                codec_order[i] = codec;
</ins><span class="cx">                 string_len += strlen(codec)+1;
</span><span class="cx">         }
</span><span class="cx">         i = 0;
</span><span class="cx">         pos = 0;
</span><del>-        device->codec_string = realloc(device->codec_string, string_len+1);
</del><ins>+        codec_string = switch_core_alloc(listener->pool, string_len+1);
</ins><span class="cx">         for (string_pos = 0; string_pos < string_len; string_pos++) {
</span><del>-                char *codec = device->codec_order[i];
</del><ins>+                char *codec = codec_order[i];
</ins><span class="cx">                 switch_assert(i < n);
</span><span class="cx">                 if(pos == strlen(codec)) {
</span><del>-                        device->codec_string[string_pos] = ',';
</del><ins>+                        codec_string[string_pos] = ',';
</ins><span class="cx">                         i++;
</span><span class="cx">                         pos = 0;
</span><span class="cx">                 } else {
</span><del>-                        device->codec_string[string_pos] = codec[pos++];
</del><ins>+                        codec_string[string_pos] = codec[pos++];
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><del>-        device->codec_string[string_len] = '\0';
-        device->codec_order_last = n;
</del><ins>+        codec_string[string_len] = '\0';
+        /* TODO SQL update */
</ins><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
</span><del>-                "Codecs %s supported.\n", device->codec_string);
</del><ins>+                "Codecs %s supported.\n", codec_string);
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> static switch_status_t skinny_handle_port_message(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><del>-        skinny_device_t *device = listener->device;
</del><ins>+        char *sql;
+        skinny_profile_t *profile;
</ins><span class="cx">
</span><del>-        device->port = request->data.as_uint16;
</del><ins>+        switch_assert(listener->profile);
+        switch_assert(listener->device_name);
+
+        profile = listener->profile;
+
+        if ((sql = switch_mprintf(
+                        "update skinny_devices set port='%d' where device_name='%s'",
+                        request->data.as_uint16,
+                        listener->device_name
+                        ))) {
+                skinny_execute_sql(profile, sql, profile->listener_mutex);
+                switch_safe_free(sql);
+        }
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+static int skinny_line_stat_request_callback(void *pArg, int argc, char **argv, char **columnNames)
+
+{
+        skinny_message_t *message = pArg;
+
+        strcpy(message->data.line_res.name, argv[2]);
+        strcpy(message->data.line_res.shortname, argv[3]);
+        strcpy(message->data.line_res.displayname, argv[4]);
+
+        return 0;
+}
+
+
</ins><span class="cx"> static switch_status_t skinny_handle_line_stat_request(listener_t *listener, skinny_message_t *request)
</span><span class="cx"> {
</span><del>-        skinny_device_t *device = listener->device;
</del><span class="cx">         skinny_message_t *message;
</span><del>-        uint32_t i = 0;
</del><ins>+        skinny_profile_t *profile;
+        char *sql;
</ins><span class="cx">
</span><ins>+        switch_assert(listener->profile);
+        switch_assert(listener->device_name);
+
+        profile = listener->profile;
+
+
</ins><span class="cx">         message = switch_core_alloc(listener->pool, 12+sizeof(message->data.line_res));
</span><span class="cx">         message->type = LINE_STAT_RES_MESSAGE;
</span><span class="cx">         message->length = 4 + sizeof(message->data.line_res);
</span><del>-        i = request->data.line_req.number;
-        if(i > 0 && i <= device->line_last) {
-                message->data.line_res.number = i;
-                strcpy(message->data.line_res.name, device->line[i-1].name);
-                strcpy(message->data.line_res.shortname, device->line[i-1].shortname);
-                strcpy(message->data.line_res.displayname, device->line[i-1].displayname);
-        } else {
-                message->data.line_res.number = i;
-                strcpy(message->data.line_res.name, "");
-                strcpy(message->data.line_res.shortname, "");
-                strcpy(message->data.line_res.displayname, "");
</del><ins>+        message->data.line_res.number = request->data.line_req.number;
+
+        if ((sql = switch_mprintf(
+                        "select * from skinny_lines where device_name='%s' and line_position='%d'",
+                        listener->device_name,
+                        request->data.line_req.number
+                        ))) {
+                skinny_execute_sql_callback(profile, profile->listener_mutex, sql, skinny_line_stat_request_callback, message);
+                switch_safe_free(sql);
</ins><span class="cx">         }
</span><span class="cx">         skinny_send_reply(listener, message);
</span><ins>+
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -1260,7 +1326,7 @@
</span><span class="cx"> {
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
</span><span class="cx">                 "Received message (type=%x,length=%d).\n", request->type, request->length);
</span><del>-        if(!listener->device && request->type != REGISTER_MESSAGE) {
</del><ins>+        if(zstr(listener->device_name) && request->type != REGISTER_MESSAGE) {
</ins><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
</span><span class="cx">                         "Device should send a register message first.\n");
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="lines">@@ -1375,60 +1441,47 @@
</span><span class="cx">         /* TODO */
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static listener_t *find_listener(char *device_name)
</del><ins>+static int dump_device_callback(void *pArg, int argc, char **argv, char **columnNames)
</ins><span class="cx"> {
</span><del>-        switch_hash_index_t *hi;
-        void *val;
-        skinny_profile_t *profile;
-        listener_t *l, *r = NULL;
-        skinny_device_t *device;
-        
-        /* walk listeners */
-        for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
-                switch_hash_this(hi, NULL, NULL, &val);
-                profile = (skinny_profile_t *) val;
</del><ins>+        switch_stream_handle_t *stream = (switch_stream_handle_t *) pArg;
</ins><span class="cx">
</span><del>-                switch_mutex_lock(profile->listener_mutex);
-                for (l = profile->listeners; l; l = l->next) {
-                        if (l->device) {
-                                device = l->device;
-                                if(!strcasecmp(device->deviceName,device_name)) {
-                                        if (switch_thread_rwlock_tryrdlock(l->rwlock) == SWITCH_STATUS_SUCCESS) {
-                                                r = l;
-                                        }
-                                        break;
-                                }
-                        }
-                }
-                switch_mutex_unlock(profile->listener_mutex);
-                if(r) {
-                        break;
-                }
-        }
-        return r;
</del><ins>+        char *device_name = argv[0];
+        char *user_id = argv[1];
+        char *instance = argv[2];
+        char *ip = argv[3];
+        char *device_type = argv[4];
+        char *max_streams = argv[5];
+        char *port = argv[6];
+        char *codec_string = argv[7];
+
+        const char *line = "=================================================================================================";
+        stream->write_function(stream, "%s\n", line);
+        stream->write_function(stream, "DeviceName \t%s\n", switch_str_nil(device_name));
+        stream->write_function(stream, "UserId \t%s\n", user_id);
+        stream->write_function(stream, "Instance \t%s\n", instance);
+        stream->write_function(stream, "IP \t%s\n", ip);
+        stream->write_function(stream, "DeviceType \t%s\n", device_type);
+        stream->write_function(stream, "MaxStreams \t%s\n", max_streams);
+        stream->write_function(stream, "Port \t%s\n", port);
+        stream->write_function(stream, "Codecs \t%s\n", codec_string);
+        stream->write_function(stream, "%s\n", line);
+
+        return 0;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t dump_listener(listener_t *listener, void *pvt)
</del><ins>+static switch_status_t dump_device(skinny_profile_t *profile, const char *device_name, switch_stream_handle_t *stream)
</ins><span class="cx"> {
</span><del>-        switch_stream_handle_t *stream = (switch_stream_handle_t *) pvt;
-        const char *line = "=================================================================================================";
-        skinny_device_t *device;
-        if(listener->device) {
-                device = listener->device;
-                stream->write_function(stream, "%s\n", line);
-                stream->write_function(stream, "DeviceName \t%s\n", switch_str_nil(device->deviceName));
-                stream->write_function(stream, "UserId \t%d\n", device->userId);
-                stream->write_function(stream, "Instance \t%d\n", device->instance);
-                stream->write_function(stream, "IP \t%s\n", inet_ntoa(device->ip));
-                stream->write_function(stream, "DeviceType \t%d\n", device->deviceType);
-                stream->write_function(stream, "MaxStreams \t%d\n", device->maxStreams);
-                stream->write_function(stream, "Port \t%d\n", device->port);
-                stream->write_function(stream, "Codecs \t%s\n", device->codec_string);
-                stream->write_function(stream, "%s\n", line);
</del><ins>+        char *sql;
+        if ((sql = switch_mprintf("select * from skinny_devices where device_name LIKE '%s'",
+                        device_name))) {
+                skinny_execute_sql_callback(profile, profile->listener_mutex, sql, dump_device_callback, stream);
+                switch_safe_free(sql);
</ins><span class="cx">         }
</span><ins>+
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+
</ins><span class="cx"> static void close_socket(switch_socket_t **sock)
</span><span class="cx"> {
</span><span class="cx">         /* TODO
</span><span class="lines">@@ -1626,7 +1679,7 @@
</span><span class="cx">                 rv = switch_socket_listen(profile->sock, 5);
</span><span class="cx">                 if (rv)
</span><span class="cx">                         goto sock_fail;
</span><del>-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Socket up listening on %s:%u\n", profile->ip, profile->port);
</del><ins>+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Socket up listening on %s:%u\n", profile->ip, profile->port);
</ins><span class="cx">
</span><span class="cx">                 break;
</span><span class="cx">          sock_fail:
</span><span class="lines">@@ -1669,7 +1722,7 @@
</span><span class="cx">                 listener->sock = inbound_socket;
</span><span class="cx">                 listener->pool = listener_pool;
</span><span class="cx">                 listener_pool = NULL;
</span><del>-                listener->device = NULL;
</del><ins>+                strcpy(listener->device_name, "");
</ins><span class="cx">                 listener->profile = profile;
</span><span class="cx">
</span><span class="cx">                 switch_mutex_init(&listener->flag_mutex, SWITCH_MUTEX_NESTED, listener->pool);
</span><span class="lines">@@ -1827,12 +1880,12 @@
</span><span class="cx">                                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Connected ODBC DSN: %s\n", profile->odbc_dsn);
</span><span class="cx">                                         switch_odbc_handle_exec(profile->master_odbc, devices_sql, NULL);
</span><span class="cx">                                         switch_odbc_handle_exec(profile->master_odbc, lines_sql, NULL);
</span><del>-                                        switch_odbc_handle_exec(profile->master_odbc, speed_sql, NULL);
</del><ins>+                                        switch_odbc_handle_exec(profile->master_odbc, speeddials_sql, NULL);
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         if ((db = switch_core_db_open_file(profile->dbname))) {
</span><span class="cx">                                                 switch_core_db_test_reactive(db, "select * from skinny_devices", NULL, devices_sql);
</span><span class="cx">                                                 switch_core_db_test_reactive(db, "select * from skinny_lines", NULL, lines_sql);
</span><del>-                                                switch_core_db_test_reactive(db, "select * from skinny_speeddials", NULL, speed_sql);
</del><ins>+                                                switch_core_db_test_reactive(db, "select * from skinny_speeddials", NULL, speeddials_sql);
</ins><span class="cx">                                         } else {
</span><span class="cx">                                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open SQL Database!\n");
</span><span class="cx">                                                 continue;
</span><span class="lines">@@ -1853,23 +1906,27 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t cmd_device(char **argv, int argc, switch_stream_handle_t *stream)
</del><ins>+static switch_status_t cmd_status_profile(const char *profile_name, switch_stream_handle_t *stream)
</ins><span class="cx"> {
</span><del>-        listener_t *listener;
-        if (argc != 1) {
-                stream->write_function(stream, "Invalid Args!\n");
-                return SWITCH_STATUS_SUCCESS;
</del><ins>+        skinny_profile_t *profile;
+        if ((profile = get_profile(profile_name))) {
+                dump_profile(profile, stream);
+        } else {
+                stream->write_function(stream, "Profile not found!\n");
</ins><span class="cx">         }
</span><del>-        
-        if (argv[0] && !strcasecmp(argv[0], "*")) {
-                walk_listeners(dump_listener, stream);
</del><ins>+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t cmd_status_profile_device(const char *profile_name, const char *device_name, switch_stream_handle_t *stream)
+{
+        skinny_profile_t *profile;
+        if ((profile = get_profile(profile_name))) {
+                dump_device(profile, device_name, stream);
</ins><span class="cx">         } else {
</span><del>-                listener=find_listener(argv[0]);
-                if(listener) {
-                        dump_listener(listener, stream);
-                }
</del><ins>+                stream->write_function(stream, "Profile not found!\n");
</ins><span class="cx">         }
</span><del>-        
</del><ins>+
</ins><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -1879,12 +1936,11 @@
</span><span class="cx">         int argc = 0;
</span><span class="cx">         char *mycmd = NULL;
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><del>-        skinny_command_t func = NULL;
</del><span class="cx">         const char *usage_string = "USAGE:\n"
</span><span class="cx">                 "--------------------------------------------------------------------------------\n"
</span><span class="cx">                 "skinny help\n"
</span><del>-                "skinny device *\n"
-                "skinny device <device_name>\n"
</del><ins>+                "skinny status profile <profile_name>\n"
+                "skinny status profile <profile_name> device <device_name>\n"
</ins><span class="cx">                 "--------------------------------------------------------------------------------\n";
</span><span class="cx">         if (session) {
</span><span class="cx">                 return SWITCH_STATUS_FALSE;
</span><span class="lines">@@ -1905,20 +1961,18 @@
</span><span class="cx">                 goto done;
</span><span class="cx">         }
</span><span class="cx">
</span><del>-        if (!strcasecmp(argv[0], "device")) {
-                func = cmd_device;
</del><ins>+        if (argc == 3 && !strcasecmp(argv[0], "status") && !strcasecmp(argv[1], "profile")) {
+                status = cmd_status_profile(argv[2], stream);
+        } else if (argc == 5 && !strcasecmp(argv[0], "status") && !strcasecmp(argv[1], "profile") && !strcasecmp(argv[3], "device")) {
+                status = cmd_status_profile_device(argv[2], argv[4], stream);
</ins><span class="cx">         } else if (!strcasecmp(argv[0], "help")) {
</span><span class="cx">                 stream->write_function(stream, "%s", usage_string);
</span><span class="cx">                 goto done;
</span><del>-        }
-        
-        if (func) {
-                status = func(&argv[1], argc - 1, stream);
</del><span class="cx">         } else {
</span><span class="cx">                 stream->write_function(stream, "Unknown Command [%s]\n", argv[0]);
</span><span class="cx">         }
</span><span class="cx">
</span><del>- done:
</del><ins>+done:
</ins><span class="cx">         switch_safe_free(mycmd);
</span><span class="cx">         return status;
</span><span class="cx"> }
</span><span class="lines">@@ -1930,7 +1984,7 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><del>-static switch_status_t skinny_list_devices(const char *line, const char *cursor, switch_console_callback_match_t **matches)
</del><ins>+static switch_status_t skinny_list_profiles(const char *line, const char *cursor, switch_console_callback_match_t **matches)
</ins><span class="cx"> {
</span><span class="cx">         switch_console_callback_match_t *my_matches = NULL;
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_FALSE;
</span><span class="lines">@@ -1938,22 +1992,56 @@
</span><span class="cx">         void *val;
</span><span class="cx">         skinny_profile_t *profile;
</span><span class="cx">         
</span><del>-        listener_t *l;
-        skinny_device_t *device;
-
-        /* walk listeners */
</del><ins>+        /* walk profiles */
</ins><span class="cx">         for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
</span><span class="cx">                 switch_hash_this(hi, NULL, NULL, &val);
</span><span class="cx">                 profile = (skinny_profile_t *) val;
</span><span class="cx">
</span><del>-                switch_mutex_lock(profile->listener_mutex);
-                for (l = profile->listeners; l; l = l->next) {
-                        if(l->device) {
-                                device = l->device;
-                                switch_console_push_match(&my_matches, device->deviceName);
-                        }
</del><ins>+                switch_console_push_match(&my_matches, profile->name);
+        }
+        
+        if (my_matches) {
+                *matches = my_matches;
+                status = SWITCH_STATUS_SUCCESS;
+        }
+        
+        return status;
+}
+
+static int skinny_list_devices_callback(void *pArg, int argc, char **argv, char **columnNames)
+{
+        switch_console_callback_match_t *my_matches = (switch_console_callback_match_t *) pArg;
+
+        char *device_name = argv[0];
+        switch_console_push_match(&my_matches, device_name);
+        pArg = my_matches;
+        return 0;
+}
+
+static switch_status_t skinny_list_devices(const char *line, const char *cursor, switch_console_callback_match_t **matches)
+{
+        switch_console_callback_match_t *my_matches = NULL;
+        switch_status_t status = SWITCH_STATUS_FALSE;
+        skinny_profile_t *profile;
+        char *sql;
+
+        char *myline;
+        char *argv[1024] = { 0 };
+        int argc = 0;
+
+        if (!(myline = strdup(line))) {
+                status = SWITCH_STATUS_MEMERR;
+                return status;
+        }
+        if (!(argc = switch_separate_string(myline, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) || argc != 5) {
+                return status;
+        }
+
+        if((profile = get_profile(argv[3]))) {
+                if ((sql = switch_mprintf("select device_name from skinny_devices"))) {
+                        skinny_execute_sql_callback(profile, profile->listener_mutex, sql, skinny_list_devices_callback, my_matches);
+                        switch_safe_free(sql);
</ins><span class="cx">                 }
</span><del>-                switch_mutex_unlock(profile->listener_mutex);
</del><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         if (my_matches) {
</span><span class="lines">@@ -2006,9 +2094,10 @@
</span><span class="cx">
</span><span class="cx">         SWITCH_ADD_API(api_interface, "skinny", "Skinny Controls", skinny_function, "<cmd> <args>");
</span><span class="cx">         switch_console_set_complete("add skinny help");
</span><del>-        switch_console_set_complete("add skinny device *");
-        switch_console_set_complete("add skinny device ::skinny::list_devices");
</del><ins>+        switch_console_set_complete("add skinny status profile ::skinny::list_profiles");
+        switch_console_set_complete("add skinny status profile ::skinny::list_profiles device ::skinny::list_devices");
</ins><span class="cx">
</span><ins>+        switch_console_add_complete_func("::skinny::list_profiles", skinny_list_profiles);
</ins><span class="cx">         switch_console_add_complete_func("::skinny::list_devices", skinny_list_devices);
</span><span class="cx">         /* indicate that the module should continue to be loaded */
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span></span></pre></div>
<a id="freeswitchtrunksrcmodendpointsmod_skinnytestskinnypl"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl (16761 => 16762)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl        2010-02-24 11:59:41 UTC (rev 16761)
+++ freeswitch/trunk/src/mod/endpoints/mod_skinny/test-skinny.pl        2010-02-24 11:59:49 UTC (rev 16762)
</span><span class="lines">@@ -11,7 +11,7 @@
</span><span class="cx"> sub skinny_connect
</span><span class="cx"> {
</span><span class="cx">         $socket = IO::Socket::INET->new(
</span><del>-                PeerAddr => '127.0.0.1',
</del><ins>+                PeerAddr => '192.168.0.6',
</ins><span class="cx">                 PeerPort => 2000,
</span><span class="cx">                 );
</span><span class="cx"> }
</span><span class="lines">@@ -96,9 +96,9 @@
</span><span class="cx">         pack("V", 2
</span><span class="cx">         ));
</span><span class="cx">
</span><del>-
-skinny_sleep(3);
-skinny_send(0x0000, # keepalive
-        "");
-skinny_recv(); # keepaliveack
-
</del><ins>+while(1) {
+        skinny_sleep(20);
+        skinny_send(0x0000, # keepalive
+                "");
+        skinny_recv(); # keepaliveack
+}
</ins></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>