示例#1
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
示例#2
0
 def _on_inbox_packet_received(self, newpacket, info, status,
                               error_message):
     from p2p import commands
     from stream import p2p_queue
     if newpacket.Command != commands.Event():
         return False
     return p2p_queue.on_event_packet_received(newpacket, info, status,
                                               error_message)
示例#3
0
    def _on_inbox_packet_received(self, newpacket, info, status, error_message):
#         import json
#         from logs import lg
#         from main import events
        from p2p import commands
#         from p2p import p2p_service
        from p2p import p2p_queue
#         from userid import global_id
#         from userid import my_id
        if newpacket.Command != commands.Event():
            return False
        return p2p_queue.on_event_packet_received(newpacket, info, status, error_message)
示例#4
0
def push_signed_message(producer_id, queue_id, data, creation_time=None):
    try:
        signed_data = signed.Packet(
            Command=commands.Event(),
            OwnerID=producer_id,
            CreatorID=producer_id,
            PacketID=packetid.UniqueID(),
            Payload=json.dumps(data),
            RemoteID=queue_id,
            KeyID=producer_id,
        )
    except:
        lg.exc()
        raise Exception('sign message failed')
    return push_message(producer_id, queue_id, data=signed_data.Serialize(), creation_time=creation_time)
示例#5
0
def push_signed_message(producer_id, queue_id, data, creation_time=None):
    # TODO: to be continue
    try:
        signed_data = signed.Packet(
            Command=commands.Event(),
            OwnerID=producer_id,
            CreatorID=my_id.getLocalID(),
            PacketID=packetid.UniqueID(),
            Payload=serialization.DictToBytes(data, keys_to_text=True),
            RemoteID=queue_id,
            KeyID=producer_id,
        )
    except:
        lg.exc()
        raise Exception('sign message failed')
    return write_message(producer_id,
                         queue_id,
                         data=signed_data.Serialize(),
                         creation_time=creation_time)
def inbox(newpacket, info, status, error_message):
    """
    """
    if newpacket.CreatorID != my_id.getLocalID(
    ) and newpacket.RemoteID != my_id.getLocalID():
        # packet is NOT for us, skip
        return False
    commandhandled = False
    if newpacket.Command == commands.Ack():
        # a response from remote node, typically handled in other places
        Ack(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Fail():
        # some operation was failed on other side
        Fail(newpacket)
        commandhandled = False
    elif newpacket.Command == commands.Retrieve():
        # retrieve some packet customer stored with us
        # handled by service_supplier()
        Retrieve(newpacket)
        commandhandled = False
    elif newpacket.Command == commands.RequestService():
        # other node send us a request to get some service
        # handled by service_p2p_hookups()
        RequestService(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.CancelService():
        # other node wants to stop the service we gave him
        # handled by service_p2p_hookups()
        CancelService(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Data():
        # new packet to store for customer, or data coming back from supplier
        # handled by service_backups() and service_supplier()
        Data(newpacket)
        commandhandled = False
    elif newpacket.Command == commands.ListFiles():
        # customer wants list of their files
        # handled by service_supplier()
        ListFiles(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Files():
        # supplier sent us list of files
        # handled by service_backups()
        Files(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.DeleteFile():
        # handled by service_supplier()
        DeleteFile(newpacket)
        commandhandled = False
    elif newpacket.Command == commands.DeleteBackup():
        # handled by service_supplier()
        DeleteBackup(newpacket)
        commandhandled = False
    elif newpacket.Command == commands.Correspondent():
        # TODO: contact asking for our current identity, not implemented yet
        Correspondent(newpacket)
        commandhandled = False
    elif newpacket.Command == commands.Broadcast():
        # handled by service_broadcasting()
        Broadcast(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Coin():
        # handled by service_accountant()
        Coin(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.RetrieveCoin():
        # handled by service_accountant()
        RetrieveCoin(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Key():
        # handled by service_keys_registry()
        Key(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Event():
        # handled by service_p2p_hookups()
        Event(newpacket, info)
        commandhandled = False
    elif newpacket.Command == commands.Message():
        # handled by service_private_messages()
        Message(newpacket, info)
        commandhandled = False

    return commandhandled
示例#7
0
def on_event_packet_received(newpacket, info, status, error_message):
    if newpacket.Command != commands.Event():
        return False
    try:
        e_json = json.loads(newpacket.Payload)
        event_id = e_json['event_id']
        payload = e_json['payload']
        queue_id = e_json.get('queue_id')
        producer_id = e_json.get('producer_id')
        message_id = e_json.get('message_id')
        created = e_json.get('created')
    except:
        lg.warn("invlid json payload")
        return False
    if queue_id and producer_id and message_id:
        # this message have an ID and producer so it came from a queue and needs to be consumed
        # also add more info comming from the queue
        if _Debug:
            lg.warn('received event from the queue at %s' % queue_id)
        payload.update(
            dict(
                queue_id=queue_id,
                producer_id=producer_id,
                message_id=message_id,
                created=created,
            ))
        events.send(event_id, data=payload)
        p2p_service.SendAck(newpacket)
        return True
    # this message does not have nor ID nor producer so it came from another user directly
    # lets' try to find a queue for that event and see if we need to publish it or not
    queue_id = global_id.MakeGlobalQueueID(
        queue_alias=event_id,
        owner_id=global_id.MakeGlobalID(idurl=newpacket.OwnerID),
        supplier_id=global_id.MakeGlobalID(idurl=my_id.getGlobalID()),
    )
    if queue_id not in queue():
        # such queue is not found locally, that means message is
        # probably addressed to that node and needs to be consumed directly
        if _Debug:
            lg.warn(
                'received event was not delivered to any queue, consume now and send an Ack'
            )
        # also add more info comming from the queue
        payload.update(
            dict(
                queue_id=queue_id,
                producer_id=producer_id,
                message_id=message_id,
                created=created,
            ))
        events.send(event_id, data=payload)
        p2p_service.SendAck(newpacket)
        return True
    # found a queue for that message, pushing there
    # TODO: add verification of producer's identity and signature
    if _Debug:
        lg.warn('pushing event to the queue %s on behalf of producer %s' %
                (queue_id, producer_id))
    try:
        push_message(
            producer_id=producer_id,
            queue_id=queue_id,
            data=payload,
            creation_time=created,
        )
    except Exception as exc:
        lg.exc()
        p2p_service.SendFail(newpacket, str(exc))
        return True
    p2p_service.SendAck(newpacket)
    return True