示例#1
0
文件: util.py 项目: zoom-c/electrum
def send_request(peers, request):
    print "Contacting %d servers" % len(peers)
    # start interfaces
    q2 = Queue.Queue()
    config = SimpleConfig()
    interfaces = map(lambda server: Interface(server, q2, config), peers)
    reached_servers = []
    for i in interfaces:
        i.start()
    t0 = time.time()
    while peers:
        try:
            i, r = q2.get(timeout=1)
        except:
            if time.time() - t0 > 10:
                print "timeout"
                break
            else:
                continue
        if i.server in peers:
            peers.remove(i.server)
        if i.is_connected():
            reached_servers.append(i)
        else:
            print "Connection failed:", i.server

    print "%d servers could be reached" % len(reached_servers)

    results_queue = Queue.Queue()
    for i in reached_servers:
        i.send_request(request, results_queue)
    results = {}
    t0 = time.time()
    while reached_servers:
        try:
            i, r = results_queue.get(timeout=1)
        except:
            if time.time() - t0 > 10:
                break
            else:
                continue
        results[i.server] = r.get('result')
        reached_servers.remove(i)
        i.stop()

    for i in reached_servers:
        print i.server, "did not answer"
    print "%d answers" % len(results)
    return results
示例#2
0
def get_interfaces(servers, timeout=10):
    
    '''Returns a map of servers to connected interfaces.  If any
    
    connections fail or timeout, they will be missing from the map.
    '''
    
    socket_queue = queue.Queue()
    
    config = SimpleConfig()
    
    connecting = {}
    
    for server in servers:
        
        if server not in connecting:
            
            connecting[server] = Connection(server, socket_queue, config.path)
            
    interfaces = {}
    
    timeout = time.time() + timeout
    
    count = 0
    
    while time.time() < timeout and count < len(servers):
        try:
            
            server, socket = socket_queue.get(True, 0.3)
            
        except queue.Empty:
            
            continue
            
        if socket:
            
            interfaces[server] = Interface(server, socket)
            
        count += 1
        
    return interfaces
示例#3
0
        return 1

    print out
    return 0


if __name__ == '__main__':

    if len(sys.argv) > 1:
        ret = handle_command(sys.argv[1])
        sys.exit(ret)

    print "using database", db_name
    conn = mdb.connect(db_instance, db_user, db_password, db_name);

    i = Interface({'server':"%s:%d:t"%(electrum_server, 50001)})
    i.start()
    

    # this process detects when addresses have paid
    in_queue = Queue.Queue()
    out_queue = Queue.Queue()
    thread.start_new_thread(electrum_input_thread, (in_queue,i))
    thread.start_new_thread(electrum_output_thread, (out_queue,i))

    thread.start_new_thread(server_thread, (conn,))


    while not stopping:
        cur = conn.cursor()
示例#4
0
文件: merchant.py 项目: nvk/electrum
        return 1

    print out
    return 0


if __name__ == "__main__":

    if len(sys.argv) > 1:
        ret = handle_command(sys.argv[1])
        sys.exit(ret)

    print "using database", db_name
    conn = mdb.connect(db_instance, db_user, db_password, db_name)

    interface = Interface({"server": "%s:%d:t" % (electrum_server, 50001)})
    interface.start()
    interface.send([("blockchain.numblocks.subscribe", [])])

    wallet.interface = interface
    interface.register_callback("updated", on_wallet_update)

    verifier = WalletVerifier(interface, wallet_config)
    wallet.set_verifier(verifier)

    synchronizer = WalletSynchronizer(wallet, wallet_config)
    synchronizer.start()

    verifier.start()

    # this process detects when addresses have paid
示例#5
0
        return 1

    print out
    return 0


if __name__ == '__main__':

    if len(sys.argv) > 1:
        ret = handle_command(sys.argv[1])
        sys.exit(ret)

    print "using database", db_name
    conn = mdb.connect(db_instance, db_user, db_password, db_name);

    interface = Interface({'server':"%s:%d:t"%(electrum_server, 50001)})
    interface.start()
    interface.send([('blockchain.numblocks.subscribe',[])])

    wallet.interface = interface
    interface.register_callback('updated', on_wallet_update)

    verifier = WalletVerifier(interface, wallet_config)
    wallet.set_verifier(verifier)

    synchronizer = WalletSynchronizer(wallet, wallet_config)
    synchronizer.start()

    verifier.start()
    
示例#6
0
        return 1

    print out
    return 0


if __name__ == '__main__':

    if len(sys.argv) > 1:
        ret = handle_command(sys.argv[1])
        sys.exit(ret)

    print "using database", db_name
    conn = mdb.connect(db_instance, db_user, db_password, db_name)

    interface = Interface({'server': "%s:%d:t" % (electrum_server, 50001)})
    interface.start()
    interface.send([('blockchain.numblocks.subscribe', [])])

    wallet.interface = interface
    interface.register_callback('updated', on_wallet_update)

    verifier = WalletVerifier(interface, wallet_config)
    wallet.set_verifier(verifier)

    synchronizer = WalletSynchronizer(wallet, wallet_config)
    synchronizer.start()

    verifier.start()

    # this process detects when addresses have paid