def _on_udp_port_modified(self, path, value, oldvalue, result): from p2p import network_connector from logs import lg lg.out(2, 'service_entangled_dht._on_udp_port_modified %s->%s : %s' % ( oldvalue, value, path)) if network_connector.A(): network_connector.A('reconnect')
def _on_p2p_connector_switched(self, oldstate, newstate, evt, *args, **kwargs): if newstate == 'INCOMMING?': if self._starting_defer is not None: self._starting_defer.callback(newstate) self._starting_defer = None from p2p import network_connector from system import tray_icon if network_connector.A(): tray_icon.state_changed(network_connector.A().state, newstate)
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
def on_transport_state_changed(transport, oldstate, newstate): """ """ global _TransportStateChangedCallbacksList if _Debug: lg.out( _DebugLevel - 8, 'gateway.on_transport_state_changed in %r : %s->%s' % (transport, oldstate, newstate)) from p2p import network_connector if network_connector.A(): network_connector.A('network-transport-state-changed', transport) for cb in _TransportStateChangedCallbacksList: cb(transport, oldstate, newstate)
def doDestroyMe(self, arg): """ Remove all references to the state machine object to destroy it. """ # gateway.remove_transport_state_changed_callback(self._on_transport_state_changed) if network_connector.A(): network_connector.A().removeStateChangedCallback( self._on_network_connector_state_changed) callback.remove_inbox_callback(self._on_inbox_packet_received) callback.remove_finish_file_sending_callback( self._on_finish_file_sending) self.unregister() global _ProxyRouter del _ProxyRouter _ProxyRouter = None
def on_tray_icon_command(cmd): from main import shutdowner from services import driver from p2p import network_connector lg.out(2, 'on_tray_icon_command %s' % cmd) if cmd == 'exit': # SendCommandToGUI('exit') shutdowner.A('stop', 'exit') elif cmd == 'restart': # SendCommandToGUI('exit') appList = bpio.find_process(['bpgui.', ]) if len(appList) > 0: shutdowner.A('stop', 'restartnshow') # ('restart', 'show')) else: shutdowner.A('stop', 'restart') # ('restart', '')) elif cmd == 'reconnect': if driver.is_on('service_network'): network_connector.A('reconnect') elif cmd == 'show': show() elif cmd == 'hide': pass # SendCommandToGUI('exit') elif cmd == 'toolbar': pass # SendCommandToGUI('toolbar') else: lg.warn('wrong command: ' + str(cmd))
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
def _on_tcp_port_modified(self, path, value, oldvalue, result): from p2p import network_connector from logs import lg lg.out( 2, 'service_tcp_connections._on_tcp_port_modified : %s->%s : %s' % (oldvalue, value, path)) network_connector.A('reconnect')
def start(self): from twisted.internet import task from p2p import network_connector network_connector.A('init') self.task = task.LoopingCall(self._do_check_network_interfaces) self.task.start(20, now=False) return True
def _on_enabled_disabled(self, path, value, oldvalue, result): from p2p import network_connector from logs import lg lg.out( 2, 'service_http_transport._on_enabled_disabled : %s->%s : %s' % (oldvalue, value, path)) network_connector.A('reconnect')
def _fallback_check_network_connected(self, err): if _Debug: lg.args(_DebugLevel, err) from p2p import network_connector if network_connector.A().state != 'CONNECTED': self.automat('ping-failed', err) else: self.automat('ping-done', [])
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
def doInit(self, *args, **kwargs): """ Action method. """ self._load_routes() network_connector.A().addStateChangedCallback( self._on_network_connector_state_changed) callback.insert_inbox_callback(0, self._on_inbox_packet_received) callback.add_finish_file_sending_callback(self._on_finish_file_sending)
def _on_tray_icon_command(self, cmd): if _Debug: lg.out(_DebugLevel, "initializer._on_tray_icon_command : [%s]" % cmd) try: from main import shutdowner if cmd == 'exit': shutdowner.A('stop', 'exit') elif cmd == 'restart': shutdowner.A('stop', 'restart') elif cmd == 'reconnect': from p2p import network_connector if driver.is_on('service_network'): network_connector.A('reconnect') elif cmd == 'show': # TODO: raise up electron window ? pass elif cmd == 'sync': try: from updates import git_proc from system import tray_icon def _sync_callback(result): if result == 'error': tray_icon.draw_icon('error') reactor.callLater( 5, tray_icon.restore_icon) # @UndefinedVariable return elif result == 'source-code-fetched': tray_icon.draw_icon('updated') reactor.callLater( 5, tray_icon.restore_icon) # @UndefinedVariable return tray_icon.restore_icon() tray_icon.draw_icon('sync') git_proc.sync(_sync_callback, update_method='reset') except: lg.exc() elif cmd == 'hide': pass elif cmd == 'toolbar': pass else: lg.warn('wrong command: ' + str(cmd)) except: lg.exc()
def _on_my_external_ip_changed(self, evt): from logs import lg if evt.data['old'].strip(): lg.info( 'need to reconnect because my external IP changed %r -> %r' % ( evt.data['old'], evt.data['new'], )) from p2p import network_connector network_connector.A('reconnect')
def doFindNewSupplier(self, *args, **kwargs): """ Action method. """ if _Debug: lg.out( _DebugLevel, 'fire_hire.doFindNewSupplier desired_suppliers=%d current_suppliers=%r' % (settings.getSuppliersNumberDesired(), contactsdb.suppliers())) from p2p import network_connector if network_connector.A().state != 'CONNECTED': if _Debug: lg.out( _DebugLevel, ' network_connector is not CONNECTED at the moment, SKIP' ) self.automat('search-failed') return position_for_new_supplier = None for pos in range(settings.getSuppliersNumberDesired()): if pos in self.hire_list: continue supplier_idurl = contactsdb.supplier(pos) if not supplier_idurl: lg.info( 'found empty supplier at position %d and going to find new supplier on that position' % pos) position_for_new_supplier = pos break if id_url.is_in(supplier_idurl, self.dismiss_list, as_field=False): lg.info( 'going to find new supplier on existing position %d to replace supplier %s' % ( pos, supplier_idurl, )) position_for_new_supplier = pos break if position_for_new_supplier is None: lg.err('did not found position for new supplier') self.automat('search-failed') return from customer import supplier_finder for idurl_txt in strng.to_text(config.conf().getData( 'services/employer/candidates')).split(','): if idurl_txt.strip(): supplier_finder.AddSupplierToHire(idurl_txt) self.hire_list.append(position_for_new_supplier) supplier_finder.A( 'start', family_position=position_for_new_supplier, ecc_map=eccmap.Current().name, family_snapshot=id_url.to_bin_list(contactsdb.suppliers()), )
def doInit(self, arg): """ Action method. """ # self.starting_transports = [] self._load_routes() network_connector.A().addStateChangedCallback( self._on_network_connector_state_changed) # gateway.add_transport_state_changed_callback(self._on_transport_state_changed) callback.insert_inbox_callback(0, self._on_inbox_packet_received) callback.add_finish_file_sending_callback(self._on_finish_file_sending)
def start(self): from twisted.internet import task from main import events from p2p import network_connector network_connector.A('init') self.task = task.LoopingCall(self._do_check_network_interfaces) self.task.start(20, now=False) events.add_subscriber(self._on_my_identity_rotate_complete, 'my-identity-rotate-complete') events.add_subscriber(self._on_my_external_ip_changed, 'my-external-ip-changed') return True
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
def _on_tray_icon_command(self, cmd): lg.out(2, "initializer._on_tray_icon_command : [%s]" % cmd) try: if cmd == 'exit': shutdowner.A('stop', 'exit') elif cmd == 'restart': shutdowner.A('stop', 'restart') elif cmd == 'reconnect': from p2p import network_connector if driver.is_on('service_network'): network_connector.A('reconnect') elif cmd == 'show': from web import control control.show() elif cmd == 'sync': try: from updates import git_proc from system import tray_icon def _sync_callback(result): if result == 'error': tray_icon.draw_icon('error') reactor.callLater(5, tray_icon.restore_icon) return elif result == 'source-code-fetched': tray_icon.draw_icon('updated') reactor.callLater(5, tray_icon.restore_icon) return tray_icon.restore_icon() tray_icon.draw_icon('sync') git_proc.sync(_sync_callback) except: lg.exc() elif cmd == 'hide': pass elif cmd == 'toolbar': pass else: lg.warn('wrong command: ' + str(cmd)) except: lg.exc()
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
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
def _do_check_network_interfaces(self): from lib.net_misc import getNetworkInterfaces from p2p import network_connector from logs import lg known_interfaces = getNetworkInterfaces() if '127.0.0.1' in known_interfaces: known_interfaces.remove('127.0.0.1') if self.current_network_interfaces is None: self.current_network_interfaces = known_interfaces lg.info('current network interfaces on START UP: %r' % self.current_network_interfaces) else: if self.current_network_interfaces != known_interfaces and known_interfaces: lg.info( 'need to reconnect, recognized changes in network interfaces: %r -> %r' % (self.current_network_interfaces, known_interfaces)) self.current_network_interfaces = known_interfaces network_connector.A('reconnect')
def doScheduleNextRestart(self, *args, **kwargs): """ Action method. """ self.hire_list = [] if not self.restart_task: self.restart_task = reactor.callLater( # @UndefinedVariable self.restart_interval, self._scheduled_restart) if _Debug: lg.out(_DebugLevel, 'fire_hire.doScheduleNextRestart after %r sec.' % self.restart_interval) from p2p import network_connector if network_connector.A().state is not 'CONNECTED': self.restart_interval = 60 * 5 else: self.restart_interval *= 1.1 else: if _Debug: lg.out(_DebugLevel, 'fire_hire.doScheduleNextRestart already scheduled - %r sec. left' % ( time.time() - self.restart_task.getTime()))
def _do_check_network_interfaces(self): from lib.net_misc import getNetworkInterfaces from p2p import network_connector from logs import lg known_interfaces = getNetworkInterfaces() if '127.0.0.1' in known_interfaces: known_interfaces.remove('127.0.0.1') if self.current_network_interfaces is None: self.current_network_interfaces = known_interfaces lg.out( 2, 'service_network._do_check_network_interfaces START UP: %s' % self.current_network_interfaces) else: if self.current_network_interfaces != known_interfaces: lg.out( 2, 'service_network._do_check_network_interfaces recognized changes: %s -> %s' % (self.current_network_interfaces, known_interfaces)) self.current_network_interfaces = known_interfaces network_connector.A('check-reconnect')
def A(self, event, *args, **kwargs): #---AT_STARTUP--- if self.state == 'AT_STARTUP': if event == 'init': self.state = 'NETWORK?' self.NeedPropagate = True self.doInit(*args, **kwargs) network_connector.A('reconnect') #---NETWORK?--- elif self.state == 'NETWORK?': if (event == 'network_connector.state' and args[0] == 'CONNECTED'): self.state = 'MY_IDENTITY' self.doUpdateMyIdentity(*args, **kwargs) elif (event == 'network_connector.state' and args[0] == 'DISCONNECTED'): self.state = 'DISCONNECTED' #---INCOMMING?--- elif self.state == 'INCOMMING?': if event == 'inbox-packet' and not self.isUsingBestProto( *args, **kwargs): self.state = 'MY_IDENTITY' self.doUpdateMyIdentity(*args, **kwargs) self.doPopBestProto(*args, **kwargs) elif event == 'timer-20sec' or (event == 'network_connector.state' and args[0] == 'DISCONNECTED'): self.state = 'DISCONNECTED' self.doInitRatings(*args, **kwargs) elif event == 'check-synchronize' or (event == 'network_connector.state' and args[0] == 'CONNECTED'): self.state = 'MY_IDENTITY' self.doUpdateMyIdentity(*args, **kwargs) elif (event == 'instant' and not self.isAnyPeersKnown(*args, **kwargs)) or ( event == 'inbox-packet' and self.isUsingBestProto(*args, **kwargs)): self.state = 'CONNECTED' self.doInitRatings(*args, **kwargs) self.doRestartCustomersRejector(*args, **kwargs) #---CONNECTED--- elif self.state == 'CONNECTED': if event == 'ping-contact': self.doSendMyIdentity(*args, **kwargs) elif (event == 'network_connector.state' and args[0] == 'DISCONNECTED'): self.state = 'DISCONNECTED' elif event == 'check-synchronize' or (event == 'network_connector.state' and args[0] == 'CONNECTED'): self.state = 'MY_IDENTITY' self.doUpdateMyIdentity(*args, **kwargs) elif (event == 'network_connector.state' and args[0] not in ['CONNECTED', 'DISCONNECTED']): self.state = 'NETWORK?' #---DISCONNECTED--- elif self.state == 'DISCONNECTED': if event == 'ping-contact': self.doSendMyIdentity(*args, **kwargs) elif event == 'inbox-packet' or event == 'check-synchronize' or ( (event == 'network_connector.state' and args[0] == 'CONNECTED')): self.state = 'MY_IDENTITY' self.doUpdateMyIdentity(*args, **kwargs) elif (event == 'network_connector.state' and args[0] not in [ 'CONNECTED', 'DISCONNECTED', ]): self.state = 'NETWORK?' #---MY_IDENTITY--- elif self.state == 'MY_IDENTITY': if event == 'my-id-updated' and self.isMyContactsChanged( *args, **kwargs): self.state = 'NETWORK?' self.NeedPropagate = True network_connector.A('check-reconnect') elif event == 'my-id-updated' and not self.isMyContactsChanged( *args, **kwargs) and (self.NeedPropagate or self.isMyIdentityChanged( *args, **kwargs)): self.state = 'PROPAGATE' self.doCheckRotatePropagateMyIdentity(*args, **kwargs) elif event == 'my-id-updated' and not ( self.NeedPropagate or self.isMyIdentityChanged( *args, **kwargs)) and (network_connector.A().state is not 'CONNECTED'): self.state = 'DISCONNECTED' elif event == 'my-id-updated' and not ( self.NeedPropagate or self.isMyIdentityChanged(*args, **kwargs)) and ( network_connector.A().state is 'CONNECTED'): self.state = 'CONNECTED' #---PROPAGATE--- elif self.state == 'PROPAGATE': if event == 'my-id-propagated': self.state = 'INCOMMING?' self.NeedPropagate = False self.doRestartFireHire(*args, **kwargs) elif ((event == 'network_connector.state' and args[0] == 'CONNECTED')) or event == 'check-synchronize': self.state = 'MY_IDENTITY' self.doUpdateMyIdentity(*args, **kwargs) return None
def start(self): from p2p import network_connector network_connector.A('init') return True
def _on_udp_port_modified(self, path, value, oldvalue, result): from p2p import network_connector from logs import lg lg.info('DHT udp port modified %s->%s : %s' % (oldvalue, value, path)) if network_connector.A(): network_connector.A('reconnect')
def _on_sending_enabled_disabled(self, path, value, oldvalue, result): from p2p import network_connector network_connector.A('reconnect')
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], ])