Exemplo n.º 1
0
    def start(self, async_func_list=None):
        """No functions given --> data pass through only; else apply function on data before forwarding

        N publishers to 1 sub; proxy 1 sub to 1 pub; publish to M subscribers
        """

        # make sure pub / sub is initialised
        if not self.pub_socket or not self.sub_socket:
            print("Both pub and sub needs to be initiliased and set to bind")
            print("Pub: {}".format(self.pub_socket))
            print("Sub: {}".format(self.sub_socket))
            sys.exit()

        # apply function to data to passing through data
        if async_func_list:
            import asyncio
            # capture ZeroMQ errors; ZeroMQ using asyncio doesn't print out errors
            try:
                asyncio.get_event_loop().run_until_complete(
                    asyncio.wait([func() for func in async_func_list]))
            except Exception as e:
                print("Error with async function")
                # print(e)
                logging.error(traceback.format_exc())
                print()

            finally:
                # TODO disconnect pub/sub
                pass

        # don't duplicate the message, just pass through
        else:
            print("Try: Proxy... CONNECT!")
            zmq.proxy(self.pub_socket, self.sub_socket)
            print("CONNECT successful!")
Exemplo n.º 2
0
    def main_loop(self):
        # set the context for zmq


        # we want to be able to use the zmq polls
        rpcloop = zmq.asyncio.ZMQEventLoop()
        asyncio.set_event_loop(rpcloop)

        # setup tasks
        tasks=[
            self.worker_loop(),
            self.rpc_loop()
        ]
        # now start the zmq message loop
        rpcloop.run_until_complete(asyncio.wait(tasks))
Exemplo n.º 3
0
    idx = "77cb1e9d44f1b8e8341e6e6848bf34ea6cb7a88bdaad0126ac1254093480f13f"
    idx = bytes.fromhex(idx)

    ec, tx_data = await client.transaction(idx)
    if ec:
        print("Couldn't fetch transaction:", ec, file=sys.stderr)
        context.stop_all()
        return
    # Should be 257 bytes.
    print("tx size is %s bytes" % len(tx_data))

    ec, height, index = await client.transaction_index(idx)
    if ec:
        print("Couldn't fetch transaction_index:", ec, file=sys.stderr)
        context.stop_all()
        return
    # 210000 4
    print(height, index)

    context.stop_all()


if __name__ == '__main__':
    tasks = [
        main(),
    ]
    tasks.extend(context.tasks())
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()
    client = context.Client("tcp://gateway.unsystem.net:9091")

    address = "13ejSKUxLT9yByyr1bsLNseLbx9H9tNj2d"

    ec, history = await client.history(address)
    if ec:
        print("Couldn't fetch history:", ec, file=sys.stderr)
        context.stop_all()
        return
    for output, spend in history:
        print("OUTPUT point=%s, height=%s, value=%s" %
              (output[0], output[1], output[2]))
        if spend is not None:
            print("-> SPEND point=%s, height=%s" %
                  (spend[0], spend[1]))
        print()
    # Calculate the balance of a Bitcoin address from its history.
    balance = sum(output[2] for output, spend in history if spend is None)
    print("Address balance:", balance)

    context.stop_all()

if __name__ == '__main__':
    tasks = [
        main(),
    ]
    tasks.extend(context.tasks())
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()