Started Client-Server based on ECS and ZeroMQ

This commit is contained in:
Swann 2019-01-25 14:40:31 +01:00
parent ef120e8d3c
commit 3f9aaf37b3
No known key found for this signature in database
GPG Key ID: B02D0B41F8B6D2EE
3 changed files with 85 additions and 13 deletions

30
main.py Normal file
View File

@ -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:])

View File

@ -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
self.function = function

30
net_systems.py Normal file
View File

@ -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")