[rcf] going deeper

This commit is contained in:
Swann 2019-01-25 17:02:04 +01:00
parent 3f9aaf37b3
commit ec4251766e
No known key found for this signature in database
GPG Key ID: B02D0B41F8B6D2EE
3 changed files with 42 additions and 23 deletions

27
main.py
View File

@ -1,12 +1,28 @@
import net_systems
import net_components
from libs.esper import esper
import zmq
import sys
import argparse
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):
def main():
# 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()
@ -14,6 +30,12 @@ def main(argv):
network_system = net_systems.NetworkSystem()
world.add_processor(network_system)
# Instanciate a session entity
session = world.create_entity()
world.add_component(
session, net_components.NetworkInterface(context=instance_context))
# A dummy main loop:
try:
while True:
@ -26,5 +48,4 @@ def main(argv):
if __name__ == '__main__':
main(sys.argv[1:])
main()

View File

@ -1,14 +1,18 @@
import zmq
from enum import Enum, auto
class Role(Enum):
NONE = 1
SERVER = 2
CLIENT = 3
NONE = 'NONE'
SERVER = 'SERVER'
CLIENT = 'CLIENT'
def __str__(self):
return self.value
class Replication(Enum):
NONE = 1
REPLICATED = 2
REPNOTIFY = 3
NONE = auto()
REPLICATED = auto()
REPNOTIFY = auto()
class User:
def __init__(self, name="default", ip="localhost",role=Role.NONE):
@ -16,14 +20,13 @@ class User:
self.role = role
class NetworkInterface:
def __init__(self, host="localhost",context=None, socket_type=zmq.REP,protocol='tcp',port=5555):
def __init__(self, host="127.0.0.1",context=None, socket_type=zmq.REP,protocol='tcp',port=5555):
self.host = host
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))
self.socket.bind("{}://{}:{}".format(protocol,host,port))
class Property:
def __init__(self, property=None, replication=Replication.NONE):

View File

@ -17,14 +17,9 @@ class NetworkSystem(esper.Processor):
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):
for ent, (net_interface, user) in self.world.get_components(net_components.NetworkInterface,net_components.User):
if user.role is net_components.Role.CLIENT:
net_interface.socket.send(b"Waiting server response")
# 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")
if user.role is net_components.Role.SERVER:
net_interface.socket.recv(b"Waiting server response")