Ejemplo n.º 1
0
 def connect_callback(self, future):
     if not future.done():
         return
     if future.exception() is None:
         self.stream = future.result()
         self.is_connected = True
         logger.debug('client connected by callback')
         try:
             self.stream.read_until(b"\n", callback=self.parse_response)
         except Exception as ex:
             logger.debug(ex.message)
         self.ioloop.add_periodic(self.send_all)
         self.ioloop.add_periodic(self.callback)
         self.ioloop.add_periodic(self.subscribe)
         if self.connect_future._callbacks is None:
             self.connect_future._callbacks = []
         self.connect_future.set_result(True)
     else:
         print future.exception()
         if self.connect_retry_time > 0:
             self.connect_retry_time -= 1
             self.ioloop.add_future(TCPClient().connect(self.ip, self.port),
                                    self.connect_callback)
             self.set_timout(timeout=3)
         else:
             self.connect_future.set_result(False)
Ejemplo n.º 2
0
 def connect_callback(future):
     if not self.client.is_connected:
         logger.debug('connect failed and retry')
         self.client = None
         self.start_client()
     else:
         self.client.add_message(
             Version([
                 Parameter().ELECTRUM_VERSION,
                 Parameter().PROTOCOL_VERSION
             ]))
Ejemplo n.º 3
0
 def get_server(self):
     ip, port, _ = self.deserialize_server(self.pick_random_server())
     port = int(port)
     logger.debug('begin to connect to %s %d' % (ip, port))
     try:
         l = socket.getaddrinfo(ip, port, socket.AF_UNSPEC,
                                socket.SOCK_STREAM)
         ip, port = l[0][-1]
     except socket.gaierror:
         logger.debug('cannot resolve hostname')
     return ip, port
Ejemplo n.º 4
0
    def run(self):
        logger.debug('ioloop starting')

        def add_features():
            if not self._futures:
                pass
            else:
                need_add = self._futures[:]
                self._futures = []
                for each in need_add:
                    self.ioloop.add_future(each[0], each[1])

        PeriodicCallback(add_features, self.loop_interval, self.ioloop).start()
        self.ioloop.start()
Ejemplo n.º 5
0
 def connect(self):
     try:
         self.stream = yield TCPClient().connect(self.ip, self.port)
         self.is_connected = True
         self.stream.read_until(b"\n", callback=self.parse_response)
         logger.debug('client connected')
         self.ioloop.add_periodic(self.send_all)
         self.ioloop.add_periodic(self.callback)
         self.ioloop.add_periodic(self.subscribe)
     except StreamClosedError as ex:
         self.is_connected = False
     except Exception as ex:
         print ex
         self.is_connected = False