def __init__(self): self._runner_process = None self._control_process = None self._watchdog = ProcessWatchdog(config.Watchdog.CHECK_INTERVAL) self.push_messages_receiver = PushMessageReceiver() self.config_builder = ConfigurationBuilder( config.Engine.CONFIGURATION_BUILDER) self.message_handler = MessageHandler(self) self.message_sender = MessageSender() self.message_router = MessageRouter( self.message_sender, self.message_handler.default_message_handler) self.state = ManagerState.EMPTY self._http_client = httpclient.HTTPClient() self._alert_registered = False self.obsi_id = getnode() # A unique identifier of this OBSI self._engine_running = False self._engine_running_lock = locks.Lock() self._processing_graph_set = False self._engine_config_builder = None self._keep_alive_periodic_callback = None self._avg_cpu = 0 self._avg_duration = 0 self._supported_elements_types = [] self._alert_messages_handler = None self._log_messages_handler = None
def main(argv): client = MessageSender() client.connect_messaging_socket("18.214.123.134", 5050) client.send_message("test message") return
def __init__(self, address=("127.0.0.1", 7222), buffer_size=65536): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) registry = Registry() self._message_receiver = MessageReceiver( IncomingMessageTranslator( [registry, Broadcaster(registry, MessageSender(OutgoingMessageTranslator(), sock)) ]), address, buffer_size)
def main(): running = True message_queue = queue.Queue() print("Creating mqtt class..") mqtt_thread = MqttConnector() mqtt_thread.start() ## Start sensors temp_sensor = TemperatureSensor(message_queue) #ir_temp_sensor = IRTemperatureSensor(message_queue) #ir2_temp_sensor = IR2TemperatureSensor(message_queue) #gps_sensor = GpsSensor(message_queue) humidity_sensor = HumiditySensor(message_queue) pressure_sensor = PressureSensor(message_queue) orientation_sensor = OrientationSensor(message_queue) acceleration_sensor = AccelerationSensor(message_queue) imu_sensor = UDPReceiver(cfg.ip_imu, cfg.port_imu, message_queue, "IMU") seanav_sensor = UDPReceiver(cfg.ip_seanav, cfg.port_seanav, message_queue, "SEANAV") temp_sensor.start() #ir_temp_sensor.start() #ir2_temp_sensor.start() #gps_sensor.start() humidity_sensor.start() pressure_sensor.start() orientation_sensor.start() acceleration_sensor.start() imu_sensor.start() seanav_sensor.start() ## Connect to databases db_handler = DBHandler(local=True, cloud=True) ## Wait for connected to server while not mqtt_thread.connected: continue print("Connected...") print("Start message sender") sender = MessageSender(message_queue, mqtt_thread, db_handler) sender.start() ## Keep from stopping program while (running): continue print("Program end")
def connect_to_next_next_hop(self): ip, port = self.next_next_hop_info try: s = socket.create_connection((ip, port)) self.sending_queue = queue.Queue(maxsize=0) self.message_sender = MessageSender(self.event_queue, self.sending_queue, s) self.message_sender.start() self.next_hop_info = self.next_next_hop_info # After we connect to a new client we have to check whether the dead client wasn't in posession # of token self.sending_queue.put( events.TokenReceivedQuestionEvent(self.last_token)) except Exception as e: logger.error(e)
def inner_next_hop_broken_event(self, _): # If we detect that the next hop connection is down we want to: # 1.Try to reconnect to the client # 2.If reconnect fails we want to connect to our next next hop # 3.When we succesfully connect to our next next hop we want to send recovery token question # in case that the dead client was holding the token the moment he died ip, port = self.next_next_hop_info # If we are the only client left we reset the data to the initial state if ip == helpers.get_self_ip_address(): self.critical_section = None self.next_hop_info = None self.next_next_hop_info = None if self.message_sender: self.message_sender.stop() self.sending_queue = None self.message_sender = None self.predecessor = None self.last_token = 0 self.paint_queue.put({'type': DrawingQueueEvent.BOARD_OPEN}) return def connect_to_next_next_hop(self): ip, port = self.next_next_hop_info try: s = socket.create_connection((ip, port)) self.sending_queue = queue.Queue(maxsize=0) self.message_sender = MessageSender(self.event_queue, self.sending_queue, s) self.message_sender.start() self.next_hop_info = self.next_next_hop_info # After we connect to a new client we have to check whether the dead client wasn't in posession # of token self.sending_queue.put( events.TokenReceivedQuestionEvent(self.last_token)) except Exception as e: logger.error(e) ip, port = self.next_hop_info try: s = socket.create_connection((ip, port)) self.sending_queue = queue.Queue(maxsize=0) self.message_sender = MessageSender(self.event_queue, self.sending_queue, s) self.message_sender.start() except ConnectionRefusedError as e: logger.error(e) connect_to_next_next_hop(self)
def handle_new_client_request(self, event): # At first we want to receive information to properly connect as new predecessor after sending init_data message_size = event.data['connection'].recv(8) message_size = int.from_bytes(message_size, byteorder='big') message = b'' while len(message) < message_size: packet = event.data['connection'].recv(message_size - len(message)) if not packet: return None message += packet client_request = json.loads(message.decode('utf-8')) first_client = not self.next_hop_info # When we detect a new client connecting we want to; # 1.Send him the initial data over the connection we already established # 2.Connect to him as a predecessor # Gather the initial board state (only the coloured spots) marked_spots = [(x, y) for x in range(len(self.board_state)) for y in range(len(self.board_state[x])) if self.board_state[x][y] == 1] # If we have next hop information we send it, if we do not have we are the first client so we send our # information as the first hop information next_hop = (helpers.get_self_ip_address(), config.getint( 'NewPredecessorListener', 'Port')) if first_client else self.next_hop_info # If we are the first client next next hop is None response = events.NewClientResponseEvent(next_hop, self.next_next_hop_info, marked_spots, self.critical_section) message = helpers.event_to_message(response) message_size = (len(message)).to_bytes(8, byteorder='big') event.data['connection'].send(message_size) event.data['connection'].send(message) try: message = event.data['connection'].recv(8) except Exception as ex: if message == b'': # Only case when we have a succesfull read of 0 bytes is when other socket shutdowns normally pass else: logger.error(ex) #Client did not initializ correctly so we abort the process return # If we are not the first client we have to update our next next hop to our previous next hop if not first_client: self.next_next_hop_info = self.next_hop_info else: # If we are the first client we update our next next hop info to self address self.next_next_hop_info = (helpers.get_self_ip_address(), config.getint('NewPredecessorListener', 'Port')) # We stop current message sender if it exists if self.message_sender: self.message_sender.stop() # We update our next hop info with the newest client request self.next_hop_info = client_request['data']['address'] ip, port = self.next_hop_info # We establish a new connection and a new message sender connection = socket.create_connection((ip, port), 100) self.sending_queue = queue.Queue(maxsize=0) self.message_sender = MessageSender(self.event_queue, self.sending_queue, connection) self.message_sender.start() if first_client and self.last_token != None: # If we are the first client we start passing of the token self.sending_queue.put(events.TokenPassEvent(self.last_token))
def __init__(self, event_queue, paint_queue, time_offset, init_data=None, init_connection=None): super(ModelThread, self).__init__() # Queues self.event_queue = event_queue self.paint_queue = paint_queue #Event handling self.handlers = {} self.initialize_handlers() # Unique uuid identifying clients in the network self.uuid = uuid.uuid4().hex # Flag indicating weather we want to enter critical section when the token comes self.want_to_enter_critical_section = False # Information about critical section like the timestamp and client uuid which is in the section self.critical_section = None # Time offset between ntp server and local time self.time_offset = time_offset # Value of the last token we have received self.last_token = None # Initial board state self.board_state = [[ 0 for _ in range(config.getint('Tkinter', 'CanvasY')) ] for _ in range(config.getint('Tkinter', 'CanvasX'))] # If we are the first client if not init_data: self.next_hop_info = None self.next_next_hop_info = None self.sending_queue = None self.message_sender = None self.predecessor = None self.last_token = 0 else: self.next_hop_info = init_data['next_hop'] if not init_data['next_next_hop']: # If there is no next_next_hop init data in the response we are the second client so we set # next next hop as our address self.next_next_hop_info = (helpers.get_self_ip_address(), config.getint( 'NewPredecessorListener', 'Port')) else: # If there are more thant two clients we set the value from the response self.next_next_hop_info = init_data['next_next_hop'] # Address of our predecessor self.predecessor = init_connection.getsockname() # We initialize connection to our next hop and we start sending queue ip, port = init_data['next_hop'] s = socket.create_connection((ip, port)) self.sending_queue = queue.Queue(maxsize=0) self.message_sender = MessageSender(self.event_queue, self.sending_queue, s) self.message_sender.start() self.initialize_board(init_data['board_state']) if init_data['critical_section_state']: self.critical_section = init_data['critical_section_state'] self.paint_queue.put({'type': DrawingQueueEvent.BOARD_CLOSED}) # We signal that client has initialized properly init_connection.shutdown(1) init_connection.close() # We start a dummy message sender event which will create dummy messages to detect connection breaks self.dummy_message_sender = DummyMessageSender(self.event_queue, self.uuid) self.dummy_message_sender.start()
from message_sender import MessageSender if __name__ == '__main__': ms = MessageSender('config.ini') ms.test_message() # ms.send_messages()
def __init__(self, recipient_emails=()): self._read_offers_ids_from_db() self.message_sender = MessageSender(recipient_emails)