示例#1
0
 def internal_create_room(internal_message, rooms):
     name = internal_message.get_arguments()
     others = [room for room in rooms if room.get_name() == name]
     if len(others) > 0:
         return False, InternalMessage(internal_message.get_identity(),
                                       Proxy.Commands.send.name,
                                       Text.ERROR_ROOM_EXIST.format(name))
     return True, InternalMessage(internal_message.get_identity(),
                                  Proxy.Commands.send.name,
                                  Text.CREATED.format(name))
 def test_is_valid_failed(self):
     message = InternalMessage("identity", "")
     self.assertFalse(message.is_valid())
     message = InternalMessage("", "command")
     self.assertFalse(message.is_valid())
     message = InternalMessage("", "")
     self.assertFalse(message.is_valid())
    def test_from_valid_json(self):
        json = {"identity": "a", "command": "b", "arguments": "c"}
        message = InternalMessage.from_json(json)
        self.assertEqual(message.get_identity(), "a")
        self.assertEqual(message.get_command(), "b")
        self.assertEqual(message.get_arguments(), "c")

        json = {"identity": "a", "command": "b"}
        message = InternalMessage.from_json(json)
        self.assertEqual(message.get_identity(), "a")
        self.assertEqual(message.get_command(), "b")
        self.assertEqual(message.get_arguments(), "")
示例#4
0
    def internal_quit(internal_message, users):
        quit_messages = [
            InternalMessage(internal_message.get_identity(),
                            Proxy.Commands.send.name, Text.QUIT),
            InternalMessage(internal_message.get_identity(),
                            Proxy.Commands.close.name)
        ]

        succeed, messages = Controller.internal_leave(internal_message, users)
        if not succeed:
            messages = []
        messages.extend(quit_messages)
        return messages
示例#5
0
 def test_internal_create_room_succeed(self):
     request = InternalMessage("a", "command", "d")
     rooms = [Room("a", 10), Room("b", 2), Room("c", 1)]
     succeed, response = self.controller.internal_create_room(request, rooms)
     self.assertEqual(succeed, True)
     self.assertEqual(response.get_identity(), "a")
     self.assertEqual(response.get_command(), Proxy.Commands.send.name)
     self.assertEqual(response.get_arguments(), Rooms.Text.CREATED.format("d"))
示例#6
0
 def test_internal_created_room_failed(self):
     request = InternalMessage("b", "command", "a")
     rooms = [Room("a", 10), Room("b", 2), Room("c", 1)]
     succeed, response = self.controller.internal_create_room(request, rooms)
     self.assertEqual(succeed, False)
     self.assertEqual(response.get_identity(), "b")
     self.assertEqual(response.get_command(), Proxy.Commands.send.name)
     self.assertEqual(response.get_arguments(), Rooms.Text.ERROR_ROOM_EXIST.format("a"))
示例#7
0
 def test_internal_broadcast_with_invalid_user(self):
     users = {
         "a": User("user1", "room1"),
         "b": User("user2", "room1"),
         "c": User("user3")
     }
     request = InternalMessage("c", "command", "Hello")
     responses = self.controller.internal_broadcast(request, users)
     expected_responses = []
     self.assertEqual(len(responses), len(expected_responses))
示例#8
0
    def test_internal_get_rooms(self):
        request = InternalMessage("a", "command")
        rooms = [Room("a", 10), Room("b", 2), Room("c", 1)]
        expected_text = Rooms.Text.ACTIVE_ROOMS + \
                        Rooms.Text.ROOM.format("a", 10) + Rooms.Text.ROOM.format("b", 2) + Rooms.Text.ROOM.format("c", 1) + \
                        Rooms.Text.END_LIST
        response = self.controller.internal_get_rooms(request, rooms)
        self.assertEqual(response.get_identity(), "a")
        self.assertEqual(response.get_command(), Proxy.Commands.send.name)
        self.assertEqual(response.get_arguments(), expected_text)

        request = InternalMessage("b", "command")
        rooms = []
        expected_text = Rooms.Text.ACTIVE_ROOMS + \
                        Rooms.Text.END_LIST
        response = self.controller.internal_get_rooms(request, rooms)
        self.assertEqual(response.get_identity(), "b")
        self.assertEqual(response.get_command(), Proxy.Commands.send.name)
        self.assertEqual(response.get_arguments(), expected_text)
示例#9
0
 def internal_leave(internal_message, users):
     user = users[internal_message.get_identity()]
     if user.get_room() == "":
         err = InternalMessage(internal_message.get_identity(),
                               Proxy.Commands.send.name,
                               Text.ROOM_NOT_JOINED)
         return False, [err]
     # Notify the users in the room
     message = Text.LEAVING_ROOM.format(user.get_room(), user.get_login())
     messages = [
         InternalMessage(key, Proxy.Commands.send.name, message)
         for (key, value) in users.items()
         if value.get_room() == user.get_room()
         and key != internal_message.get_identity()
     ]
     message = Text.Y_LEAVING_ROOM.format(user.get_room(), user.get_login())
     messages.append(
         InternalMessage(internal_message.get_identity(),
                         Proxy.Commands.send.name, message))
     return True, messages
示例#10
0
 def internal_configure(internal_message, users):
     name = internal_message.get_arguments()
     # Compare if the name is taken
     others = [
         key for (key, value) in users.items() if value.get_login() == name
     ]
     succeed = len(others) == 0
     message = Text.WELCOME_LOGGED.format(
         name) if succeed else Text.LOGIN_ALREADY_USED
     return succeed, InternalMessage(internal_message.get_identity(),
                                     Proxy.Commands.send.name, message)
示例#11
0
 def test_internal_broadcast_with_valid_user(self):
     users = {
         "a": User("user1", "room1"),
         "b": User("user2", "room1"),
         "c": User("user3")
     }
     request = InternalMessage("a", "command", "Hello")
     responses = self.controller.internal_broadcast(request, users)
     expected_responses = [
         InternalMessage("a", Proxy.Commands.send.name,
                         Chat.Text.TO_SEND.format("user1", "Hello")),
         InternalMessage("b", Proxy.Commands.send.name,
                         Chat.Text.TO_SEND.format("user1", "Hello"))
     ]
     self.assertEqual(len(responses), len(expected_responses))
     for i in range(0, len(responses)):
         self.assertEqual(responses[i].get_identity(),
                          expected_responses[i].get_identity())
         self.assertEqual(responses[i].get_command(),
                          expected_responses[i].get_command())
         self.assertEqual(responses[i].get_arguments(),
                          expected_responses[i].get_arguments())
示例#12
0
 def internal_broadcast(internal_message, users):
     messages = []
     user = users[internal_message.get_identity()]
     if user.get_room() == "":
         return messages
     # Get the users in the same room
     room_users = sorted([key for (key, value) in users.items() if value.get_room() == user.get_room()])
     # Send the messages
     to_send = Text.TO_SEND.format(user.get_login(), internal_message.get_arguments())
     for identity in room_users:
         message = InternalMessage(identity, Proxy.Commands.send.name, to_send)
         messages.append(message)
     return messages
示例#13
0
 def from_client(self, json):
     # Check if the message is valid
     message = TcpMessage.from_json(json)
     if not message.is_valid():
         print("Invalid message")
         return
     print("Message: {0}".format(message.get_body()))
     # Check if it is an new user
     if message.get_identity() not in self.users.keys():
         self.create_user(message.get_identity(), "")
         return
     user = self.users[message.get_identity()]
     # Check if the user has a valid login
     if len(user.get_login()) == 0:
         result = re.search(self.configure[0], message.get_body())
         if result:
             self.configure[1](self, message.get_identity(),
                               result.groups())
         return
     # Check the commands
     for command in self.commands.keys():
         result = re.search(command, message.get_body())
         if result:
             self.commands[command](self, message.get_identity(),
                                    result.groups())
             return
     # Check if the user is in a room and is chatting
     if user.get_room() != "":
         result = re.search(self.broadcast[0], message.get_body())
         if result:
             self.broadcast[1](self, message.get_identity(),
                               result.groups())
             return
     # Send an error if the command isn't recognized
     internal = InternalMessage(message.get_identity(),
                                Proxy.Commands.send.name, Text.HELP_MESSAGE)
     self.proxy_pusher.send_json(internal.to_json())
示例#14
0
    def run(self):
        poller = zmq.Poller()
        poller.register(self.puller, zmq.POLLIN)
        poller.register(self.sub, zmq.POLLIN)

        while True:
            sockets = dict(poller.poll())

            if self.puller in sockets and sockets[self.puller] == zmq.POLLIN:
                json = self.puller.recv_json()
                internal = InternalMessage.from_json(json)
                self.from_client(internal)
            if self.sub in sockets and sockets[self.sub] == zmq.POLLIN:
                topic, message = self.sub.recv_multipart()
                self.from_broadcast(topic.decode(), message.decode())
示例#15
0
 def internal_join(internal_message, users, rooms):
     # Check if the room exist
     user = users[internal_message.get_identity()]
     room = internal_message.get_arguments()
     if len([item for item in rooms if item.get_name() == room]) == 0:
         err = InternalMessage(internal_message.get_identity(),
                               Proxy.Commands.send.name,
                               Text.ROOM_NOT_FOUND.format(room))
         return False, [err]
     # Notify the users in the room
     message = Text.JOINED.format(room, user.get_login())
     others = [(key, value) for (key, value) in users.items()
               if value.get_room() == room]
     messages = [
         InternalMessage(key, Proxy.Commands.send.name, message)
         for (key, value) in others
     ]
     # Notify the user
     message = Controller.build_entering_message(room, others, user)
     messages.append(
         InternalMessage(internal_message.get_identity(),
                         Proxy.Commands.send.name, message))
     return True, sorted(messages,
                         key=lambda message: message.get_identity())
示例#16
0
 def receive_from_internal(self, json):
     internal = InternalMessage.from_json(json)
     if not internal.is_valid():
         return
     if internal.get_command() in self.commands.keys():
         self.commands[internal.get_command()](self, internal)
示例#17
0
 def join_room(self, identity, arguments):
     print("Join room")
     message = InternalMessage(identity, Users.Commands.join.name,
                               arguments[0])
     self.users_pusher.send_json(message.to_json())
示例#18
0
 def internal_create(internal_message):
     return InternalMessage(internal_message.get_identity(),
                            Proxy.Commands.send.name, Text.WELCOME)
示例#19
0
 def leave_room(self, identity, arguments):
     print("Leave room")
     message = InternalMessage(identity, Users.Commands.leave.name)
     self.users_pusher.send_json(message.to_json())
示例#20
0
 def brutaly_quit(self, identity, arguments):
     print("Brutaly quit")
     message = InternalMessage(identity, Users.Commands.hard_quit.name)
     self.users_pusher.send_json(message.to_json())
示例#21
0
 def create_room(self, identity, arguments):
     print("Create room")
     message = InternalMessage(identity, Rooms.Commands.create.name,
                               arguments[0])
     self.rooms_pusher.send_json(message.to_json())
 def test_is_valid_succeed(self):
     message = InternalMessage("identity", "command")
     self.assertTrue(message.is_valid())
     message = InternalMessage("identity", "command", "arguments")
     self.assertTrue(message.is_valid())
示例#23
0
 def list_rooms(self, identity, arguments):
     print("List rooms")
     message = InternalMessage(identity, Rooms.Commands.list.name)
     self.rooms_pusher.send_json(message.to_json())
示例#24
0
 def chat_broadcast(self, identity, arguments):
     print("Broadcast")
     message = InternalMessage(identity, Chat.Commands.broadcast.name,
                               arguments[0])
     self.chat_pusher.send_json(message.to_json())
示例#25
0
 def internal_get_rooms(internal_message, rooms):
     message = Controller.build_list_rooms(rooms)
     return InternalMessage(internal_message.get_identity(),
                            Proxy.Commands.send.name, message)
示例#26
0
 def create_user(self, identity, arguments):
     print("Create user")
     message = InternalMessage(identity, Users.Commands.create.name)
     self.users_pusher.send_json(message.to_json())
示例#27
0
 def configure_user(self, identity, arguments):
     print("Configure user")
     message = InternalMessage(identity, Users.Commands.configure.name,
                               arguments[0])
     self.users_pusher.send_json(message.to_json())