Exemplo n.º 1
0
def keepAlive():
	while True:
		try:
			# Wait keepAlive period
			time.sleep(config.timeout / 2)
		except:
			break

		# Connect to master and ask for status
		print("{time:s} Send KeepAlive".format(time=Utilities.current_time_formatted()))
		try:
			conn = messages.Connection(socket.create_connection((config.parent_address, config.parent_port)))
			connections_manager.add(conn)
			msg = messages.Status(config.id, [{ "State": "Idle" }])
			conn.send(msg)
			# Inform parent about lower backup registrations and deregistrations
			for msg in backup_servers.getParentMessages():
				conn.send(msg)
			conn.socket.shutdown(socket.SHUT_WR)

			# Read all messages from client separated by 0x17 until EOC (write stream closed)
			for msg in conn:
				parseMessageBCS(msg)

			conn.socket.close()
			connections_manager.remove(conn)
		except OSError as msg:
			print("KeepAlive connection error: {:s}".format(str(msg)))
			print("Assuming main communication server role")
			# Refresh components timeout, else when assume main cs role all would be thought dead
			for component in components.active():
				component.touch()
			config.is_backup = False
			break
Exemplo n.º 2
0
def handleConnection(conn):
	print("{time:s} Connection from {server:s}:{port:d}".format(time=strftime("%H:%M:%S"), server=conn.socket.getpeername()[0], port=conn.socket.getpeername()[1]))
	try:
		parseMessages(conn)
	except AssertionError as e:
		print("Assertion error: {0}".format(e))
	finally:
		# Close connection
		conn.socket.shutdown(socket.SHUT_WR)
		conn.socket.close()
		connections_manager.remove(conn)
Exemplo n.º 3
0
def registerBackup(address, port, assumeId=None):
	print("{time:s} Connecting to {address:s}:{port:d}...".format(time=Utilities.current_time_formatted(), address=address, port=port))
	while True:
		try:
			conn = messages.Connection(connect((address, port), ('', args.port)))
			connections_manager.add(conn)
			break
		except Exception as msg:
			print("Trying to bind...")
			time.sleep(1)

	print("Send Register message")
	request = messages.Register(Type="CommunicationServer", SolvableProblems=[], ParallelThreads=0, Id=assumeId)
	conn.send(request)
	conn.socket.shutdown(socket.SHUT_WR)

	for msg in conn:
		if type(msg) == messages.RegisterResponse:
			# Has BackupCommunicationServers field -> connect to next backup in list
			if msg.BackupCommunicationServers:
				backup = msg.BackupCommunicationServers[0]
				print("Sent to next BCS at {address:s}:{port:d}".format(address=backup['address'], port=backup['port']))
				conn.socket.close()
				connections_manager.remove(conn)
				registerBackup(backup['address'], backup['port'], msg.Id)
				return
			# Registered at last communication server in list
			else:
				print("Registered successfully")
				config.id = msg.Id
				config.timeout = msg.Timeout
				config.parent_address = address
				config.parent_port = port
		# Messages with state, sent from higher communication server
		else:
			parseMessageBCS(msg)

	conn.socket.close()
	connections_manager.remove(conn)