Example #1
0
 def _send_command(self, command):
     socket = JSONSocket(
         self._server_address,
         self._server_port
     )
     try:
         socket.send_dict(
             CommandTranscoder.encode_command(
                 command
             )
         )
     except Exception:
         return False
     return True
Example #2
0
    def _receive_result(self):
        socket = JSONSocket(
            self._server_address,
            self._server_port + 1
        )
        try:
            dictionary = socket.receive_dict()
        except Exception as e:
            print(e)
            return None

        if "_type" in dictionary.keys():
            if dictionary["_type"] in CommandTranscoder.type_class_map.keys():
                response = CommandTranscoder.decode_command(dictionary)
                return response
        return dictionary
Example #3
0
    def _work_receive(self):
        try:
            self.socket = JSONSocket(self._address, self._port)
            message = self.socket.receive_dict()
        except Exception:
            return

        in_free.acquire()
        in_mutex.acquire()
        self._queue.append(message)
        in_mutex.release()
        in_full.release()
Example #4
0
    def _work_send(self):
        self.socket = JSONSocket(self._address, self._port)

        out_full.acquire()
        out_mutex.acquire()
        message = self._queue.pop(0)
        out_mutex.release()
        out_free.release()

        try:
            self.socket.send_dict(message)
        except Exception:
            out_free.acquire()
            out_mutex.acquire()
            self._queue.insert(0, message)
            out_mutex.release()
            out_full.release()
Example #5
0
class QueueWorker(Thread):
    """
    A thread which operates on networking message queues
    via JSONSocket in two different modes:

    1. receive
    The worker thread will accept JSONSocket connections,
    receive dictionaries, creates command objects and inserts them
    into the appropriate queue (in_queue)

    2. send
    The worker thread will wait for command objects to appear inside
    the outgoing queue (out_queue), convert them to dictionaries
    and send them to the client via a JSONSocket.
    """

    def __init__(
            self,
            queue: List[dict],
            mode: str,
            address: str,
            port: int
    ):
        self._queue = queue
        self._mode = mode
        self._address = address
        self._port = port

        super().__init__()
        self.start()

    def run(self):
        while True:
            if thread_should_end_mutex.acquire(timeout=1000):  # TODO: put socket in timeout mode
                if thread_should_end:
                    return
                thread_should_end_mutex.release()
            if self._mode == "receive":
                self._work_receive()
            else:
                self._work_send()

    def _work_receive(self):
        try:
            self.socket = JSONSocket(self._address, self._port)
            message = self.socket.receive_dict()
        except Exception:
            return

        in_free.acquire()
        in_mutex.acquire()
        self._queue.append(message)
        in_mutex.release()
        in_full.release()

    def _work_send(self):
        self.socket = JSONSocket(self._address, self._port)

        out_full.acquire()
        out_mutex.acquire()
        message = self._queue.pop(0)
        out_mutex.release()
        out_free.release()

        try:
            self.socket.send_dict(message)
        except Exception:
            out_free.acquire()
            out_mutex.acquire()
            self._queue.insert(0, message)
            out_mutex.release()
            out_full.release()