Mercurial > libervia-web
annotate libervia.tac @ 39:305e81c7a32c
Tarot game: a game can now be finished
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 22 May 2011 00:15:01 +0200 |
parents | 7bea2ae0c4fb |
children | 7782a786b2f0 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Libervia: a Salut à Toi frontend | |
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU Affero General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU Affero General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU Affero General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from twisted.application import internet, service | |
23 from twisted.internet import glib2reactor | |
24 glib2reactor.install() | |
25 from twisted.internet import reactor, defer | |
26 | |
27 from twisted.web import server | |
28 from twisted.web import error as weberror | |
29 from twisted.web.static import File | |
30 from twisted.web.resource import Resource | |
10 | 31 from twisted.words.protocols.jabber.jid import JID |
0 | 32 from txjsonrpc.web import jsonrpc |
33 from txjsonrpc import jsonrpclib | |
34 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
35 import re |
36 | 36 import glob |
37 import os.path | |
10 | 38 from server_side.blog import MicroBlog |
39 | |
0 | 40 TIMEOUT = 120 #Session's time out, after that the user will be disconnected |
36 | 41 LIBERVIA_DIR = "output/" |
42 CARDS_DIR = "cards/" | |
0 | 43 |
44 class MethodHandler(jsonrpc.JSONRPC): | |
45 | |
46 def __init__(self, sat_host): | |
47 jsonrpc.JSONRPC.__init__(self) | |
48 self.sat_host=sat_host | |
49 | |
50 def render(self, request): | |
1 | 51 self.session = request.getSession() |
0 | 52 try: |
1 | 53 profile = self.session.sat_profile |
0 | 54 except AttributeError: |
55 #user is not identified, we return a jsonrpc fault | |
56 parsed = jsonrpclib.loads(request.content.read()) | |
57 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
58 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
59 return jsonrpc.JSONRPC.render(self, request) | |
19 | 60 |
61 def jsonrpc_getProfileJid(self): | |
62 """Return the jid of the profile""" | |
63 profile = self.session.sat_profile | |
64 self.session.sat_jid = self.sat_host.bridge.getParamA("JabberID", "Connection", profile_key=profile) | |
65 return self.session.sat_jid | |
0 | 66 |
67 def jsonrpc_getContacts(self): | |
68 """Return all passed args.""" | |
1 | 69 profile = self.session.sat_profile |
70 return self.sat_host.bridge.getContacts(profile) | |
20 | 71 |
72 def jsonrpc_setStatus(self, status): | |
73 """Change the status""" | |
74 profile = self.session.sat_profile | |
75 print "new status received:", status | |
76 self.sat_host.bridge.setPresence('', '', 0, {'':status}, profile) | |
77 | |
19 | 78 |
79 def jsonrpc_sendMessage(self, to_jid, msg, subject, type): | |
80 """send message""" | |
81 profile = self.session.sat_profile | |
82 return self.sat_host.bridge.sendMessage(to_jid, msg, subject, type, profile) | |
0 | 83 |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
84 def jsonrpc_sendMblog(self, raw_text): |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
85 """Parse raw_text of the microblog box, and send message consequently""" |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
86 profile = self.session.sat_profile |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
87 match = re.match(r'@(.+?): *(.*$)', raw_text) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
88 if match: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
89 recip = match.group(1) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
90 text = match.group(2) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
91 if recip == '@' and text: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
92 #This text if for the public microblog |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
93 print "Sending message to everybody" |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
94 return self.sat_host.bridge.sendPersonalEvent("MICROBLOG", {'content':text}, profile) |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
95 else: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
96 return self.sat_host.bridge.sendGroupBlog([recip], text, profile) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
97 |
20 | 98 def jsonrpc_getPresenceStatus(self): |
99 """Get Presence information for connected contacts""" | |
100 profile = self.session.sat_profile | |
101 return self.sat_host.bridge.getPresenceStatus(profile) | |
102 | |
19 | 103 def jsonrpc_getHistory(self, from_jid, to_jid, size): |
104 """Return history for the from_jid/to_jid couple""" | |
105 #FIXME: this method should definitely be asynchrone, need to fix it !!! | |
106 profile = self.session.sat_profile | |
107 try: | |
108 _jid = JID(self.session.sat_jid) | |
109 except: | |
110 error("No jid saved for this profile") | |
111 return {} | |
112 if JID(from_jid).userhost() != _jid.userhost() and JID(to_jid) != _jid.userhost(): | |
113 error("Trying to get history from a different jid, maybe a hack attempt ?") | |
114 return {} | |
24 | 115 return self.sat_host.bridge.getHistory(from_jid, to_jid, size) |
19 | 116 |
30
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
117 def jsonrpc_getRoomJoined(self): |
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
118 """Return list of room already joined by user""" |
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
119 profile = self.session.sat_profile |
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
120 return self.sat_host.bridge.getRoomJoined(profile) |
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
121 |
24 | 122 def jsonrpc_launchTarotGame(self, other_players): |
123 """Create a room, invite the other players and start a Tarot game""" | |
124 profile = self.session.sat_profile | |
125 self.sat_host.bridge.tarotGameLaunch(other_players, profile) | |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
126 |
36 | 127 def jsonrpc_getTarotCardsPaths(self): |
128 """Give the path of all the tarot cards""" | |
129 return map(lambda x: x[len(LIBERVIA_DIR):],glob.glob(os.path.join(LIBERVIA_DIR,CARDS_DIR,'*_*.png'))); | |
130 | |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
131 def jsonrpc_tarotGameReady(self, player, referee): |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
132 """Tell to the server that we are ready to start the game""" |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
133 profile = self.session.sat_profile |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
134 self.sat_host.bridge.tarotGameReady(player, referee) |
36 | 135 |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
136 def jsonrpc_tarotGameContratChoosed(self, player_nick, referee, contrat): |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
137 """Tell to the server that we are ready to start the game""" |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
138 profile = self.session.sat_profile |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
139 self.sat_host.bridge.tarotGameContratChoosed(player_nick, referee, contrat, profile) |
39
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
140 |
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
141 def jsonrpc_tarotGamePlayCards(self, player_nick, referee, cards): |
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
142 """Tell to the server that we are ready to start the game""" |
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
143 profile = self.session.sat_profile |
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
144 self.sat_host.bridge.tarotGamePlayCards(player_nick, referee, cards, profile) |
36 | 145 |
0 | 146 class Register(jsonrpc.JSONRPC): |
147 """This class manage the registration procedure with SàT | |
148 It provide an api for the browser, check password and setup the web server""" | |
149 | |
150 def __init__(self, sat_host): | |
151 jsonrpc.JSONRPC.__init__(self) | |
152 self.sat_host=sat_host | |
153 self.profiles_waiting={} | |
154 self.request=None | |
155 | |
156 def getWaitingRequest(self, profile): | |
157 """Tell if a profile is trying to log in""" | |
158 if self.profiles_waiting.has_key(profile): | |
159 return self.profiles_waiting[profile] | |
160 else: | |
161 return None | |
162 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
163 def _fillMblogNodes(self, result, session): |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
164 """Fill the microblog nodes association for this session""" |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
165 print "Filling session for %s with %s" % (session.sat_profile, result) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
166 session.sat_mblog_nodes = dict(result) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
167 |
0 | 168 def render(self, request): |
169 """ | |
170 Render method with some hacks: | |
171 - if login is requested, try to login with form data | |
172 - except login, every method is jsonrpc | |
173 - user doesn't need to be authentified for isRegistered, but must be for all other methods | |
174 """ | |
175 if request.postpath==['login']: | |
176 return self.login(request) | |
177 _session = request.getSession() | |
178 parsed = jsonrpclib.loads(request.content.read()) | |
179 if parsed.get("method")!="isRegistered": | |
180 #if we don't call login or isRegistered, we need to be identified | |
181 try: | |
182 profile = _session.sat_profile | |
183 except AttributeError: | |
184 #user is not identified, we return a jsonrpc fault | |
185 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
186 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
187 self.request = request | |
188 return jsonrpc.JSONRPC.render(self, request) | |
189 | |
190 def login(self, request): | |
191 """ | |
192 this method is called with the POST information from the registering form | |
193 it test if the password is ok, and log in if it's the case, | |
194 else it return an error | |
195 @param request: request of the register formulaire, must have "login" and "password" as arguments | |
196 @return: A constant indicating the state: | |
197 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login) | |
198 - AUTH ERROR: either the profile or the password is wrong | |
199 - ALREADY WAITING: a request has already be made for this profile | |
200 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError | |
201 """ | |
202 try: | |
203 _login = request.args['login'][0] | |
204 if _login.startswith('@'): | |
205 raise Exception('No profile_key allowed') | |
206 _pass = request.args['password'][0] | |
207 except KeyError: | |
208 return "BAD REQUEST" | |
209 | |
210 _profile_check = self.sat_host.bridge.getProfileName(_login) | |
211 _profile_pass = self.sat_host.bridge.getParamA("Password", "Connection", profile_key=_login) | |
212 | |
213 if not _profile_check or _profile_check != _login or _profile_pass != _pass: | |
214 return "AUTH ERROR" | |
215 | |
216 if self.profiles_waiting.has_key(_login): | |
217 return "ALREADY WAITING" | |
218 | |
219 if self.sat_host.bridge.isConnected(_login): | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
220 return self._logged(_login, request, finish=False) |
0 | 221 |
222 self.profiles_waiting[_login] = request | |
223 self.sat_host.bridge.connect(_login) | |
224 return server.NOT_DONE_YET | |
225 | |
226 def __cleanWaiting(self, login): | |
227 """Remove login from waiting queue""" | |
228 try: | |
229 del self.profiles_waiting[login] | |
230 except KeyError: | |
231 pass | |
232 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
233 def _logged(self, profile, request, finish=True): |
0 | 234 """Set everything when a user just logged |
235 and return "LOGGED" to the requester""" | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
236 self.__cleanWaiting(profile) |
0 | 237 _session = request.getSession() |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
238 _session.sat_profile = profile |
24 | 239 self.sat_host.prof_connected.add(profile) |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
240 d = defer.Deferred() |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
241 self.sat_host.bridge.getMblogNodes(profile, d.callback, d.errback) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
242 d.addCallback(self._fillMblogNodes, _session) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
243 if finish: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
244 request.write('LOGGED') |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
245 request.finish() |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
246 else: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
247 return "LOGGED" |
0 | 248 |
249 def _logginError(self, login, request, error_type): | |
250 """Something went wrong during loggin, return an error""" | |
251 self.__cleanWaiting(login) | |
252 return error_type | |
253 | |
254 def jsonrpc_isConnected(self): | |
255 _session = self.request.getSession() | |
256 profile = _session.sat_profile | |
257 return self.sat_host.bridge.isConnected(profile) | |
258 | |
259 def jsonrpc_connect(self): | |
260 _session = self.request.getSession() | |
261 profile = _session.sat_profile | |
262 if self.profiles_waiting.has_key(profile): | |
263 raise jsonrpclib.Fault('1','Already waiting') #FIXME: define some standard error codes for libervia | |
264 self.profiles_waiting[profile] = self.request | |
265 self.sat_host.bridge.connect(profile) | |
266 return server.NOT_DONE_YET | |
267 | |
268 def jsonrpc_isRegistered(self): | |
269 """Tell if the user is already registered""" | |
270 _session = self.request.getSession() | |
271 try: | |
272 profile = _session.sat_profile | |
273 except AttributeError: | |
274 return False | |
275 return True | |
276 | |
277 class SignalHandler(jsonrpc.JSONRPC): | |
278 | |
279 def __init__(self, sat_host): | |
280 Resource.__init__(self) | |
281 self.register=None | |
282 self.sat_host=sat_host | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
283 self.signalDeferred = {} |
24 | 284 self.queue = {} #XXX: gof: don't forgot to purge queue on session end |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
285 |
0 | 286 def plugRegister(self, register): |
287 self.register = register | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
288 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
289 def jsonrpc_getSignals(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
290 """Keep the connection alive until a signal is received, then send it |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
291 @return: (signal, *signal_args)""" |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
292 _session = self.request.getSession() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
293 profile = _session.sat_profile |
24 | 294 if profile in self.queue: #if we have signals to send in queue |
295 if self.queue[profile]: | |
296 return self.queue[profile].pop(0) | |
297 else: | |
298 #the queue is empty, we delete the profile from queue | |
299 del self.queue[profile] | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
300 self.signalDeferred[profile] = defer.Deferred() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
301 return self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
302 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
303 def getGenericCb(self, function_name): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
304 """Return a generic function which send all params to signalDeferred.callback |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
305 function must have profile as last argument""" |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
306 def genericCb(*args): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
307 profile = args[-1] |
24 | 308 if not profile in self.sat_host.prof_connected: |
309 return | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
310 if profile in self.signalDeferred: |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
311 self.signalDeferred[profile].callback((function_name,args[:-1])) |
24 | 312 del self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
313 else: |
24 | 314 if not self.queue.has_key(profile): |
315 self.queue[profile] = [] | |
316 self.queue[profile].append((function_name, args[:-1])) | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
317 return genericCb |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
318 |
0 | 319 def connected(self, profile): |
320 assert(self.register) #register must be plugged | |
321 request = self.register.getWaitingRequest(profile) | |
322 if request: | |
323 self.register._logged(profile, request) | |
324 | |
325 def connectionError(self, error_type, profile): | |
326 assert(self.register) #register must be plugged | |
327 request = self.register.getWaitingRequest(profile) | |
328 if request: #The user is trying to log in | |
329 if error_type == "AUTH_ERROR": | |
330 _error_t = "AUTH ERROR" | |
331 else: | |
332 _error_t = "UNKNOWN" | |
333 self.register._logginError(profile, request, _error_t) | |
334 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
335 def render(self, request): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
336 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
337 Render method wich reject access if user is not identified |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
338 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
339 _session = request.getSession() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
340 parsed = jsonrpclib.loads(request.content.read()) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
341 try: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
342 profile = _session.sat_profile |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
343 except AttributeError: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
344 #user is not identified, we return a jsonrpc fault |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
345 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
346 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
347 self.request = request |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
348 return jsonrpc.JSONRPC.render(self, request) |
0 | 349 |
10 | 350 |
0 | 351 class Libervia(service.Service): |
352 | |
353 def __init__(self): | |
36 | 354 root = File(LIBERVIA_DIR) |
0 | 355 self.signal_handler = SignalHandler(self) |
356 _register = Register(self) | |
357 self.signal_handler.plugRegister(_register) | |
358 self.sessions = {} #key = session value = user | |
24 | 359 self.prof_connected = set() #Profiles connected |
0 | 360 ## bridge ## |
361 try: | |
362 self.bridge=DBusBridgeFrontend() | |
363 except BridgeExceptionNoService: | |
364 print(u"Can't connect to SàT backend, are you sure it's launched ?") | |
365 import sys | |
366 sys.exit(1) | |
367 self.bridge.register("connected", self.signal_handler.connected) | |
368 self.bridge.register("connectionError", self.signal_handler.connectionError) | |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
369 for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage', 'roomJoined', 'roomUserJoined', 'roomUserLeft', 'tarotGameStarted', 'tarotGameNew', |
39
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
370 'tarotGameChooseContrat', 'tarotGameShowCards', 'tarotGameInvalidCards', 'tarotGameCardsPlayed', 'tarotGameYourTurn']: |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
371 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) |
10 | 372 root.putChild('json_signal_api', self.signal_handler) |
373 root.putChild('json_api', MethodHandler(self)) | |
374 root.putChild('register_api', _register) | |
375 root.putChild('blog', MicroBlog(self)) | |
376 root.putChild('css', File("server_css/")) | |
377 self.site = server.Site(root) | |
0 | 378 |
379 def startService(self): | |
380 reactor.listenTCP(8080, self.site) | |
1 | 381 |
0 | 382 def run(self): |
383 reactor.run() | |
384 | |
385 def stop(self): | |
386 reactor.stop() | |
387 | |
388 | |
389 | |
390 application = service.Application('Libervia') | |
391 service = Libervia() | |
392 service.setServiceParent(application) |