Started Client-Server based on ECS and ZeroMQ
This commit is contained in:
parent
ef120e8d3c
commit
3f9aaf37b3
30
main.py
Normal file
30
main.py
Normal 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:])
|
||||||
|
|
@ -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:
|
class User:
|
||||||
def __init__(self, name="default", ip="localhost"):
|
def __init__(self, name="default", ip="localhost",role=Role.NONE):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.ip = ip
|
self.role = role
|
||||||
|
|
||||||
class Session:
|
class NetworkInterface:
|
||||||
def __init__(self, name="default", host="localhost"):
|
def __init__(self, host="localhost",context=None, socket_type=zmq.REP,protocol='tcp',port=5555):
|
||||||
self.name = name
|
|
||||||
self.host = host
|
self.host = host
|
||||||
|
self.context = context
|
||||||
|
self.socket_type = socket_type
|
||||||
|
self.socket = context.socket(socket_type)
|
||||||
|
|
||||||
class Position:
|
#TODO: Is this right to it here?
|
||||||
def __init__(self, x=0, y=0,z=0):
|
self.socket.bind("{}://{}:{}" % (protocol,host,port))
|
||||||
self.x = x
|
|
||||||
self.y = y
|
|
||||||
self.z = z
|
|
||||||
|
|
||||||
class Property:
|
class Property:
|
||||||
def __init__(self, property=None):
|
def __init__(self, property=None, replication=Replication.NONE):
|
||||||
self.property = property
|
self.property = property
|
||||||
|
self.replication = replication
|
||||||
|
|
||||||
class Function:
|
class Function:
|
||||||
def __init__(self, function=None):
|
def __init__(self, function=None):
|
||||||
self.function = function
|
self.function = function
|
||||||
|
|
||||||
|
30
net_systems.py
Normal file
30
net_systems.py
Normal 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")
|
Loading…
x
Reference in New Issue
Block a user