Exemple #1
0
    def client(self):

        LOGGER.debug('connecting to server %s:%s', self._host, self._port)
        try:
            stream = yield tcpclient.TCPClient().connect(self._host, self._port)
        except socket.error:
            LOGGER.error('failed to connect')
            self.close()
            return

        while not stream.closed():
            try:
                res = yield stream.read_bytes(NUM_START_BYTES)
            except iostream.StreamClosedError:
                self.close()
                return

            tag, length = struct.unpack('>BB', res)

            if length > 0:
                try:
                    value = yield stream.read_bytes(length)
                    r, g, b = struct.unpack('BBB', value)
                except iostream.StreamClosedError:
                    LOGGER.warning('connection closed in the middle of command')
                    self.close()
                    return
                value = (r, g, b)
            else:
                value = None

            LOGGER.debug('got message %s %s %s', tag, length, repr(value))
            self.execute_command(tag, value)
            print(self.lamp.draw())
Exemple #2
0
 def handle_stream(self, stream, address):
     LOGGER.info('torch %s connected', address)
     for _ in range(20):
         msg = random.choice([
             struct.pack('>BB', 18, 0),
             struct.pack('>BB', 19, 0),
             struct.pack('>BBBBB', 32, 3, random.randint(0, 250), random.randint(0, 250), random.randint(0, 250))
         ])
         try:
             stream.write(msg)
         except iostream.StreamClosedError:
             LOGGER.warning(
                 'connection with %s closed unexpectedly', address)
             return
         time.sleep(.5)
     LOGGER.info('closing connection with %s', address)