def run(self): global init_received if not init_received: # First message we'll receive will always be init init_received = True init_message = bank_pb2.BranchMessage() # Accept the init message from controller init_message_from_socket = self.client_socket.recv( MAX_REQUEST_SIZE) if len(init_message_from_socket) == 0: # The controller just opened the connection but didn't send any message print( "ERROR! The controller didn't send anything as Init Message!" ) self.client_socket.close() return 0 init_message.ParseFromString(init_message_from_socket) # Connect to the list of branches got from init_branch message connect_to_branches(init_message.init_branch) # Start the thread to send money to these branches mt_thread = MoneyTransferThread(init_message.init_branch.balance) mt_thread.daemon = True mt_thread.start() # Start handling further incoming messages from controller self.start_handling_messages() # Else, we're connected to a branch # Let's get introduced. self.client_socket.send(branch_name) remote_branch_name = self.client_socket.recv(MAX_REQUEST_SIZE) self.remote_branch_name = remote_branch_name print "Connected to : " + str(remote_branch_name) # Add this thread object to the pool of threads such that it can be accessed whenever necessary ThreadPool.add_thread(self) # Start handling further incoming messages from connected branch self.start_handling_messages()