clean: remove old files
This commit is contained in:
59
client.py
59
client.py
@ -1,59 +0,0 @@
|
||||
|
||||
|
||||
"""
|
||||
Clone Client Model One
|
||||
|
||||
Author: Min RK <benjaminrk@gmail.com>
|
||||
|
||||
"""
|
||||
|
||||
import random
|
||||
import time
|
||||
import msgpack
|
||||
import zmq
|
||||
|
||||
from libs import kvsimple
|
||||
|
||||
def main():
|
||||
# Prepare our context and publisher socket
|
||||
ctx = zmq.Context()
|
||||
|
||||
# Update socket binding
|
||||
updates = ctx.socket(zmq.SUB)
|
||||
updates.linger = 0
|
||||
updates.connect("tcp://localhost:5555")
|
||||
updates.setsockopt_string(zmq.SUBSCRIBE, '')
|
||||
|
||||
state_request = ctx.socket(zmq.DEALER)
|
||||
state_request.setsockopt(zmq.IDENTITY, b"PEER2")
|
||||
state_request.linger = 0
|
||||
state_request.connect("tcp://localhost:5556")
|
||||
|
||||
# poller for socket aggregation
|
||||
poller = zmq.Poller()
|
||||
poller.register(updates, zmq.POLLIN)
|
||||
|
||||
while True:
|
||||
try:
|
||||
socks = dict(poller.poll(10))
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
|
||||
if updates in socks:
|
||||
message = updates.recv_multipart(zmq.NOBLOCK)
|
||||
print(message)
|
||||
|
||||
# Send update
|
||||
|
||||
new_state= b"test"
|
||||
state_request.send(new_state)
|
||||
print("Sending {}".format(new_state))
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
@ -1,67 +0,0 @@
|
||||
|
||||
|
||||
"""
|
||||
|
||||
kvsimple - simple key-value message class for example applications
|
||||
|
||||
Author: Min RK <benjaminrk@gmail.com>
|
||||
|
||||
"""
|
||||
|
||||
import struct # for packing integers
|
||||
import sys
|
||||
|
||||
import zmq
|
||||
|
||||
class KVMsg(object):
|
||||
"""
|
||||
Message is formatted on wire as 3 frames:
|
||||
frame 0: key (0MQ string)
|
||||
frame 1: sequence (8 bytes, network order)
|
||||
frame 2: body (blob)
|
||||
"""
|
||||
key = None # key (string)
|
||||
sequence = 0 # int
|
||||
body = None # blob
|
||||
|
||||
def __init__(self, sequence, key=None, body=None):
|
||||
assert isinstance(sequence, int)
|
||||
self.sequence = sequence
|
||||
self.key = key
|
||||
self.body = body
|
||||
|
||||
def store(self, dikt):
|
||||
"""Store me in a dict if I have anything to store"""
|
||||
# this seems weird to check, but it's what the C example does
|
||||
if self.key is not None and self.body is not None:
|
||||
dikt[self.key] = self
|
||||
|
||||
def send(self, socket):
|
||||
"""Send key-value message to socket; any empty frames are sent as such."""
|
||||
key = b'' if self.key is None else self.key.encode()
|
||||
seq_s = struct.pack('!l', self.sequence)
|
||||
body = b'' if self.body is None else self.body.encode()
|
||||
socket.send_multipart([ key, seq_s, body ])
|
||||
|
||||
@classmethod
|
||||
def recv(cls, socket):
|
||||
"""Reads key-value message from socket, returns new kvmsg instance."""
|
||||
key, seq_s, body = socket.recv_multipart(zmq.NOBLOCK)
|
||||
key = key.decode() if key else None
|
||||
seq = struct.unpack('!l',seq_s)[0]
|
||||
body = body.decode() if body else None
|
||||
return cls(seq, key=key, body=body)
|
||||
|
||||
def dump(self):
|
||||
if self.body is None:
|
||||
size = 0
|
||||
data='NULL'
|
||||
else:
|
||||
size = len(self.body)
|
||||
data=repr(self.body)
|
||||
print >> sys.stderr, "[seq:{seq}][key:{key}][size:{size}] {data}".format(
|
||||
seq=self.sequence,
|
||||
key=self.key,
|
||||
size=size,
|
||||
data=data,
|
||||
)
|
@ -1,57 +0,0 @@
|
||||
import time
|
||||
import zmq
|
||||
import asyncio
|
||||
|
||||
print('asyncio test')
|
||||
|
||||
context = zmq.Context()
|
||||
|
||||
async def server(ctx):
|
||||
print("Launching server...")
|
||||
|
||||
socket = ctx.socket(zmq.REP)
|
||||
socket.bind("tcp://*:5555")
|
||||
|
||||
while True:
|
||||
#print('start loop')
|
||||
await asyncio.sleep(0.016)
|
||||
try:
|
||||
# this is currenlty necessary to ensure no dropped monitor messages
|
||||
# see LIBZMQ-248 for more info
|
||||
message = socket.recv_multipart()
|
||||
print("Received request: {}".format(message))
|
||||
|
||||
socket.send(b"Hello from server")
|
||||
except zmq.ZMQError:
|
||||
pass
|
||||
|
||||
|
||||
async def client(ctx):
|
||||
print("Launching client...")
|
||||
|
||||
socket = ctx.socket(zmq.REQ)
|
||||
socket.connect("tcp://localhost:5555")
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# Do 10 requests, waiting each time for a response
|
||||
for request in range(10):
|
||||
print("Sending request %s ..." % request)
|
||||
socket.send(b"Hello from cli")
|
||||
|
||||
await asyncio.sleep(.016)
|
||||
# Get the reply.
|
||||
try:
|
||||
message = socket.recv_multipart(zmq.NOBLOCK)
|
||||
print("Received reply %s [ %s ]" % (request, message))
|
||||
except zmq.ZMQError:
|
||||
pass
|
||||
|
||||
|
||||
print("setup server")
|
||||
asyncio.create_task(server(context))
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
print("setup client")
|
||||
asyncio.create_task(client(context))
|
42
server.py
42
server.py
@ -1,42 +0,0 @@
|
||||
"""
|
||||
Clone server Model One
|
||||
|
||||
"""
|
||||
import time
|
||||
import zmq
|
||||
|
||||
|
||||
def main():
|
||||
# Prepare our context and publisher socket
|
||||
ctx = zmq.Context()
|
||||
|
||||
# Update all clients
|
||||
publisher = ctx.socket(zmq.PUB)
|
||||
publisher.bind("tcp://*:5555")
|
||||
time.sleep(0.2)
|
||||
|
||||
# Update receiver
|
||||
state_request = ctx.socket(zmq.ROUTER)
|
||||
state_request.bind("tcp://*:5556")
|
||||
|
||||
# poller for socket aggregation
|
||||
poller = zmq.Poller()
|
||||
poller.register(state_request, zmq.POLLIN)
|
||||
|
||||
|
||||
while True:
|
||||
try:
|
||||
socks = dict(poller.poll(1))
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
|
||||
if state_request in socks:
|
||||
msg = state_request.recv_multipart(zmq.NOBLOCK)
|
||||
print("{}:{}".format(msg[0].decode('ascii'),msg[1].decode()))
|
||||
publisher.send(b'Server update')
|
||||
|
||||
# publisher.send_string('test')
|
||||
# print('msg')
|
||||
# time.sleep(1)
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user