Exemplo n.º 1
0
 def __init__(self):
     self.lstFS = []
     self.commands_handlers = self.__initHandlersUser()
     self.balancer = Balancer()
     d = deferLater(reactor, 5, self.__pollServers)
Exemplo n.º 2
0
class DFSServerProtocol(WebSocketServerProtocol):

    commands = commands.commands_server

    def __init__(self):
        self.lstFS = []
        self.commands_handlers = self.__initHandlersUser()
        self.balancer = Balancer()
        d = deferLater(reactor, 5, self.__pollServers)

    def __updateStatusDB(self):
        """
            Updating status field in DB for every available server
        """
        dump = self.lstFS
        Queries.updateServersStatus(dump)

    def __pollServers(self):
        """
            Polling all available file servers
        """
        def getResult(result):
            self.lstFS.append(result)

        log.msg("[POLL] Start daemon for polling servers...")
        servers = Queries.getAllFileServers()
        for server in servers:
            dlGetStatus = deferLater(reactor, 0, checkServerStatus, server.ip, server.port).addCallback(getResult)
        dlUpdateDB = deferLater(reactor, 0, self.__updateStatusDB)
        dlUpdate = deferLater(reactor, 5, self.__updateFileServerList)

    def __updateFileServerList(self):
        """
            Periodical update data about servers in balancer
        """
        self.balancer.updateFileServerList(self.lstFS)
        self.lstFS = []
        log.msg("[POLL] Polling process has complete!")
        d = deferLater(reactor, POLL_TIME, self.__pollServers)

    def __initHandlersUser(self):
        """
            Initialize handlers for every command
        """
        handlers = commands.commands_handlers_server
        handlers['REGS'] = self.registration
        handlers['FSRV'] = self.fileserver_auth
        handlers['AUTH'] = self.authorization
        handlers['READ'] = self.read_fs
        handlers['WRTE'] = self.write_file
        handlers['DELT'] = self.delete_file
        handlers['RNME'] = self.rename_file
        handlers['SYNC'] = self.fileSync
        handlers['CRLN'] = self.create_link
        handlers['LINK'] = self.download_by_link
        handlers['LIST'] = self.get_fs_structure
        # for gui app
        handlers['REAF'] = self.read_fs_all
        handlers['GETF'] = self.get_all_user_files
        handlers['WRTF'] = self.massive_write_files
        handlers['DELF'] = self.massive_delete_files
        handlers['RENF'] = self.massive_rename_files
        return handlers

    def registration(self, data):
        log.msg("[REGS] New user=%s: want to create account" % (data['user']))
        try:
            checker = Queries.getSimilarUsers(data['user'])
            if len(checker) == 0:
                if len(data['user']) < 3:
                    raise ValueError('Length of username was been more than 3 symbols!')
                elif len(data['password']) < 6:
                    raise ValueError('Length of password was been more than 6 symbols!')
                elif len(data['fullname']) == 0:
                    raise ValueError("Full name can't be empty!")

                log.msg("[REGS] Creating new user...")
                catalog_name = str(data['user'] + "_main")
                fs_name = str(data['user'] + "_fs")
                Queries.createNewUser(catalog_name, fs_name, data['user'], data['password'], data['fullname'], data['email'])
                data['error'] = []
                data['auth'] = True
                data['cmd'] = 'CREG'
                log.msg("[REGS] All operations successfully completed!")
            else:
                log.msg("[REGS] User with ID=%s already contains at DB" % (data['user']))
                data['error'].append('ERROR: This user already exists! Please, check another login...')
                data['cmd'] = 'RREG'
        except ValueError, exc:
            data['error'].append('ERROR: %s' % exc.message)
            data['cmd'] = 'RREG'
        return data