Ejemplo n.º 1
0
    def removeUser(self, user):
        """Remove user from the Room. Also update the User.room

        Returns:
            True <- If task successfull
            Fasle <- if unable to add user
        """
        if user not in self.activeUsers:
            logger.error(
                "Unable to remove user %s; user not in room %s"\
                %(user.userName, self.roomName)
            )
            return False

        back_self_user = self.activeUsers[:]
        back_self_count = self.activeUserCount
        try:
            self.activeUsers.remove(user)
            self.activeUserCount -= 1
            user.leaveRoom(self)  # returns bool value
        except Exception as err:
            logger.error(
                "Unable to remove user %s; %s"\
                %(user.userName, err)
            )
            self.activeUsers = back_self_user
            self.activeUserCount = back_self_count
            return False

        return True
Ejemplo n.º 2
0
    def addUser(self, user):
        """Adds a user to the Room. Also updates the User.room
       
        Returns:
            True <- If task successfull
            Fasle <- if unable to add user
        """
        if user.currentRoom() != None:
            logger.error("User %s is already in room %s" %
                         (user.userName, user.room))
            return False

        back_self_user = self.activeUsers[:]
        back_self_count = self.activeUserCount

        try:
            self.activeUsers.append(user)
            self.activeUserCount += 1
            back_room = user.joinRoom(self)
        except Exception as err:
            logger.error("Error adding user %s to room %s" %
                         (user.userName, self.roomName))
            self.activeUsers = back_self_user
            self.activeUserCount = back_self_count
            return False

        return True
Ejemplo n.º 3
0
def checkAcceptRange(url: str) -> (bool, str):
    """Return a tuple, (bool, fileSize)

    Validates if partial download is possible over the url.
    Also returns the Content-Length, and Content-Type if true
    """
    status = (False, None)

    try:
        r = requests.head(url)
        r.raise_for_status()
    except requests.exceptions.HTTPError as err:
        logger.error(err)
        # sys.exit(1)
        return status

    if r.headers.get('Accept-Ranges'):
        if r.headers.get('Accept-Ranges') == "bytes":
            logger.info("Supports partial download")
            # Parse the file size from the header
            fileSize = int(r.headers.get('Content-Length'))
            return (True, fileSize)
        else:
            # Handle unsupported partial download
            logger.fatal("Unsupported partial Download for file")
    else:
        logger.warn("File not suitable for download")
    return status
Ejemplo n.º 4
0
def get_host_details():
    """Returns the hostname and IP address of the node
    """
    hostName = ""
    ip_addr = ""
    try:
        hostName = socket.gethostname()
        ip_addr = socket.gethostbyname(hostName)
    except Exception as err:
        logger.error("Unable to obtain Host Details: %s" % err)

    return hostName, ip_addr
Ejemplo n.º 5
0
    def leaveRoom(self, roomObject) -> bool:
        """ Leave the specified room.

        Arguments:
        roomObject: object of class Room

        Return:
        True <- Successfully exited Room roomObject
        False <- Unable to leave the Room roomObject
        """
        if self.room == roomObject:
            self.room = None
            logger.info("User %s exited room %s" %
                        (self.userName, roomObject.roomName))
            return True

        logger.error("User %s unable to exit room %s" %
                     (self.userName, roomObject.roomName))
        return False
Ejemplo n.º 6
0
    def joinRoom(self, roomObject):
        """ Join the specified Room.

        Arguments:
        roomObject: object of class Room

        Return:
        None <- Unsuccessful
        roomObject<- Successfull
        """
        if not isinstance(roomObject, Room):
            # Format string %s is error; FIXME later
            logger.error(
                "User %s cannot join Room: %s is not a valid Room object" %
                (self.userName, roomObject))
            return None
        self.room = roomObject
        logger.info("User %s joined room %s" %
                    (self.userName, roomObject.roomName))
        return self.room
Ejemplo n.º 7
0
    def changeStatus(self, statusCode):
        """Change the status of the Room to statusCode

        Returns:
           True <- if successfull
           False <- if unsuccessfull
        """
        backup_status = self.status
        try:
            self.status = statusCode
            self.status_message = Room.statusCodes[self.status]
            logger.info("Room %s switched to status: %s " %
                        (self.roomName, self.status))
        except Exception as err:
            logger.error(
                "Error changing room %s status from %s to %s ;; %s"\
                %(self.roomName, backup_status, statusCode, err)
            )
            self.status = backup_status
            self.status_message = Room.statusCodes[self.status]
            return False
        return True
Ejemplo n.º 8
0
 def terminate(self):
     try:
         self.httpd.server_close()
         logger.info("Terminated the File Server")
     except Exception as err:
         logger.error("Unable to terminate the File Server: %s" % err)