def _process_messages(self): """ Message Processor: - processes incoming messages from CarTalker - creates collision objects and adds them to collisions """ while not self._kill: message = self._talker.get_message() if message is None: time.sleep(utils.THREAD_SLEEP) continue if message.other_name not in self._collisions: if message.type != Message.ROW: print "ERROR: Got response for nonexistant collision" else: print "NEW MESSAGE RECEIVED" message.my_name = self.name c = Collision(message.other_name, message.frame_num, message.location, message=message) c.state = CollisionState.RECEIVED_MESSAGE self._collisions[message.other_name] = c else: collision = self._collisions[message.other_name] collision.lock.acquire() if message.type == Message.ROW: if collision.state == CollisionState.WAITING: # we are already waiting, so just send a confirmation that other has ROW print "ALREADY WAITING" self._talker.send_message( Message(Message.GO, self.name, message.other_name, collision.location, collision.frame_num)) elif collision.state == CollisionState.RESOLVED: # we are already going, so send a stay message print "ALREADY RESOLVED" self._talker.send_message( Message(Message.STAY, self.name, message.other_name, collision.location, collision.frame_num)) elif collision.state == CollisionState.NEW: # if we haven't already sent a message, set collision to received in interim collision.state = CollisionState.RECEIVED_MESSAGE message.my_name = self.name collision.message = message print "Got message about unprocessed collision" elif collision.state == CollisionState.SENT_MESSAGE: # if collision.message.type == Message.ROW: print "ALREADY SENT ROW... RESOLVING CONFLICT" # compare the ROW we sent and the one we received if collision.priority_val > message.priority_val: # we win self._talker.send_message( Message(Message.STAY, self.name, message.other_name, collision.location, collision.frame_num)) collision.state = CollisionState.RESOLVED print "RESOLVED DUPLICATED ROW... WE GO" elif collision.priority_val < message.priority_val: # we lose self._talker.send_message( Message(Message.GO, self.name, message.other_name, collision.location, collision.frame_num)) collision.state = CollisionState.WAITING print "RESOLVED DUPLICATED ROW... WE WAIT" else: # tie, restart process by sending ROW self._talker.send_message( Message(Message.ROW, self.name, message.other_name, collision.location, collision.frame_num)) collision.state = CollisionState.SENT_MESSAGE print "TIED DUPLICATED ROW... RESEND" collision.message = None else: if collision.state == CollisionState.SENT_MESSAGE: if message.type == Message.STAY: collision.state = CollisionState.WAITING collision.message = None print "WE STAY" else: collision.state = CollisionState.RESOLVED collision.message = None print "RESOLVED... WE GO" else: print "ERROR: got response without having sent a message" collision.lock.release() time.sleep(utils.THREAD_SLEEP)
def _process_messages(self): """ Message Processor: - processes incoming messages from CarTalker - creates collision objects and adds them to collisions """ while not self._kill: message = self._talker.get_message() if message is None: time.sleep(utils.THREAD_SLEEP) continue if message.other_name not in self._collisions: if message.type != Message.ROW: print "ERROR: Got response for nonexistant collision" else: print "NEW MESSAGE RECEIVED" message.my_name = self.name c = Collision(message.other_name, message.frame_num, message.location, message=message) c.state = CollisionState.RECEIVED_MESSAGE self._collisions[message.other_name] = c else: collision = self._collisions[message.other_name] collision.lock.acquire() if message.type == Message.ROW: if collision.state == CollisionState.WAITING: # we are already waiting, so just send a confirmation that other has ROW print "ALREADY WAITING" self._talker.send_message(Message(Message.GO, self.name, message.other_name, collision.location, collision.frame_num)) elif collision.state == CollisionState.RESOLVED: # we are already going, so send a stay message print "ALREADY RESOLVED" self._talker.send_message(Message(Message.STAY, self.name, message.other_name, collision.location, collision.frame_num)) elif collision.state == CollisionState.NEW: # if we haven't already sent a message, set collision to received in interim collision.state = CollisionState.RECEIVED_MESSAGE message.my_name = self.name collision.message = message print "Got message about unprocessed collision" elif collision.state == CollisionState.SENT_MESSAGE: # if collision.message.type == Message.ROW: print "ALREADY SENT ROW... RESOLVING CONFLICT" # compare the ROW we sent and the one we received if collision.priority_val > message.priority_val: # we win self._talker.send_message(Message(Message.STAY, self.name, message.other_name, collision.location, collision.frame_num)) collision.state = CollisionState.RESOLVED print "RESOLVED DUPLICATED ROW... WE GO" elif collision.priority_val < message.priority_val: # we lose self._talker.send_message(Message(Message.GO, self.name, message.other_name, collision.location, collision.frame_num)) collision.state = CollisionState.WAITING print "RESOLVED DUPLICATED ROW... WE WAIT" else: # tie, restart process by sending ROW self._talker.send_message(Message(Message.ROW, self.name, message.other_name, collision.location, collision.frame_num)) collision.state = CollisionState.SENT_MESSAGE print "TIED DUPLICATED ROW... RESEND" collision.message = None else: if collision.state == CollisionState.SENT_MESSAGE: if message.type == Message.STAY: collision.state = CollisionState.WAITING collision.message = None print "WE STAY" else: collision.state = CollisionState.RESOLVED collision.message = None print "RESOLVED... WE GO" else: print "ERROR: got response without having sent a message" collision.lock.release() time.sleep(utils.THREAD_SLEEP)