def SendMessage(remote_idurl, packet_id=None, payload=None, wide=True, callbacks={}, response_timeout=None): """ """ if packet_id is None: packet_id = packetid.UniqueID() if _Debug: lg.out( _DebugLevel, "p2p_service.SendMessage to %s with packet_id=%s" % ( nameurl.GetName(remote_idurl), packet_id, )) outpacket = signed.Packet( Command=commands.Message(), OwnerID=my_id.getIDURL(), CreatorID=my_id.getIDURL(), PacketID=packet_id, Payload=payload, RemoteID=remote_idurl, ) result = gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=response_timeout) return result, outpacket
def _on_inbox_packet_received(self, newpacket, info, status, error_message): from p2p import commands from chat import message if newpacket.Command != commands.Message(): return False return message.on_incoming_message(newpacket, info, status, error_message)
def SendMessage(remote_idurl, packet_id=None, payload=None, wide=True, callbacks={}, response_timeout=None): """ """ if packet_id is None: packet_id = packetid.UniqueID() outpacket = signed.Packet( Command=commands.Message(), OwnerID=my_id.getLocalID(), CreatorID=my_id.getLocalID(), PacketID=packet_id, Payload=payload, RemoteID=remote_idurl, ) result = gateway.outbox(outpacket, wide=wide, callbacks=callbacks, response_timeout=response_timeout) return result, outpacket
def send_message(message_body, recipient_global_id, packet_id=None): """ Send command.Message() packet to remote peer. Returns Deferred (if remote_idurl was not cached yet) or outbox packet object. """ global _OutgoingMessageCallbacks if not packet_id: packet_id = packetid.UniqueID() remote_idurl = global_id.GlobalUserToIDURL(recipient_global_id) remote_identity = identitycache.FromCache(remote_idurl) # make sure we have remote identity cached if remote_identity is None: d = identitycache.immediatelyCaching(remote_idurl, timeout=10) d.addCallback(lambda src: send_message( message_body, recipient_global_id, packet_id)) d.addErrback(lambda err: lg.warn('failed to retrieve %s : %s' (remote_idurl, err))) return d lg.out(6, "message.send_message to %s with %d bytes message" % (recipient_global_id, len(message_body))) try: private_message_object = PrivateMessage(recipient_global_id=recipient_global_id) private_message_object.encrypt(message_body) except Exception as exc: return fail(exc) # Payload = misc.ObjectToString(Amessage) Payload = private_message_object.serialize() lg.out(6, "message.send_message payload is %d bytes, remote idurl is %s" % (len(Payload), remote_idurl)) outpacket = signed.Packet( commands.Message(), my_id.getLocalID(), my_id.getLocalID(), packet_id, Payload, remote_idurl, ) result = gateway.outbox(outpacket, wide=True) try: for cp in _OutgoingMessageCallbacks: cp(message_body, private_message_object, remote_identity, outpacket, result) except: lg.exc() return result
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
def do_send_message(json_data, recipient_global_id, packet_id, timeout, result_defer=None): global _OutgoingMessageCallbacks remote_idurl = global_id.GlobalUserToIDURL(recipient_global_id) if not remote_idurl: raise Exception('invalid recipient') remote_identity = identitycache.FromCache(remote_idurl) if not remote_identity: raise Exception('remote identity object not exist in cache') message_body = serialization.DictToBytes( json_data, pack_types=True, encoding='utf-8', ) lg.out( 4, "message.do_send_message to %s with %d bytes message" % (recipient_global_id, len(message_body))) try: private_message_object = PrivateMessage( recipient_global_id=recipient_global_id) private_message_object.encrypt(message_body) except Exception as exc: lg.exc() raise Exception('message encryption failed') Payload = private_message_object.serialize() lg.out( 4, " payload is %d bytes, remote idurl is %s" % (len(Payload), remote_idurl)) outpacket = signed.Packet( commands.Message(), my_id.getLocalID(), my_id.getLocalID(), packet_id, Payload, remote_idurl, ) result = gateway.outbox( outpacket, wide=True, response_timeout=timeout, callbacks={ commands.Ack(): lambda response, info: on_message_delivered( remote_idurl, json_data, recipient_global_id, packet_id, response, info, result_defer, ), commands.Fail(): lambda response, info: on_message_failed( remote_idurl, json_data, recipient_global_id, packet_id, response, info, result_defer, ), None: lambda pkt_out: on_message_failed( remote_idurl, json_data, recipient_global_id, packet_id, None, None, result_defer, ), # timeout }) try: for cp in _OutgoingMessageCallbacks: cp(json_data, private_message_object, remote_identity, outpacket, result) except: lg.exc() raise Exception('failed sending message') return result
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 Retrieve(newpacket) commandhandled = True elif newpacket.Command == commands.RequestService(): # other node send us a request to get some service RequestService(newpacket, info) commandhandled = True elif newpacket.Command == commands.CancelService(): # other node wants to stop the service we gave him CancelService(newpacket, info) commandhandled = True elif newpacket.Command == commands.Data(): # new packet to store for customer commandhandled = Data(newpacket) elif newpacket.Command == commands.ListFiles(): # customer wants list of their files ListFiles(newpacket) commandhandled = True elif newpacket.Command == commands.Files(): # supplier sent us list of files Files(newpacket, info) commandhandled = True elif newpacket.Command == commands.DeleteFile(): # will Delete a customer file for them DeleteFile(newpacket) commandhandled = True elif newpacket.Command == commands.DeleteBackup(): # will Delete all files starting in a backup DeleteBackup(newpacket) commandhandled = True elif newpacket.Command == commands.Message(): # will be handled in message.py commandhandled = False elif newpacket.Command == commands.Correspondent(): # contact asking for our current identity Correspondent(newpacket) commandhandled = True 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 return commandhandled