def handle_add_frame_connection(self, frame_ws): """ Handles frame:connected event. Should add the new frame connection to the in-memory connected frame list. @param frame_ws - a reference to the websocket connection object """ print('FrameManager::handle_add_frame_connection: ' + frame_ws.frame_id) # mark frame "connected" in DB then attach the frame object to # the websocket connection object for easy access later frame_ws.frame = Frames.update_by_id(frame_ws.frame_id, {"connected": True}) frame_ws.frame = Frames.get_by_id(frame_ws.frame_id) # add the frame websocket connection to the in-memory list of frames self.frames[frame_ws.frame_id] = frame_ws # If the connected frame has current content set, send it along if 'current_content' in frame_ws.frame: # send content to frame if frame connected self.frames[frame_ws.frame_id].send( 'frame:update_content', frame_ws.frame['current_content'])
def get_by_frame_id(frame_id): """ Get a list of users which have access this frame """ frame = Frames.get_by_id(frame_id) users = Users.collection.find({'username': {'$in': frame['users']}}) return users
def get(self, frame_id=None): if frame_id: print('get frame: ' + frame_id) frame_resp = Frames.get_by_id(frame_id) else: frame_resp = Frames.get_all() _unify_ids(frame_resp) self.write(dumps(frame_resp))
def handle_mirror_frame(self, frame_id, mirrored_frame_id): """ Set a frame to mirror another frame. Returns the from which is mirroring the other. """ # if the mirroring frame was mirroring a different frame, # update the previous frame's mirror count previous_mirroring_id = None frame = Frames.get_by_id(frame_id) if 'mirroring' in frame and frame['mirroring'] is not None: previous_mirroring_id = frame['mirroring'] mirrored_frame = Frames.get_by_id(mirrored_frame_id) content = mirrored_frame['current_content'] doc = { 'mirroring': mirrored_frame_id, 'mirror_meta': { 'name': mirrored_frame['name'], 'owner': mirrored_frame['owner'] }, 'current_content': content } # update this frame to mirror mirrored_frame_id frame = Frames.update_by_id(frame_id, doc) # if set, update the previously mirrored frame if previous_mirroring_id: Frames.update_mirroring_count(previous_mirroring_id) self.pubsub.publish( 'frame:frame_updated', frame_id=previous_mirroring_id) # update the mirroring_count on the newly mirrored frame Frames.update_mirroring_count(mirrored_frame_id) self.pubsub.publish( 'frame:frame_updated', frame_id=mirrored_frame_id) # kick off the recursive content updates down the mirror graph self.update_mirroring_frames(frame, doc)
def update_admin_frame(self, frame=None, frame_id=None): print('AdminManager::update_admin_frame') print(frame) if frame_id is not None: frame = Frames.get_by_id(frame_id) # get users for this frame users = Users.get_by_frame_id(frame['_id']) # for each user, if the user is connected, send frame:updated event to # websocket client (i.e. to admin page) for user in users: user_id = user['_id'] if user_id in self.admins: for ws_uuid in self.admins[user_id]: self.admins[user_id][ws_uuid].send( 'frame:frame_updated', frame)
def remove_frame_connection(self, frame_ws): print('AdminManager::remove_frame_connection: ' + frame_ws.frame_id) # get frame object from db frame = Frames.get_by_id(frame_ws.frame_id) # get users for this frame users = Users.get_by_frame_id(frame_ws.frame_id) # for each user, if the user is connected, # send frame:disconnected to the # websocket client for user in users: user_id = user['_id'] if user_id in self.admins: for ws_uuid in self.admins[user_id]: self.admins[user_id][ws_uuid].send( 'frame:disconnected', frame)
def get(self, frame_id, username, framename): frame = Frames.get_by_id(frame_id) print(frame) if not frame: print("No frame, create it.") Frames.insert({ "_id": frame_id, "owner": username, "name": framename, "users": [ username ], "settings": { "visible": True } }) self.render("frame.html", frame_id=frame_id, port=options.port)