Beispiel #1
0
    def _request_to_server(sc, request_type, user_id):
        # is this request type available?
        request_types = messaging.recv_message(sc)
        if not request_type in request_types:
            raise BoxServerNoBoardException(
                f"{request_type} is no available request type; available types are {request_types}"
            )

        # send request
        request = [request_type, user_id]
        messaging.send_message(sc, request)

        # receive result
        return messaging.recv_message(sc)
Beispiel #2
0
 def send_command(self, command):
     if not command in self.board_commands:
         msg = f"{command} not in {self.board_commands}"
         print(msg)
         #raise Exception(msg)
         return
     messaging.send_message(self.sc, command)
     self.board_commands = messaging.recv_message(self.sc)
Beispiel #3
0
    def start(self):
        self.sc.connect((self.hostname, self.port))

        try:
            # select to request a board and select board type
            board_types = BoxClient._request_to_server(self.sc, "get_board",
                                                       self.user_id)
            # fix case insensitive board type
            board_types_match = list(
                filter(lambda x: x.lower() == self.board_type.lower(),
                       board_types))
            if len(board_types_match) == 1:
                self.board_type = board_types_match[0]
            if not self.board_type in board_types:
                raise BoxServerNoBoardException(
                    f"{self.board_type} is no available board type; available types are {board_types}"
                )
            messaging.send_message(self.sc, self.board_type)

            # select board
            board_ids = messaging.recv_message(self.sc)
            #print(board_ids)
            logging.info("requesting board")
            if self.box_name == None or self.board_name == None:
                messaging.send_message(self.sc, -1)
            else:
                board_id_choose = [self.box_name, self.board_name]
                if not board_id_choose in board_ids:
                    raise BoxServerNoBoardException(
                        f"board id {board_id_choose} is not available; available boards are {board_ids}"
                    )
                messaging.send_message(self.sc,
                                       board_ids.index(board_id_choose))

            # recieve claimed board
            (self.board_idx, self.board_id,
             msg) = messaging.recv_message(self.sc)
            if self.board_idx < 0:
                raise BoxServerNoBoardException(msg)

            self.board_commands = messaging.recv_message(self.sc)
        except:
            self.sc.close()
            raise
Beispiel #4
0
	def socket_handle(self, sc, addr):
		try:
			with sc:
				logging.info(f"Connection established to {addr[0]}:{addr[1]}")
				#while True:
				#	messaging.send_message(sc, messaging.recv_message(sc))

				# 0a. send available request types
				request_types = list(self.request_handlers.keys())
				messaging.send_message(sc, request_types)

				# 0b. receive request type
				request = messaging.recv_message(sc)
				if not isinstance(request, list) and len(request) != 2:
					raise Exception("input error, request has to contain the request type and a user_id")
				request_type = request[0]
				user_id = request[1]
				if not request_type in request_types:
					raise Exception("input error, request type not available")

				self.request_handlers[request_type](sc, user_id)

		finally:
			logging.info(f"Connection lost with {addr[0]}:{addr[1]}")
Beispiel #5
0
	def socket_handle_get_board(self, sc, user_id):
		# 1. send available board types
		board_types = self.config.get_board_types()
		messaging.send_message(sc, board_types)

		# 2a. receive required board
		board_type = messaging.recv_message(sc)
		if not board_type in board_types:
			raise Exception("input error, requested board type is not available")

		# 2b. send available board_ids
		board_ids = set(self.config.get_boards(board_type))
		with self.lock: # for safety under the lock
			board_ids = board_ids - self.claimed_boards
		board_ids = list(board_ids)
		messaging.send_message(sc, board_ids)

		# 2c. take selection as "wish index" relative to the list of boards sent before, unless negative
		user_idx = messaging.recv_message(sc)
		if not isinstance(user_idx, int):
			raise Exception("input format error, board_index")
		if (user_idx >= 0) and (user_idx >= len(board_ids)):
			raise Exception("input format error, board_index")

		# 3. claim one from the available boards and send its name
		board_id = -1
		try:
			# calim board
			try:
				board_id = self.claim_board(user_id, board_ids, user_idx)
			except BoardNotAvailableException:
				logging.warning(f"No {board_type} available")
				messaging.send_message(sc, [-1,board_id,"no board available"])
				return
			#initialize board
			try:
				self.init_board(board_id)
			except:
				logging.error(f"Error while initializing {board_id}")
				messaging.send_message(sc, [-2,board_id,"could not initialize board"])
				raise
			# claimed and initialize, inform the client
			messaging.send_message(sc, [self.config.get_board(board_id)['index'],board_id,"ok"])

			logging.info(f"Creating command handlers for board {board_id}")
			commands = {}
			if 'pin_reset' in self.config.get_board(board_id).keys():
				commands["stop"]  = lambda board_id=board_id: self.set_board_reset(board_id, True)
				commands["start"] = lambda board_id=board_id: self.set_board_reset(board_id, False)

			if "client_cmds" in self.config.get_board(board_id).keys():
				client_cmds = self.config.get_board(board_id)["client_cmds"]
				for client_cmd in client_cmds.keys():
					commands[client_cmd] = lambda board_id=board_id, client_cmd=client_cmd: self.exec_client_cmd(board_id, client_cmd)

			while True:
				# 4. send available commands
				messaging.send_message(sc, list(commands.keys()))

				# 5. receive selected command and act
				command = messaging.recv_message(sc)
				if not command in commands.keys():
					raise Exception("input error, requested command is not available")
				else:
					commands[command]()
		except messaging.SocketDiedException:
			pass
		except ConnectionResetError:
			pass
		finally:
			# always yield the board here
			self.yield_board(board_id)