Esempio n. 1
0
    def _on_files_received(self, newpacket, info):
        from logs import lg
        from lib import serialization
        from main import settings
        from main import events
        from p2p import p2p_service
        from storage import backup_fs
        from storage import backup_control
        from crypt import encrypted
        from crypt import my_keys
        from userid import my_id
        from userid import global_id
        from storage import backup_matrix
        from supplier import list_files
        from contacts import contactsdb
        list_files_global_id = global_id.ParseGlobalID(newpacket.PacketID)
        if not list_files_global_id['idurl']:
            lg.warn('invalid PacketID: %s' % newpacket.PacketID)
            return False
        trusted_customer_idurl = list_files_global_id['idurl']
        incoming_key_id = list_files_global_id['key_id']
        if trusted_customer_idurl == my_id.getGlobalID():
            lg.warn('skip %s packet which seems to came from my own supplier' %
                    newpacket)
            # only process list Files() from other users who granted me access
            return False
        if not my_keys.is_valid_key_id(incoming_key_id):
            lg.warn('ignore, invalid key id in packet %s' % newpacket)
            return False
        if not my_keys.is_key_private(incoming_key_id):
            lg.warn('private key is not registered : %s' % incoming_key_id)
            p2p_service.SendFail(newpacket, 'private key is not registered')
            return False
        try:
            block = encrypted.Unserialize(
                newpacket.Payload,
                decrypt_key=incoming_key_id,
            )
        except:
            lg.exc(newpacket.Payload)
            return False
        if block is None:
            lg.warn('failed reading data from %s' % newpacket.RemoteID)
            return False
#         if block.CreatorID != trusted_customer_idurl:
#             lg.warn('invalid packet, creator ID must be present in packet ID : %s ~ %s' % (
#                 block.CreatorID, list_files_global_id['idurl'], ))
#             return False
        try:
            raw_files = block.Data()
        except:
            lg.exc()
            return False
        if block.CreatorID == trusted_customer_idurl:
            # this is a trusted guy sending some shared files to me
            try:
                json_data = serialization.BytesToDict(raw_files,
                                                      keys_to_text=True)
                json_data['items']
            except:
                lg.exc()
                return False
            count = backup_fs.Unserialize(
                raw_data=json_data,
                iter=backup_fs.fs(trusted_customer_idurl),
                iterID=backup_fs.fsID(trusted_customer_idurl),
                from_json=True,
            )
            p2p_service.SendAck(newpacket)
            events.send(
                'shared-list-files-received',
                dict(
                    customer_idurl=trusted_customer_idurl,
                    new_items=count,
                ))
            if count == 0:
                lg.warn('no files were imported during file sharing')
            else:
                backup_control.Save()
                lg.info('imported %d shared files from %s, key_id=%s' % (
                    count,
                    trusted_customer_idurl,
                    incoming_key_id,
                ))
            return True
        # otherwise this must be an external supplier sending us a files he stores for trusted customer
        external_supplier_idurl = block.CreatorID
        try:
            supplier_raw_list_files = list_files.UnpackListFiles(
                raw_files, settings.ListFilesFormat())
            backup_matrix.SaveLatestRawListFiles(
                supplier_idurl=external_supplier_idurl,
                raw_data=supplier_raw_list_files,
                customer_idurl=trusted_customer_idurl,
            )
        except:
            lg.exc()
            return False
        # need to detect supplier position from the list of packets
        # and place that supplier on the correct position in contactsdb
        real_supplier_pos = backup_matrix.DetectSupplierPosition(
            supplier_raw_list_files)
        known_supplier_pos = contactsdb.supplier_position(
            external_supplier_idurl, trusted_customer_idurl)
        if real_supplier_pos >= 0:
            if known_supplier_pos >= 0 and known_supplier_pos != real_supplier_pos:
                lg.warn(
                    'external supplier %s position is not matching to list files, rewriting for customer %s'
                    % (external_supplier_idurl, trusted_customer_idurl))
                contactsdb.erase_supplier(
                    idurl=external_supplier_idurl,
                    customer_idurl=trusted_customer_idurl,
                )
            contactsdb.add_supplier(
                idurl=external_supplier_idurl,
                position=real_supplier_pos,
                customer_idurl=trusted_customer_idurl,
            )
            contactsdb.save_suppliers(customer_idurl=trusted_customer_idurl)
        else:
            lg.warn(
                'not possible to detect external supplier position for customer %s'
                % trusted_customer_idurl)
        # finally send ack packet back
        p2p_service.SendAck(newpacket)
        lg.info(
            'received list of packets from external supplier %s for customer %s'
            % (external_supplier_idurl, trusted_customer_idurl))
        return True
Esempio n. 2
0
 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())
Esempio n. 3
0
 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