示例#1
0
文件: context.py 项目: tjguk/pyzo
    def __init__(self, verbose=0, queue_params=None):

        # Whether or not to write status information
        self._verbose = verbose

        # Store queue parameters
        if queue_params is None:
            queue_params = BUF_MAX_LEN, 'old'
        if not (isinstance(queue_params, tuple) and len(queue_params) == 2):
            raise ValueError('queue_params should be a 2-element tuple.')
        self._queue_params = queue_params

        # Create unique key to identify this context
        self._id = UID().get_int()

        # Channels currently registered. Maps slots to channel instance.
        self._sending_channels = {}
        self._receiving_channels = {}

        # The list of connections
        self._connections = []
        self._connections_lock = threading.RLock()

        # Queue used during startup to collect packages
        # This queue is also subject to the _connections_lock
        self._startupQueue = PackageQueue(*queue_params)

        # For numbering and routing the packages
        self._send_seq = 0
        self._recv_seq = 0
        self._source_map = {}
示例#2
0
    def shake_hands_as_host(self, id):
        """_shake_hands_as_host(id)

        As the host, we wait for the client to ask stuff, so when
        for example a http client connects, we can stop the connection.

        Returns (success, info), where info is the id of the context at
        the other end, or the error message in case success is False.

        """

        # Make our message with id and pid
        message = "YOTON!%s.%i" % (UID(id).get_hex(), os.getpid())

        # Get request
        request = self._recv_during_handshaking()

        if not request:
            return False, STOP_HANDSHAKE_TIMEOUT
        elif request.startswith("YOTON!"):
            # Get id
            try:
                tmp = request[6:].split(".", 1)  # partition not in Python24
                id2_str, pid2_str = tmp[0], tmp[1]
                id2, pid2 = int(id2_str, 16), int(pid2_str, 10)
            except Exception:
                self._send_during_handshaking("ERROR: could not parse id.")
                return False, STOP_HANDSHAKE_FAILED
            # Respond and return
            self._send_during_handshaking(message)  # returns error?
            if id == id2:
                return False, STOP_HANDSHAKE_SELF
            else:
                return True, (id2, pid2)
        else:
            # Client is not yoton
            self._send_during_handshaking("ERROR: this is Yoton.")
            return False, STOP_HANDSHAKE_FAILED
示例#3
0
    def shake_hands_as_client(self, id):
        """_shake_hands_as_client(id)

        As the client, we ask the host whether it is a Yoton context
        and whether the channels we want to support are all right.

        Returns (success, info), where info is the id of the context at
        the other end, or the error message in case success is False.

        """

        # Make our message with id and pif
        message = "YOTON!%s.%i" % (UID(id).get_hex(), os.getpid())

        # Do request
        self._send_during_handshaking(message)  # returns error?

        # Get response
        response = self._recv_during_handshaking()

        # Process
        if not response:
            return False, STOP_HANDSHAKE_TIMEOUT
        elif response.startswith("YOTON!"):
            # Get id
            try:
                tmp = response[6:].split(".", 1)  # Partition not in Python24
                id2_str, pid2_str = tmp[0], tmp[1]
                id2, pid2 = int(id2_str, 16), int(pid2_str, 10)
            except Exception:
                return False, STOP_HANDSHAKE_FAILED
            if id == id2:
                return False, STOP_HANDSHAKE_SELF
            else:
                return True, (id2, pid2)
        else:
            return False, STOP_HANDSHAKE_FAILED