예제 #1
0
def SendIdentity(remote_idurl, wide=False, timeout=10, callbacks={}):
    """
    """
    packet_id = 'identity:%s' % packetid.UniqueID()
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendIdentity to %s wide=%s packet_id=%r" % (
                nameurl.GetName(remote_idurl),
                wide,
                packet_id,
            ))
    result = signed.Packet(
        Command=commands.Identity(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=my_id.getLocalIdentity().serialize(),
        RemoteID=remote_idurl,
    )
    gateway.outbox(result,
                   wide=wide,
                   callbacks=callbacks,
                   response_timeout=timeout)
    return result
예제 #2
0
def send(customer_idurl, packet_id, format_type):
    customer_name = nameurl.GetName(customer_idurl)
    MyID = my_id.getLocalID()
    RemoteID = customer_idurl
    PacketID = packet_id
    if _Debug:
        lg.out(
            _DebugLevel, "list_files.send to %s, format is '%s'" %
            (customer_name, format_type))
    custdir = settings.getCustomersFilesDir()
    ownerdir = os.path.join(custdir, nameurl.UrlFilename(customer_idurl))
    if not os.path.isdir(ownerdir):
        if _Debug:
            lg.out(_DebugLevel,
                   "list_files.send did not found customer dir: " + ownerdir)
        src = PackListFiles('', format_type)
        result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src,
                               RemoteID)
        gateway.outbox(result)
        return result
    plaintext = TreeSummary(ownerdir)
    if _Debug:
        lg.out(_DebugLevel + 8, '\n%s' % (plaintext))
    src = PackListFiles(plaintext, format_type)
    result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src,
                           RemoteID)
    gateway.outbox(result)
    return result
예제 #3
0
def SendToID(idurl, ack_handler=None, Payload=None, NeedAck=False, wide=False):
    """
    Create ``packet`` with my Identity file and calls
    ``transport.gateway.outbox()`` to send it.
    """
    lg.out(
        8, "propagate.SendToID [%s] wide=%s" %
        (nameurl.GetName(idurl), str(wide)))
    if ack_handler is None:
        ack_handler = HandleAck
    thePayload = Payload
    if thePayload is None:
        thePayload = my_id.getLocalIdentity().serialize()
    p = signed.Packet(
        commands.Identity(),
        my_id.getLocalID(),  # MyID,
        my_id.getLocalID(),  # MyID,
        'Identity',  # my_id.getLocalID(), #PacketID,
        thePayload,
        idurl)
    # callback.register_interest(AckHandler, p.RemoteID, p.PacketID)
    gateway.outbox(p,
                   wide,
                   callbacks={
                       commands.Ack(): ack_handler,
                       commands.Fail(): ack_handler,
                   })
    if wide:
        # this is a ping packet - need to clear old info
        stats.ErasePeerProtosStates(idurl)
        stats.EraseMyProtosStates(idurl)
예제 #4
0
def SendEvent(remote_idurl, event_id, payload=None,
              producer_id=None, message_id=None, created=None,
              packet_id=None, wide=False, callbacks={}, response_timeout=5):
    if packet_id is None:
        packet_id = packetid.UniqueID()
    e_json = {
        'event_id': event_id,
        'payload': payload,
    }
    if producer_id and message_id:
        e_json['producer_id'] = producer_id
        e_json['message_id'] = message_id
    if created:
        e_json['created'] = created
    e_json_src = serialization.DictToBytes(e_json)
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendEvent to %s with %d bytes message json data" % (
            remote_idurl, len(e_json_src)))
    outpacket = signed.Packet(
        Command=commands.Event(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=e_json_src,
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=response_timeout)
    return outpacket
예제 #5
0
def SendListFiles(target_supplier, customer_idurl=None, key_id=None, wide=False, callbacks={}):
    """
    This is used as a request method from your supplier : if you send him a ListFiles() packet
    he will reply you with a list of stored files in a Files() packet.
    """
    MyID = my_id.getLocalID()
    if not customer_idurl:
        customer_idurl = MyID
    if not str(target_supplier).isdigit():
        RemoteID = target_supplier
    else:
        RemoteID = contactsdb.supplier(target_supplier, customer_idurl=customer_idurl)
    if not RemoteID:
        lg.warn("RemoteID is empty target_supplier=%s" % str(target_supplier))
        return None
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendListFiles to %s" % nameurl.GetName(RemoteID))
    if not key_id:
        key_id = global_id.MakeGlobalID(idurl=customer_idurl, key_alias='customer')
    PacketID = "%s:%s" % (key_id, packetid.UniqueID(), )
    Payload = settings.ListFilesFormat()
    result = signed.Packet(
        Command=commands.ListFiles(),
        OwnerID=MyID,
        CreatorID=MyID,
        PacketID=PacketID,
        Payload=Payload,
        RemoteID=RemoteID,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #6
0
def SendRequestService(remote_idurl,
                       service_name,
                       json_payload={},
                       wide=False,
                       callbacks={},
                       timeout=10):
    service_info = {
        'name': service_name,
        'payload': json_payload,
    }
    service_info_raw = serialization.DictToBytes(service_info)
    if _Debug:
        lg.out(
            _DebugLevel, 'p2p_service.SendRequestService "%s" to %s with %r' %
            (service_name, remote_idurl, service_info))
    result = signed.Packet(
        commands.RequestService(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        service_info_raw,
        remote_idurl,
    )
    gateway.outbox(result,
                   wide=wide,
                   callbacks=callbacks,
                   response_timeout=timeout)
    return result
예제 #7
0
def SendContacts(remote_idurl, json_payload={}, wide=False, callbacks={}):
    """
    """
    MyID = my_id.getLocalID()
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendContacts to %s" % nameurl.GetName(remote_idurl))
    PacketID = packetid.UniqueID()
    try:
        json_payload['type']
        json_payload['space']
    except:
        lg.err()
        return None
    Payload = serialization.DictToBytes(json_payload)
    result = signed.Packet(
        Command=commands.Contacts(),
        OwnerID=MyID,
        CreatorID=MyID,
        PacketID=PacketID,
        Payload=Payload,
        RemoteID=remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #8
0
def ListFiles(request):
    """
    We will want to use this to see what needs to be resent, and expect normal
    case is very few missing.

    This is to build the ``Files()`` we are holding for a customer.
    """
    if not driver.is_started("service_supplier"):
        return SendFail(request, "supplier service is off")
    MyID = my_id.getLocalID()
    RemoteID = request.OwnerID
    PacketID = request.PacketID
    Payload = request.Payload
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.ListFiles from [%s], format is %s" % (nameurl.GetName(request.OwnerID), Payload)
        )
    custdir = settings.getCustomersFilesDir()
    ownerdir = os.path.join(custdir, nameurl.UrlFilename(request.OwnerID))
    if not os.path.isdir(ownerdir):
        if _Debug:
            lg.out(_DebugLevel, "p2p_service.ListFiles did not find customer dir " + ownerdir)
        src = PackListFiles("", Payload)
        result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID)
        gateway.outbox(result)
        return result
    plaintext = TreeSummary(ownerdir)
    if _Debug:
        lg.out(_DebugLevel + 4, "\n%s" % (plaintext))
    src = PackListFiles(plaintext, Payload)
    result = signed.Packet(commands.Files(), MyID, MyID, PacketID, src, RemoteID)
    gateway.outbox(result)
    return result
예제 #9
0
def SendCancelService(remote_idurl,
                      service_name,
                      json_payload={},
                      wide=False,
                      callbacks={}):
    service_info = {
        'name': service_name,
        'payload': json_payload,
    }
    service_info_raw = json.dumps(service_info)
    if _Debug:
        lg.out(
            _DebugLevel,
            'p2p_service.SendCancelService "%s" to %s with %d bytes payload' %
            (service_name, remote_idurl, len(service_info_raw)))
    result = signed.Packet(
        commands.CancelService(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        service_info_raw,
        remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #10
0
def SendKey(
    remote_idurl,
    encrypted_key_data,
    packet_id=None,
    wide=False,
    callbacks={},
    timeout=10,
):
    if packet_id is None:
        packet_id = packetid.UniqueID()
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendKey [%s] to %s with %d bytes encrypted key data" %
            (packet_id, remote_idurl, len(encrypted_key_data)))
    outpacket = signed.Packet(
        Command=commands.Key(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=encrypted_key_data,
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket,
                   wide=wide,
                   callbacks=callbacks,
                   response_timeout=timeout)
    return outpacket
예제 #11
0
def SendCancelService(remote_idurl,
                      service_name,
                      json_payload={},
                      wide=False,
                      callbacks={}):
    service_info = {
        'name': service_name,
        'payload': json_payload,
    }
    service_info_raw = serialization.DictToBytes(service_info)
    if _Debug:
        lg.out(
            _DebugLevel,
            'p2p_service.SendCancelService "%s" to %s with %d bytes payload' %
            (service_name, remote_idurl, len(service_info_raw)))
    result = signed.Packet(
        Command=commands.CancelService(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetid.UniqueID(),
        Payload=service_info_raw,
        RemoteID=remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #12
0
def SendAuditKey(remote_idurl,
                 encrypted_payload,
                 packet_id=None,
                 timeout=10,
                 wide=False,
                 callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAuditKey to %s with %d bytes in json payload data"
            % (remote_idurl, len(encrypted_payload)))
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        Command=commands.AuditKey(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=encrypted_payload,
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket,
                   wide=wide,
                   callbacks=callbacks,
                   response_timeout=timeout)
    return outpacket
예제 #13
0
def SendToIDs(idlist, wide=False, ack_handler=None, timeout_handler=None, response_timeout=20):
    """
    Same, but send to many IDs and also check previous packets to not re-send.
    """
    global _PropagateCounter
    if _Debug:
        lg.out(_DebugLevel, "propagate.SendToIDs to %d users, wide=%s" % (len(idlist), wide))
    if ack_handler is None:
        ack_handler = HandleAck
    if timeout_handler is None:
        timeout_handler = HandleTimeOut
    LocalIdentity = my_id.getLocalIdentity()
    Payload = strng.to_bin(LocalIdentity.serialize())
    alreadysent = set()
    totalsent = 0
    inqueue = {}
    found_previous_packets = 0
    for pkt_out in packet_out.queue():
        if id_url.is_in(pkt_out.remote_idurl, idlist, as_field=False):
            if pkt_out.description.count('Identity'):
                if pkt_out.remote_idurl not in inqueue:
                    inqueue[pkt_out.remote_idurl] = 0
                inqueue[pkt_out.remote_idurl] += 1
                found_previous_packets += 1
    for contact in idlist:
        if not contact:
            continue
        if contact in alreadysent:
            # just want to send once even if both customer and supplier
            continue
        if contact in inqueue and inqueue[contact] > 2:
            # now only 2 protocols is working: tcp and udp
            if _Debug:
                lg.out(_DebugLevel, '        skip sending [Identity] to %s, packet already in the queue' % contact)
            continue
        p = signed.Packet(
            Command=commands.Identity(),
            OwnerID=my_id.getLocalID(),
            CreatorID=my_id.getLocalID(),
            PacketID=('propagate:%d:%s' % (_PropagateCounter, packetid.UniqueID())),
            Payload=Payload,
            RemoteID=contact,
        )
        _PropagateCounter += 1
        if _Debug:
            lg.out(_DebugLevel, "        sending [Identity] to %s" % nameurl.GetName(contact))
        gateway.outbox(p, wide, response_timeout=response_timeout, callbacks={
            commands.Ack(): ack_handler,
            commands.Fail(): ack_handler,
            None: timeout_handler,
        })
        if wide:
            # this is a ping packet - need to clear old info
            p2p_stats.ErasePeerProtosStates(contact)
            p2p_stats.EraseMyProtosStates(contact)
        alreadysent.add(contact)
        totalsent += 1
    del alreadysent
    return totalsent
예제 #14
0
def SendRetreiveCoin(remote_idurl, query, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendRetreiveCoin to %s" % remote_idurl)
    outpacket = signed.Packet(
        commands.Coin(), my_id.getLocalID(), my_id.getLocalID(), packetid.UniqueID(), json.dumps(query), remote_idurl
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
예제 #15
0
def SendDeleteBackup(SupplierID, BackupID):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendDeleteBackup SupplierID=%s  BackupID=%s" % (SupplierID, BackupID))
    MyID = my_id.getLocalID()
    PacketID = packetid.RemotePath(BackupID)
    RemoteID = SupplierID
    result = signed.Packet(commands.DeleteBackup(), MyID, MyID, PacketID, "", RemoteID)
    gateway.outbox(result)
    return result
예제 #16
0
def SendDeleteFile(SupplierID, pathID):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendDeleteFile SupplierID=%s PathID=%s" % (SupplierID, pathID))
    MyID = my_id.getLocalID()
    PacketID = pathID
    RemoteID = SupplierID
    result = signed.Packet(commands.DeleteFile(), MyID, MyID, PacketID, "", RemoteID)
    gateway.outbox(result)
    return result
예제 #17
0
def SendAckNoRequest(remoteID, packetid, response="", wide=False, callbacks={}):
    result = signed.Packet(commands.Ack(), my_id.getLocalID(), my_id.getLocalID(), packetid, response, remoteID)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAckNoRequest %s to %s    response: %s ..."
            % (result.PacketID, result.RemoteID, str(response)[:15]),
        )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
예제 #18
0
def SendDeleteBackup(SupplierID, BackupID):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendDeleteBackup SupplierID=%s  BackupID=%s " % (SupplierID, BackupID))
    MyID = my_id.getLocalID()
    PacketID = BackupID
    RemoteID = SupplierID
    result = signed.Packet(commands.DeleteBackup(), MyID, MyID, PacketID, "", RemoteID)
    gateway.outbox(result)
    return result
예제 #19
0
def SendRetrieveCoin(remote_idurl, query, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel,
               "p2p_service.SendRetrieveCoin to %s" % remote_idurl)
    outpacket = signed.Packet(commands.RetrieveCoin(), my_id.getLocalID(),
                              my_id.getLocalID(), packetid.UniqueID(),
                              json.dumps(query), remote_idurl)
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
예제 #20
0
def SendDeleteListBackups(SupplierID, ListBackupIDs):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendDeleteListBackups SupplierID=%s BackupIDs: %s" % (SupplierID, ListBackupIDs))
    MyID = my_id.getLocalID()
    PacketID = packetid.UniqueID()
    RemoteID = SupplierID
    Payload = '\n'.join(ListBackupIDs)
    result = signed.Packet(commands.DeleteBackup(), MyID, MyID, PacketID, Payload, RemoteID)
    gateway.outbox(result)
    return result
예제 #21
0
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCoin to %s" % remote_idurl)
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        commands.Coin(), my_id.getLocalID(), my_id.getLocalID(), packet_id, json.dumps(coins), remote_idurl
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
예제 #22
0
 def _send(c):
     from transport.udp import udp_stream
     for idurl in sys.argv[2:]:
         print('_send', list(udp_stream.streams().keys()))
         p = signed.Packet(commands.Data(), my_id.getLocalID(),
                           my_id.getLocalID(), 'packet%d' % c,
                           bpio.ReadBinaryFile(sys.argv[1]), idurl)
         gateway.outbox(p)
     if c > 1:
         reactor.callLater(0.01, _send, c - 1)
예제 #23
0
def SendCancelService(remote_idurl, service_info, callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendCancelService [%s]" %
            service_info.replace('\n', ' ')[:40])
    result = signed.Packet(commands.CancelService(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid.UniqueID(),
                           service_info, remote_idurl)
    gateway.outbox(result, callbacks=callbacks)
    return result
예제 #24
0
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCoin to %s with %d records" % (remote_idurl, len(coins)))
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        commands.Coin(), my_id.getLocalID(),
        my_id.getLocalID(), packet_id,
        serialization.DictToBytes(coins), remote_idurl)
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
예제 #25
0
def SendAck(packettoack, response='', wide=False, callbacks={}, packetid=None):
    result = signed.Packet(commands.Ack(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid
                           or packettoack.PacketID, response,
                           packettoack.OwnerID)
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendAck %s to %s  with %d bytes" %
            (result.PacketID, result.RemoteID, len(response)))
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #26
0
def SendRequestService(remote_idurl, service_info, wide=False, callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendRequestService to %s [%s]" %
            (nameurl.GetName(remote_idurl), service_info.replace('\n',
                                                                 ' ')[:40]))
    result = signed.Packet(commands.RequestService(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid.UniqueID(),
                           service_info, remote_idurl)
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #27
0
def SendIdentity(remote_idurl, wide=False, callbacks={}):
    """
    """
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendIdentity to %s" % nameurl.GetName(remote_idurl))
    result = signed.Packet(commands.Identity(), my_id.getLocalID(),
                           my_id.getLocalID(), 'identity',
                           my_id.getLocalIdentity().serialize(), remote_idurl)
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #28
0
 def _send(c):
     from transport.udp import udp_stream
     for idurl in sys.argv[2:]:
         print '_send', udp_stream.streams().keys()
         p = signed.Packet(commands.Data(),
                           my_id.getLocalID(),
                           my_id.getLocalID(),
                           'packet%d' % c,
                           bpio.ReadBinaryFile(sys.argv[1]),
                           idurl)
         gateway.outbox(p)
     if c > 1:
         reactor.callLater(0.01, _send, c - 1)
예제 #29
0
def SendAckNoRequest(remoteID,
                     packetid,
                     response='',
                     wide=False,
                     callbacks={}):
    result = signed.Packet(commands.Ack(), my_id.getLocalID(),
                           my_id.getLocalID(), packetid, response, remoteID)
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAckNoRequest %s to %s    response: %s ..." %
            (result.PacketID, result.RemoteID, str(response)[:15]))
    gateway.outbox(result, wide=wide, callbacks=callbacks)
예제 #30
0
def SendCancelService(remote_idurl, service_info, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCancelService [%s]" % service_info.replace("\n", " ")[:40])
    result = signed.Packet(
        commands.CancelService(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        service_info,
        remote_idurl,
    )
    gateway.outbox(result, callbacks=callbacks)
    return result
예제 #31
0
def SendAckNoRequest(remoteID, packetid, response='', wide=False, callbacks={}):
    result = signed.Packet(
        Command=commands.Ack(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetid,
        Payload=response,
        RemoteID=remoteID,
    )
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendAckNoRequest packetID=%s to %s  with %d bytes" % (
            result.PacketID, result.RemoteID, len(response)))
    gateway.outbox(result, wide=wide, callbacks=callbacks)
예제 #32
0
def SendDeleteListBackups(SupplierID, ListBackupIDs):
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendDeleteListBackups SupplierID=%s BackupIDs number: %d" % (SupplierID, len(ListBackupIDs)),
        )
    MyID = my_id.getLocalID()
    PacketID = packetid.UniqueID()
    RemoteID = SupplierID
    Payload = "\n".join(ListBackupIDs)
    result = signed.Packet(commands.DeleteBackup(), MyID, MyID, PacketID, Payload, RemoteID)
    gateway.outbox(result)
    return result
예제 #33
0
def SendFailNoRequest(remoteID, packetID, response=''):
    result = signed.Packet(
        Command=commands.Fail(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetID,
        Payload=response,
        RemoteID=remoteID,
    )
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendFailNoRequest packetID=%s to %s" % (result.PacketID, result.RemoteID))
    gateway.outbox(result)
    return result
예제 #34
0
def SendRetrieveCoin(remote_idurl, query, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendRetrieveCoin to %s" % remote_idurl)
    outpacket = signed.Packet(
        Command=commands.RetrieveCoin(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packetid.UniqueID(),
        Payload=serialization.DictToBytes(query),
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
예제 #35
0
def SendDeleteListPaths(SupplierID, ListPathIDs):
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendDeleteListPaths SupplierID=%s PathIDs number: %d"
            % (SupplierID, len(ListPathIDs)))
    MyID = my_id.getLocalID()
    PacketID = packetid.UniqueID()
    RemoteID = SupplierID
    Payload = '\n'.join(ListPathIDs)
    result = signed.Packet(commands.DeleteFile(), MyID, MyID, PacketID,
                           Payload, RemoteID)
    gateway.outbox(result)
    return result
예제 #36
0
def SendFail(request, response="", remote_idurl=None):
    if remote_idurl is None:
        remote_idurl = request.OwnerID
    result = signed.Packet(
        commands.Fail(), my_id.getLocalID(), my_id.getLocalID(), request.PacketID, response, remote_idurl
    )
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendFail %s to %s    response: %s ..."
            % (result.PacketID, result.RemoteID, str(response)[:15]),
        )
    gateway.outbox(result)
    return result
예제 #37
0
 def doSendMyIdentity(self, *args, **kwargs):
     """
     Action method.
     """
     global _KnownChannels
     self.ping_attempts += 1
     if self.fake_identity:
         identity_object = self.fake_identity
     else:
         identity_object = my_id.getLocalIdentity()
     if not identity_object.Valid():
         raise Exception('can not use invalid identity for ping')
     if self.channel_counter:
         packet_id = '%s:%d:%d:%s' % (self.channel, _KnownChannels[self.channel], self.ping_attempts, packetid.UniqueID())
     else:
         packet_id = '%s:%d:%s' % (self.channel, self.ping_attempts, packetid.UniqueID())
     ping_packet = signed.Packet(
         Command=commands.Identity(),
         OwnerID=my_id.getIDURL(),
         CreatorID=my_id.getIDURL(),
         PacketID=packet_id,
         Payload=strng.to_bin(identity_object.serialize()),
         RemoteID=self.remote_idurl,
     )
     if self.skip_outbox:
         packet_out.create(
             outpacket=ping_packet,
             wide=True,
             response_timeout=self.ack_timeout,
             callbacks={
                 commands.Ack(): lambda response, info: self.automat('ack-received', response=response, info=info),
                 commands.Fail(): lambda response, info: self.automat('fail-received', response=response, info=info),
                 None: lambda pkt_out: self.automat('ack-timeout', pkt_out),
             },
             keep_alive=self.keep_alive,
         )
     else:
         gateway.outbox(
             outpacket=ping_packet,
             wide=True,
             response_timeout=self.ack_timeout,
             callbacks={
                 commands.Ack(): lambda response, info: self.automat('ack-received', response=response, info=info),
                 commands.Fail(): lambda response, info: self.automat('fail-received', response=response, info=info),
                 None: lambda pkt_out: self.automat('ack-timeout', pkt_out),
             },
             keep_alive=self.keep_alive,
         )
     if _Debug:
         lg.args(_DebugLevel, packet_id=packet_id, remote_idurl=self.remote_idurl, ping_attempts=self.ping_attempts)
예제 #38
0
def SendFailNoRequest(remoteID, packetID, response=''):
    result = signed.Packet(
        commands.Fail(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetID,
        response,
        remoteID,
    )
    if _Debug:
        lg.out(
            _DebugLevel, "p2p_service.SendFailNoRequest packetID=%s to %s" %
            (result.PacketID, result.RemoteID))
    gateway.outbox(result)
    return result
예제 #39
0
def RequestIdentity(request):
    """
    Someone is requesting a copy of our current identity.

    Already verified that they are a contact. Can also be used as a sort
    of "ping" test to make sure we are alive.
    """
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.RequestIdentity from %s" % request.OwnerID)
    MyID = my_id.getLocalID()
    RemoteID = request.OwnerID
    PacketID = request.PacketID
    identitystr = my_id.getLocalIdentity().serialize()
    result = signed.Packet(commands.Identity(), MyID, MyID, PacketID, identitystr, RemoteID)
    gateway.outbox(result, False)
예제 #40
0
def SendCoin(remote_idurl, coins, packet_id=None, wide=False, callbacks={}):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendCoin to %s with %d records" % (remote_idurl, len(coins)))
    if packet_id is None:
        packet_id = packetid.UniqueID()
    outpacket = signed.Packet(
        Command=commands.Coin(),
        OwnerID=my_id.getLocalID(),
        CreatorID=my_id.getLocalID(),
        PacketID=packet_id,
        Payload=serialization.DictToBytes(coins, keys_to_text=True),
        RemoteID=remote_idurl,
    )
    gateway.outbox(outpacket, wide=wide, callbacks=callbacks)
    return outpacket
예제 #41
0
def Retrieve(request):
    """
    Customer is asking us for data he previously stored with us.

    We send with ``outboxNoAck()`` method because he will ask again if
    he does not get it
    """
    # TODO: rename to RetreiveData()
    if not driver.is_started("service_supplier"):
        return SendFail(request, "supplier service is off")
    if not contactsdb.is_customer(request.OwnerID):
        lg.warn("had unknown customer " + request.OwnerID)
        SendFail(request, "not a customer")
        return
    filename = makeFilename(request.OwnerID, request.PacketID)
    if filename == "":
        lg.warn("had empty filename")
        SendFail(request, "empty filename")
        return
    if not os.path.exists(filename):
        lg.warn("did not find requested file locally " + filename)
        SendFail(request, "did not find requested file locally")
        return
    if not os.access(filename, os.R_OK):
        lg.warn("no read access to requested packet " + filename)
        SendFail(request, "no read access to requested packet")
        return
    data = bpio.ReadBinaryFile(filename)
    if not data:
        lg.warn("empty data on disk " + filename)
        SendFail(request, "empty data on disk")
        return
    outpacket = signed.Unserialize(data)
    del data
    if outpacket is None:
        lg.warn("Unserialize fails, not Valid packet " + filename)
        SendFail(request, "unserialize fails")
        return
    if not outpacket.Valid():
        lg.warn("unserialized packet is not Valid " + filename)
        SendFail(request, "unserialized packet is not Valid")
        return
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.Retrieve sending %r back to %s" % (outpacket, nameurl.GetName(outpacket.CreatorID)),
        )
    gateway.outbox(outpacket, target=outpacket.CreatorID)
예제 #42
0
 def doSuppliersSendDBInfo(self, arg):
     from p2p import contact_status
     lg.out(4, 'backup_db_keeper.doSuppliersSendDBInfo')
     packetID = settings.BackupIndexFileName()
     self.sentSuppliers.clear()
     src = bpio.ReadBinaryFile(settings.BackupIndexFilePath())
     localID = my_id.getLocalID()
     b = encrypted.Block(
         localID,
         packetID,
         0,
         key.NewSessionKey(),
         key.SessionKeyType(),
         True,
         src)
     Payload = b.Serialize()
     for supplierId in contactsdb.suppliers():
         if not supplierId:
             continue
         if not contact_status.isOnline(supplierId):
             continue
         newpacket = signed.Packet(
             commands.Data(), localID, localID, packetID,
             Payload, supplierId)
         pkt_out = gateway.outbox(newpacket, callbacks={
             commands.Ack(): self._supplier_acked,
             commands.Fail(): self._supplier_acked, })
         self.sentSuppliers.add(supplierId)
         lg.out(
             4, '    %s sending to %s' %
             (pkt_out, nameurl.GetName(supplierId)))
예제 #43
0
def SendAck(packettoack, response="", wide=False, callbacks={}, packetid=None):
    result = signed.Packet(
        commands.Ack(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid or packettoack.PacketID,
        response,
        packettoack.OwnerID,
    )
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendAck %s to %s    response: %s ..." % (result.PacketID, result.RemoteID, str(response)[:15]),
        )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #44
0
def SendIdentity(remote_idurl, wide=False, callbacks={}):
    """
    
    """
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendIdentity to %s" % nameurl.GetName(remote_idurl))
    result = signed.Packet(
        commands.Identity(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        "identity",
        my_id.getLocalIdentity().serialize(),
        remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #45
0
def SendMessage(remote_idurl, messagebody, packet_id=None):
    """
    Send command.Message() packet to remote peer.
    """
    global _OutgoingMessageCallback
    if not packet_id:
        packet_id = packetid.UniqueID()
    remote_identity = identitycache.FromCache(remote_idurl)
    if remote_identity is None:
        d = identitycache.immediatelyCaching(remote_idurl, 20)
        d.addCallback(lambda src: SendMessage(
            remote_idurl, messagebody, packet_id))
        d.addErrback(lambda err: lg.warn('failed to retrieve ' + remote_idurl))
        return d
    Amessage = MessageClass(remote_identity, messagebody)
    Payload = misc.ObjectToString(Amessage)
    lg.out(6, "message.SendMessage to %s with %d bytes" % (remote_idurl, len(Payload)))
    outpacket = signed.Packet(
        commands.Message(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packet_id,
        Payload,
        remote_idurl)
    result = gateway.outbox(outpacket, wide=True)
    if _OutgoingMessageCallback:
        _OutgoingMessageCallback(result, messagebody, remote_identity, packet_id)
    return result
예제 #46
0
def SendRequestListFiles(supplierNumORidurl):
    if not str(supplierNumORidurl).isdigit():
        RemoteID = supplierNumORidurl
    else:
        RemoteID = contactsdb.supplier(supplierNumORidurl)
    if not RemoteID:
        lg.warn("RemoteID is empty supplierNumORidurl=%s" % str(supplierNumORidurl))
        return None
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendRequestListFiles [%s]" % nameurl.GetName(RemoteID))
    MyID = my_id.getLocalID()
    PacketID = packetid.UniqueID()
    Payload = settings.ListFilesFormat()
    result = signed.Packet(commands.ListFiles(), MyID, MyID, PacketID, Payload, RemoteID)
    gateway.outbox(result)
    return result
예제 #47
0
def SendRequestService(remote_idurl, service_info, wide=False, callbacks={}):
    if _Debug:
        lg.out(
            _DebugLevel,
            "p2p_service.SendRequestService to %s [%s]"
            % (nameurl.GetName(remote_idurl), service_info.replace("\n", " ")[:40]),
        )
    result = signed.Packet(
        commands.RequestService(),
        my_id.getLocalID(),
        my_id.getLocalID(),
        packetid.UniqueID(),
        service_info,
        remote_idurl,
    )
    gateway.outbox(result, wide=wide, callbacks=callbacks)
    return result
예제 #48
0
def SendData(raw_data, ownerID, creatorID, remoteID, packetID, callbacks={}):
    """
    
    """
    # TODO:
    newpacket = signed.Packet(commands.Data(), ownerID, creatorID, packetID, raw_data, remoteID)
    result = gateway.outbox(newpacket, callbacks=callbacks)
    return result
예제 #49
0
 def doSuppliersRequestDBInfo(self, arg):
     lg.out(4, 'backup_db_keeper.doSuppliersRequestDBInfo')
     packetID = settings.BackupIndexFileName()
     self.requestedSuppliers.clear()
     Payload = ''
     localID = my_id.getLocalID()
     for supplierId in contactsdb.suppliers():
         if not supplierId:
             continue
         newpacket = signed.Packet(
             commands.Retrieve(),
             localID,
             localID,
             packetID,
             Payload,
             supplierId)
         gateway.outbox(newpacket, callbacks={
             commands.Data(): self._supplier_response,
             commands.Fail(): self._supplier_response, })
         self.requestedSuppliers.add(supplierId)
예제 #50
0
 def doSuppliersRequestIndexFile(self, arg):
     """
     Action method.
     """
     if _Debug:
         lg.out(_DebugLevel, 'index_synchronizer.doSuppliersRequestIndexFile')
     if driver.is_started('service_backups'):
         from storage import backup_control
         self.current_local_revision = backup_control.revision()
     else:
         self.current_local_revision = -1
     self.latest_supplier_revision = -1
     self.requesting_suppliers.clear()
     self.requested_suppliers_number = 0
     packetID = settings.BackupIndexFileName()
     localID = my_id.getLocalID()
     for supplierId in contactsdb.suppliers():
         if not supplierId:
             continue
         if not contact_status.isOnline(supplierId):
             continue
         newpacket = signed.Packet(
             commands.Retrieve(),
             localID,
             localID,
             packetID,
             '',
             supplierId)
         pkt_out = gateway.outbox(newpacket, callbacks={
             commands.Data(): self._on_supplier_response,
             commands.Fail(): self._on_supplier_response, })
         if pkt_out:
             self.requesting_suppliers.add(supplierId)
             self.requested_suppliers_number += 1
         if _Debug:
             lg.out(_DebugLevel, '    %s sending to %s' %
                    (pkt_out, nameurl.GetName(supplierId)))
예제 #51
0
 def doSuppliersSendIndexFile(self, arg):
     """
     Action method.
     """
     if _Debug:
         lg.out(_DebugLevel, 'index_synchronizer.doSuppliersSendIndexFile')
     packetID = settings.BackupIndexFileName()
     self.sending_suppliers.clear()
     self.sent_suppliers_number = 0
     src = bpio.ReadBinaryFile(settings.BackupIndexFilePath())
     localID = my_id.getLocalID()
     b = encrypted.Block(
         localID,
         packetID,
         0,
         key.NewSessionKey(),
         key.SessionKeyType(),
         True,
         src)
     Payload = b.Serialize()
     for supplierId in contactsdb.suppliers():
         if not supplierId:
             continue
         if not contact_status.isOnline(supplierId):
             continue
         newpacket = signed.Packet(
             commands.Data(), localID, localID, packetID,
             Payload, supplierId)
         pkt_out = gateway.outbox(newpacket, callbacks={
             commands.Ack(): self._on_supplier_acked,
             commands.Fail(): self._on_supplier_acked, })
         if pkt_out:
             self.sending_suppliers.add(supplierId)
             self.sent_suppliers_number += 1
         if _Debug:
             lg.out(_DebugLevel, '    %s sending to %s' %
                    (pkt_out, nameurl.GetName(supplierId)))
예제 #52
0
 def mine_and_send(self, data):
     from transport import gateway
     outpacket = ''
     gateway.outbox(outpacket)
예제 #53
0
def SendBroadcastMessage(outpacket):
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendBroadcastMessage to %s" % outpacket.RemoteID)
    gateway.outbox(outpacket)
    return outpacket
예제 #54
0
def SendFailNoRequest(remoteID, packetID, response):
    result = signed.Packet(commands.Fail(), my_id.getLocalID(), my_id.getLocalID(), packetID, response, remoteID)
    if _Debug:
        lg.out(_DebugLevel, "p2p_service.SendFailNoRequest %s to %s" % (result.PacketID, result.RemoteID))
    gateway.outbox(result)
    return result