Restart config server when connection settings change
[ipypbx] / src / ipypbx / http.py
1 # Copyright (c) Stas Shtin, 2010
2
3 # This file is part of IPyPBX.
4
5 # IPyPBX is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # IPyPBX is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
17
18 import xml.etree.ElementTree as etree
19 from PyQt4 import QtCore, QtNetwork
20
21
22 class FreeswitchConfigServer(QtNetwork.QTcpServer):
23     """
24     TCP server that receives config requests from freeswitch.
25     """
26     def __init__(self, parent=None):
27         super(FreeswitchConfigServer, self).__init__(parent)
28
29         self.host = None
30         self.port = None
31         self.is_running = False
32         
33         self.httpRequestParser = HttpRequestParser()
34         
35     def setSocketData(self, host, port):
36         """
37         Set host and port for socket to listen on.
38
39         If the settings differ from previous values, server gets restarted.
40         """
41         # Check if restart is needed before new settings are applied.
42         needs_restart = (host, port) != (self.host, self.port)
43
44         # Save new settings.
45         self.host = host
46         self.port = port
47
48         # Restart server if necessary.
49         if needs_restart:
50             print 'restartin', self.host, self.port
51             self.restartServer()
52
53     def startServer(self):
54         """
55         Start listening on our socket.
56         """
57         if not self.is_running:
58             if self.host and self.port:
59                 self.newConnection.connect(self.clientConnecting)
60                 self.listen(QtNetwork.QHostAddress(self.host), self.port)
61                 self.is_running = True
62
63     def stopServer(self):
64         """
65         Stop listening on our socket.
66         """
67         if self.is_running:
68             self.close()
69             self.is_running = False
70
71     def restartServer(self):
72         """
73         Restart server.
74         """
75         self.stopServer()
76         self.startServer()
77
78     def clientConnecting(self):
79         """
80         Handle client connection.
81         """
82         if self.hasPendingConnections():
83             self.socket = self.nextPendingConnection()
84             self.socket.readyRead.connect(self.receiveData)
85
86     def receiveData(self):
87         # TODO: read in chunks.
88         for line in str(self.socket.readAll()).split('\r\n'):
89             self.httpRequestParser.handle(line)
90
91
92 class HttpParseError(Exception):
93     """
94     Error parsing HTTP request.
95     """
96
97
98 class HttpRequestParser(object):
99     """
100     A simple state machine for parsing HTTP requests.
101     """
102     HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE = range(5)
103     HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE']
104     
105     def __init__(self):
106         super(HttpRequestParser, self).__init__()
107         self.reset()
108
109     def reset(self):
110         """
111         Reset parser to initial state.
112         """
113         # Initial values for request data.
114         self.method = None
115         self.request_path = None
116         self.http_version = None
117         self.headers = {}
118         self.data = {}
119         
120         # Set initial state.
121         self.state = self.HTTP_NONE        
122
123     def handle(self, line):
124         """
125         Dispatch line to current state handler.
126         """
127         for state in self.HTTP_STATES:
128             if getattr(self, 'HTTP_%s' % state) == self.state:
129                 print self.state, line
130                 getattr(self, 'handle%s' % state.title())(line)
131                 break
132         else:
133             raise HttpParseError('Unknown HTTP state')
134                 
135     def handleNone(self, line):
136         """
137         Pass line to next state.
138         """
139         self.state += 1
140         self.handle(line)
141
142     def handleRequest(self, line):
143         """
144         Retrieve HTTP method, request path and HTTP version from request.
145         """
146         self.method, self.request_path, self.http_version = line.split(' ')
147         self.state += 1
148
149     def handleHeaders(self, line):
150         """
151         Parse headers while not found an empty line.
152         """
153         if line:
154             key, value = line.split(': ')
155             self.headers[key] = value
156         else:
157             self.state += 1
158             self.handle(line)
159
160     def handleEmpty(self, line):
161         """
162         Empty line separator is found - proceed to next state.
163         """
164         self.state += 1
165
166     def handleMessage(self, line):
167         """
168         Append to message body.
169         """
170         self.data = dict(pair.split('=', 2) for pair in line.split('&'))
171
172         for k, v in self.data.items():
173             print k, '=>', v
174         print
175
176
177
178 class FreeswitchConfigGenerator(object):
179     """
180     Base class for generating XML configs.
181     """
182     
183     param_match = {}
184     section_name = None
185
186     def __init__(self, model):
187         self.model = model
188
189     def check_params(self, params):
190         for key, value in self.param_match.iteritems():
191             if params.get(key, None) != value:
192                 return False
193         else:
194             return True
195
196     def base_elements(self):
197         root_elt = etree.Element('document')
198         section_elt = etree.SubElement(
199             root_elt, 'section', name=self.section_name)
200         return root_elt, section_elt
201     base_elements = property(base_elements)
202
203     def generate_config(self, params):
204         return NotImplemented
205
206     def add_params(parent_elt, params):
207         for name, value in params:
208             etree.SubElement(parent_elt, 'param', name=name, value=value)
209             
210         
211 class SofiaConfGenerator(FreeswitchConfigGenerator):
212     """
213     Generates sofia.conf.xml config file.
214     """
215     param_match = {'section': 'configuration', 'key_value': 'sofia.conf'}
216     section_name = 'configuration'
217     config_name = 'sofia.conf'
218
219     def generate_config(self, params):
220         # Get base elements.
221         root_elt, section_elt = self.base_elements
222
223         # Create configuration, settings and profiles elements.
224         configuration_elt = etree.SubElement(
225             section_elt, 'configuration', name=self.config_name,
226             description='%s config' % self.config_name)
227         settings_elt = etree.SubElement(configuration_elt, 'settings')
228         profiles_elt = etree.SubElement(self.settings_elt, 'profiles')
229
230         # Create all profiles for current host.
231         for profile in self.parent.get_profiles():
232             profile_elt = etree.SubElement(profiles_elt, 'profile')
233
234             # Create domains for current profile.
235             domains_elt = etree.SubElement(profile_elt, 'domains')
236             for domain in self.parent.get_domains_for_profile(profile):
237                 domain_elt = etree.SubElement(
238                     domains_elt, 'domain', name=domain.host_name,
239                     alias='true', parse='true')
240
241             # Create settings for current profile.
242             settings_elt = etree.SubElement(profile_elt, 'settings')
243             params = (
244                 ('dialplan', 'XML,enum'),
245                 ('ext-sip-ip', profile.ext_sip_ip),
246                 ('ext-rtp-ip', profile.ext_rtp_ip),
247                 ('sip-ip', profile.sip_ip),
248                 ('rtp-ip', profile.rtp_ip),
249                 ('sip-port', profile.sip_port),
250                 ('nonce-ttl', '60'),
251                 ('rtp-timer-name', 'soft'),
252                 ('codec-prefs', 'PCMU@20i'),
253                 ('debug', '1'),
254                 ('rfc2833-pt', '1'),
255                 ('dtmf-duration', '100'),
256                 ('codec-ms', '20'),
257                 ('accept-blind-reg', profile.accept_blind_registration),
258                 ('auth-calls', profile.authenticate_calls))
259             self.add_params(settings_elt, params)
260
261             # Create gateways for current profile.
262             gateways_elt = etree.SubElement(profile, 'gateways')
263             for gateway in self.parent.get_gateways_for_profile(profile):
264                 gateway_elt = etree.SubElement(gateways_elt, 'gateway', name=gateway.name)
265                 params = (
266                     ('username', gateway.username),
267                     ('realm', gateway.realm),
268                     ('from-domain', gateway.from_domain),
269                     ('password', gateway.password),
270                     ('retry-seconds', gateway.retry_seconds),
271                     ('expire-seconds', gateway.expire_seconds),
272                     ('caller-id-in-from', gateway.caller_id_in_from),
273                     ('extension', gateway.extension),
274                     # TODO: proxy, register
275                     ('expire-seconds', gateway.expire_seconds),
276                     ('retry-seconds', gateway.retry_seconds))
277                 self.add_params(gateway_elt, params)
278
279         return root_elt