From 3f9aaf37b3b664c569faa20e28f9cc0ce00ed34e Mon Sep 17 00:00:00 2001 From: Swann Date: Fri, 25 Jan 2019 14:40:31 +0100 Subject: [PATCH] Started Client-Server based on ECS and ZeroMQ --- main.py | 30 ++++++++++++++++++++++++++++++ net_components.py | 38 +++++++++++++++++++++++++------------- net_systems.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 main.py create mode 100644 net_systems.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..14e8ec4 --- /dev/null +++ b/main.py @@ -0,0 +1,30 @@ +import net_systems +from libs.esper import esper +import zmq +import sys +import time + +# TODO: Implement a manager class for each aspect (ex: Network_Manager) +# TODO: Is it right to implement server-client as ESC ?... +def main(argv): + # Create a World instance to hold everything: + world = esper.World() + + # Instantiate a Processor (or more), and add them to the world: + network_system = net_systems.NetworkSystem() + world.add_processor(network_system) + + # A dummy main loop: + try: + while True: + # Call world.process() to run all Processors. + world.process() + time.sleep(1) + + except KeyboardInterrupt: + return + + +if __name__ == '__main__': + main(sys.argv[1:]) + diff --git a/net_components.py b/net_components.py index 64eacf5..f00e18d 100644 --- a/net_components.py +++ b/net_components.py @@ -1,24 +1,36 @@ +import zmq + +class Role(Enum): + NONE = 1 + SERVER = 2 + CLIENT = 3 + +class Replication(Enum): + NONE = 1 + REPLICATED = 2 + REPNOTIFY = 3 class User: - def __init__(self, name="default", ip="localhost"): + def __init__(self, name="default", ip="localhost",role=Role.NONE): self.name = name - self.ip = ip + self.role = role -class Session: - def __init__(self, name="default", host="localhost"): - self.name = name +class NetworkInterface: + def __init__(self, host="localhost",context=None, socket_type=zmq.REP,protocol='tcp',port=5555): self.host = host - -class Position: - def __init__(self, x=0, y=0,z=0): - self.x = x - self.y = y - self.z = z + self.context = context + self.socket_type = socket_type + self.socket = context.socket(socket_type) + + #TODO: Is this right to it here? + self.socket.bind("{}://{}:{}" % (protocol,host,port)) class Property: - def __init__(self, property=None): + def __init__(self, property=None, replication=Replication.NONE): self.property = property + self.replication = replication class Function: def __init__(self, function=None): - self.function = function \ No newline at end of file + self.function = function + diff --git a/net_systems.py b/net_systems.py new file mode 100644 index 0000000..bbb3c04 --- /dev/null +++ b/net_systems.py @@ -0,0 +1,30 @@ +import time + +import zmq + +import net_components +from libs.esper import esper + + +class NetworkSystem(esper.Processor): + """ + Handle Client-Server session managment + """ + + def __init__(self): + super().__init__() + + + def process(self): + # This will iterate over every Entity that has BOTH of these components: + for ent, session in self.world.get_component(net_components.Session): + + # Wait for next request from client + message = session.socket.recv() + print("Received request: %s" % message) + + # Do some 'work' + time.sleep(1) + + # Send reply back to client + session.socket.send(b"World")