예제 #1
0
    def test_remote(self):
        remote = RemoteConnection().start_listener("localhost", 0)
        remote.register("main-actor", actor_of(MultiplyingActor))
        remote.start_all()

        # port is dynamic
        port = remote.listener.socket.port

        client1 = RemoteConnection().actor_for("main-actor", "localhost", port)
        res = client1.query("mult", [1, 2, 3, 4])
        self.assertEqual(res.get(timeout=3), 24)

        # check, that I can use another client
        client2 = RemoteConnection().actor_for("main-actor", "localhost", port)
        res = client2.query("mult", [4, 4, 4])
        self.assertEqual(res.get(timeout=3), 64)

        # check, that the first still works
        res = client1.query("mult", [2, 2, 4])
        self.assertEqual(res.get(timeout=3), 16)

        # check a remote identifier which does not work
        # sorry, no error propagation at the moment,
        # need to use a timeout
        client2 = RemoteConnection().actor_for("unknown-actor", "localhost", port)
        res = client2.query("mult", [1, 4, 4])
        self.assertRaises(Queue.Empty, res.get, timeout=1)

        remote.stop()
예제 #2
0
파일: test_actor.py 프로젝트: B-Rich/pelita
    def test_not_running(self):
        remote = RemoteConnection().start_listener("localhost", 0)
        remote.register("main-actor", actor_of(MultiplyingActor))

        # port is dynamic
        port = remote.listener.socket.port

        client = RemoteConnection().actor_for("main-actor", "localhost", port)
        req = client.query("mult", [1, 4, 4])
        res = req.get(timeout=3)
        self.assertTrue("error" in res)

        remote.stop()
예제 #3
0
파일: test_actor.py 프로젝트: B-Rich/pelita
    def test_remote(self):
        remote = RemoteConnection().start_listener("localhost", 0)
        remote.register("main-actor", actor_of(MultiplyingActor))

        remote.start_all()

        # port is dynamic
        port = remote.listener.socket.port

        client1 = RemoteConnection().actor_for("main-actor", "localhost", port)
        res = client1.query("mult", [1, 2, 3, 4])
        self.assertEqual(res.get(timeout=3), 24)

        # check, that I can use another client
        client2 = RemoteConnection().actor_for("main-actor", "localhost", port)
        res = client2.query("mult", [4, 4, 4])
        self.assertEqual(res.get(timeout=3), 64)

        # check, that the first still works
        res = client1.query("mult", [2, 2, 4])
        self.assertEqual(res.get(timeout=3), 16)

        remote.stop()
예제 #4
0
파일: test_actor.py 프로젝트: B-Rich/pelita
    def test_bad_actors(self):
        remote = RemoteConnection().start_listener("localhost", 0)
        remote.register("main-actor", actor_of(MultiplyingActor))

        remote.start_all()

        # port is dynamic
        port = remote.listener.socket.port

        # check a remote identifier which does not work
        # should reply with an error message
        client = RemoteConnection().actor_for("unknown-actor", "localhost", port)
        req = client.query("mult", [1, 4, 4])
        res = req.get(timeout=3)
        self.assertTrue("error" in res)

        remote.stop()
예제 #5
0
class _ClientActor(DispatchingActor):
    """ Actor used to communicate with the Server.
    """
    def on_start(self):
        self.team = None
        self.server_actor = None

    @expose
    def register_team(self, message, team):
        """ We register the team.
        """
        # TODO: Maybe throw an exception, if a team
        # is already registered.
        # Also: investigate how to deal with concurrency issues
        self.team = team

    @expose
    def say_hello(self, message, main_actor, team_name, host=None, port=None):
        """ Opens a connection to the remote main_actor,
        and sends it a "hello" message with the given team_name.
        """

        try:
            if port is None:
                # assume local game (TODO: put somewhere else?)
                self.server_actor = actor_registry.get_by_name(main_actor)
            else:
                self.server_actor = RemoteConnection().actor_for(main_actor, host, port)
        except DeadConnection:
            # no connection could be established
            self.ref.reply("failed")

        if not self.server_actor:
            _logger.warning("Actor %r not found." % main_actor)
            return

        try:
            if self.server_actor.query("hello", [team_name, self.ref.uuid]).get(2) == "ok":
                _logger.info("Connection accepted")
                self.ref.reply("ok")
        except Queue.Empty:
            self.ref.reply("actor no reply")

    @expose
    def set_bot_ids(self, message, *bot_ids):
        """ Called by the server. This method sets the available bot_ids for this team.
        """
        self.ref.reply(self.team._set_bot_ids(bot_ids))

    @expose
    def set_initial(self, message, universe):
        """ Called by the server. This method tells us the initial universe.
        """
        self.ref.reply(self.team._set_initial(universe))

    @expose
    def play_now(self, message, bot_index, universe):
        """ Called by the server. This message requests a new move
        from the bot with index `bot_index`.
        """
        move = self.team._get_move(bot_index, universe)
        self.ref.reply(move)
예제 #6
0
    def slow_series(self, message, *params):
        res = slow_series(*params)
        self.ref.reply(res)

    @expose
    def random_int(self, message):
        import random
        self.ref.reply(random.randint(0, 10))

actor = actor_of(ClientActor)
actor.start()

port = 50007

remote_actor = RemoteConnection().actor_for("main-actor", "localhost", port)

res = remote_actor.query("multiply", [1, 2, 3, 4])
print res.get()

remote_actor.notify("hello", [str(actor.uuid)])

try:
    while actor.is_alive:
        actor.join(1)

except KeyboardInterrupt:
    print "Interrupted"
    actor.stop()