Files
multi-user/net_systems.py

46 lines
1.3 KiB
Python
Raw Normal View History

import time
2019-02-06 18:12:50 +01:00
import asyncio
import zmq
2019-02-06 18:12:50 +01:00
import zmq.asyncio
2019-01-25 17:02:04 +01:00
import net_components
from libs.esper import esper
2019-02-06 18:12:50 +01:00
class SessionSystem(esper.Processor):
"""
Handle Client-Server session managment
"""
def __init__(self):
super().__init__()
2019-01-25 17:28:53 +01:00
# Initialize poll set
2019-01-25 17:07:45 +01:00
# TODO: Use zmq_poll..
def process(self):
# This will iterate over every Entity that has BOTH of these components:
2019-01-25 17:02:04 +01:00
for ent, (net_interface, user) in self.world.get_components(net_components.NetworkInterface,net_components.User):
if user.role is net_components.Role.SERVER:
2019-01-25 17:28:53 +01:00
print("Server loops")
try:
2019-02-06 18:12:50 +01:00
message = net_interface.socket.recv()
2019-01-25 17:28:53 +01:00
print("test")
except KeyboardInterrupt:
2019-02-06 18:12:50 +01:00
break
2019-01-25 17:28:53 +01:00
print("test")
2019-02-06 18:12:50 +01:00
if user.role is net_components.Role.CLIENT:
# Send reply back to client
socket.send(b"Hello")
@asyncio.coroutine
def recv_and_process():
sock = ctx.socket(zmq.PULL)
sock.bind(url)
msg = yield from sock.recv_multipart() # waits for msg to be ready
reply = yield from async_process(msg)
yield from sock.send_multipart(reply)