Example #1
0
def test_auto_reconnect(app, watcher, manager):
    # add client
    client = App().register(manager)
    node = Node().register(client)
    chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1)
    assert watcher.wait('connected', channel=chan)

    # close server
    app.fire(close(), app.channel)
    assert watcher.wait('closed', channel=app.channel)

    app.unregister()
    assert watcher.wait('unregistered', channel=app.channel)


    for i in range(5):
        watcher.clear()
        assert watcher.wait('connect', channel=chan)
        assert watcher.wait('unreachable', channel=chan)

    # open server
    app = Node(port=app.bind[1], server_ip=app.bind[0])
    app.register(manager)

    assert watcher.wait('registered', channel=app.channel)
    assert watcher.wait('connected_to', channel=app.channel)

    client.unregister()
Example #2
0
def app(manager, watcher, bind):
    server = Node(port=bind[1], server_ip=bind[0])
    server.register(manager)
    server.bind = bind
    assert watcher.wait('registered', channel='node')

    return server
Example #3
0
class Client(Network_Node, circuits.core.BaseComponent):
    def __init__(self, host="0.0.0.0", port=50552, socket=None):
        super().__init__()
        self.node = Node((self.host, self.port))
        self.node.register(self)
        self.socket = socket
        #This is the channel that the server's client is listening on.
        #The fact that I have to use this to talk exclusively to the server strongly
        #suggests that either circuits' API is all sorts of broken or I horribly misunderstand
        #it
        #This is GROOOOOSSSS
        self.server_channel = ""
        #Eventually, I'll need to grab player name somehow.
        self.player = Player()
    def connect(self, host="0.0.0.0", port=50551):
        self.node.add("server", host, port)
        self.server_channel = sha256("{0:s}:{1:d}".format(host, port).encode("utf-8")
                                    ).hexdigest()
    @circuits.handler("connected")
    def connected(self, host, port):
        #self.fire(remote(write(Join_Event(self.player).bytify()), "server"))
        self.fire(write(bytify(Join_Event(self.player))), self.server_channel)
    def go(self):
        self.gui = views.GUI()
        debug("Made gui")
        self.gui.first_view_class = views.Login_View
        self.start()
        debug("Srtarting gui")
        self.gui.run()
        debug("Started")

    def update(self):
        self.game.update()
Example #4
0
    def init(self, args, opts):
        if opts.debug:
            Debugger().register(self)

        if ":" in opts.bind:
            address, port = opts.bind.split(":")
            port = int(port)
        else:
            address, port = opts.bind, 8000

        node = Node().register(self)
        node.add('peer_name', address, port)
    def init(self, args, opts):
        if opts.debug:
            Debugger().register(self)

        if ":" in opts.bind:
            address, port = opts.bind.split(":")
            port = int(port)
        else:
            address, port = opts.bind, 8000

        auto_remote_event = {
            'increment': '*'
        }

        node = Node().register(self)
        node.add('peer_name', address, port, auto_remote_event=auto_remote_event)
Example #6
0
def app(request, manager, watcher, bind):
    app = App().register(manager)
    node = Node().register(app)
    watcher.wait('ready')

    child = (App() + Node(port=bind[1], server_ip=bind[0]))
    child.start(process=True)

    node.add('child', *bind)
    watcher.wait('connected')

    def finalizer():
        child.stop()

    request.addfinalizer(finalizer)

    return app
Example #7
0
def app(request, manager, watcher, bind):
    app = App().register(manager)
    node = Node().register(app)
    watcher.wait("ready")

    child = App() + Node(port=bind[1], server_ip=bind[0])
    child.start(process=True)

    node.add("child", *bind)
    watcher.wait("connected")

    def finalizer():
        child.stop()

    request.addfinalizer(finalizer)

    return app
Example #8
0
    def init(self, args, opts):
        if opts.debug:
            Debugger().register(self)

        if ":" in opts.bind:
            address, port = opts.bind.split(":")
            port = int(port)
        else:
            address, port = opts.bind, 8000

        auto_remote_event = {
            'increment': '*'
        }

        node = Node().register(self)
        node.add(
            'peer_name', address, port, auto_remote_event=auto_remote_event)
Example #9
0
def test_server_send_all(app, watcher, manager):
    client1 = App().register(manager)
    node1 = Node().register(client1)
    chan = node1.add('client1', *app.bind)
    assert watcher.wait('connected', channel=chan)

    client2 = App().register(manager)
    node2 = Node().register(client2)
    chan = node2.add('client2', *app.bind)
    assert watcher.wait('connected', channel=chan)

    event = return_value()
    app.server.send_all(event)
    assert watcher.wait('return_value')

    client1.unregister()
    client2.unregister()
Example #10
0
def test_auto_reconnect(app, watcher, manager):
    # add client
    client = App().register(manager)
    node = Node().register(client)
    chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1)
    assert watcher.wait('connected', channel=chan)
    watcher.clear()

    # close server
    app.fire(close(), app.channel)
    assert watcher.wait('closed', channel=app.channel)
    watcher.clear()

    # client gets an unreachable
    assert watcher.wait('connect', channel=chan)
    assert watcher.wait('unreachable', channel=chan)
    watcher.clear()

    # start a new server
    node2 = Node(port=app.bind[1], server_ip=app.bind[0])
    node2.register(manager)
    assert watcher.wait('ready', channel=node2.channel)
    watcher.clear()

    assert watcher.wait('connected', channel=chan)

    client.unregister()
Example #11
0
def test_auto_reconnect(app, watcher, manager):
    # add client
    client = App().register(manager)
    node = Node().register(client)
    chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1)
    assert watcher.wait('connected', channel=chan)

    # close server
    app.fire(close(), app.channel)
    assert watcher.wait('closed', channel=app.channel)

    app.unregister()
    assert watcher.wait('unregistered', channel=app.channel)

    for _ in range(5):
        watcher.clear()
        assert watcher.wait('connect', channel=chan)
        assert watcher.wait('unreachable', channel=chan)

    # open server
    app = Node(port=app.bind[1], server_ip=app.bind[0])
    app.register(manager)

    assert watcher.wait('registered', channel=app.channel)
    assert watcher.wait('connected_to', channel=app.channel)

    client.unregister()
Example #12
0
class Server(Network_Node, circuits.core.BaseComponent):
    def __init__(self, host="0.0.0.0", port=50551):
        super().__init__(host=host, port=port)
        #This maps user_id to client
        self.clients = {}
        #This is a temporary design. Ultimately this will be part of a client.
        #This is just here until clients are implemented
        #TODO: I'm from future and I don't remember what
        #this is talking about. Please investigate.
        self.sockets = set()
        self.id_generator = count()
        self.node = Node((self.host, self.port))
        self.node.register(self)
        self.id_generator = count() 
            
    @circuits.handler("connect")
    def connect(self, socket, host, port):
        self.sockets.add(socket)
        client = Client(host, port, socket=socket)
        self.clients[next(self.id_generator)] = client
    
    @circuits.handler("disconnect")
    def disconnect(self, socket):
        #This is an intentionally local event
        print("Updating code")
        events.Update_Code_Event().handle(self)

    def broadcast(self, event, exclude_list=None):
        exclude_list = exclude_list or []
        for client in filter(lambda client: client not in exclude_list,
                            self.clients.values()):
            self.fire(write(client.socket, bytify(event).encode("utf-8")))
    
    def cleanup(self):
        self.node.stop()
        self.stop()
    
    def shutdown(self):
        self.cleanup()
        #the fact that it tries to do this is gross.
        #Will need to replace the functionality somehow.
        #sys.exit(1)
    def restart(self):
        self.cleanup()
Example #13
0
def test_server_send(app, watcher, manager):
    client1 = App().register(manager)
    node1 = Node().register(client1)
    chan1 = node1.add('client1', *app.bind)
    assert watcher.wait('connected', channel=chan1)

    client2 = App().register(manager)
    node2 = Node().register(client2)
    chan2 = node2.add('client2', *app.bind)
    assert watcher.wait('connected', channel=chan2)

    event = return_value()
    app.server.send(event, app.server.get_socks()[0], no_result=True)
    assert watcher.wait('return_value')
    watcher.clear()
    assert not watcher.wait('return_value')

    client1.unregister()
    client2.unregister()
Example #14
0
    def init(self, args, opts):
        if opts.debug:
            Debugger().register(self)

        if ":" in opts.bind:
            address, port = opts.bind.split(":")
            port = int(port)
        else:
            address, port = opts.bind, 8000

        self.node = Node(port=port, server_ip=address).register(self)
Example #15
0
def test_server_send_multicast(app, watcher, manager):
    client1 = App().register(manager)
    node1 = Node().register(client1)
    chan1 = node1.add('client1', *app.bind)
    assert watcher.wait('connected', channel=chan1)


    client2 = App().register(manager)
    node2 = Node().register(client2)
    chan2 = node2.add('client2', *app.bind)
    assert watcher.wait('connected', channel=chan2)

    client3 = App().register(manager)
    node3 = Node().register(client3)
    chan3 = node3.add('client3', *app.bind)
    assert watcher.wait('connected', channel=chan3)

    event = return_value()
    app.server.send_to(event, app.server.get_socks()[:2])
    assert watcher.wait('return_value')

    event_cnt = 0
    with watcher._lock:
        for event in watcher.events:
            if event.name == 'return_value':
                event_cnt += 1

    assert event_cnt == 2

    client1.unregister()
    client2.unregister()
    client3.unregister()
Example #16
0
 def __init__(self, host="0.0.0.0", port=50551):
     super().__init__(host=host, port=port)
     #This maps user_id to client
     self.clients = {}
     #This is a temporary design. Ultimately this will be part of a client.
     #This is just here until clients are implemented
     #TODO: I'm from future and I don't remember what
     #this is talking about. Please investigate.
     self.sockets = set()
     self.id_generator = count()
     self.node = Node((self.host, self.port))
     self.node.register(self)
     self.id_generator = count() 
Example #17
0
 def __init__(self, host="0.0.0.0", port=50552, socket=None):
     super().__init__()
     self.node = Node((self.host, self.port))
     self.node.register(self)
     self.socket = socket
     #This is the channel that the server's client is listening on.
     #The fact that I have to use this to talk exclusively to the server strongly
     #suggests that either circuits' API is all sorts of broken or I horribly misunderstand
     #it
     #This is GROOOOOSSSS
     self.server_channel = ""
     #Eventually, I'll need to grab player name somehow.
     self.player = Player()
Example #18
0
def test_server_send(app, watcher, manager):
    client1 = App().register(manager)
    node1 = Node().register(client1)
    chan1 = node1.add('client1', *app.bind)
    assert watcher.wait('connected', channel=chan1)

    client2 = App().register(manager)
    node2 = Node().register(client2)
    chan2 = node2.add('client2', *app.bind)
    assert watcher.wait('connected', channel=chan2)

    event = return_value()
    app.server.send(event, app.server.get_socks()[0], no_result=True)
    assert watcher.wait('return_value')

    client1.unregister()
    client2.unregister()
Example #19
0
def test_server_send_all(app, watcher, manager):
    client1 = App().register(manager)
    node1 = Node().register(client1)
    chan = node1.add('client1', *app.bind)
    assert watcher.wait('connected', channel=chan)

    client2 = App().register(manager)
    node2 = Node().register(client2)
    chan = node2.add('client2', *app.bind)
    assert watcher.wait('connected', channel=chan)

    event = return_value()
    app.server.send_all(event)
    assert watcher.wait('return_value')

    client1.unregister()
    client2.unregister()
Example #20
0
    def init(self, args, opts):
        """Initialize our ``ChatServer`` Component.

        This uses the convenience ``init`` method which is called after the
        component is proeprly constructed and initialized and passed the
        same args and kwargs that were passed during construction.
        """

        self.args = args
        self.opts = opts

        self.clients = {}

        if opts.debug:
            Debugger().register(self)

        if ":" in opts.bind:
            address, port = opts.bind.split(":")
            port = int(port)
        else:
            address, port = opts.bind, 8000

        Node(port=port, server_ip=address).register(self)
Example #21
0
    """hello Event"""


class App(Component):

    def ready(self, client):
        print("Ready!")

    def connected(self, host, port):
        print("Connected to {}:{}".format(host, port))
        print("Try: x = app.fire(hello())")

    def hello(self):
        print("Now try: y = app.fire(remote(hello(), \"test\"))")
        return "Hello World! ({0:d})".format(getpid())


# Setup app1 with a debugger
app = App()
node = Node().register(app)

host = sys.argv[1]
port = int(sys.argv[2])
bind = (host, port)

# Add an address of a node to talk to called "test"
node.add("test", *bind)

# Start app as a thread
app.start()