Sofia.conf generation works
authorStas Shtin <antisvin@gmail.com>
Fri, 16 Apr 2010 22:00:44 +0000 (02:00 +0400)
committerStas Shtin <antisvin@gmail.com>
Fri, 16 Apr 2010 22:00:44 +0000 (02:00 +0400)
src/ipypbx/controllers.py
src/ipypbx/http.py

index 414eae0..316e329 100644 (file)
@@ -244,7 +244,6 @@ class ConnectionController(BaseController):
         if not _ok:
             connection_id = None
         
-        print local_ip_address, local_port, connection_id
         return local_ip_address, local_port, connection_id
     
     def connectionChange(self, index):
index a049e42..4636866 100644 (file)
@@ -23,6 +23,20 @@ class FreeswitchConfigServer(QtNetwork.QTcpServer):
     """
     TCP server that receives config requests from freeswitch.
     """
+    configNotFound = '''
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<document type="freeswitch/xml">
+  <section name="result">
+    <result status="not found" />
+  </section>
+</document>
+    '''
+    responseTemplate = '''HTTP/1.1 200 OK
+Content-Type: text/xml; charset=utf-8
+Content-Length: %i
+
+%s'''
+  
     def __init__(self, window):
         super(FreeswitchConfigServer, self).__init__(window)
 
@@ -92,8 +106,14 @@ class FreeswitchConfigServer(QtNetwork.QTcpServer):
     def receiveData(self):
         # TODO: read in chunks.
         for line in str(self.socket.readAll()).split('\r\n'):
+            print line
             self.httpRequestParser.handle(line)
 
+        response = self.httpRequestParser.result or self.configNotFound
+        http_response = self.responseTemplate % (len(response), response)
+        self.socket.write(http_response)
+        self.httpRequestParser.reset()        
+        self.socket.close()
 
 class HttpParseError(Exception):
     """
@@ -123,6 +143,7 @@ class HttpRequestParser(object):
         self.http_version = None
         self.headers = {}
         self.data = {}
+        self.result = None
         
         # Set initial state.
         self.state = self.HTTP_NONE        
@@ -175,17 +196,16 @@ class HttpRequestParser(object):
         """
         self.data = dict(pair.split('=', 2) for pair in line.split('&'))
 
-        for k, v in self.data.items():
-            print k, '=>', v
-        print
+        #for k, v in self.data.items():
+        #    print k, '=>', v
+        #print
 
         for generator in self.parent.generators:
             if generator.canHandle(self.data):
                 self.state += 1
-                print generator.generateConfig(self.headers)
-        else:
-            print 'No generator found'
-            
+                self.result = etree.tostring(generator.generateConfig(
+                    self.headers))
+                break
 
 
 class FreeswitchConfigGenerator(object):
@@ -202,7 +222,6 @@ class FreeswitchConfigGenerator(object):
 
     def canHandle(self, params):
         for key, value in self.param_match.iteritems():
-            print key, value, params.get(key, None)
             if params.get(key, None) != value:
                 return False
         else:
@@ -221,7 +240,8 @@ class FreeswitchConfigGenerator(object):
     @staticmethod
     def addParams(parent_elt, params):
         for name, value in params:
-            etree.SubElement(parent_elt, 'param', name=name, value=value)
+            etree.SubElement(
+                parent_elt, 'param', name=name, value=str(value))
             
         
 class SofiaConfGenerator(FreeswitchConfigGenerator):
@@ -247,15 +267,19 @@ class SofiaConfGenerator(FreeswitchConfigGenerator):
         
         # Create all profiles for current host.
         profiles_query = database.exec_(
-            'select id from ipypbxweb_sipprofile where connection_id = %i' %
-            self.parent.connection_id)
+            '''
+            select id, name, external_sip_ip, external_rtp_ip, sip_ip, rtp_ip,
+            sip_port, accept_blind_registration, authenticate_calls
+            from ipypbxweb_sipprofile where connection_id = %i
+            ''' % self.parent.connection_id)
         while profiles_query.next():
             profile_id, _ok = profiles_query.value(0).toInt()
-            profile_elt = etree.SubElement(profiles_elt, 'profile')
+            profile_elt = etree.SubElement(
+                profiles_elt, 'profile',
+                name=profiles_query.value(1).toString())
 
             # Create domains for current profile.
             domains_elt = etree.SubElement(profile_elt, 'domains')
-
             domains_query = database.exec_(
                 'select host_name from ipypbxweb_domain where profile_id = '
                 '%i' % profile_id)
@@ -264,15 +288,17 @@ class SofiaConfGenerator(FreeswitchConfigGenerator):
                     domains_elt, 'domain', name=domains_quey.value(0),
                     alias='true', parse='true')
 
+            profile_sip_port, _ok = profiles_query.value(6).toInt()
+
             # Create settings for current profile.
             settings_elt = etree.SubElement(profile_elt, 'settings')
             params = (
                 ('dialplan', 'XML,enum'),
-                ('ext-sip-ip', profile.ext_sip_ip),
-                ('ext-rtp-ip', profile.ext_rtp_ip),
-                ('sip-ip', profile.sip_ip),
-                ('rtp-ip', profile.rtp_ip),
-                ('sip-port', profile.sip_port),
+                ('ext-sip-ip', profiles_query.value(2).toString()),
+                ('ext-rtp-ip', profiles_query.value(3).toString()),
+                ('sip-ip', profiles_query.value(4).toString()),
+                ('rtp-ip', profiles_query.value(5).toString()),
+                ('sip-port', profile_sip_port),
                 ('nonce-ttl', '60'),
                 ('rtp-timer-name', 'soft'),
                 ('codec-prefs', 'PCMU@20i'),
@@ -280,26 +306,34 @@ class SofiaConfGenerator(FreeswitchConfigGenerator):
                 ('rfc2833-pt', '1'),
                 ('dtmf-duration', '100'),
                 ('codec-ms', '20'),
-                ('accept-blind-reg', profile.accept_blind_registration),
-                ('auth-calls', profile.authenticate_calls))
-            self.add_params(settings_elt, params)
+                ('accept-blind-reg', profiles_query.value(7).toBool()),
+                ('auth-calls', profiles_query.value(8).toBool()))
+            self.addParams(settings_elt, params)
 
             # Create gateways for current profile.
-            gateways_elt = etree.SubElement(profile, 'gateways')
-            for gateway in self.parent.get_gateways_for_profile(profile):
-                gateway_elt = etree.SubElement(gateways_elt, 'gateway', name=gateway.name)
+            gateways_elt = etree.SubElement(profile_elt, 'gateways')
+            gateways_query = database.exec_(
+                '''
+                select name, username, realm, from_domain, password,
+                retry_seconds, expire_seconds, caller_id_in_from, extension
+                from ipypbxweb_gateway where sipprofile_id = %i
+                '''  % profile_id)
+            while gateways_query.next():
+                gateway_elt = etree.SubElement(
+                    gateways_elt, 'gateway', name=gateways_query.value(0).toString())
+                retry_seconds, _ok = gateways_query.value(5).toInt()
+                expire_seconds, _ok = gateways_query.value(6).toInt()
                 params = (
-                    ('username', gateway.username),
-                    ('realm', gateway.realm),
-                    ('from-domain', gateway.from_domain),
-                    ('password', gateway.password),
-                    ('retry-seconds', gateway.retry_seconds),
-                    ('expire-seconds', gateway.expire_seconds),
-                    ('caller-id-in-from', gateway.caller_id_in_from),
-                    ('extension', gateway.extension),
+                    ('username', gateways_query.value(1).toString()),
+                    ('realm', gateways_query.value(2).toString()),
+                    ('from-domain', gateways_query.value(3).toString()),
+                    ('password', gateways_query.value(4).toString()),
+                    ('retry-seconds', retry_seconds),
+                    ('expire-seconds', expire_seconds),
+                    ('caller-id-in-from', gateways_query.value(7).toBool()),
+                    ('extension', gateways_query.value(8).toString()),
                     # TODO: proxy, register
-                    ('expire-seconds', gateway.expire_seconds),
-                    ('retry-seconds', gateway.retry_seconds))
-                self.add_params(gateway_elt, params)
+                    )
+                self.addParams(gateway_elt, params)
 
         return root_elt