annotate libervia.tac @ 0:140cec48224a

Initial commit
author Goffi <goffi@goffi.org>
date Sun, 30 Jan 2011 21:50:22 +0100
parents
children 0a7c685faa53
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
3
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
5 Libervia: a Salut à Toi frontend
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
7
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.application import internet, service
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import glib2reactor
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24 glib2reactor.install()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.internet import reactor, defer
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
26
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.web import server
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.web import error as weberror
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.web.static import File
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.web.resource import Resource
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from txjsonrpc.web import jsonrpc
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from txjsonrpc import jsonrpclib
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
35 TIMEOUT = 120 #Session's time out, after that the user will be disconnected
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 class MethodHandler(jsonrpc.JSONRPC):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 def __init__(self, sat_host):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41 jsonrpc.JSONRPC.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42 self.sat_host=sat_host
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
43
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def render(self, request):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
45 _session = request.getSession()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
46 try:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 profile = _session.sat_profile
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 except AttributeError:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49 #user is not identified, we return a jsonrpc fault
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
50 parsed = jsonrpclib.loads(request.content.read())
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
51 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53 return jsonrpc.JSONRPC.render(self, request)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def jsonrpc_getContacts(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """Return all passed args."""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
58 d = defer.Deferred()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59 reactor.callLater(10, d.callback, [unicode(contact[0]) for contact in self.sat_host.bridge.getContacts()])
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60 return d
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
61
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class Register(jsonrpc.JSONRPC):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """This class manage the registration procedure with SàT
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 It provide an api for the browser, check password and setup the web server"""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
65
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def __init__(self, sat_host):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67 jsonrpc.JSONRPC.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.sat_host=sat_host
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.profiles_waiting={}
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.request=None
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
72 def getWaitingRequest(self, profile):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """Tell if a profile is trying to log in"""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
74 if self.profiles_waiting.has_key(profile):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
75 return self.profiles_waiting[profile]
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
76 else:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
77 return None
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def render(self, request):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81 Render method with some hacks:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 - if login is requested, try to login with form data
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83 - except login, every method is jsonrpc
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 - user doesn't need to be authentified for isRegistered, but must be for all other methods
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if request.postpath==['login']:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
87 return self.login(request)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
88 _session = request.getSession()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89 parsed = jsonrpclib.loads(request.content.read())
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 if parsed.get("method")!="isRegistered":
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 #if we don't call login or isRegistered, we need to be identified
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92 try:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
93 profile = _session.sat_profile
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
94 except AttributeError:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
95 #user is not identified, we return a jsonrpc fault
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
96 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98 self.request = request
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return jsonrpc.JSONRPC.render(self, request)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def login(self, request):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
103 this method is called with the POST information from the registering form
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
104 it test if the password is ok, and log in if it's the case,
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
105 else it return an error
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @param request: request of the register formulaire, must have "login" and "password" as arguments
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
107 @return: A constant indicating the state:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
108 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
109 - AUTH ERROR: either the profile or the password is wrong
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
110 - ALREADY WAITING: a request has already be made for this profile
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
111 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
113 try:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
114 _login = request.args['login'][0]
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115 if _login.startswith('@'):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
116 raise Exception('No profile_key allowed')
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117 _pass = request.args['password'][0]
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118 except KeyError:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119 return "BAD REQUEST"
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121 _profile_check = self.sat_host.bridge.getProfileName(_login)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122 _profile_pass = self.sat_host.bridge.getParamA("Password", "Connection", profile_key=_login)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if not _profile_check or _profile_check != _login or _profile_pass != _pass:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return "AUTH ERROR"
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127 if self.profiles_waiting.has_key(_login):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
128 return "ALREADY WAITING"
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
129
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if self.sat_host.bridge.isConnected(_login):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
131 return self._logged(_login, request)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
132
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
133 self.profiles_waiting[_login] = request
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
134 self.sat_host.bridge.connect(_login)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
135 return server.NOT_DONE_YET
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
136
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
137 def __cleanWaiting(self, login):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
138 """Remove login from waiting queue"""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
139 try:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
140 del self.profiles_waiting[login]
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
141 except KeyError:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 pass
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 def _logged(self, login, request):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
145 """Set everything when a user just logged
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
146 and return "LOGGED" to the requester"""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
147 self.__cleanWaiting(login)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 _session = request.getSession()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149 _session.sat_profile = login
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return 'LOGGED'
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
152 def _logginError(self, login, request, error_type):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 """Something went wrong during loggin, return an error"""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 self.__cleanWaiting(login)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return error_type
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
156
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def jsonrpc_isConnected(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
158 _session = self.request.getSession()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
159 profile = _session.sat_profile
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
160 return self.sat_host.bridge.isConnected(profile)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
161
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
162 def jsonrpc_connect(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
163 _session = self.request.getSession()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164 profile = _session.sat_profile
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if self.profiles_waiting.has_key(profile):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
166 raise jsonrpclib.Fault('1','Already waiting') #FIXME: define some standard error codes for libervia
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
167 self.profiles_waiting[profile] = self.request
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.sat_host.bridge.connect(profile)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
169 return server.NOT_DONE_YET
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
170
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
171 def jsonrpc_isRegistered(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
172 """Tell if the user is already registered"""
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
173 _session = self.request.getSession()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
174 try:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
175 profile = _session.sat_profile
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
176 except AttributeError:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
177 return False
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
178 return True
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
179
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
180 class SignalHandler(jsonrpc.JSONRPC):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
181
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
182 def __init__(self, sat_host):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
183 Resource.__init__(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184 self.register=None
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185 self.sat_host=sat_host
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def plugRegister(self, register):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
188 self.register = register
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
189
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
190 def presenceUpdate(self, entity, show, priority, statuses, profile):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
191 print "update de",entity
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
192
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
193 def connected(self, profile):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
194 assert(self.register) #register must be plugged
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195 request = self.register.getWaitingRequest(profile)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
196 if request:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
197 self.register._logged(profile, request)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
198
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
199 def connectionError(self, error_type, profile):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
200 assert(self.register) #register must be plugged
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
201 request = self.register.getWaitingRequest(profile)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
202 if request: #The user is trying to log in
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
203 if error_type == "AUTH_ERROR":
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
204 _error_t = "AUTH ERROR"
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
205 else:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
206 _error_t = "UNKNOWN"
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
207 self.register._logginError(profile, request, _error_t)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
208
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
209
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
210 class Libervia(service.Service):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
211
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
212 def __init__(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
213 root = File("output/")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
214 self.signal_handler = SignalHandler(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
215 root.putChild('test', self.signal_handler)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
216 _register = Register(self)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
217 self.signal_handler.plugRegister(_register)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
218 root.putChild('json_api', MethodHandler(self))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
219 root.putChild('register_api', _register)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
220 self.site = server.Site(root)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
221 self.sessions = {} #key = session value = user
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
222 ## bridge ##
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
223 try:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
224 self.bridge=DBusBridgeFrontend()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
225 except BridgeExceptionNoService:
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
226 print(u"Can't connect to SàT backend, are you sure it's launched ?")
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
227 import sys
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
228 sys.exit(1)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
229 self.bridge.register("presenceUpdate", self.signal_handler.presenceUpdate)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
230 self.bridge.register("connected", self.signal_handler.connected)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
231 self.bridge.register("connectionError", self.signal_handler.connectionError)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
232
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def startService(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
234 reactor.listenTCP(8080, self.site)
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
235
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
236 def run(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
237 debug(_("running app"))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
238 reactor.run()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
239
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
240 def stop(self):
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
241 debug(_("stopping app"))
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
242 reactor.stop()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
243
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
244
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
245
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
246 application = service.Application('Libervia')
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
247 service = Libervia()
140cec48224a Initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
248 service.setServiceParent(application)