def add_client_to_room(request, room_id, client_id, is_loopback): key = get_memcache_key_for_room(request.host_url, room_id) memcache_client = memcache.Client() error = None retries = 0 room = None # Compare and set retry loop. while True: is_initiator = None messages = [] room_state = '' room = memcache_client.gets(key) if room is None: # 'set' and another 'gets' are needed for CAS to work. print "ppt, in add_client_to_room, go to memcache_client.set." if not memcache_client.set(key, Room()): logging.warning('memcache.Client.set failed for key ' + key) error = constants.RESPONSE_ERROR break room = memcache_client.gets(key) occupancy = room.get_occupancy() if occupancy >= 2: error = constants.RESPONSE_ROOM_FULL break if room.has_client(client_id): error = constants.RESPONSE_DUPLICATE_CLIENT break if occupancy == 0: is_initiator = True room.add_client(client_id, Client(is_initiator)) if is_loopback: room.add_client(constants.LOOPBACK_CLIENT_ID, Client(False)) else: is_initiator = False other_client = room.get_other_client(client_id) messages = other_client.messages room.add_client(client_id, Client(is_initiator)) other_client.clear_messages() if memcache_client.cas(key, room, constants.ROOM_MEMCACHE_EXPIRATION_SEC): logging.info('Added client %s in room %s, retries = %d' \ %(client_id, room_id, retries)) if room.get_occupancy() == 2: analytics.report_event(analytics.EventType.ROOM_SIZE_2, room_id, host=request.host) success = True break else: retries = retries + 1 return { 'error': error, 'is_initiator': is_initiator, 'messages': messages, 'room_state': str(room) }
def add_client_to_room(request, room_id, client_id, is_loopback): key = get_memcache_key_for_room(request.host_url, room_id) memcache_client = memcache.Client() error = None retries = 0 room = None # Compare and set retry loop. while True: is_initiator = None messages = [] room_state = '' room = memcache_client.gets(key) if room is None: # 'set' and another 'gets' are needed for CAS to work. if not memcache_client.set(key, Room()): logging.warning('memcache.Client.set failed for key ' + key) error = constants.RESPONSE_ERROR break room = memcache_client.gets(key) occupancy = room.get_occupancy() if occupancy >= 2: error = constants.RESPONSE_ROOM_FULL break if room.has_client(client_id): error = constants.RESPONSE_DUPLICATE_CLIENT break if occupancy == 0: is_initiator = True room.add_client(client_id, Client(is_initiator)) if is_loopback: room.add_client(constants.LOOPBACK_CLIENT_ID, Client(False)) else: is_initiator = False other_client = room.get_other_client(client_id) messages = other_client.messages room.add_client(client_id, Client(is_initiator)) other_client.clear_messages() if memcache_client.cas(key, room, constants.ROOM_MEMCACHE_EXPIRATION_SEC): logging.info('Added client %s in room %s, retries = %d' \ %(client_id, room_id, retries)) if room.get_occupancy() == 2: analytics.report_event(analytics.EventType.ROOM_SIZE_2, room_id, host=request.host) success = True break else: retries = retries + 1 return {'error': error, 'is_initiator': is_initiator, 'messages': messages, 'room_state': str(room)}
def testModule(self): event_type = "an_event_with_everything" room_id = "my_room_that_is_the_best" time_ms = 50 * 1000.0 client_time_ms = 60 * 1000.0 host = "super_host.domain.org:8112" analytics.report_event(event_type, room_id=room_id, time_ms=time_ms, client_time_ms=client_time_ms, host=host) kwargs = {"room_id": room_id, "time_ms": time_ms, "client_time_ms": client_time_ms, "host": host} self.assertEqual((event_type,), self.analytics_fake.report_event.last_args) self.assertEqual(kwargs, self.analytics_fake.report_event.last_kwargs)
def _handle_event(self, msg): request_time_ms = msg.get(RequestField.REQUEST_TIME_MS) client_type = msg.get(RequestField.CLIENT_TYPE) event = msg.get(RequestField.EVENT) if event is None: return constants.RESPONSE_INVALID_REQUEST event_type = event.get(RequestField.EventField.EVENT_TYPE) if event_type is None: return constants.RESPONSE_INVALID_REQUEST room_id = event.get(RequestField.EventField.ROOM_ID) flow_id = event.get(RequestField.EventField.FLOW_ID) # Time that the event occurred according to the client clock. try: client_event_time_ms = float(event.get( RequestField.EventField.EVENT_TIME_MS)) except (TypeError, ValueError): return constants.RESPONSE_INVALID_REQUEST # Time the request was sent based on the client clock. try: request_time_ms = float(request_time_ms) except (TypeError, ValueError): return constants.RESPONSE_INVALID_REQUEST # Server time at the time of request. receive_time_ms = self._time() * 1000. # Calculate event time as client event time adjusted to server # local time. Server clock offset is gived by the difference # between the time the client sent thes request and the time the # server received the request. This method ignores the latency of # sending the request to the server. event_time_ms = client_event_time_ms + (receive_time_ms - request_time_ms) analytics.report_event(event_type=event_type, room_id=room_id, time_ms=event_time_ms, client_time_ms=client_event_time_ms, host=self.request.host, flow_id=flow_id, client_type=client_type) return constants.RESPONSE_SUCCESS
def testModule(self): event_type = 'an_event_with_everything' room_id = 'my_room_that_is_the_best' time_ms = 50*1000. client_time_ms = 60*1000. host = 'super_host.domain.org:8112' analytics.report_event(event_type, room_id=room_id, time_ms=time_ms, client_time_ms=client_time_ms, host=host) kwargs = { 'room_id': room_id, 'time_ms': time_ms, 'client_time_ms': client_time_ms, 'host': host, } self.assertEqual((event_type,), self.analytics_fake.report_event.last_args) self.assertEqual(kwargs, self.analytics_fake.report_event.last_kwargs)
def add_client_to_room(request, room_id, client_id, is_loopback, room_type, allow_room_creation, allowed_clients=None): key = get_memcache_key_for_room(request.host_url, room_id) memcache_client = memcache.Client() error = None room = None # Compare and set retry loop. for retries in xrange(constants.ROOM_MEMCACHE_RETRY_LIMIT): is_initiator = None messages = [] room_state = '' room = memcache_client.gets(key) if room is None: if allow_room_creation: # 'set' and another 'gets' are needed for CAS to work. if not memcache_client.set(key, Room(room_type)): logging.warning('memcache.Client.set failed for key ' + key) error = constants.RESPONSE_INTERNAL_ERROR break room = memcache_client.gets(key) else: logging.warning( 'Room did not exist and room creation is not ' + ' allowed. room_id: ' + room_id + ' client: ' + client_id) error = constants.RESPONSE_INVALID_ROOM break occupancy = room.get_occupancy() if occupancy >= 2: error = constants.RESPONSE_ROOM_FULL break if room.has_client(client_id): error = constants.RESPONSE_DUPLICATE_CLIENT break if room.room_type != room_type: logging.warning('Room type did not match while adding client ' + client_id + ' to room ' + room_id + ' room type is ' + str(room.room_type) + ' requested room type is ' + str(room_type)) error = constants.RESPONSE_INVALID_ROOM break if occupancy == 0: is_initiator = True room.add_client(client_id, client.Client(is_initiator)) if is_loopback: room.add_client(constants.LOOPBACK_CLIENT_ID, Client(False)) if allowed_clients is not None: for allowed_client in allowed_clients: room.add_allowed_client(allowed_client) else: is_initiator = False other_client = room.get_other_client(client_id) messages = other_client.messages room.add_client(client_id, client.Client(is_initiator)) other_client.clear_messages() # Check if the callee was the callee intended by the caller. if not room.is_client_allowed(client_id): logging.warning('Client ' + client_id + ' not allowed in room ' + room_id + ' allowed clients: ' + ','.join(room.allowed_clients)) error = constants.RESPONSE_INVALID_ROOM break if memcache_client.cas(key, room, constants.ROOM_MEMCACHE_EXPIRATION_SEC): logging.info('Added client %s in room %s, retries = %d' \ %(client_id, room_id, retries)) if room.get_occupancy() == 2: analytics.report_event(constants.EventType.ROOM_SIZE_2, room_id, host=request.host) success = True break return { 'error': error, 'is_initiator': is_initiator, 'messages': messages, 'room_state': str(room) }