예제 #1
0
def main():
    verbose = '-v' in sys.argv
    ctx = zmq.Context()

    # Create MDP client session with short timeout
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    client.timeout = 1000  # 1 sec
    client.retries = 1  # only 1 retry

    request_pipe, peer = zpipe(ctx)
    request_thread = threading.Thread(target=titanic_request, args=(peer, ))
    request_thread.daemon = True
    request_thread.start()
    reply_thread = threading.Thread(target=titanic_reply)
    reply_thread.daemon = True
    reply_thread.start()
    close_thread = threading.Thread(target=titanic_close)
    close_thread.daemon = True
    close_thread.start()

    poller = zmq.Poller()
    poller.register(request_pipe, zmq.POLLIN)

    queue_filename = os.path.join(TITANIC_DIR, 'queue')

    # Main dispatcher loop
    while True:
        # Ensure message directory exists
        if not os.path.exists(TITANIC_DIR):
            os.mkdir(TITANIC_DIR)
            f = open(queue_filename, 'wb')
            f.close()
        # We'll dispatch once per second, if there's no activity
        try:
            items = poller.poll(1000)
        except KeyboardInterrupt:
            break
            # Interrupted

        if items:
            # Append UUID to queue, prefixed with '-' for pending
            suuid = request_pipe.recv().decode('utf-8')
            with open(queue_filename, 'ab') as f:
                line = "-%s\n" % suuid
                f.write(line.encode('utf-8'))

        # Brute-force dispatcher
        with open(queue_filename, 'rb+') as f:
            for entry in f.readlines():
                entry = entry.decode('utf-8')
                # UUID is prefixed with '-' if still waiting
                if entry[0] == '-':
                    suuid = entry[1:].rstrip()  # rstrip '\n' etc.
                    print("I: processing request %s" % suuid)
                    if service_success(client, suuid):
                        # mark queue entry as processed
                        here = f.tell()
                        f.seek(-1 * len(entry), os.SEEK_CUR)
                        f.write('+'.encode('utf-8'))
                        f.seek(here, os.SEEK_SET)
예제 #2
0
def main():
    verbose = '-v' in sys.argv
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    request = b"sq"
    reply = client.send(b"mmi.service", request)

    if reply:
        replycode = reply[0].decode()
        print("Lookup echo services:", replycode)
    else:
        print("E: no response from broker, make sure it's running")
예제 #3
0
def main():
    verbose = '-v' in sys.argv
    ctx = zmq.Context()

    # Create MDP client session with short timeout
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    client.timeout = 1000  # 1 sec
    client.retries = 1  # only 1 retry

    request_pipe, peer = zpipe(ctx)
    request_thread = threading.Thread(target=titanic_request, args=(peer,))
    request_thread.daemon = True
    request_thread.start()
    reply_thread = threading.Thread(target=titanic_reply)
    reply_thread.daemon = True
    reply_thread.start()
    close_thread = threading.Thread(target=titanic_close)
    close_thread.daemon = True
    close_thread.start()

    poller = zmq.Poller()
    poller.register(request_pipe, zmq.POLLIN)
    # Main dispatcher loop
    while True:
        # Ensure message directory exists
        if not os.path.exists(TITANIC_DIR):
            os.mkdir(TITANIC_DIR)
        # We'll dispatch once per second, if there's no activity
        try:
            items = poller.poll(1000)
        except KeyboardInterrupt:
            break              # Interrupted

        if items:

            # Append UUID to queue, prefixed with '-' for pending
            uuid = request_pipe.recv()
            with open(os.path.join(TITANIC_DIR, 'queue'), 'a') as f:
                f.write("-%s\n" % uuid)

        # Brute-force dispatcher
        #
        with open(os.path.join(TITANIC_DIR, 'queue'), 'r+b') as f:
            for entry in f.readlines():
                # UUID is prefixed with '-' if still waiting
                if entry[0] == '-':
                    uuid = entry[1:].rstrip()  # rstrip '\n' etc.
                    print "I: processing request %s" % uuid
                    if service_success(client, uuid):
                        # mark queue entry as processed
                        here = f.tell()
                        f.seek(-1*len(entry), os.SEEK_CUR)
                        f.write('+')
                        f.seek(here, os.SEEK_SET)
예제 #4
0
파일: mmiecho.py 프로젝트: 343829084/zguide
def main():
    verbose = '-v' in sys.argv
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    request = "echo"
    reply = client.send("mmi.service", request)
    
    if reply:
        replycode = reply[0]
        print "Lookup echo service:", replycode
    else:
        print "E: no response from broker, make sure it's running"
예제 #5
0
def main():
    verbose = '-v' in sys.argv
    request = b"echo"
    for arg in sys.argv:
        if arg.startswith('-s='):
            request = arg.split('=')[1].encode()

    client = MajorDomoClient("tcp://localhost:5555", verbose)
    reply = client.send(b"mmi.service", request)

    if reply:
        replycode = reply[0]
        print(f"Lookup echo service: {replycode.decode()}")
    else:
        print("E: no response from broker, make sure it's running")
예제 #6
0
def main():
    verbose = '-v' in sys.argv
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    count = 0
    while count < 100000:
        request = "Hello world"
        try:
            reply = client.send("echo", request)
        except KeyboardInterrupt:
            break
        else:
            # also break on failure to reply:
            if reply is None:
                break
        count += 1
    print "%i requests/replies processed" % count
예제 #7
0
파일: ticlient.py 프로젝트: zouaixu/zguide
def main():
    verbose = '-v' in sys.argv
    session = MajorDomoClient("tcp://localhost:5555", verbose)

    #  1. Send 'echo' request to Titanic
    request = [b"echo", b"Hello world"]
    reply = service_call(session, b"titanic.request", request)

    uuid = None

    if reply:
        uuid = reply.pop(0)
        print("I: request UUID ", uuid)

    #  2. Wait until we get a reply
    while True:
        time.sleep(.1)
        request = [uuid]
        reply = service_call(session, b"titanic.reply", request)

        if reply:
            reply_string = reply[-1]
            print("I: reply:", reply_string)

            #  3. Close request
            request = [uuid]
            reply = service_call(session, b"titanic.close", request)
            break
        else:
            print("I: no reply yet, trying again...")
            time.sleep(5)  #  Try again in 5 seconds
    return 0
예제 #8
0
def main():
    verbose = '-v' in sys.argv
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    count = 0
    while count < 10000:
        request = str(random.randint(100, 200)).encode()
        try:
            reply = client.send(b"square", request)
            print("2", request.decode(), reply[0].decode())
            if count < 10:
                time.sleep(1)
        except KeyboardInterrupt:
            break
        else:
            # also break on failure to reply:
            if reply is None:
                break
        count += 1
    print ("%i requests/replies processed" % count)
예제 #9
0
def main():
    verbose = '-v' in sys.argv
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    requests = 100000
    for i in range(requests):
        request = b"Hello world"
        try:
            client.send(b"echo", request)
        except KeyboardInterrupt:
            print("send interrupted, aborting")
            return

    count = 0
    while count < requests:
        try:
            reply = client.recv()
        except KeyboardInterrupt:
            break
        else:
            # also break on failure to reply:
            if reply is None:
                break
        count += 1
    print("%i requests/replies processed" % count)
예제 #10
0
파일: ticlient.py 프로젝트: ibqn/zmq-python
def main():
    verbose = '-v' in sys.argv
    session = MajorDomoClient("tcp://localhost:5555", verbose)

    service = b'echo'
    for arg in sys.argv:
        if arg.startswith('-s='):
            service = arg.split('=')[1].encode()

    msg = b'Hello world'
    for arg in sys.argv:
        if arg.startswith('-r='):
            msg = arg.split('=')[1].encode()

    # 1. Send 'echo' request to Titanic
    request = [service, msg]
    reply = service_call(session, b"titanic.request", request)

    uuid = None

    if reply:
        uuid = reply.pop(0)
        print(f"I: request UUID {uuid.decode()}")

    # 2. Wait until we get a reply
    while True:
        time.sleep(.1)
        request = [uuid]
        reply = service_call(session, b"titanic.reply", request)

        if reply:
            reply_string = reply[-1]
            print(f"Reply: {reply_string.decode()}")

            # 3. Close request
            request = [uuid]
            reply = service_call(session, b"titanic.close", request)
            break
        else:
            print("I: no reply yet, trying again…")
            time.sleep(5)  # Try again in 5 seconds
    return 0
예제 #11
0
def main():
    verbose = '-v' in sys.argv
    ctx = zmq.Context()

    # Create MDP client session with short timeout
    # this client is used by service_success method
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    client.timeout = 1000  # 1 sec
    client.retries = 1  # only 1 retry

    request_pipe, peer = zpipe(ctx)
    request_thread = threading.Thread(target=titanic_request,
                                      args=(
                                          peer,
                                          verbose,
                                      ))
    request_thread.daemon = True
    request_thread.start()
    reply_thread = threading.Thread(target=titanic_reply, args=(verbose, ))
    reply_thread.daemon = True
    reply_thread.start()
    close_thread = threading.Thread(target=titanic_close, args=(verbose, ))
    close_thread.daemon = True
    close_thread.start()

    poller = zmq.Poller()
    poller.register(request_pipe, zmq.POLLIN)

    # Ensure message directory exists
    TITANIC_DIR.mkdir(parents=True, exist_ok=True)
    # create the dispatcher queue file, if not present
    queue = TITANIC_DIR.joinpath('queue')
    queue.touch()

    # Main dispatcher loop
    while True:
        # We'll dispatch once per second, if there's no activity
        try:
            items = poller.poll(1000)
        except KeyboardInterrupt:
            break  # Interrupted

        if items:
            # Append UUID to queue, prefixed with '-' for pending
            uuid = request_pipe.recv()
            with open(queue, 'a') as f:
                f.write(f"-{uuid.decode()}\n")

        # Brute-force dispatcher
        with open(queue, 'r+b') as f:
            for entry in f.readlines():
                entry = entry.decode()
                # UUID is prefixed with '-' if still waiting
                if entry[0] == '-':
                    uuid = entry[1:].rstrip()  # rstrip '\n' etc.
                    print(f"I: processing request {uuid}")
                    if service_success(client, uuid):
                        # mark queue entry as processed
                        here = f.tell()
                        f.seek(-1 * len(entry), os.SEEK_CUR)
                        f.write(b'+')
                        f.seek(here, os.SEEK_SET)
                        print(f"completed {uuid}")