def remove_connected(c_id, room): c_id = str(c_id) room = str(room) mem_client = memcache.Client() c_list = ConnectedClient.query(ConnectedClient.client_id == c_id and ConnectedClient.room == room).fetch() if len(c_list) > 0: c = c_list[0] while True: mem_client.gets(room, namespace="channels") old_list = ConnectedClient.query(ConnectedClient.room == room).fetch() if not old_list: old_list = list() if c in old_list: old_list.remove(c) if mem_client.cas(room, old_list, namespace="channels"): break else: break c.key.delete()
def add_connected(c_id, room, client_e, client_n): c_id = str(c_id) room = str(room) c_client = ConnectedClient() c_client.client_id = c_id c_client.room = room c_client.e = client_e c_client.n = client_n c_client.connected = True c_client.put() mem_client = memcache.Client() while True: old_list = mem_client.gets(room, namespace="channels") if not old_list: old_list = list() old_list = ConnectedClient.query(ConnectedClient.room == room).fetch() mem_client.set(room, old_list, namespace="channels") old_list.append(c_client) if mem_client.cas(room, old_list, namespace="channels"): break
def post(self): user = self.request.get('from') client = ConnectedClient.query(user == ConnectedClient.client_id).fetch() if len(client) > 0: client[0].connected = True client[0].put()
def get_clients(room): client_list = memcache.get(room, namespace="channels", for_cas=True) if not client_list: client_list = ConnectedClient.query(ConnectedClient.room == room).fetch() return client_list