Skip to content

Commit

Permalink
Python server fixes
Browse files Browse the repository at this point in the history
* Wasn't correctly supplying cowebkey when browser did not specify one.
* Did not correctly handle app sync events (change in channel format)
  • Loading branch information
ccotter committed Sep 26, 2012
1 parent e6eb7e5 commit 8c2945a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
28 changes: 13 additions & 15 deletions servers/python/coweb/admin.py
Expand Up @@ -11,12 +11,16 @@
# std lib
import json
import uuid
import hashlib
# coweb
import bayeux
import session

log = logging.getLogger('coweb.admin')

def generate_collab_key():
return hashlib.md5(uuid.uuid4().urn).hexdigest()

class AdminHandler(tornado.web.RequestHandler):
def prepare(self):
'''Hold a reference to the app container.'''
Expand All @@ -39,21 +43,15 @@ def post(self):

# decode params
args = json.loads(self.request.body)
try:
key = args['key']
except KeyError:
key = uuid.uuid4().hex
# no key given, abort
#log.warn('admin rejected user %s prep, missing key', username)
#raise tornado.web.HTTPError(400)
# get collab flag
collab = args.get('collab', True)
key = args.get('key', None)
collab = args.get('collab', None)
cacheState = args.get('cacheState', False)
try:
# check if there is a session for this key already
sessionId = self.application.get_session_id(key, collab, cacheState)
except KeyError:
sessionId = None

# Generate key if none given.
if key is None:
key = generate_collab_key()

sessionId = self.application.get_session_id(key, collab, cacheState)

# allow container to dictate session access
access = self._container.access
Expand Down Expand Up @@ -83,4 +81,4 @@ def post(self):
'info': sessionInfo,
'username' : username
}
self.write(resp)
self.write(resp)
8 changes: 4 additions & 4 deletions servers/python/coweb/application.py
Expand Up @@ -33,10 +33,10 @@ def add_session_handler(self, session, handler):
self._sessionIds[(session.key, session.collab, session.cacheState)] = session.sessionId
self._sessions[session.sessionId] = session

# Returns None if the sessionId doesn't exist.
def get_session_id(self, key, collab, cacheState):
'''Gets a session ID from the key, collab flag and cacheState flag.'''
return self._sessionIds[(key, collab, cacheState)]

return self._sessionIds.get((key, collab, cacheState), None)

def get_session_url(self, sessionId):
'''Gets a session URL from a session ID.'''
return self._container.webSessionRoot + sessionId
Expand Down Expand Up @@ -96,4 +96,4 @@ def remove_handler(self, host_pattern, url_pattern):
if handler.regex.pattern == url_pattern:
handlers.pop(i)
else:
i += 1
i += 1
39 changes: 22 additions & 17 deletions servers/python/coweb/session/collab.py
Expand Up @@ -11,9 +11,12 @@
import uuid
import logging
import random
import re
# coweb
import session

session_sync_regex = re.compile("/session/([A-z0-9]+)/sync(.*)");

log = logging.getLogger('coweb.session')

class CollabSession(session.Session):
Expand Down Expand Up @@ -318,23 +321,25 @@ def on_publish(self, cl, req, res):
# bad updater, disconnect and assign a new one
self._manager.remove_bad_client(cl)
return
elif channel.startswith('/session/sync/'):
# handle sync events
try:
# put siteId on message
req['data']['siteId'] = cl.siteId
self._manager.ensure_updater(cl)
except (KeyError, AttributeError):
# not allowed to publish syncs, disconnect
self._manager.remove_bad_client(cl)
return
# last state no longer valid
self._manager.clear_last_state()
if channel == '/session/sync/app':
# put total order sequence number on message
req['data']['order'] = self._manager.get_order()
# let manager deal with the sync if forwarding it to services
self._manager.sync_for_service(cl, req)
else:
matches = session_sync_regex.match(channel);
if matches:
# handle sync events
try:
# put siteId on message
req['data']['siteId'] = cl.siteId
self._manager.ensure_updater(cl)
except (KeyError, AttributeError):
# not allowed to publish syncs, disconnect
self._manager.remove_bad_client(cl)
return
# last state no longer valid
self._manager.clear_last_state()
if '/app' == matches.group(2):
# put total order sequence number on message
req['data']['order'] = self._manager.get_order()
# let manager deal with the sync if forwarding it to services
self._manager.sync_for_service(cl, req)
# delegate all other handling to base class
super(CollabSessionConnection, self).on_publish(cl, req, res)

Expand Down

0 comments on commit 8c2945a

Please sign in to comment.