def doRemoveSuppliers(self, *args, **kwargs): """ Action method. """ current_suppliers = contactsdb.suppliers() desired_suppliers = settings.getSuppliersNumberDesired() if len(current_suppliers) < desired_suppliers: lg.warn('must have more suppliers %d<%d' % ( len(current_suppliers), desired_suppliers)) removed_suppliers = [] for supplier_idurl in self.dismiss_list: if supplier_idurl not in current_suppliers: lg.warn('%s not a supplier' % supplier_idurl) continue pos = current_suppliers.index(supplier_idurl) # current_suppliers.remove(supplier_idurl) current_suppliers[pos] = '' removed_suppliers.append((pos, supplier_idurl,)) misc.writeSupplierData( supplier_idurl, 'disconnected', time.strftime('%d-%m-%Y %H:%M:%S'), my_id.getLocalID(), ) current_suppliers = current_suppliers[:desired_suppliers] contactsdb.update_suppliers(current_suppliers) contactsdb.save_suppliers() from main import control control.on_suppliers_changed(current_suppliers) for position, supplier_idurl in removed_suppliers: events.send('supplier-modified', dict( new_idurl=None, old_idurl=supplier_idurl, position=position, )) lg.out(2, '!!!!!!!!!!! REMOVE SUPPLIERS : %d' % len(self.dismiss_list))
def doRemoveSuppliers(self, arg): """ Action method. """ current_suppliers = contactsdb.suppliers() desired_suppliers = settings.getSuppliersNumberDesired() if len(current_suppliers) < desired_suppliers: lg.warn('must have more suppliers %d<%d' % ( len(current_suppliers), desired_suppliers)) for supplier_idurl in self.dismiss_list: if supplier_idurl not in current_suppliers: lg.warn('%s not a supplier' % supplier_idurl) continue pos = current_suppliers.index(supplier_idurl) # current_suppliers.remove(supplier_idurl) current_suppliers[pos] = '' misc.writeSupplierData( supplier_idurl, 'disconnected', time.strftime('%d-%m-%Y %H:%M:%S')) current_suppliers = current_suppliers[:desired_suppliers] contactsdb.update_suppliers(current_suppliers) contactsdb.save_suppliers() if settings.NewWebGUI(): from web import control control.on_suppliers_changed(current_suppliers) else: from web import webcontrol webcontrol.OnListSuppliers() lg.out(2, '!!!!!!!!!!! REMOVE SUPPLIERS : %d' % len(self.dismiss_list))
def doSubstituteSupplier(self, arg): """ Action method. """ new_idurl = arg current_suppliers = list(contactsdb.suppliers()) if new_idurl in current_suppliers: raise Exception('%s is already supplier' % new_idurl) position = -1 old_idurl = None for i in range(len(current_suppliers)): if current_suppliers[i].strip() == '': position = i break if current_suppliers[i] in self.dismiss_list: # self.dismiss_list.remove(current_suppliers[i]) position = i old_idurl = current_suppliers[i] break lg.out(10, 'fire_hire.doSubstituteSupplier position=%d' % position) if position < 0: current_suppliers.append(new_idurl) else: current_suppliers[position] = new_idurl contactsdb.update_suppliers(current_suppliers) contactsdb.save_suppliers() misc.writeSupplierData( new_idurl, 'connected', time.strftime('%d-%m-%Y %H:%M:%S'), my_id.getLocalID(), ) if settings.NewWebGUI(): from web import control control.on_suppliers_changed(current_suppliers) else: from web import webcontrol webcontrol.OnListSuppliers() if position < 0: lg.out(2, '!!!!!!!!!!! ADDED NEW SUPPLIER : %s' % (new_idurl)) events.send('supplier-modified', dict( new_idurl=new_idurl, old_idurl=None, position=(len(current_suppliers) - 1), )) else: if old_idurl: lg.out(2, '!!!!!!!!!!! SUBSTITUTE EXISTING SUPPLIER %d : %s->%s' % (position, old_idurl, new_idurl)) events.send('supplier-modified', dict( new_idurl=new_idurl, old_idurl=old_idurl, position=position, )) else: lg.out(2, '!!!!!!!!!!! REPLACE EMPTY SUPPLIER %d : %s' % (position, new_idurl)) events.send('supplier-modified', dict( new_idurl=new_idurl, old_idurl=None, position=position, )) self.restart_interval = 1.0
def doSubstituteSupplier(self, arg): """ Action method. """ new_idurl = arg current_suppliers = list(contactsdb.suppliers()) if new_idurl in current_suppliers: raise Exception('%s is already supplier' % new_idurl) position = -1 old_idurl = None for i in range(len(current_suppliers)): if current_suppliers[i].strip() == '': position = i break if current_suppliers[i] in self.dismiss_list: # self.dismiss_list.remove(current_suppliers[i]) position = i old_idurl = current_suppliers[i] break lg.out(10, 'fire_hire.doSubstituteSupplier position=%d' % position) if position < 0: current_suppliers.append(new_idurl) else: current_suppliers[position] = new_idurl contactsdb.update_suppliers(current_suppliers) contactsdb.save_suppliers() misc.writeSupplierData( new_idurl, 'connected', time.strftime('%d-%m-%Y %H:%M:%S')) if settings.NewWebGUI(): from web import control # control.on_suppliers_changed(current_suppliers) else: from web import webcontrol webcontrol.OnListSuppliers() if position < 0: lg.out(2, '!!!!!!!!!!! ADD SUPPLIER : %s' % (new_idurl)) else: if old_idurl: lg.out( 2, '!!!!!!!!!!! SUBSTITUTE SUPPLIER %d : %s->%s' % (position, old_idurl, new_idurl)) else: lg.out( 2, '!!!!!!!!!!! REPLACE EMPTY SUPPLIER %d : %s' % (position, new_idurl)) self.restart_interval = 1.0
def doRemoveSuppliers(self, *args, **kwargs): """ Action method. """ current_suppliers = contactsdb.suppliers() desired_suppliers = settings.getSuppliersNumberDesired() if len(current_suppliers) < desired_suppliers: lg.warn('must have more suppliers %d<%d' % (len(current_suppliers), desired_suppliers)) removed_suppliers = [] for supplier_idurl in self.dismiss_list: if id_url.is_not_in(supplier_idurl, current_suppliers, as_field=False): lg.warn('%s not a supplier' % supplier_idurl) continue pos = current_suppliers.index(id_url.field(supplier_idurl)) current_suppliers[pos] = '' removed_suppliers.append(( pos, supplier_idurl, )) misc.writeSupplierData( supplier_idurl, 'disconnected', time.strftime('%d-%m-%Y %H:%M:%S'), my_id.getLocalID(), ) current_suppliers = current_suppliers[:desired_suppliers] contactsdb.update_suppliers(current_suppliers) contactsdb.save_suppliers() from main import control control.on_suppliers_changed(current_suppliers) for position, supplier_idurl in removed_suppliers: events.send( 'supplier-modified', dict( new_idurl=None, old_idurl=supplier_idurl, position=position, )) lg.info( 'removed some suppliers : %d desired_suppliers=%d current_suppliers=%d' % (len(self.dismiss_list), desired_suppliers, len(contactsdb.suppliers()))) if _Debug: lg.out(_DebugLevel, ' my current suppliers: %r' % contactsdb.suppliers())
def doSubstituteSupplier(self, *args, **kwargs): """ Action method. """ new_idurl = id_url.field(args[0]) family_position = kwargs.get('family_position') current_suppliers = list(contactsdb.suppliers()) desired_suppliers = settings.getSuppliersNumberDesired() old_idurl = None if family_position in self.hire_list: self.hire_list.remove(family_position) lg.info( 'found position on which new supplier suppose to be hired: %d' % family_position) else: lg.warn('did not found position for new supplier to be hired on') if new_idurl in current_suppliers: raise Exception('%s is already supplier' % new_idurl) if family_position is None or family_position == -1: lg.warn( 'unknown family_position from supplier results, will pick first empty spot' ) position = -1 old_idurl = None for i in range(len(current_suppliers)): if not current_suppliers[i].strip(): position = i break if id_url.is_in(current_suppliers[i], self.dismiss_list, as_field=False): position = i old_idurl = current_suppliers[i] break family_position = position if _Debug: lg.out( _DebugLevel, 'fire_hire.doSubstituteSupplier family_position=%d' % family_position) contactsdb.add_supplier(idurl=new_idurl, position=family_position) contactsdb.save_suppliers() misc.writeSupplierData( new_idurl, 'connected', time.strftime('%d-%m-%Y %H:%M:%S'), my_id.getIDURL(), ) from main import control control.on_suppliers_changed(current_suppliers) if family_position < 0: lg.info( 'added new supplier, family position unknown: %s desired_suppliers=%d current_suppliers=%d' % (new_idurl, desired_suppliers, len(contactsdb.suppliers()))) events.send('supplier-modified', data=dict( new_idurl=new_idurl, old_idurl=None, position=family_position, ecc_map=eccmap.Current().name, family_snapshot=id_url.to_bin_list( contactsdb.suppliers()), )) else: if old_idurl: lg.info( 'hired new supplier and substitute existing supplier on position %d : %s->%s desired_suppliers=%d current_suppliers=%d' % (family_position, old_idurl, new_idurl, desired_suppliers, len(contactsdb.suppliers()))) events.send('supplier-modified', data=dict( new_idurl=new_idurl, old_idurl=old_idurl, position=family_position, ecc_map=eccmap.Current().name, family_snapshot=id_url.to_bin_list( contactsdb.suppliers()), )) else: lg.info( 'hired new supplier on empty position %d : %s desired_suppliers=%d current_suppliers=%d' % (family_position, new_idurl, desired_suppliers, len(contactsdb.suppliers()))) events.send('supplier-modified', data=dict( new_idurl=new_idurl, old_idurl=None, position=family_position, ecc_map=eccmap.Current().name, family_snapshot=id_url.to_bin_list( contactsdb.suppliers()), )) self.restart_interval = 1.0 if _Debug: lg.out(_DebugLevel, ' my current suppliers: %r' % contactsdb.suppliers())
def ListContacts(request): """ Called when "ListContacts" packet is received, it keeps a list of suppliers OR customers and some extra info. I think this is a most important method here, call different code from here. """ global _CentralStatusDict global legal_codes global OnListSuppliersFunc global OnListCustomersFunc data = request.Payload dhnio.Dprint(6, 'central_service.ListContacts\n%s' % data) words = data.split('\n', 1) if len(words) < 2: dhnio.Dprint(1, 'central_service.ListContacts ERROR wrong data packet [%s]' % str(request.Payload)) return code = words[0] if not code in legal_codes: dhnio.Dprint(1, 'central_service.ListContacts ERROR wrong data in the packet [%s] ' % str(request.Payload)) return current_contacts = [] clist, tail = dhnio._unpack_list(words[1]) fire_flag = code.startswith('f') ban_flag = code.startswith('b') contact_type = code.lower()[-1] error_flag = code[-1].islower() spaceDict = None if tail is not None: # extract info about who is alive at the moment onlineArray = '' spaceDict = dhnio._unpack_dict_from_list(tail) if spaceDict.has_key('online'): onlineArray = spaceDict.pop('online') for i in range(len(onlineArray)): if i < len(clist): if clist[i]: _CentralStatusDict[clist[i]] = onlineArray[i] # extract info about contacts local ip # if they are in same LAN we need to connect to local IP, not external local_ips = {} i = 0 while True: idurl_and_local_ip = spaceDict.get('localip%03d' % i, None) if idurl_and_local_ip is None: break try: contact_idurl, contact_local_ip = idurl_and_local_ip.split('|') except: break local_ips[contact_idurl] = contact_local_ip spaceDict.pop('localip%03d' % i) i += 1 dhnio.Dprint(6, 'central_service.ListContacts got local IP for %s: %s' % (nameurl.GetName(contact_idurl), contact_local_ip)) identitycache.SetLocalIPs(local_ips) #---suppliers if contact_type == 's': current_contacts = contacts.getSupplierIDs() contacts.setSupplierIDs(clist) eccmap.init() contacts.saveSupplierIDs() for supid in contacts.getSupplierIDs(): if supid.strip() == '': error_flag = True break dhnio.Dprint(4, "central_service.ListContacts (SUPPLIERS) code:%s error:%s length:%d" % (str(code), str(error_flag), len(clist))) for oldidurl in current_contacts: if oldidurl: if oldidurl not in clist: events.info('central_service', 'supplier %s were disconnected' % nameurl.GetName(oldidurl),) misc.writeSupplierData(oldidurl, 'disconnected', time.strftime('%d%m%y %H:%M:%S')) dhnio.Dprint(6, 'central_service.ListContacts supplier %s were disconnected' % nameurl.GetName(oldidurl)) for newidurl in clist: if newidurl: if newidurl not in current_contacts: transport_control.ClearAliveTime(newidurl) misc.writeSupplierData(newidurl, 'connected', time.strftime('%d%m%y %H:%M:%S')) events.info('central_service', 'new supplier %s connected' % nameurl.GetName(newidurl), '',) dhnio.Dprint(6, 'central_service.ListContacts new supplier %s connected' % nameurl.GetName(newidurl)) backup_control.SetSupplierList(clist) if not fire_flag and current_contacts != clist: dhnio.Dprint(6, 'central_service.ListContacts going to call suppliers') identitypropagate.suppliers(wide=True) if OnListSuppliersFunc is not None: OnListSuppliersFunc() #---customers elif contact_type == 'c': current_contacts = contacts.getCustomerIDs() contacts.setCustomerIDs(clist) contacts.saveCustomerIDs() if spaceDict is not None: dhnio._write_dict(settings.CustomersSpaceFile(), spaceDict) reactor.callLater(3, local_tester.TestUpdateCustomers) # if not fire_flag and current_contacts != clist: identitypropagate.customers(wide=True) dhnio.Dprint(4, "central_service.ListContacts (CUSTOMERS) code:%s error:%s length:%d" % (str(code), str(error_flag), len(clist))) for oldidurl in current_contacts: if oldidurl not in clist: events.info('central_service', 'customer %s were disconnected' % nameurl.GetName(oldidurl),) dhnio.Dprint(6, 'central_service.ListContacts customer %s were disconnected' % nameurl.GetName(oldidurl)) for newidurl in clist: if newidurl not in current_contacts: transport_control.ClearAliveTime(newidurl) events.info('central_service', 'new customer %s connected' % nameurl.GetName(newidurl)) dhnio.Dprint(6, 'central_service.ListContacts new customer %s connected' % nameurl.GetName(newidurl)) if OnListCustomersFunc is not None: OnListCustomersFunc() #---fire_flag if fire_flag: if contact_type == 's': index = -1 for index in range(len(clist)): if clist[index] != current_contacts[index]: break if index >= 0: # we want to send our Identity to new supplier # and than ask a list of files he have # so it should start rebuilding backups immediately # right after we got ListFiles from him identitypropagate.single(clist[index], wide=True) #---ban_flag if ban_flag: events.notify('central_service', 'you have negative balance, all your suppliers was removed', '',) dhnio.Dprint(2, 'central_service.ListContacts !!! you have negative balance, all your suppliers was removed !!!') #---error_flag if error_flag: #reactor.callLater(settings.DefaultNeedSuppliersPacketTimeOut(), SendRequestSuppliers) events.info('central_service', 'could not find available suppliers', 'Central server can not find available suppliers for you.\nCheck your central settings.\n',) dhnio.Dprint(2, 'central_service.ListContacts !!! could not find available suppliers !!!') #---send ack transport_control.SendAck(request) #---automats if contact_type == 's': central_connector.A('list-suppliers', clist) fire_hire.A('list-suppliers', (current_contacts, clist)) data_sender.A('restart') elif contact_type == 'c': central_connector.A('list-customers', clist) #---transport_udp if transport_control._TransportUDPEnable: import lib.transport_udp as transport_udp new_contacts = contacts.getContactsAndCorrespondents() transport_udp.ListContactsCallback(current_contacts, new_contacts)
def doSubstituteSupplier(self, *args, **kwargs): """ Action method. """ new_idurl = strng.to_bin(args[0]) family_position = kwargs.get('family_position') current_suppliers = list(contactsdb.suppliers()) old_idurl = None if family_position in self.hire_list: self.hire_list.remove(family_position) lg.info('found position on which new supplier suppose to be hired: %d' % family_position) else: lg.warn('did not found position for new supplier to be hired on') if new_idurl in current_suppliers: raise Exception('%s is already supplier' % new_idurl) if not family_position: lg.warn('unknown family_position from supplier results, will pick first empty spot') position = -1 old_idurl = None for i in range(len(current_suppliers)): if not current_suppliers[i].strip(): position = i break if current_suppliers[i] in self.dismiss_list: # self.dismiss_list.remove(current_suppliers[i]) position = i old_idurl = current_suppliers[i] break family_position = position lg.out(10, 'fire_hire.doSubstituteSupplier family_position=%d' % family_position) contactsdb.add_supplier(idurl=new_idurl, position=family_position) contactsdb.save_suppliers() misc.writeSupplierData( new_idurl, 'connected', time.strftime('%d-%m-%Y %H:%M:%S'), my_id.getLocalID(), ) from main import control control.on_suppliers_changed(current_suppliers) if family_position < 0: lg.out(2, '!!!!!!!!!!! ADDED NEW SUPPLIER : %s' % new_idurl) events.send('supplier-modified', dict( new_idurl=new_idurl, old_idurl=None, position=family_position, ecc_map=eccmap.Current().name, family_snapshot=contactsdb.suppliers(), )) else: if old_idurl: lg.out(2, '!!!!!!!!!!! SUBSTITUTE EXISTING SUPPLIER %d : %s->%s' % (family_position, old_idurl, new_idurl)) events.send('supplier-modified', dict( new_idurl=new_idurl, old_idurl=old_idurl, position=family_position, ecc_map=eccmap.Current().name, family_snapshot=contactsdb.suppliers(), )) else: lg.out(2, '!!!!!!!!!!! REPLACE EMPTY SUPPLIER %d : %s' % (family_position, new_idurl)) events.send('supplier-modified', dict( new_idurl=new_idurl, old_idurl=None, position=family_position, ecc_map=eccmap.Current().name, family_snapshot=contactsdb.suppliers(), )) self.restart_interval = 1.0