def connect_immediate(self, server: str, portstring: Union[str, int],
                          code: str) -> bool:
        port = get_port(portstring)
        proto = socket.getprotobyname("tcp")
        try:
            self.immsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                                         proto)
            self.immsock.connect((server, port))
        except socket.error as x:
            self.immsock.close()
            self.immsock = None
            log.error("ERROR creating/connecting immediate socket: " + str(x))
            return False
        log.info("Connected to immediate port " + str(port) + " on server " +
                 server)

        # Disable the Nagle algorithm:
        self.immsock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        self.immsock.setblocking(True)
        self.send_immediate("Link " + code)
        sleeptime = 0.1
        log.info("Sleeping for " + str(sleeptime) +
                 " seconds as the Nagle-disabling feature of Python "
                 "isn't working properly...")
        time.sleep(sleeptime)
        # the Nagle business isn't working; the Link packet is getting
        # amalgamated with anything the main calling program starts to send.
        # So pause.
        log.info("... continuing. Immediate socket should now be "
                 "correctly linked.")
        return True
    def connect_immediate(self, server: str, portstring: Union[str, int],
                          code: str) -> bool:
        port = get_port(portstring)
        proto = socket.getprotobyname("tcp")
        try:
            self.immsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                                         proto)
            self.immsock.connect((server, port))
        except socket.error as x:
            self.immsock.close()
            self.immsock = None
            log.error("ERROR creating/connecting immediate socket: " +
                      str(x))
            return False
        log.info("Connected to immediate port " + str(port) +
                 " on server " + server)

        # Disable the Nagle algorithm:
        self.immsock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        self.immsock.setblocking(True)
        self.send_immediate("Link " + code)
        sleeptime = 0.1
        log.info("Sleeping for " + str(sleeptime) +
                 " seconds as the Nagle-disabling feature of Python "
                 "isn't working properly...")
        time.sleep(sleeptime)
        # the Nagle business isn't working; the Link packet is getting
        # amalgamated with anything the main calling program starts to send.
        # So pause.
        log.info("... continuing. Immediate socket should now be "
                 "correctly linked.")
        return True
Ejemplo n.º 3
0
 def connect(self, server: str, port: Union[str, int]) -> None:
     self.server = server
     self.mainport = get_port(port)
     log.info(
         "Attempting to connect to Whisker server {s} on port {p}".format(
             s=self.server, p=self.mainport))
     # noinspection PyUnresolvedReferences
     reactor.connectTCP(self.server, self.mainport, self.mainfactory)
 def connect(self, server: str, port: Union[str, int]) -> None:
     self.server = server
     self.mainport = get_port(port)
     log.info(
         "Attempting to connect to Whisker server {s} on port {p}".format(
             s=self.server,
             p=self.mainport
         ))
     # noinspection PyUnresolvedReferences
     reactor.connectTCP(self.server, self.mainport, self.mainfactory)
    def connect_main(self, server: str, portstring: Union[str, int]) -> bool:
        """Connect the main port to the server."""
        log.info("Connecting main port to server.")
        port = get_port(portstring)
        proto = socket.getprotobyname("tcp")
        try:
            self.mainsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                                          proto)
            self.mainsock.connect((server, port))
        except socket.error as x:
            # "except socket.error, msg" used to work; see
            # http://stackoverflow.com/questions/2535760
            self.mainsock.close()
            self.mainsock = None
            log.error("ERROR creating/connecting main socket: " + str(x))
            return False
        log.info("Connected to main port " + str(port) +
                 " on server " + server)

        # Disable the Nagle algorithm:
        self.mainsock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        return True
    def connect_main(self, server: str, portstring: Union[str, int]) -> bool:
        """Connect the main port to the server."""
        log.info("Connecting main port to server.")
        port = get_port(portstring)
        proto = socket.getprotobyname("tcp")
        try:
            self.mainsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                                          proto)
            self.mainsock.connect((server, port))
        except socket.error as x:
            # "except socket.error, msg" used to work; see
            # http://stackoverflow.com/questions/2535760
            self.mainsock.close()
            self.mainsock = None
            log.error("ERROR creating/connecting main socket: " + str(x))
            return False
        log.info("Connected to main port " + str(port) + " on server " +
                 server)

        # Disable the Nagle algorithm:
        self.mainsock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        return True
Ejemplo n.º 7
0
    def incoming_message(self, msg: str) -> None:
        # log.debug("INCOMING MESSAGE: " + str(msg))
        handled = False
        if not self.immport:
            m = re.search(r"^ImmPort: (\d+)", msg)
            if m:
                self.immport = get_port(m.group(1))
                handled = True
        if not self.code:
            m = re.search(r"^Code: (\w+)", msg)
            if m:
                self.code = m.group(1)
                handled = True
        if (not self.immsocket) and (self.immport and self.code):
            self.connect_immediate()
        if handled:
            return

        (msg, timestamp) = split_timestamp(msg)

        if msg == "Ping":
            # If the server has sent us a Ping, acknowledge it.
            self.send("PingAcknowledged")
            return

        if msg.startswith(EVENT_PREFIX):
            # The server has sent us an event.
            event = msg[len(EVENT_PREFIX):]
            self.incoming_event(event, timestamp)
            return

        if msg.startswith(KEY_EVENT_PREFIX):
            kmsg = msg[len(KEY_EVENT_PREFIX):]
            # key on|off document
            m = re.match(r"(\w+)\s+(\w+)\s+(\w+)", kmsg)
            if m:
                key = m.group(1)
                depressed = on_off_to_boolean(m.group(2))
                document = m.group(3)
                self.incoming_key_event(key, depressed, document, timestamp)
            return

        if msg.startswith(CLIENT_MESSAGE_PREFIX):
            cmsg = msg[len(CLIENT_MESSAGE_PREFIX):]
            # fromclientnum message
            m = re.match(r"(\w+)\s+(.+)", cmsg)
            if m:
                try:
                    fromclientnum = int(m.group(1))
                    clientmsg = m.group(2)
                    self.incoming_client_message(fromclientnum, clientmsg,
                                                 timestamp)
                except (TypeError, ValueError):
                    pass
            return

        if msg.startswith(INFO_PREFIX):
            self.incoming_info(msg)
            return

        if msg.startswith(WARNING_PREFIX):
            self.incoming_warning(msg)
            return

        if msg.startswith(SYNTAX_ERROR_PREFIX):
            self.incoming_syntax_error(msg)
            return

        if msg.startswith(ERROR_PREFIX):
            self.incoming_error(msg)
            return

        log.debug("Unhandled incoming_message: " + str(msg))
    def incoming_message(self, msg: str) -> None:
        # log.debug("INCOMING MESSAGE: " + str(msg))
        handled = False
        if not self.immport:
            m = re.search(r"^ImmPort: (\d+)", msg)
            if m:
                self.immport = get_port(m.group(1))
                handled = True
        if not self.code:
            m = re.search(r"^Code: (\w+)", msg)
            if m:
                self.code = m.group(1)
                handled = True
        if (not self.immsocket) and (self.immport and self.code):
            self.connect_immediate()
        if handled:
            return

        (msg, timestamp) = split_timestamp(msg)

        if msg == "Ping":
            # If the server has sent us a Ping, acknowledge it.
            self.send("PingAcknowledged")
            return

        if msg.startswith(EVENT_PREFIX):
            # The server has sent us an event.
            event = msg[len(EVENT_PREFIX):]
            self.incoming_event(event, timestamp)
            return

        if msg.startswith(KEY_EVENT_PREFIX):
            kmsg = msg[len(KEY_EVENT_PREFIX):]
            # key on|off document
            m = re.match(r"(\w+)\s+(\w+)\s+(\w+)", kmsg)
            if m:
                key = m.group(1)
                depressed = on_off_to_boolean(m.group(2))
                document = m.group(3)
                self.incoming_key_event(key, depressed, document, timestamp)
            return

        if msg.startswith(CLIENT_MESSAGE_PREFIX):
            cmsg = msg[len(CLIENT_MESSAGE_PREFIX):]
            # fromclientnum message
            m = re.match(r"(\w+)\s+(.+)", cmsg)
            if m:
                try:
                    fromclientnum = int(m.group(1))
                    clientmsg = m.group(2)
                    self.incoming_client_message(fromclientnum, clientmsg,
                                                 timestamp)
                except (TypeError, ValueError):
                    pass
            return

        if msg.startswith(INFO_PREFIX):
            self.incoming_info(msg)
            return

        if msg.startswith(WARNING_PREFIX):
            self.incoming_warning(msg)
            return

        if msg.startswith(SYNTAX_ERROR_PREFIX):
            self.incoming_syntax_error(msg)
            return

        if msg.startswith(ERROR_PREFIX):
            self.incoming_error(msg)
            return

        log.debug("Unhandled incoming_message: " + str(msg))