update tests

This commit is contained in:
Swann
2019-02-06 18:12:50 +01:00
parent fa9c594cce
commit b933bcdf0e
10 changed files with 179 additions and 112 deletions

82
main.py
View File

@ -1,62 +1,52 @@
import net_systems
import replication_system
import net_components
from libs.esper import esper
import zmq
import sys
import argparse
import time
from zmq.asyncio import Context, ZMQEventLoop
import asyncio
Url = 'tcp://127.0.0.1:5555'
Ctx = Context()
@asyncio.coroutine
def server():
print("Getting ready for hello world client. Ctrl-C to exit.\n")
socket = Ctx.socket(zmq.REP)
socket.bind(Url)
while True:
# Wait for next request from client
message = yield from socket.recv()
print("Received request: {}".format(message))
# Do some "work"
yield from asyncio.sleep(1)
# Send reply back to client
message = message.decode('utf-8')
message = '{}, world'.format(message)
message = message.encode('utf-8')
print("Sending reply: {}".format(message))
yield from socket.send(message)
# TODO: Implement a manager class for each aspect (ex: Network_Manager)
# TODO: Is it right to implement server-client as ESC ?...
def main():
args = sys.argv[1:]
if len(args) != 0:
sys.exit(__doc__)
# Argument parsing
parser = argparse.ArgumentParser(
description='Launch an instance of collaboration system')
parser.add_argument('-r', choices=list(net_components.Role),
type=net_components.Role, help='role for the instance ')
args = parser.parse_args()
instance_role = args.r
instance_context = zmq.Context()
print("Starting a {} instance \n".format(instance_role))
# Create a World instance to hold everything:
world = esper.World()
# Instantiate a Processor (or more), and add them to the world:
world.add_processor(net_systems.SessionSystem())
world.add_processor(replication_system.ReplicationSystem())
# Instanciate a session entity
session = world.create_entity()
world.add_component(
session, net_components.NetworkInterface(context=instance_context))
world.add_component(
session, net_components.User(role=instance_role))
# A dummy main loop:
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(server())
while True:
# Call world.process() to run all Processors.
world.process()
time.sleep(1)
except KeyboardInterrupt:
print('\nFinished (interrupted)')
sys.exit(0)
# Socket to talk to server
print("Connecting to hello world server...")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
# Do 10 requests, waiting each time for a response
for request in range(10):
print("Sending request %s ..." % request)
socket.send(b"Hello")
# Get the reply.
message = socket.recv()
print("Received reply %s [ %s ]" % (request, message))
return
if __name__ == '__main__':