Esempio n. 1
0
 def __init__(self, server, s):
     threading.Thread.__init__(self)
     self.server = server
     self.daemon = True
     self.client_pipe = util.SocketPipe(s)
     self.response_queue = Queue.Queue()
     self.server.add_client(self)
Esempio n. 2
0
    def __init__(self, socket, config=None):

        if config is None:
            config = {}  # Do not use mutables as default arguments!
        threading.Thread.__init__(self)
        self.config = SimpleConfig(config) if type(config) == type({}) else config
        self.message_id = 0
        self.unanswered_requests = {}
        self.subscriptions = {}
        self.debug = False
        self.lock = threading.Lock()
        self.pending_transactions_for_notifications = []
        self.callbacks = {}
        self.running = True
        self.daemon = True

        if socket:
            self.pipe = util.SocketPipe(socket)
            self.network = None
        else:
            self.network = Network(config)
            self.pipe = util.QueuePipe(send_queue=self.network.requests_queue)
            self.network.start(self.pipe.get_queue)
            for key in ['status', 'banner', 'updated', 'servers', 'interfaces']:
                value = self.network.get_status_value(key)
                self.pipe.get_queue.put({'method': 'network.status', 'params': [key, value]})

        # status variables
        self.status = 'connecting'
        self.servers = {}
        self.banner = ''
        self.blockchain_height = 0
        self.server_height = 0
        self.interfaces = []
Esempio n. 3
0
 def __init__(self, server, s):
     util.DaemonThread.__init__(self)
     self.server = server
     self.client_pipe = util.SocketPipe(s)
     self.response_queue = Queue.Queue()
     self.server.add_client(self)
     self.subscriptions = set()
    def run(self):
        self.s = self.get_socket()
        if self.s:
            self.s.settimeout(60)
            self.is_connected = True
            print_error("connected to", self.host, self.port)
            self.pipe = util.SocketPipe(self.s)

        self.change_status()
        if not self.is_connected:
            return

        t = 0
        while self.is_connected:
            # ping the server with server.version
            if time.time() - t > 60:
                if self.is_ping:
                    print_error("ping timeout", self.server)
                    self.is_connected = False
                    break
                else:
                    self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
                    self.is_ping = True
                    t = time.time()
            try:
                response = self.pipe.get()
            except util.timeout:
                continue
            if response is None:
                self.is_connected = False
                break
            self.process_response(response)

        self.change_status()
        print_error("closing connection:", self.server)
Esempio n. 5
0
    def __init__(self, socket, config=None):

        if config is None:
            config = {}  # Do not use mutables as default arguments!
        util.DaemonThread.__init__(self)
        self.config = SimpleConfig(config) if type(config) == type({}) else config
        self.message_id = 0
        self.unanswered_requests = {}
        self.subscriptions = {}
        self.debug = False
        self.lock = threading.Lock()
        self.callbacks = {}

        if socket:
            self.pipe = util.SocketPipe(socket)
            self.network = None
        else:
            self.pipe = util.QueuePipe()
            self.network = Network(self.pipe, config)
            self.network.start()
            for key in ['fee','status','banner','updated','servers','interfaces']:
                value = self.network.get_status_value(key)
                self.pipe.get_queue.put({'method':'network.status', 'params':[key, value]})

        # status variables
        self.status = 'unknown'
        self.servers = {}
        self.banner = ''
        self.blockchain_height = 0
        self.server_height = 0
        self.interfaces = []
        # value returned by estimatefee
        self.fee = None
Esempio n. 6
0
 def __init__(self, server, s):
     threading.Thread.__init__(self)
     self.server = server
     self.daemon = True
     self.client_pipe = util.SocketPipe(s)
     self.daemon_pipe = util.QueuePipe(
         send_queue=self.server.network.requests_queue)
     self.server.add_client(self)
Esempio n. 7
0
    def __init__(self, server, s):
        """Create a new client connection.

        Args:
            server (NetworkServer): Server handling all connections.
            s (socket): Socket for the daemon connection.
        """
        util.DaemonThread.__init__(self)
        self.server = server
        self.client_pipe = util.SocketPipe(s)
        self.response_queue = Queue.Queue()
        self.server.add_client(self)
        self.subscriptions = set()
Esempio n. 8
0
    def run(self):
        self.s = self.get_socket()
        if self.s:
            self.pipe = util.SocketPipe(self.s)
            self.s.settimeout(2)
            self.is_connected = True
            self.print_error("connected")

        self.change_status()
        if not self.is_connected:
            return

        # ping timer
        ping_time = 0
        # request timer
        request_time = False
        while self.is_connected:
            # ping the server with server.version
            if time.time() - ping_time > 60:
                if self.is_ping:
                    self.print_error("ping timeout")
                    self.is_connected = False
                    break
                else:
                    self.send_request({'method':'server.version', 'params':[ELECTRUM_VERSION, PROTOCOL_VERSION]})
                    self.is_ping = True
                    ping_time = time.time()
            try:
                response = self.pipe.get()
            except util.timeout:
                if self.unanswered_requests:
                    if request_time is False:
                        request_time = time.time()
                        self.print_error("setting timer")
                    else:
                        if time.time() - request_time > 10:
                            self.print_error("request timeout", len(self.unanswered_requests))
                            self.is_connected = False
                            break
                continue
            if response is None:
                self.is_connected = False
                break
            if request_time is not False:
                self.print_error("stopping timer")
                request_time = False
            self.process_response(response)

        self.change_status()
Esempio n. 9
0
    def __init__(self, server, socket):
        self.server = server
        self.host, _, _ = server.split(':')
        self.socket = socket

        self.pipe = util.SocketPipe(socket)
        self.pipe.set_timeout(0.0)  # Don't wait for data
        # Dump network messages.  Set at runtime from the console.
        self.debug = False
        self.unsent_requests = []
        self.unanswered_requests = {}
        # Set last ping to zero to ensure immediate ping
        self.last_request = time.time()
        self.last_ping = 0
        self.closed_remotely = False
Esempio n. 10
0
    def run(self):
        s = self.get_socket()
        if s:
            self.pipe = util.SocketPipe(s)
            s.settimeout(0.1)
            self.print_error("connected")
            # Indicate to parent that we've connected
            self.change_status()
            while self.is_connected():
                self.maybe_ping()
                self.send_requests()
                self.get_and_process_response()
            if self.connected:  # Don't shutdown() a closed socket
                s.shutdown(socket.SHUT_RDWR)
                s.close()

        # Also for the s is None case
        self.connected = False
        # Indicate to parent that the connection is now down
        self.change_status()
Esempio n. 11
0
    def run(self):
        s = self.get_socket()
        if s:
            self.pipe = util.SocketPipe(s)
            s.settimeout(0.1)
            self.print_error("connected")
            self._status = CS_CONNECTED
            # Indicate to parent that we've connected
            self.notify_status()
            while self.is_connected():
                self.maybe_ping()
                self.send_requests()
                self.get_and_process_response()
            s.shutdown(socket.SHUT_RDWR)
            s.close()

        # Also for the s is None case 
        self._status = CS_FAILED
        # Indicate to parent that the connection is now down
        self.notify_status()
Esempio n. 12
0
                    print_error("wrong certificate", self.host)
                return
            except Exception:
                print_error("wrap_socket failed", self.host)
                traceback.print_exc(file=sys.stderr)
                return

            if is_new:
                print_error("saving certificate for", self.host)
                os.rename(temporary_path, cert_path)

        s.settimeout(60)
        self.s = s
        self.is_connected = True
        print_error("connected to", self.host, self.port)
        self.pipe = util.SocketPipe(s)

    def send_request(self, request, queue=None):
        _id = request.get('id')
        method = request.get('method')
        params = request.get('params')
        with self.lock:
            try:
                r = {'id': self.message_id, 'method': method, 'params': params}
                self.pipe.send(r)
                if self.debug:
                    print_error("-->", r)
            except socket.error, e:
                print_error("socked error:", self.server, e)
                self.is_connected = False
                return