예제 #1
0
            def Start(*args, **kwargs):

                try:

                    port = service.GetPort()

                    try:

                        connection = HydrusNetworking.GetLocalConnection(port)
                        connection.close()

                        raise Exception(
                            'Something was already bound to port ' + str(port))

                    except:

                        if service_type == HC.SERVER_ADMIN:

                            http_factory = ServerServer.HydrusServiceAdmin(
                                service)

                        elif service_type == HC.FILE_REPOSITORY:

                            http_factory = ServerServer.HydrusServiceRepositoryFile(
                                service)

                        elif service_type == HC.TAG_REPOSITORY:

                            http_factory = ServerServer.HydrusServiceRepositoryTag(
                                service)

                        else:

                            return

                        (ssl_cert_path, ssl_key_path) = self.db.GetSSLPaths()

                        sslmethod = twisted.internet.ssl.SSL.TLSv1_2_METHOD

                        context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory(
                            ssl_key_path, ssl_cert_path, sslmethod)

                        self._service_keys_to_connected_ports[
                            service_key] = reactor.listenSSL(
                                port, http_factory, context_factory)

                        try:

                            connection = HydrusNetworking.GetLocalConnection(
                                port)
                            connection.close()

                        except:

                            raise Exception('Tried to bind port ' + str(port) +
                                            ' but it failed.')

                except Exception as e:

                    HydrusData.Print(traceback.format_exc())
예제 #2
0
            def StartService(*args, **kwargs):

                try:

                    if 'port' not in options: return

                    port = options['port']

                    try:

                        connection = HydrusNetworking.GetLocalConnection(port)
                        connection.close()

                        raise Exception(
                            'Something was already bound to port ' + str(port))

                    except:

                        message = options['message']

                        if service_type == HC.SERVER_ADMIN:
                            service_object = ServerServer.HydrusServiceAdmin(
                                service_key, service_type, message)
                        elif service_type == HC.FILE_REPOSITORY:
                            service_object = ServerServer.HydrusServiceRepositoryFile(
                                service_key, service_type, message)
                        elif service_type == HC.TAG_REPOSITORY:
                            service_object = ServerServer.HydrusServiceRepositoryTag(
                                service_key, service_type, message)
                        elif service_type == HC.MESSAGE_DEPOT:
                            return

                        self._services[service_key] = reactor.listenTCP(
                            port, service_object)

                        try:

                            connection = HydrusNetworking.GetLocalConnection(
                                port)
                            connection.close()

                        except:

                            raise Exception('Tried to bind port ' + str(port) +
                                            ' but it failed.')

                except Exception as e:

                    HydrusData.Print(traceback.format_exc())
예제 #3
0
    def __init__(self):

        HydrusSerialisable.SerialisableBase.__init__(self)

        self.engine = None

        self._dirty = False

        self._lock = threading.Lock()

        self._network_contexts_to_bandwidth_trackers = collections.defaultdict(
            HydrusNetworking.BandwidthTracker)
        self._network_contexts_to_bandwidth_rules = collections.defaultdict(
            HydrusNetworking.BandwidthRules)

        for context_type in [
                CC.NETWORK_CONTEXT_GLOBAL, CC.NETWORK_CONTEXT_HYDRUS,
                CC.NETWORK_CONTEXT_DOMAIN, CC.NETWORK_CONTEXT_DOWNLOADER_PAGE,
                CC.NETWORK_CONTEXT_SUBSCRIPTION,
                CC.NETWORK_CONTEXT_WATCHER_PAGE
        ]:

            self._network_contexts_to_bandwidth_rules[
                ClientNetworkingContexts.NetworkContext(
                    context_type)] = HydrusNetworking.BandwidthRules()
예제 #4
0
            def StartServer(*args, **kwargs):

                try:

                    try:

                        connection = HydrusNetworking.GetLocalConnection(port)
                        connection.close()

                        text = 'The client\'s local server could not start because something was already bound to port ' + str(
                            port) + '.'
                        text += os.linesep * 2
                        text += 'This usually means another hydrus client is already running and occupying that port. It could be a previous instantiation of this client that has yet to shut itself down.'
                        text += os.linesep * 2
                        text += 'You can change the port this client tries to host its local server on in file->options.'

                        HydrusData.ShowText(text)

                    except:

                        import ClientLocalServer

                        self._local_service = reactor.listenTCP(
                            port,
                            ClientLocalServer.HydrusServiceLocal(
                                CC.LOCAL_FILE_SERVICE_KEY, HC.LOCAL_FILE,
                                'This is the local file service.'),
                            interface='127.0.0.1')

                        try:

                            connection = HydrusNetworking.GetLocalConnection(
                                port)
                            connection.close()

                        except Exception as e:

                            text = 'Tried to bind port ' + str(
                                port) + ' for the local server, but it failed:'
                            text += os.linesep * 2
                            text += HydrusData.ToUnicode(e)

                            HydrusData.ShowText(text)

                except Exception as e:

                    wx.CallAfter(HydrusData.ShowException, e)
예제 #5
0
    def __init__(self,
                 method,
                 url,
                 body=None,
                 referral_url=None,
                 temp_path=None):

        self.engine = None

        self._lock = threading.Lock()

        self._method = method
        self._url = url
        self._body = body
        self._referral_url = referral_url
        self._temp_path = temp_path

        self._files = None
        self._for_login = False

        self._current_connection_attempt_number = 1

        self._additional_headers = {}

        self._creation_time = HydrusData.GetNow()

        self._bandwidth_tracker = HydrusNetworking.BandwidthTracker()

        self._wake_time = 0

        self._content_type = None

        self._stream_io = cStringIO.StringIO()

        self._error_exception = Exception(
            'Exception not initialised.')  # PyLint hint, wew
        self._error_exception = None
        self._error_text = None

        self._is_done_event = threading.Event()

        self._is_done = False
        self._is_cancelled = False
        self._bandwidth_manual_override = False
        self._bandwidth_manual_override_delayed_timestamp = None

        self._last_time_ongoing_bandwidth_failed = 0

        self._status_text = u'initialising\u2026'
        self._num_bytes_read = 0
        self._num_bytes_to_read = 1

        self._file_import_options = None

        self._network_contexts = self._GenerateNetworkContexts()

        (self._session_network_context, self._login_network_context
         ) = self._GenerateSpecificNetworkContexts()
예제 #6
0
            def StartServer(*args, **kwargs):

                try:

                    try:

                        connection = HydrusNetworking.GetLocalConnection(port)
                        connection.close()

                        text = 'The client\'s booru server could not start because something was already bound to port ' + str(
                            port) + '.'
                        text += os.linesep * 2
                        text += 'This usually means another hydrus client is already running and occupying that port. It could be a previous instantiation of this client that has yet to shut itself down.'
                        text += os.linesep * 2
                        text += 'You can change the port this client tries to host its local server on in services->manage services.'

                        HydrusData.ShowText(text)

                    except:

                        import ClientLocalServer

                        self._booru_port_connection = reactor.listenTCP(
                            port,
                            ClientLocalServer.HydrusServiceBooru(service))

                        try:

                            connection = HydrusNetworking.GetLocalConnection(
                                port)
                            connection.close()

                        except Exception as e:

                            text = 'Tried to bind port ' + str(
                                port) + ' for the local booru, but it failed:'
                            text += os.linesep * 2
                            text += HydrusData.ToUnicode(e)

                            HydrusData.ShowText(text)

                except Exception as e:

                    wx.CallAfter(HydrusData.ShowException, e)
예제 #7
0
 def GetValue( self ):
     
     bandwidth_rules = HydrusNetworking.BandwidthRules()
     
     for ( bandwidth_type, time_delta, max_allowed ) in self._listctrl.GetClientData():
         
         bandwidth_rules.AddRule( bandwidth_type, time_delta, max_allowed )
         
     
     return bandwidth_rules
예제 #8
0
    def GetTracker(self, network_context):

        with self._lock:

            if network_context in self._network_contexts_to_bandwidth_trackers:

                return self._network_contexts_to_bandwidth_trackers[
                    network_context]

            else:

                return HydrusNetworking.BandwidthTracker()
예제 #9
0
    def test_no_rules(self):

        rules = HydrusNetworking.BandwidthRules()

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))
예제 #10
0
 def InitView( self ):
     
     HydrusController.HydrusController.InitView( self )
     
     if not self._no_daemons:
         
         self._daemons.append( HydrusThreading.DAEMONWorker( self, 'DeleteOrphans', ServerDaemons.DAEMONDeleteOrphans, period = 86400 ) )
         self._daemons.append( HydrusThreading.DAEMONWorker( self, 'GenerateUpdates', ServerDaemons.DAEMONGenerateUpdates, period = 600, init_wait = 10 ) )
         self._daemons.append( HydrusThreading.DAEMONWorker( self, 'SaveDirtyObjects', ServerDaemons.DAEMONSaveDirtyObjects, period = 30 ) )
         self._daemons.append( HydrusThreading.DAEMONWorker( self, 'UPnP', ServerDaemons.DAEMONUPnP, ( 'notify_new_options', ), period = 43200 ) )
         
     
     #
     
     self._services = self.Read( 'services' )
     
     [ self._admin_service ] = [ service for service in self._services if service.GetServiceType() == HC.SERVER_ADMIN ]
     
     port = self._admin_service.GetPort()
     
     already_bound = False
     
     try:
         
         connection = HydrusNetworking.GetLocalConnection( port )
         connection.close()
         
         already_bound = True
         
     except:
         
         pass
         
     
     if already_bound:
         
         HydrusData.Print( 'Something is already bound to port ' + str( port ) + ', so your administration service cannot be started. Please quit the server and retry once the port is clear.' )
         
     else:
         
         for service in self._services:
             
             self.StartService( service )
예제 #11
0
 def CheckIfAdminPortInUse( self ):
     
     ( service_type, options ) = self.Read( 'service_info', HC.SERVER_ADMIN_KEY )
     
     port = options[ 'port' ]
     
     already_bound = False
     
     try:
         
         connection = HydrusNetworking.GetLocalConnection( port )
         connection.close()
         
         already_bound = True
         
     except: pass
     
     if already_bound:
         
         raise HydrusExceptions.PermissionException( 'Something was already bound to port ' + str( port ) )
예제 #12
0
    def DeleteHistory(self, network_contexts):

        with self._lock:

            for network_context in network_contexts:

                if network_context in self._network_contexts_to_bandwidth_trackers:

                    del self._network_contexts_to_bandwidth_trackers[
                        network_context]

                    if network_context == ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT:

                        # just to reset it, so we have a 0 global context at all times
                        self._network_contexts_to_bandwidth_trackers[
                            ClientNetworkingContexts.
                            GLOBAL_NETWORK_CONTEXT] = HydrusNetworking.BandwidthTracker(
                            )

            self._SetDirty()
예제 #13
0
    def test_bandwidth_ok(self):

        PERMISSIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 1048576)

        HYDRUS_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_HYDRUS, MOCK_HYDRUS_SERVICE_KEY)

        #

        job = self._GetJob()

        job.engine.bandwidth_manager.ReportDataUsed([HYDRUS_NETWORK_CONTEXT],
                                                    50)

        self.assertEqual(job.BandwidthOK(), True)

        job.engine.bandwidth_manager.SetRules(HYDRUS_NETWORK_CONTEXT,
                                              PERMISSIVE_DATA_RULES)

        self.assertEqual(job.BandwidthOK(), True)

        #

        job = self._GetJob(for_login=True)

        job.engine.bandwidth_manager.ReportDataUsed([HYDRUS_NETWORK_CONTEXT],
                                                    50)

        self.assertEqual(job.BandwidthOK(), True)

        job.engine.bandwidth_manager.SetRules(HYDRUS_NETWORK_CONTEXT,
                                              PERMISSIVE_DATA_RULES)

        self.assertEqual(job.BandwidthOK(), True)
예제 #14
0
    def test_bandwidth_exceeded(self):

        RESTRICTIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        RESTRICTIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 10)

        DOMAIN_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_DOMAIN, MOCK_DOMAIN)

        #

        job = self._GetJob()

        self.assertEqual(job.BandwidthOK(), True)

        job.engine.bandwidth_manager.ReportDataUsed([DOMAIN_NETWORK_CONTEXT],
                                                    50)

        job.engine.bandwidth_manager.SetRules(DOMAIN_NETWORK_CONTEXT,
                                              RESTRICTIVE_DATA_RULES)

        self.assertEqual(job.BandwidthOK(), False)

        #

        job = self._GetJob(for_login=True)

        self.assertEqual(job.BandwidthOK(), True)

        job.engine.bandwidth_manager.ReportDataUsed([DOMAIN_NETWORK_CONTEXT],
                                                    50)

        job.engine.bandwidth_manager.SetRules(DOMAIN_NETWORK_CONTEXT,
                                              RESTRICTIVE_DATA_RULES)

        self.assertEqual(job.BandwidthOK(), True)
예제 #15
0
    def OnInit(self):

        self.SetAssertMode(wx.PYAPP_ASSERT_SUPPRESS)

        HC.app = self
        HC.http = HydrusNetworking.HTTPConnectionManager()

        self._timestamps = collections.defaultdict(lambda: 0)

        self._timestamps['boot'] = HC.GetNow()

        self._just_woke_from_sleep = False

        self._local_service = None
        self._booru_service = None

        self.Bind(HC.EVT_PUBSUB, self.EventPubSub)

        try:

            splash = ClientGUI.FrameSplash('boot')

            return True

        except:

            print('There was an error trying to start the splash screen!')

            print(traceback.format_exc())

            try:
                wx.CallAfter(splash.Destroy)
            except:
                pass

            return False
예제 #16
0
import time
import unittest
import HydrusData
import HydrusGlobals as HG
import HydrusNetworking
from mock import patch

now = HydrusData.GetNow()

now_10 = now + 10

now_20 = now + 20

with patch.object(HydrusData, 'GetNow', return_value=now):

    HIGH_USAGE = HydrusNetworking.BandwidthTracker()

    for i in range(100):

        HIGH_USAGE.ReportRequestUsed()
        HIGH_USAGE.ReportDataUsed(random.randint(512, 1024))

    LOW_USAGE = HydrusNetworking.BandwidthTracker()

    LOW_USAGE.ReportRequestUsed()
    LOW_USAGE.ReportDataUsed(1024)

    ZERO_USAGE = HydrusNetworking.BandwidthTracker()


class TestBandwidthRules(unittest.TestCase):
예제 #17
0
def SetDefaultBandwidthManagerRules( bandwidth_manager ):
    
    KB = 1024
    MB = 1024 ** 2
    GB = 1024 ** 3
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 1, 5 ) # stop accidental spam
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 60, 120 ) # smooth out heavy usage/bugspam. db and gui prob need a break
    
    rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 86400, 10 * GB ) # check your inbox lad
    
    bandwidth_manager.SetRules( ClientNetworking.GLOBAL_NETWORK_CONTEXT, rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 1, 1 ) # don't ever hammer a domain
    
    rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 86400, 2 * GB ) # don't go nuts on a site in a single day
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_DOMAIN ), rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 86400, 64 * MB ) # don't sync a giant db in one day
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_HYDRUS ), rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_DOWNLOADER ), rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 300, 100 ) # after that first sample of small files, take it easy
    
    rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 300, 128 * MB ) # after that first sample of big files, take it easy
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_DOWNLOADER_QUERY ), rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 86400, 200 ) # catch up on a big sub in little chunks every day
    
    rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 86400, 256 * MB ) # catch up on a big sub in little chunks every day
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_SUBSCRIPTION ), rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 300, 100 ) # after that first sample of small files, take it easy
    
    rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 300, 128 * MB ) # after that first sample of big files, take it easy
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_THREAD_WATCHER_THREAD ), rules )
    
    #
    
    rules = HydrusNetworking.BandwidthRules()
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 60 * 7, 80 )
    
    rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 4, 1 )
    
    bandwidth_manager.SetRules( ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_DOMAIN, 'sankakucomplex.com' ), rules )
예제 #18
0
    def test_bandwidth_tracker(self):

        bandwidth_tracker = HydrusNetworking.BandwidthTracker()

        self.assertEqual(bandwidth_tracker.GetCurrentMonthSummary(),
                         'used 0.0B in 0 requests this month')

        now = HydrusData.GetNow()

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 0), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 0), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 1), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 1), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 2), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 2), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 6), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 6), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 3600), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 3600),
                0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, None), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, None),
                0)

            #

            bandwidth_tracker.ReportDataUsed(1024)
            bandwidth_tracker.ReportRequestUsed()

            self.assertEqual(bandwidth_tracker.GetCurrentMonthSummary(),
                             'used 1024B in 1 requests this month')

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 0), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 0), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 1), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 1), 1)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 2), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 2), 1)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 6), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 6), 1)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 3600), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 3600),
                1)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, None), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, None),
                1)

        #

        five_secs_from_now = now + 5

        with patch.object(HydrusData,
                          'GetNow',
                          return_value=five_secs_from_now):

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 0), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 0), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 1), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 1), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 2), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 2), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 6), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 6), 1)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 3600), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 3600),
                1)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, None), 1024)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, None),
                1)

            #

            bandwidth_tracker.ReportDataUsed(32)
            bandwidth_tracker.ReportRequestUsed()

            bandwidth_tracker.ReportDataUsed(32)
            bandwidth_tracker.ReportRequestUsed()

            self.assertEqual(bandwidth_tracker.GetCurrentMonthSummary(),
                             'used 1.1KB in 3 requests this month')

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 0), 0)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 0), 0)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 1), 64)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 1), 2)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 2), 64)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 2), 2)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 6), 1088)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 6), 3)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, 3600), 1088)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, 3600),
                3)

            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_DATA, None), 1088)
            self.assertEqual(
                bandwidth_tracker.GetUsage(HC.BANDWIDTH_TYPE_REQUESTS, None),
                3)
예제 #19
0
def ShutdownSiblingInstance(db_dir):

    port_found = False

    ports = HydrusData.GetSiblingProcessPorts(db_dir, 'server')

    if ports is None:

        raise HydrusExceptions.PermissionException(
            'Could not figure out the existing server\'s ports, so could not shut it down!'
        )

    for port in ports:

        try:

            connection = HydrusNetworking.GetLocalConnection(port, https=True)

            connection.request('GET', '/')

            response = connection.getresponse()

            response.read()

            server_name = response.getheader('Server')

        except:

            text = 'Could not contact existing server\'s port ' + str(
                port) + '!'
            text += os.linesep
            text += traceback.format_exc()

            raise HydrusExceptions.PermissionException(text)

        if 'server administration' in server_name:

            port_found = True

            HydrusData.Print(u'Sending shut down instruction\u2026')

            connection.request('POST', '/shutdown')

            response = connection.getresponse()

            result = response.read()

            if response.status != 200:

                text = 'When told to shut down, the existing server gave an error!'
                text += os.linesep
                text += result

                raise HydrusExceptions.PermissionException(text)

            time_waited = 0

            while HydrusData.IsAlreadyRunning(db_dir, 'server'):

                time.sleep(1)

                time_waited += 1

                if time_waited > 20:

                    raise HydrusExceptions.PermissionException(
                        'Attempted to shut the existing server down, but it took too long!'
                    )

            break

    if not port_found:

        raise HydrusExceptions.PermissionException(
            'The existing server did not have an administration service!')

    HydrusData.Print('The existing server is shut down!')
예제 #20
0
    def test_per_sec(self):

        # at short time deltas, we can always start based on data alone

        rules = HydrusNetworking.BandwidthRules()

        rules.AddRule(HC.BANDWIDTH_TYPE_DATA, 1, 10240)

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertFalse(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_10):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_20):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        #

        rules = HydrusNetworking.BandwidthRules()

        rules.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, 1, 1)

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertFalse(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_10):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_20):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        #

        rules = HydrusNetworking.BandwidthRules()

        rules.AddRule(HC.BANDWIDTH_TYPE_DATA, 1, 10240)
        rules.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, 1, 1)

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertFalse(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertFalse(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_10):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_20):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertTrue(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))
예제 #21
0
    def test_can_start(self):

        EMPTY_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 1048576)

        PERMISSIVE_REQUEST_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_REQUEST_RULES.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, None,
                                         10000)

        RESTRICTIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        RESTRICTIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 10)

        RESTRICTIVE_REQUEST_RULES = HydrusNetworking.BandwidthRules()

        RESTRICTIVE_REQUEST_RULES.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, None, 1)

        DOMAIN_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_DOMAIN, MOCK_DOMAIN)
        SUBDOMAIN_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_DOMAIN, MOCK_SUBDOMAIN)

        GLOBAL_NETWORK_CONTEXTS = [
            ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT
        ]
        DOMAIN_NETWORK_CONTEXTS = [
            ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
            DOMAIN_NETWORK_CONTEXT
        ]
        SUBDOMAIN_NETWORK_CONTEXTS = [
            ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
            DOMAIN_NETWORK_CONTEXT, SUBDOMAIN_NETWORK_CONTEXT
        ]

        #

        fast_forward = HydrusData.GetNow() + 3600

        with patch.object(HydrusData, 'GetNow', return_value=fast_forward):

            bm = ClientNetworkingBandwidth.NetworkBandwidthManager()

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.ReportRequestUsed(DOMAIN_NETWORK_CONTEXTS)
            bm.ReportDataUsed(DOMAIN_NETWORK_CONTEXTS, 50)
            bm.ReportRequestUsed(SUBDOMAIN_NETWORK_CONTEXTS)
            bm.ReportDataUsed(SUBDOMAIN_NETWORK_CONTEXTS, 25)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        EMPTY_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, EMPTY_RULES)
            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, EMPTY_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        PERMISSIVE_DATA_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, PERMISSIVE_DATA_RULES)
            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, PERMISSIVE_DATA_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        PERMISSIVE_REQUEST_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)
            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, RESTRICTIVE_DATA_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, RESTRICTIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(DOMAIN_NETWORK_CONTEXT, RESTRICTIVE_DATA_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(DOMAIN_NETWORK_CONTEXT, RESTRICTIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(DOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        RESTRICTIVE_DATA_RULES)

            self.assertFalse(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        RESTRICTIVE_REQUEST_RULES)

            self.assertFalse(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        RESTRICTIVE_DATA_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, RESTRICTIVE_REQUEST_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, EMPTY_RULES)

            self.assertFalse(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))
예제 #22
0
    def test_per_month(self):

        rules = HydrusNetworking.BandwidthRules()

        rules.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 10240)

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_10):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_20):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        #

        rules = HydrusNetworking.BandwidthRules()

        rules.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, None, 10)

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_10):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_20):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        #

        rules = HydrusNetworking.BandwidthRules()

        rules.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 10240)
        rules.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, None, 10)

        with patch.object(HydrusData, 'GetNow', return_value=now):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_10):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))

        with patch.object(HydrusData, 'GetNow', return_value=now_20):

            self.assertTrue(rules.CanStartRequest(ZERO_USAGE))
            self.assertTrue(rules.CanStartRequest(LOW_USAGE))
            self.assertFalse(rules.CanStartRequest(HIGH_USAGE))

            self.assertTrue(rules.CanContinueDownload(ZERO_USAGE))
            self.assertTrue(rules.CanContinueDownload(LOW_USAGE))
            self.assertTrue(rules.CanContinueDownload(HIGH_USAGE))