示例#1
0
    def logout(self, user):
        TN_INFO(user.getName() + " logged out.")

        room = user.getRoom()
        if room:
            self.userExitRoom(user, room)

        online_time = (datetime.now() - user.getLoginTime()).total_seconds()
        UserDAO.getInstance().addOnlineTime(user.getName(), int(online_time))

        self._online_users.remove(user)
        del user.getConnection()['user']
        del user
示例#2
0
    def onLogin(self, connection, decoded_request):
        if ('username' not in decoded_request):
            raise InvalidRequestError("key 'username' not found.")
        username = decoded_request['username']

        if ('password' not in decoded_request):
            raise InvalidRequestError("key 'password' not found.")
        password = decoded_request['password']

        dao = UserDAO.getInstance()
        if (dao.existUser(username) == False):
            raise RequestFailedError('Username does not exist.')

        if (dao.getPassword(username) != password):
            raise RequestFailedError('Wrong password.')

        # check whether user has been online
        for onlined_user in self._online_users:
            if username == onlined_user.getName():
                user_connection = onlined_user.getConnection()
                if user_connection.getRemoteAddr() != connection.getRemoteAddr(
                ):
                    raise RequestFailedError(
                        'This user has already logined on another computer.')
                else:
                    self.logout(user)

        user = User(username, connection)
        self._online_users.append(user)
        user.setLoginTime(datetime.now())
        connection['user'] = user
        TN_INFO(username + " logged in.")
        response = {'code': Code.SUCCESS}
        response['online_time'] = dao.getOnlineTime(username)
        connection.send(Protocol.makeMessageFromDict(response))
示例#3
0
    def onRegister(self, connection, decoded_request):
        if ('username' not in decoded_request):
            raise InvalidRequestError("key 'username' not found.")
        username = decoded_request['username']

        if ('password' not in decoded_request):
            raise InvalidRequestError("key 'password' not found.")
        password = decoded_request['password']

        dao = UserDAO.getInstance()
        if (dao.existUser(username)):
            raise RequestFailedError('Username already exists.')

        dao.addUser(username, password)
        response = {'code': Code.SUCCESS}
        connection.send(Protocol.makeMessageFromDict(response))