def _on_supplier_response(self, newpacket, info): wrapped_packet = signed.Unserialize(newpacket.Payload) if _Debug: lg.args(_DebugLevel, newpacket=newpacket, wrapped_packet=wrapped_packet) if not wrapped_packet or not wrapped_packet.Valid(): lg.err('incoming Data() is not valid') return supplier_idurl = wrapped_packet.RemoteID from storage import backup_control supplier_revision = backup_control.IncomingSupplierBackupIndex( wrapped_packet) self.requesting_suppliers.discard(supplier_idurl) if supplier_revision is not None: reactor.callLater(0, self.automat, 'index-file-received', ( newpacket, supplier_revision, )) # @UndefinedVariable if _Debug: lg.out( _DebugLevel, 'index_synchronizer._on_supplier_response %s from %r, pending: %d, total: %d' % (newpacket, supplier_idurl, len(self.requesting_suppliers), self.requested_suppliers_number)) if len(self.requesting_suppliers) == 0: reactor.callLater(0, self.automat, 'all-responded') # @UndefinedVariable
def _on_inbox_packet_received(self, newpacket, info, status, error_message): from logs import lg from main import settings from contacts import contactsdb from userid import my_id from userid import global_id from storage import backup_control from p2p import commands if newpacket.Command == commands.Data(): if newpacket.OwnerID != my_id.getLocalID(): # only catch data belongs to me return False lg.out( self.debug_level, "service_backups._on_inbox_packet_received: %r for us from %s" % ( newpacket, newpacket.RemoteID, )) if newpacket.PacketID == global_id.MakeGlobalID( idurl=my_id.getLocalID(), path=settings.BackupIndexFileName(), ): # TODO: move to service_backup_db backup_control.IncomingSupplierBackupIndex(newpacket) return True if newpacket.Command == commands.Files(): if not newpacket.PacketID.startswith(my_id.getGlobalID() + ':'): # skip Files() which are from another customer return False if not contactsdb.is_supplier(newpacket.OwnerID): # skip Files() if this is not my supplier return False lg.out( self.debug_level, "service_backups._on_inbox_packet_received: %r for us from %s" % ( newpacket, newpacket.RemoteID, )) return backup_control.IncomingSupplierListFiles(newpacket) return False
def _on_supplier_response(self, newpacket, pkt_out): if newpacket.Command == commands.Data(): wrapped_packet = signed.Unserialize(newpacket.Payload) if not wrapped_packet or not wrapped_packet.Valid(): lg.err('incoming Data() is not valid') return from storage import backup_control backup_control.IncomingSupplierBackupIndex(wrapped_packet) # p2p_service.SendAck(newpacket) self.requesting_suppliers.discard(wrapped_packet.RemoteID) elif newpacket.Command == commands.Fail(): self.requesting_suppliers.discard(newpacket.OwnerID) else: raise Exception('wrong type of response') if _Debug: lg.out( _DebugLevel, 'index_synchronizer._on_supplier_response %s, pending: %d, total: %d' % (newpacket, len(self.requesting_suppliers), self.requested_suppliers_number)) if len(self.requesting_suppliers) == 0: self.automat('all-responded')
def Data(request): """ This is when we 1) save my requested data to restore the backup 2) or save the customer file on our local HDD. """ if _Debug: lg.out( _DebugLevel, 'p2p_service.Data %d bytes in [%s] by %s | %s' % (len(request.Payload), request.PacketID, request.OwnerID, request.CreatorID)) # 1. this is our Data! if request.OwnerID == my_id.getLocalID(): if _Debug: lg.out( _DebugLevel, "p2p_service.Data %r for us from %s" % (request, nameurl.GetName(request.RemoteID))) if driver.is_on('service_backups'): # TODO: move this into callback settings.BackupIndexFileName() indexPacketID = global_id.MakeGlobalID( idurl=my_id.getLocalID(), path=settings.BackupIndexFileName()) if request.PacketID == indexPacketID: from storage import backup_control backup_control.IncomingSupplierBackupIndex(request) return True return False # 2. this Data is not belong to us if not driver.is_on('service_supplier'): return SendFail(request, 'supplier service is off') if not contactsdb.is_customer(request.OwnerID): # SECURITY lg.warn("%s not a customer, packetID=%s" % (request.OwnerID, request.PacketID)) SendFail(request, 'not a customer') return glob_path = global_id.ParseGlobalID(request.PacketID) if not glob_path['path']: # backward compatible check glob_path = global_id.ParseGlobalID(my_id.getGlobalID() + ':' + request.PacketID) if not glob_path['path']: lg.warn("got incorrect PacketID") SendFail(request, 'incorrect PacketID') return # TODO: process files from another customer : glob_path['idurl'] filename = makeFilename(request.OwnerID, glob_path['path']) if not filename: lg.warn("got empty filename, bad customer or wrong packetID? ") SendFail(request, 'empty filename') return dirname = os.path.dirname(filename) if not os.path.exists(dirname): try: bpio._dirs_make(dirname) except: lg.warn("ERROR can not create sub dir " + dirname) SendFail(request, 'write error') return data = request.Serialize() donated_bytes = settings.getDonatedBytes() if not os.path.isfile(settings.CustomersSpaceFile()): bpio._write_dict(settings.CustomersSpaceFile(), {'free': donated_bytes}) if _Debug: lg.out(_DebugLevel, 'p2p_service.Data created a new space file') space_dict = bpio._read_dict(settings.CustomersSpaceFile()) if request.OwnerID not in space_dict.keys(): lg.warn("no info about donated space for %s" % request.OwnerID) SendFail(request, 'no info about donated space') return used_space_dict = bpio._read_dict(settings.CustomersUsedSpaceFile(), {}) if request.OwnerID in used_space_dict.keys(): try: bytes_used_by_customer = int(used_space_dict[request.OwnerID]) bytes_donated_to_customer = int(space_dict[request.OwnerID]) if bytes_donated_to_customer - bytes_used_by_customer < len(data): lg.warn("no free space for %s" % request.OwnerID) SendFail(request, 'no free space') return except: lg.exc() if not bpio.WriteFile(filename, data): lg.warn("ERROR can not write to " + str(filename)) SendFail(request, 'write error') return SendAck(request, str(len(request.Payload))) from supplier import local_tester reactor.callLater(0, local_tester.TestSpaceTime) del data if _Debug: lg.out( _DebugLevel, "p2p_service.Data saved from [%s | %s] to %s" % ( request.OwnerID, request.CreatorID, filename, ))