[Freeswitch-svn] [commit] r14021 - in freeswitch/trunk/contrib/mod/endpoints/mod_khomp: include src

FreeSWITCH SVN jmesquita at freeswitch.org
Sat Jun 27 22:29:30 PDT 2009


Author: jmesquita
Date: Sun Jun 28 00:29:29 2009
New Revision: 14021

Log:
More elegant channel/board lookup and some documentation. Thanks Raul...

Modified:
   freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h
   freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp

Modified: freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h
==============================================================================
--- freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h	(original)
+++ freeswitch/trunk/contrib/mod/endpoints/mod_khomp/include/khomp_pvt.h	Sun Jun 28 00:29:29 2009
@@ -47,6 +47,13 @@
     }
 
 
+    /*!
+      \brief Lookup channels and boards when dialed.
+      \param allocation_string The dialstring as put on Dialplan. [Khomp/[a|A|0-board_high]/[a|A|0-channel_high]/dest].
+      \param new_session Session allocated for this call.
+      \param[out] Cause returned. Returns NULL if suceeded if not, the proper cause.
+      \return KhompPvt to be used on the call.
+      */
     static KhompPvt * find_channel(char* allocation_string, switch_core_session_t * new_session, switch_call_cause_t * cause);
 
     static void initialize(void)

Modified: freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp
==============================================================================
--- freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp	(original)
+++ freeswitch/trunk/contrib/mod/endpoints/mod_khomp/src/khomp_pvt.cpp	Sun Jun 28 00:29:29 2009
@@ -40,6 +40,7 @@
         if(*argv[0] == 'a')
         {
             reverse_first_board_available = true;
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Doing reverse board lookup!\n");
         }
     }
     else
@@ -55,6 +56,7 @@
         if(*argv[1] == 'a')
         {
             reverse_first_channel_available = true;
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Doing reverse channel lookup!\n");
         }
     }
     else
@@ -75,202 +77,65 @@
 
     switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Channel selection: board (%d-%d), channel (%d-%d)!\n", board_low, board_high, channel_low, channel_high);
     
-    // TODO: Stop this nonsense of having duplicate code?
-    if(reverse_first_board_available)
+    int board = board_low;
+    for ((reverse_first_board_available == false ? board = board_low : board = board_high-1);
+            (reverse_first_board_available == false ? board <= board_high : board >= board_low);
+            (reverse_first_board_available == false ? board++ : board--))
     {
-        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Doing reverse board lookup.\n");
-        for (int board = board_high-1 ; board >= board_low; board--)
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking board %d\n", board);
+        
+        if(pvt != NULL)
+            break;
+        
+        if(first_channel_available)
         {
-            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking board %d\n", board);
-            
-            if(pvt != NULL)
-                break;
-            
-            if(first_channel_available)
-            {
-                channel_low = 0;
-                channel_high = Globals::_k3lapi.channel_count(board);
-            }
-            else
-            {
-                if(channel_low < 0 || channel_high > Globals::_k3lapi.channel_count(board))
-                {
-                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid channel selection (%d-%d) !\n", channel_low, channel_high);
-                    switch_mutex_unlock(_pvts_mutex);
-                    return NULL;
-                }
-            }
-            if (reverse_first_channel_available)
+            channel_low = 0;
+            channel_high = Globals::_k3lapi.channel_count(board);
+        }
+        else
+        {
+            if(channel_low < 0 || channel_high > Globals::_k3lapi.channel_count(board))
             {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Doing a reverse free channel lookup\n", board_low, board_high, channel_low, channel_high);
-                for (int channel = channel_high-1 ; channel >= channel_low ; channel--) 
-                {
-                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking if (%d-%d) is free\n", board, channel);
-                    try 
-                    {
-                        K3L_CHANNEL_CONFIG channelConfig;
-                        channelConfig = Globals::_k3lapi.channel_config( board, channel );
-                    }
-                    catch (...)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception while retrieving channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    K3L_CHANNEL_STATUS status;
-                    if (k3lGetDeviceStatus( board, channel + ksoChannel, &status, sizeof(status) ) != ksSuccess)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "k3lGetDeviceStatus failed to retrieve channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    if(status.CallStatus == kcsFree)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Channel (%d-%d) is free, let's check if the session is available ...\n", board, channel);
-                        pvt = KhompPvt::khompPvt(board, channel);
-                        if(pvt != NULL && pvt->session() == NULL)
-                        {
-                            pvt->session(new_session);
-                            break;
-                        }
-                        pvt = NULL;
-                    }
-                }
-            }
-            else
-            {            
-                for (int channel = channel_low ; channel <= channel_high ; channel++) 
-                {
-                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking if (%d-%d) is free\n", board, channel);
-                    try 
-                    {
-                        K3L_CHANNEL_CONFIG channelConfig;
-                        channelConfig = Globals::_k3lapi.channel_config( board, channel );
-                    }
-                    catch (...)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception while retrieving channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    K3L_CHANNEL_STATUS status;
-                    if (k3lGetDeviceStatus( board, channel + ksoChannel, &status, sizeof(status) ) != ksSuccess)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "k3lGetDeviceStatus failed to retrieve channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    if(status.CallStatus == kcsFree)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Channel (%d-%d) is free, let's check if the session is available ...\n", board, channel);
-                        pvt = KhompPvt::khompPvt(board, channel);
-                        if(pvt != NULL && pvt->session() == NULL)
-                        {
-                            pvt->session(new_session);
-                            break;
-                        }
-                        pvt = NULL;
-                    }
-                }
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid channel selection (%d-%d) !\n", channel_low, channel_high);
+                switch_mutex_unlock(_pvts_mutex);
+                return NULL;
             }
         }
-    }
-    else
-    {
-        
-        for (int board = board_low ; board <= board_high; board++)
+
+        int channel = channel_low;
+        for ((reverse_first_channel_available == true ? channel = channel_high-1 : channel = channel_low);
+                (reverse_first_channel_available == true ? channel >= channel_low : channel <= channel_high);
+                (reverse_first_channel_available == true ? channel-- : channel++)) 
         {
-            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking board %d\n", board);
-            
-            if(pvt != NULL)
-                break;
-            
-            if(first_channel_available)
+            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking if (%d-%d) is free\n", board, channel);
+            try 
             {
-                channel_low = 0;
-                channel_high = Globals::_k3lapi.channel_count(board);
+                K3L_CHANNEL_CONFIG channelConfig;
+                channelConfig = Globals::_k3lapi.channel_config( board, channel );
             }
-            else
+            catch (...)
             {
-                if(channel_low < 0 || channel_high > Globals::_k3lapi.channel_count(board))
-                {
-                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid channel selection (%d-%d) !\n", channel_low, channel_high);
-                    switch_mutex_unlock(_pvts_mutex);
-                    return NULL;
-                }
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception while retrieving channel config for board %d, channel%d!\n", board, channel);
+                switch_mutex_unlock(_pvts_mutex);
+                return NULL;
             }
-            if (reverse_first_channel_available)
+            K3L_CHANNEL_STATUS status;
+            if (k3lGetDeviceStatus( board, channel + ksoChannel, &status, sizeof(status) ) != ksSuccess)
             {
-                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Doing a reverse free channel lookup\n", board_low, board_high, channel_low, channel_high);
-                for (int channel = channel_high-1 ; channel >= channel_low ; channel--) 
-                {
-                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking if (%d-%d) is free\n", board, channel);
-                    try 
-                    {
-                        K3L_CHANNEL_CONFIG channelConfig;
-                        channelConfig = Globals::_k3lapi.channel_config( board, channel );
-                    }
-                    catch (...)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception while retrieving channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    K3L_CHANNEL_STATUS status;
-                    if (k3lGetDeviceStatus( board, channel + ksoChannel, &status, sizeof(status) ) != ksSuccess)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "k3lGetDeviceStatus failed to retrieve channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    if(status.CallStatus == kcsFree)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Channel (%d-%d) is free, let's check if the session is available ...\n", board, channel);
-                        pvt = KhompPvt::khompPvt(board, channel);
-                        if(pvt != NULL && pvt->session() == NULL)
-                        {
-                            pvt->session(new_session);
-                            break;
-                        }
-                        pvt = NULL;
-                    }
-                }
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "k3lGetDeviceStatus failed to retrieve channel config for board %d, channel%d!\n", board, channel);
+                switch_mutex_unlock(_pvts_mutex);
+                return NULL;
             }
-            else
-            {            
-                for (int channel = channel_low ; channel <= channel_high ; channel++) 
+            if(status.CallStatus == kcsFree)
+            {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Channel (%d-%d) is free, let's check if the session is available ...\n", board, channel);
+                pvt = KhompPvt::khompPvt(board, channel);
+                if(pvt != NULL && pvt->session() == NULL)
                 {
-                    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking if (%d-%d) is free\n", board, channel);
-                    try 
-                    {
-                        K3L_CHANNEL_CONFIG channelConfig;
-                        channelConfig = Globals::_k3lapi.channel_config( board, channel );
-                    }
-                    catch (...)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Exception while retrieving channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    K3L_CHANNEL_STATUS status;
-                    if (k3lGetDeviceStatus( board, channel + ksoChannel, &status, sizeof(status) ) != ksSuccess)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "k3lGetDeviceStatus failed to retrieve channel config for board %d, channel%d!\n", board, channel);
-                        switch_mutex_unlock(_pvts_mutex);
-                        return NULL;
-                    }
-                    if(status.CallStatus == kcsFree)
-                    {
-                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Channel (%d-%d) is free, let's check if the session is available ...\n", board, channel);
-                        pvt = KhompPvt::khompPvt(board, channel);
-                        if(pvt != NULL && pvt->session() == NULL)
-                        {
-                            pvt->session(new_session);
-                            break;
-                        }
-                        pvt = NULL;
-                    }
+                    pvt->session(new_session);
+                    break;
                 }
+                pvt = NULL;
             }
         }
     }



More information about the Freeswitch-svn mailing list