def sendUserListMainRoom(self, user_name, host_port):
        user_list_packet = Packet(19, self.num_seq[user_name], 2, [], "!LH")

        user_chat_room_dict = {}
        user_chat_room_dict["main_room"] = []
        for movie in self.serverProxy.getMovieList():
            user_chat_room_dict[movie.movieTitle] = []
        #user_chat_room_dict={"main_room":[5,alice,3,bob];"batmann":[5,mario]...

        user_list = self.serverProxy.getUserList()
        for user in user_list:
            if user.userChatRoom == ROOM_IDS.MAIN_ROOM:
                user_chat_room_dict["main_room"].append(len(user.userName))
                user_chat_room_dict["main_room"].append(
                    user.userName.encode('utf8'))

            else:
                user_chat_room_dict[user.userChatRoom].append(
                    len(user.userName))
                user_chat_room_dict[user.userChatRoom].append(
                    user.userName.encode('utf8'))

            user_list_packet.packet_length += 1 + len(user.userName)

        data = []
        #data= [Nombre de room,NombreUtilisateurMainRoom,5,alice,3,bob]

        user_list_packet.packet_format += "H"
        data.append(len(user_chat_room_dict.keys()))
        data.append(int(len(user_chat_room_dict["main_room"]) / 2))
        data += user_chat_room_dict["main_room"]
        user_list_packet.packet_length += 2
        i = 0
        while i < len(user_chat_room_dict["main_room"]):

            user_list_packet.packet_format += "B" + str(
                user_chat_room_dict["main_room"][i]) + "s"

            i += 2
        del user_chat_room_dict["main_room"]

        for movie in sorted(user_chat_room_dict.keys()):

            user_list_packet.packet_length += 2
            user_list_packet.packet_format += "H"

            data.append(int(len(user_chat_room_dict[movie]) / 2))
            data += user_chat_room_dict[movie]
            i = 0

            while i < len(user_chat_room_dict[movie]):

                user_list_packet.packet_format += "B" + str(
                    user_chat_room_dict[movie][i]) + "s"

                i += 2

        user_list_packet.data = data

        self.sendPacket(user_list_packet, host_port)
    def connectionResponse(self, datagram, packet, host_port):

        packet = Packet(0, 0, 0, 0)
        packet.unpackHeader(datagram)

        format = '!LB' + str(packet.packet_length - 1) + 's'
        packet.data = [0, 0]
        headerDec, packet.data[0], packet.data[1] = struct.unpack(
            format, datagram)

        userName = packet.data[1].decode("utf-8")

        if self.serverProxy.userExists(userName):

            #num seq a traiter #todo

            self.expected_num_seq[host_port] = 1
            self.num_seq[host_port] = 0
            self.sent_packet_history[host_port] = {}
            self.received_packet_history[host_port] = {}
            self.failed(host_port)

            self.errorDict[host_port] = "userExists"
        elif self.malformed(userName, user_name=True) == True:
            self.expected_num_seq[host_port] = 1
            self.num_seq[host_port] = 0
            self.sent_packet_history[host_port] = {}
            self.received_packet_history[host_port] = {}
            self.failed(host_port)
            self.errorDict[host_port] = "malformed User Name"

        elif len(self.serverProxy.getUserList()) >= 513:

            self.expected_num_seq[host_port] = 1
            self.num_seq[host_port] = 0
            self.sent_packet_history[host_port] = {}
            self.received_packet_history[host_port] = {}
            self.failed(host_port)
            self.errorDict[host_port] = "serverSaturated"

        else:
            self.expected_num_seq[userName] = 1
            self.num_seq[userName] = 0
            self.sent_packet_history[userName] = {}
            self.received_packet_history[userName] = {}

            self.serverProxy.addUser(userName, ROOM_IDS.MAIN_ROOM, None,
                                     host_port)
            self.movieRoom[userName] = ROOM_IDS.MAIN_ROOM
            connectionDone = Packet(1, self.num_seq[userName], 0, '')

            self.sendPacket(connectionDone, host_port)

            #?????
            self.serverProxy.updateUserChatroom(userName, ROOM_IDS.MAIN_ROOM)
Exemple #3
0
    def errorReceived(self, buf):

        packet = Packet(0, 0, 0, 0)
        packet.unpackHeader(buf)

        format = '!LB' + str(packet.packet_length - 1) + 's'
        packet.data = [0, 0]
        headerDec, packet.data[0], packet.data[1] = struct.unpack(format, buf)
        print("ERREUR", packet.data[1].decode("utf8"))
        if 'wrongNumSeq:' in packet.data[1].decode('utf8'):
            self.num_seq = int(packet.data[1][12:].decode("utf8"))
            print("new num seq", self.num_seq)
        print("error :", packet.data[1].decode("utf8"))
        return 0
    def errorReceived(self, datagram, user_name):

        packet = Packet(0, 0, 0, 0)
        packet.unpackHeader(datagram)

        format = '!LB' + str(packet.packet_length - 1) + 's'
        packet.data = [0, 0]
        headerDec, packet.data[0], packet.data[1] = struct.unpack(
            format, datagram)

        if 'wrongNumSeq:' in packet.data[1].decode('utf8'):
            self.num_seq[user_name] = int(packet.data[1][12:].decode("utf8"))
        print("error :", packet.data[1].decode("utf8"))
        return 0
Exemple #5
0
    def errorReceived(self,datagram):

        packet = Packet(0, 0, 0, 0)
        packet.unpackHeader(datagram)

        format = '!LB' + str(packet.packet_length - 1) + 's'
        packet.data = [0, 0]
        headerDec, packet.data[0], packet.data[1] = struct.unpack(format, datagram)

        if packet.data[1].decode('utf8') in ["wrongUserName","serverSaturated","userExists"]:
            self.clientProxy.connectionRejectedONE(packet.data[1].decode("utf8"))
        elif 'wrongNumSeq:' in packet.data[1].decode("utf8"):
            self.num_seq=int(packet.data[1][12:].decode("utf8"))
        print("error :",packet.data[1].decode("utf8"))
        return 0
Exemple #6
0
    def connectionMovieRoom(self, buf):

        packet = Packet(0, 0, 0, 0)
        packet.unpackHeader(buf)

        format = '!LB' + str(packet.packet_length - 1) + 's'
        packet.data = [0, 0]
        headerDec, packet.data[0], packet.data[1] = struct.unpack(format, buf)
        # the joined room
        if self.existMovie(packet.data[1].decode("utf-8")) == False:
            self.error('Movie Room Does Not Exist')
            print(packet.data[1].decode("utf-8"), 'Movie Room Does Not Exist',
                  self.serverProxy.getMovieList())
        else:
            print('the room does exist ')
            self.movieRoom = (packet.data[1].decode("utf-8"))
            # mettre à jour la liste des utilisateur du movie room dans le serveur
            self.serverProxy.updateUserChatroom(self.user_name, self.movieRoom)
            # self.serverProxy.startStreamingMovie(self.movieRoom)
            self.updateUserList(movie_room=self.movieRoom)