예제 #1
0
 def start(self):
     from twisted.internet.defer import Deferred
     from transport import callback
     from main import events
     from main import listeners
     from p2p import online_status
     from p2p import p2p_service
     from p2p import p2p_connector
     from p2p import network_connector
     from p2p import ratings
     p2p_service.init()
     online_status.init()
     ratings.init()
     self._starting_defer = Deferred()
     p2p_connector.A('init')
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_switched)
     network_connector.A().addStateChangedCallback(
         self._on_network_connector_switched)
     callback.append_inbox_callback(self._on_inbox_packet_received)
     callback.append_inbox_callback(p2p_service.inbox)
     events.add_subscriber(self._on_identity_url_changed, 'identity-url-changed')
     events.add_subscriber(self._on_my_identity_url_changed, 'my-identity-url-changed')
     if listeners.is_populate_requered('online_status'):
         listeners.populate_later().remove('online_status')
         online_status.populate_online_statuses()
     return True
예제 #2
0
 def _on_network_connector_switched(self, oldstate, newstate, evt, *args, **kwargs):
     from p2p import p2p_connector
     from system import tray_icon
     if oldstate != newstate:
         if newstate == 'CONNECTED' or newstate == 'DISCONNECTED':
             p2p_connector.A('network_connector.state', newstate)
             tray_icon.state_changed(newstate, p2p_connector.A().state)
예제 #3
0
 def A(self, event, arg):
     #---NO_FILES---
     if self.state == 'NO_FILES':
         if event == 'need-files':
             self.state = 'LOCAL_FILES'
             self.doReadLocalFiles(arg)
         elif event == 'init':
             pass
     #---LOCAL_FILES---
     elif self.state == 'LOCAL_FILES':
         if event == 'local-files-done' and p2p_connector.A(
         ).state is 'CONNECTED':
             self.state = 'REMOTE_FILES'
             self.doRequestRemoteFiles(arg)
         elif event == 'local-files-done' and p2p_connector.A(
         ).state is not 'CONNECTED':
             self.state = 'NO_FILES'
     #---REMOTE_FILES---
     elif self.state == 'REMOTE_FILES':
         if (event == 'timer-10sec' and self.isSomeListFilesReceived(arg)
             ) or (event == 'inbox-files'
                   and self.isAllListFilesReceived(arg)):
             self.state = 'SAW_FILES'
         elif event == 'timer-10sec' and not self.isSomeListFilesReceived(
                 arg):
             self.state = 'NO_FILES'
     #---SAW_FILES---
     elif self.state == 'SAW_FILES':
         if event == 'need-files':
             self.state = 'LOCAL_FILES'
             self.doReadLocalFiles(arg)
     return None
예제 #4
0
def rate_all_users():
    from p2p import online_status
    from p2p import p2p_connector
    if not p2p_connector.A():
        lg.warn('ratings update skipped, p2p_connector() is not running')
        return
    if p2p_connector.A().state != 'CONNECTED':
        lg.warn('ratings update skipped, p2p_connector() is CONNECTED')
        return
    if _Debug:
        lg.out(_DebugLevel, 'ratings.rate_all_users')
    monthStr = time.strftime('%B')
    for idurl in contactsdb.contacts_remote(include_all=True):
        if not idurl:
            continue
        isalive = online_status.isOnline(idurl)
        mall, malive, tall, talive = increase_rating(idurl, isalive)
        month_percent = 100.0 * float(malive) / float(mall)
        total_percent = 100.0 * float(talive) / float(tall)
        if _Debug:
            lg.out(
                _DebugLevel,
                '[%6.2f%%: %s/%s] in %s and [%6.2f%%: %s/%s] total - %s' % (
                    month_percent,
                    malive,
                    mall,
                    monthStr,
                    total_percent,
                    talive,
                    tall,
                    nameurl.GetName(idurl),
                ))
    read_index()
예제 #5
0
 def start(self):
     from storage import backup_fs
     from storage import backup_control
     from storage import backup_matrix
     from storage import backup_monitor
     from main.config import conf
     from main import control
     from transport import callback
     from p2p import p2p_connector
     backup_fs.init()
     backup_control.init()
     backup_matrix.init()
     backup_matrix.SetBackupStatusNotifyCallback(control.on_backup_stats)
     backup_matrix.SetLocalFilesNotifyCallback(control.on_read_local_files)
     backup_monitor.A('init')
     backup_monitor.A('restart')
     conf().addCallback('services/backups/keep-local-copies-enabled',
                        self._on_keep_local_copies_modified)
     conf().addCallback('services/backups/wait-suppliers-enabled',
                        self._on_wait_suppliers_modified)
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_state_changed, 'INCOMMING?', 'CONNECTED')
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_state_changed, 'MY_IDENTITY', 'CONNECTED')
     callback.append_inbox_callback(self._on_inbox_packet_received)
     return True
예제 #6
0
 def start(self):
     from storage import backup_fs
     from storage import backup_control
     from storage import backup_matrix
     from storage import backup_monitor
     from main import settings
     from main.config import conf
     from p2p import p2p_connector
     backup_fs.init()
     backup_control.init()
     backup_matrix.init()
     if settings.NewWebGUI():
         from web import control
         backup_matrix.SetBackupStatusNotifyCallback(
             control.on_backup_stats)
         backup_matrix.SetLocalFilesNotifyCallback(
             control.on_read_local_files)
     else:
         from web import webcontrol
         backup_matrix.SetBackupStatusNotifyCallback(
             webcontrol.OnBackupStats)
         backup_matrix.SetLocalFilesNotifyCallback(
             webcontrol.OnReadLocalFiles)
     backup_monitor.A('init')
     backup_monitor.A('restart')
     conf().addCallback('services/backups/keep-local-copies-enabled',
                        self._on_keep_local_copies_modified)
     conf().addCallback('services/backups/wait-suppliers-enabled',
                        self._on_wait_suppliers_modified)
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_state_changed, 'INCOMMING?', 'CONNECTED')
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_state_changed, 'MY_IDENTITY', 'CONNECTED')
     return True
예제 #7
0
 def start(self):
     from storage import backup_fs
     from storage import backup_control
     from storage import backup_matrix
     from storage import backup_monitor
     from main.config import conf
     from main import control
     from main import events
     from main import listeners
     from transport import callback
     from p2p import p2p_connector
     backup_fs.init()
     backup_control.init()
     backup_matrix.init()
     backup_matrix.SetBackupStatusNotifyCallback(control.on_backup_stats)
     backup_matrix.SetLocalFilesNotifyCallback(control.on_read_local_files)
     backup_monitor.A('init')
     backup_monitor.A('restart')
     conf().addConfigNotifier('services/backups/keep-local-copies-enabled',
                        self._on_keep_local_copies_modified)
     conf().addConfigNotifier('services/backups/wait-suppliers-enabled',
                        self._on_wait_suppliers_modified)
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_state_changed, 'INCOMMING?', 'CONNECTED')
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_state_changed, 'MY_IDENTITY', 'CONNECTED')
     callback.append_inbox_callback(self._on_inbox_packet_received)
     events.add_subscriber(self._on_my_identity_rotated, 'my-identity-rotated')
     events.add_subscriber(self._on_key_erased, 'key-erased')
     if listeners.is_populate_requered('remote_version'):
         listeners.populate_later().remove('remote_version')
         backup_matrix.populate_remote_versions()
     return True
예제 #8
0
 def state_changed(self, oldstate, newstate, event, arg):
     global_state.set_global_state('NETWORK ' + newstate)
     if driver.is_on('service_p2p_hookups'):
         from p2p import p2p_connector
         from system import tray_icon
         p2p_connector.A('network_connector.state', newstate)
         tray_icon.state_changed(self.state, p2p_connector.A().state)
예제 #9
0
 def A(self, event, *args, **kwargs):
     #---NO_FILES---
     if self.state == 'NO_FILES':
         if event == 'need-files':
             self.state = 'LOCAL_FILES'
             self.doReadLocalFiles(*args, **kwargs)
         elif event == 'init':
             pass
     #---LOCAL_FILES---
     elif self.state == 'LOCAL_FILES':
         if event == 'local-files-done' and p2p_connector.A().state == 'CONNECTED':
             self.state = 'REMOTE_FILES'
             self.doRequestFilesAllSuppliers(*args, **kwargs)
         elif event == 'local-files-done' and p2p_connector.A().state != 'CONNECTED':
             self.state = 'NO_FILES'
     #---REMOTE_FILES---
     elif self.state == 'REMOTE_FILES':
         if event == 'supplier-connected':
             self.doRequestFilesOneSupplier(*args, **kwargs)
         elif event == 'timer-20sec' and not self.isEnoughListFilesReceived(*args, **kwargs) and not self.isSomeConnecting(*args, **kwargs):
             self.state = 'NO_FILES'
         elif ( event == 'timer-2sec' and self.isEnoughListFilesReceived(*args, **kwargs) ) or ( event == 'inbox-files' and self.isAllListFilesReceived(*args, **kwargs) ):
             self.state = 'SAW_FILES'
     #---SAW_FILES---
     elif self.state == 'SAW_FILES':
         if event == 'need-files':
             self.state = 'LOCAL_FILES'
             self.doReadLocalFiles(*args, **kwargs)
     return None
예제 #10
0
 def _on_transport_state_changed(self, transport, oldstate, newstate):
     from p2p import p2p_connector
     if self.starting_deferred:
         if newstate == 'LISTENING' and oldstate != 'LISTENING':
             self.starting_deferred.callback(newstate)
             self.starting_deferred = None
             p2p_connector.A('check-synchronize')
         if newstate == 'OFFLINE' and oldstate in ['STARTING', 'STOPPING', ]:
             self.starting_deferred.callback(newstate)
             self.starting_deferred = None
             p2p_connector.A('check-synchronize')
예제 #11
0
 def state_changed(self, oldstate, newstate, event, *args, **kwargs):
     global_state.set_global_state('NETWORK ' + newstate)
     if driver.is_on('service_p2p_hookups'):
         from p2p import p2p_connector
         from system import tray_icon
         p2p_connector.A('network_connector.state', newstate)
         tray_icon.state_changed(self.state, p2p_connector.A().state)
     if oldstate != 'CONNECTED' and newstate == 'CONNECTED':
         # TODO: redesign the state machine to cover that
         if self.last_bytes_in_counter < p2p_stats.get_total_bytes_in():
             lg.info('HELLO BITDUST WORLD !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
         else:
             lg.warn('SEEMS I AM OFFLINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
         self.last_bytes_in_counter = p2p_stats.get_total_bytes_in()
예제 #12
0
 def start(self):
     from p2p import p2p_service
     from p2p import contact_status
     from p2p import p2p_connector
     from p2p import network_connector
     from twisted.internet.defer import Deferred
     p2p_service.init()
     contact_status.init()
     self._starting_defer = Deferred()
     p2p_connector.A('init')
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_switched)
     network_connector.A().addStateChangedCallback(
         self._on_network_connector_switched)
     return True
예제 #13
0
 def stop(self):
     from storage import backup_fs
     from storage import backup_monitor
     from storage import backup_control
     from transport import callback
     from p2p import p2p_connector
     from main.config import conf
     callback.remove_inbox_callback(self._on_inbox_packet_received)
     if p2p_connector.A():
         p2p_connector.A().removeStateChangedCallback(self._on_p2p_connector_state_changed)
     backup_monitor.Destroy()
     backup_fs.shutdown()
     backup_control.shutdown()
     conf().removeCallback('services/backups/keep-local-copies-enabled')
     return True
 def _on_transport_state_changed(self, transport, oldstate, newstate):
     from logs import lg
     from p2p import p2p_connector
     lg.info('%s -> %s in %r  starting_deferred=%r' %
             (oldstate, newstate, transport, bool(self.starting_deferred)))
     if self.starting_deferred:
         if newstate == 'LISTENING' and oldstate != newstate:
             self.starting_deferred.callback(True)
             self.starting_deferred = None
             p2p_connector.A('check-synchronize')
         if newstate == 'OFFLINE' and oldstate != newstate and oldstate not in [
                 'INIT',
         ]:
             self.starting_deferred.errback(Exception(newstate))
             self.starting_deferred = None
             p2p_connector.A('check-synchronize')
예제 #15
0
 def start(self):
     from transport import callback
     from p2p import contact_status
     from p2p import p2p_service
     from p2p import p2p_connector
     from p2p import network_connector
     from twisted.internet.defer import Deferred
     p2p_service.init()
     contact_status.init()
     self._starting_defer = Deferred()
     p2p_connector.A('init')
     p2p_connector.A().addStateChangedCallback(
         self._on_p2p_connector_switched)
     network_connector.A().addStateChangedCallback(
         self._on_network_connector_switched)
     callback.append_inbox_callback(self._on_inbox_packet_received)
     callback.append_inbox_callback(p2p_service.inbox)
     return True
예제 #16
0
 def stop(self):
     from storage import backup_fs
     from storage import backup_monitor
     from storage import backup_control
     from transport import callback
     from p2p import p2p_connector
     from main import events
     from main.config import conf
     events.remove_subscriber(self._on_key_erased, 'key-erased')
     events.remove_subscriber(self._on_my_identity_rotated, 'my-identity-rotated')
     callback.remove_inbox_callback(self._on_inbox_packet_received)
     if p2p_connector.A():
         p2p_connector.A().removeStateChangedCallback(self._on_p2p_connector_state_changed)
     backup_monitor.Destroy()
     backup_fs.shutdown()
     backup_control.shutdown()
     conf().removeConfigNotifier('services/backups/keep-local-copies-enabled')
     return True
예제 #17
0
 def stop(self):
     from p2p import p2p_service
     from p2p import contact_status
     from p2p import p2p_connector
     from p2p import network_connector
     if network_connector.A():
         network_connector.A().removeStateChangedCallback(
             self._on_network_connector_switched)
     p2p_connector.A().removeStateChangedCallback(
         self._on_p2p_connector_switched)
     contact_status.shutdown()
     p2p_connector.Destroy()
     p2p_service.shutdown()
     return True
예제 #18
0
 def stop(self):
     from transport import callback
     from p2p import contact_status
     from p2p import p2p_service
     from p2p import p2p_connector
     from p2p import network_connector
     callback.remove_inbox_callback(self._on_inbox_packet_received)
     callback.remove_inbox_callback(p2p_service.inbox)
     if network_connector.A():
         network_connector.A().removeStateChangedCallback(
             self._on_network_connector_switched)
     p2p_connector.A().removeStateChangedCallback(
         self._on_p2p_connector_switched)
     contact_status.shutdown()
     p2p_connector.Destroy()
     p2p_service.shutdown()
     return True
예제 #19
0
 def stop(self):
     from transport import callback
     from main import events
     from p2p import online_status
     from p2p import p2p_service
     from p2p import p2p_connector
     from p2p import network_connector
     from p2p import ratings
     events.remove_subscriber(self._on_my_identity_url_changed, 'my-identity-url-changed')
     events.remove_subscriber(self._on_identity_url_changed, 'identity-url-changed')
     callback.remove_inbox_callback(self._on_inbox_packet_received)
     callback.remove_inbox_callback(p2p_service.inbox)
     if network_connector.A():
         network_connector.A().removeStateChangedCallback(
             self._on_network_connector_switched)
     p2p_connector.A().removeStateChangedCallback(
         self._on_p2p_connector_switched)
     ratings.shutdown()
     online_status.shutdown()
     p2p_connector.Destroy()
     p2p_service.shutdown()
     return True
예제 #20
0
    def doDecideToDismiss(self, *args, **kwargs):
        """
        Action method.
        """
        global _SuppliersToFire
        from p2p import p2p_connector
        from p2p import network_connector
        from customer import supplier_connector
        from p2p import online_status
        # take any actions only if I am connected to the network
        if not p2p_connector.A() or not network_connector.A():
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'fire_hire.doDecideToDismiss    p2p_connector() is not ready yet, SKIP'
                )
            self.automat('made-decision', [])
            return
        if not network_connector.A():
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'fire_hire.doDecideToDismiss    network_connector() is not ready yet, SKIP'
                )
            self.automat('made-decision', [])
            return
        if p2p_connector.A().state != 'CONNECTED' or network_connector.A(
        ).state != 'CONNECTED':
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'fire_hire.doDecideToDismiss    p2p/network is not connected at the moment, SKIP'
                )
            self.automat('made-decision', [])
            return
        # if certain suppliers needs to be removed by manual/external request just do that
        to_be_fired = id_url.to_list(set(_SuppliersToFire))
        _SuppliersToFire = []
        if to_be_fired:
            lg.info('going to fire %d suppliers from external request' %
                    len(to_be_fired))
            self.automat('made-decision', to_be_fired)
            return
        # make sure to not go too far when i just want to decrease number of my suppliers
        number_desired = settings.getSuppliersNumberDesired()
        redundant_suppliers = set()
        if contactsdb.num_suppliers() > number_desired:
            for supplier_index in range(number_desired,
                                        contactsdb.num_suppliers()):
                idurl = contactsdb.supplier(supplier_index)
                if idurl:
                    lg.info('found REDUNDANT supplier %s at position %d' % (
                        idurl,
                        supplier_index,
                    ))
                    redundant_suppliers.add(idurl)
        if redundant_suppliers:
            result = list(redundant_suppliers)
            lg.info('will replace redundant suppliers: %s' % result)
            self.automat('made-decision', result)
            return
        # now I need to look more careful at my suppliers
        potentialy_fired = set()
        connected_suppliers = set()
        disconnected_suppliers = set()
        requested_suppliers = set()
        online_suppliers = set()
        offline_suppliers = set()
        # if you have some empty suppliers need to get rid of them,
        # but no need to dismiss anyone at the moment.
        my_suppliers = contactsdb.suppliers()
        if _Debug:
            lg.args(_DebugLevel, my_suppliers=my_suppliers)
        if id_url.is_some_empty(my_suppliers):
            lg.warn('SKIP, found empty supplier')
            self.automat('made-decision', [])
            return
        for supplier_idurl in my_suppliers:
            sc = supplier_connector.by_idurl(supplier_idurl)
            if not sc:
                lg.warn('SKIP, supplier connector for supplier %s not exist' %
                        supplier_idurl)
                continue
            if sc.state == 'NO_SERVICE':
                lg.warn('found "NO_SERVICE" supplier: %s' % supplier_idurl)
                disconnected_suppliers.add(supplier_idurl)
                potentialy_fired.add(supplier_idurl)
            elif sc.state == 'CONNECTED':
                connected_suppliers.add(supplier_idurl)
            elif sc.state in [
                    'DISCONNECTED',
                    'REFUSE',
            ]:
                disconnected_suppliers.add(supplier_idurl)
#             elif sc.state in ['QUEUE?', 'REQUEST', ]:
#                 requested_suppliers.add(supplier_idurl)
            if online_status.isOffline(supplier_idurl):
                offline_suppliers.add(supplier_idurl)
            elif online_status.isOnline(supplier_idurl):
                online_suppliers.add(supplier_idurl)
            elif online_status.isCheckingNow(supplier_idurl):
                requested_suppliers.add(supplier_idurl)
        if not connected_suppliers or not online_suppliers:
            lg.warn('SKIP, no ONLINE suppliers found at the moment')
            self.automat('made-decision', [])
            return
        if requested_suppliers:
            lg.warn('SKIP, still waiting response from some of suppliers')
            self.automat('made-decision', [])
            return
        if not disconnected_suppliers:
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'fire_hire.doDecideToDismiss    SKIP, no OFFLINE suppliers found at the moment'
                )
            # TODO: add more conditions to fire "slow" suppliers - they are still connected but useless
            self.automat('made-decision', [])
            return
        if len(offline_suppliers) + len(online_suppliers) != number_desired:
            lg.warn('SKIP, offline + online != total count: %s %s %s' %
                    (offline_suppliers, online_suppliers, number_desired))
            self.automat('made-decision', [])
            return
        max_offline_suppliers_count = eccmap.GetCorrectableErrors(
            number_desired)
        if len(offline_suppliers) > max_offline_suppliers_count:
            lg.warn(
                'SKIP, too many OFFLINE suppliers at the moment : %d > %d' % (
                    len(offline_suppliers),
                    max_offline_suppliers_count,
                ))
            self.automat('made-decision', [])
            return
        critical_offline_suppliers_count = eccmap.GetFireHireErrors(
            number_desired)
        if len(offline_suppliers) >= critical_offline_suppliers_count and len(
                offline_suppliers) > 0:
            if config.conf().getBool(
                    'services/employer/replace-critically-offline-enabled'):
                # TODO: check that issue
                # too aggressive replacing suppliers who still have the data is very dangerous !!!
                one_dead_supplier = offline_suppliers.pop()
                lg.warn(
                    'found "CRITICALLY_OFFLINE" supplier %s, max offline limit is %d'
                    % (
                        one_dead_supplier,
                        critical_offline_suppliers_count,
                    ))
                potentialy_fired.add(one_dead_supplier)
        if not potentialy_fired:
            if _Debug:
                lg.out(
                    _DebugLevel,
                    'fire_hire.doDecideToDismiss   found no "bad" suppliers, all is good !!!!!'
                )
            self.automat('made-decision', [])
            return
        # only replace suppliers one by one at the moment
        result = list(potentialy_fired)
        lg.info('will replace supplier %s' % result[0])
        self.automat('made-decision', [
            result[0],
        ])
예제 #21
0
 def _on_network_connector_switched(self, oldstate, newstate, evt, *args, **kwargs):
     from p2p import p2p_connector
     if oldstate != newstate:
         if newstate == 'CONNECTED' or newstate == 'DISCONNECTED':
             p2p_connector.A('network_connector.state', newstate)