Esempio n. 1
0
def main():
    commutils.set_comm(create_comm())
    comm = commutils.comm()

    if commutils.procid() == 0:
        while True:
            msg = commutils.time()
            comm.send(msg, dest=1, tag=1)
            time.sleep(0.5)

    elif commutils.procid() == 1:
        while True:
            comm.recv(source=0, tag=1)
            time.sleep(0.5)
Esempio n. 2
0
 def processCommand(self, clientsock, clientaddr):
     # Comm.recv just sits and waits until it hears SOMETHING from the client
     commandMessage = comm.recv(clientsock)
     if commandMessage == "getClassInstance":
         # If that's "Get Class" it should be immediately followed by a class definition
         className = comm.recv(clientsock)
         # Get an instance of that class (it should already exist, but if it doesn't GetClassInstance will try to create it)
         classInstance = self.getClassInstance(className)
         # Then return the instance to the client
         comm.send(clientsock, classInstance)
     elif commandMessage == "saveClassInstance":
         # If that's "Save Class" it should be immediately followed by a remote system path, then the class instance
         modulePaths = comm.recv(clientsock)
         classInstance = comm.recv(clientsock)
         # Save that instance (overwriting any existing instances of the same class)
         self.saveClassInstance(modulePaths, classInstance)
     elif commandMessage == "break":
         self.dying = True
Esempio n. 3
0
 def processCommand(self, clientsock, clientaddr):
     # Comm.recv just sits and waits until it hears SOMETHING from the client
     commandMessage = comm.recv(clientsock)
     if commandMessage == "getClassInstance":
         # If that's "Get Class" it should be immediately followed by a class definition
         className = comm.recv(clientsock)
         # Get an instance of that class (it should already exist, but if it doesn't GetClassInstance will try to create it)
         classInstance = self.getClassInstance(className)
         # Then return the instance to the client
         comm.send(clientsock, classInstance)
     elif commandMessage == "saveClassInstance":
         # If that's "Save Class" it should be immediately followed by a remote system path, then the class instance
         modulePaths = comm.recv(clientsock)
         classInstance = comm.recv(clientsock)
         # Save that instance (overwriting any existing instances of the same class)
         self.saveClassInstance(modulePaths, classInstance)
     elif commandMessage == "break":
         self.dying = True
Esempio n. 4
0
 def getClassInstance(self, className):
     sock = self.connect()
     # Request instance of TestClass above
     comm.send(sock, "getClassInstance")
     # After a "getClassInstance" request, we must provide a class name
     comm.send(sock, className)
     # Then we receive back the remote instance of that class
     classInstance = comm.recv(sock)
     sock.close()
     return classInstance
Esempio n. 5
0
    def __init__(self, host, port):
        """
        Args:
            host (str):
            port (int):
        """
        super().__init__()
        self.connect((host, port))

        self.setblocking(True)
        try:
            self.i = comm.recv(self)
        except ConnectionRefusedError:
            self.i = b''
        if self.i == b'' or self.i == -1:
            self.close()
            print("Cannot reach the server")
            return
        tokens_center = comm.recv(self)
        dices_val = comm.recv(self)
        characters = comm.recv(self)
        areas = comm.recv(self)
        active_player = comm.recv(self)
        self.setblocking(False)

        self.game = game.Game(self, tokens_center, dices_val, characters,
                              areas, active_player)
        self.game.run()
Esempio n. 6
0
    def poll(self):
        """
        Tries to read data from the server

        Returns:
            None
        """

        r, _, _ = select.select([self], [], [], 0)
        if r:
            msg = comm.recv(self)
            if msg == b'':
                print("Lost connection from server")
                self.close()
                return

            if msg[0] == 'token':
                self.game.tokens[msg[1]].center = msg[2]
            elif msg[0] == 'dices':
                self.game.dices[0].roll_to(msg[1][0])
                self.game.dices[1].roll_to(msg[1][1])
            elif msg[0] == 'reveal':
                self.game.characters[msg[1]].revealed = True
            elif msg[0] == 'turn':
                self.game.active_player.i = msg[1]
            elif msg[0] == 'draw':
                if card.TYPES[msg[2]] == card.CardVision:
                    if self.i == msg[1]:
                        self.send_vision(
                            msg[3],
                            self.game.cards[msg[2]].draw(msg[3], msg[1]))
                else:
                    self.game.cards[msg[2]].draw(msg[3], msg[1])
            elif msg[0] == 'vision':
                self.game.cards[card.TYPES.index(card.CardVision)].answer(
                    msg[1], msg[2])
            elif msg[0] == 'take':
                self.game.characters[msg[1]].equipments.append(
                    self.game.characters[msg[2]].equipments.pop(msg[3]))
            else:
                print(msg)
Esempio n. 7
0
    def handle_read(self):
        """
        Called when the client sends data.

        The message should be a list, where the first item is a string specifying the client request:
            'token': the client moved it's token, send the new token coordinates to every other client
            'dices': the client rolled the dices, notify every client and send the dice values
            'reveal': the client revealed it's character, notify every client
            'turn': the client ended it's turn, notify every client
            'draw': the client draw a card, choose which and notify every client
            'vision: the client send a vision card, to another client, notify the other client
            'take': the client takes an equipment from another player, notify every client

        Returns:
            None
        """
        msg = comm.recv(self)
        if msg == b'':
            print("Lost client {0}".format(game.PLAYERS[self.i][0]))
            self.server.clients[self.i] = None
            self.close()
            return

        if msg[0] == 'token':
            print("Player {0} moved it's {1} token".format(
                game.PLAYERS[self.i][0], 'second' if msg[1] % 2 else 'first'))
            self.server.tokens_center[msg[1]] = msg[2]
            for client in self.server.clients:
                if client is not None and client is not self.server.clients[
                        self.i]:
                    comm.send(client, msg)
        elif msg[0] == 'dices':
            print("Player {0} rolled the dices".format(
                game.PLAYERS[self.i][0]))
            self.server.dices_val = [
                random.randint(1, 4),
                random.randint(1, 6)
            ]
            for client in self.server.clients:
                if client is not None:
                    comm.send(client, ['dices', self.server.dices_val])
        elif msg[0] == 'reveal':
            print("Player {0} came out of the closet".format(
                game.PLAYERS[self.i][0]))
            self.server.characters[self.i][2] = True
            for client in self.server.clients:
                if client is not None:
                    comm.send(client, ['reveal', self.i])
        elif msg[0] == 'turn':
            print("Player {0} ended it's turn".format(game.PLAYERS[self.i][0]))
            self.server.active_player = (self.server.active_player +
                                         1) % _N_PLAYERS
            for client in self.server.clients:
                if client is not None:
                    comm.send(client, ['turn', self.server.active_player])
        elif msg[0] == 'draw':
            if self.server.cards[msg[1]]:
                print("Player {0} draw a card".format(game.PLAYERS[self.i][0]))
                i_card = self.server.cards[msg[1]].pop()
                if card.TYPES[msg[1]] != card.CardVision and card.TYPES[
                        msg[1]].CARDS[i_card][1]:
                    self.server.characters[self.i][3].append((msg[1], i_card))
                for client in self.server.clients:
                    if client is not None:
                        comm.send(client, ['draw', self.i, msg[1], i_card])
            else:
                print("Cannot draw card of type {0}".format(msg[1]))
        elif msg[0] == 'vision':
            if self.server.clients[msg[2]]:
                print("Player {0} send vision card to player {1}".format(
                    game.PLAYERS[self.i][0], game.PLAYERS[msg[2]][0]))
                comm.send(self.server.clients[msg[2]],
                          ['vision', msg[1], self.i])
            else:
                print("Error: Client {0} is not connected".format(msg[2]))
        elif msg[0] == 'take':
            self.server.characters[self.i][3].append(
                self.server.characters[msg[1]][3].pop(msg[2]))
            for client in self.server.clients:
                if client is not None:
                    comm.send(client, ['take', self.i, msg[1], msg[2]])