HiRPC Post direction
State of the signature in the HiRPC
Message type
Check if is T is a message
Creates a sender with a runtime method name
Creates error response sender from a receiver
Creates error response sender from a receiver
* Generate a random id * Returns: random id
Creates a sender via opDispatch.method with argument params
Creates a receiver from a Document doc
Create a return sender including the return value
Get the id of the document doc
get the message to of the message
HiRPC error response for a method
HiRPC call method
HiRPC Post (Sender,Receiver)
HiRPC result from a method
1 import tagion.crypto.SecureNet; 2 import tagion.hibon.HiBONRecord; 3 import tagion.crypto.secp256k1.NativeSecp256k1; 4 5 class HiRPCNet : StdSecureNet { 6 this(string passphrase) { 7 const h = new StdHashNet; 8 super(h); 9 generateKeyPair(passphrase); 10 } 11 } 12 13 immutable passphrase = "Very secret password for the server"; 14 enum func_name = "func_name"; 15 16 { 17 HiRPC hirpc = HiRPC(new HiRPCNet(passphrase)); 18 HiRPC bad_hirpc = HiRPC(new BadSecureNet(passphrase)); 19 auto params = new HiBON; 20 params["test"] = 42; 21 // Create a send method name func_name and argument params 22 const sender = hirpc.action(func_name, params); 23 assert(sender.params == Document(params)); 24 // Sender with bad credentials 25 const invalid_sender = bad_hirpc.action(func_name, params, sender.method.id); 26 27 const doc = sender.toDoc; 28 const invalid_doc = invalid_sender.toDoc; 29 30 // Convert the do to a received HiRPC 31 const receiver = hirpc.receive(doc); 32 const invalid_receiver = hirpc.receive(invalid_doc); 33 34 assert(receiver.method.id is sender.method.id); 35 assert(receiver.method.name == sender.method.name); 36 // Check that the received HiRPC is sign correctly 37 assert(receiver.signed is HiRPC.SignedState.VALID); 38 39 assert(invalid_receiver.method.id is sender.method.id); 40 assert(invalid_receiver.method.name == sender.method.name); 41 assert(invalid_receiver.signed is HiRPC.SignedState.INVALID); 42 43 static struct ResultStruct { 44 int x; 45 mixin HiBONRecord; 46 } 47 48 { // Response 49 auto hibon = new HiBON; 50 hibon["x"] = 42; 51 const send_back = hirpc.result(receiver, hibon); 52 assert(send_back.result == Document(hibon)); 53 const result = ResultStruct(send_back.response.result); 54 assert(result.x is 42); 55 } 56 57 { // Error 58 const send_error = hirpc.error(receiver, "Some error", -1); 59 assert(send_error.error.message == "Some error"); 60 assert(send_error.error.code == -1); 61 assert(send_error.hasSignature); 62 } 63 } 64 65 { 66 HiRPC.Method method; 67 /* static assert(0, typeof(receiver)); */ 68 method.name = "Sexy.Banjo.cow"; 69 70 assert(method.full_name == "Sexy.Banjo.cow"); 71 assert(method.name == "cow"); 72 assert(method.entity == "Sexy.Banjo"); 73 74 method.name = "spaceship"; 75 assert(method.full_name == "spaceship"); 76 assert(method.name == "spaceship"); 77 assert(method.entity == ""); 78 } 79 80 { 81 HiRPC hirpc; 82 { /// Unsigned message (no permission) 83 HiBON t = new HiBON(); 84 t["$test"] = 5; 85 86 const sender = hirpc.action("action", t); 87 88 auto test2 = sender.toDoc; 89 // writeln(test2.toJSON); 90 // writefln("sender.isSigned=%s", sender.isSigned); 91 assert(!sender.hasSignature, "This message is un-sigend, which is fine because the HiRPC does not contain a SecureNet"); 92 { 93 const receiver = hirpc.receive(sender.toDoc); 94 // writefln("receiver=%s", receiver); 95 assert(receiver.method.id is sender.method.id); 96 // writefln("receiver.method.name is sender.method.name", receiver.method.name, sender.method.name); 97 assert(receiver.method.name == sender.method.name); 98 assert(receiver.signed is HiRPC.SignedState.NOSIGN); 99 100 const params = receiver.method.params; 101 assert(params["$test"].get!int is 5); 102 } 103 } 104 // writefln("recever.verified=%s", recever.verified); 105 { 106 const method = hirpc.action("id", Document(), 8); 107 const re_method = hirpc.receive(method); 108 assert(re_method.getId == 8); 109 110 const res = hirpc.result(re_method, Document()); 111 const re_res = hirpc.receive(res); 112 assert(re_res.getId == 8); 113 114 const err = hirpc.error(3, "bad"); 115 const re_err = hirpc.receive(err); 116 assert(re_err.getId == 3); 117 118 HiRPC.Receiver empty; 119 assert(empty.getId == 0); 120 } 121 } 122 123 { 124 const hirpc = HiRPC(null); 125 { 126 const sender = hirpc.action("action"); 127 assert(sender.method.name == "action"); 128 assert(sender.method.full_name == "action"); 129 assert(sender.method.domain.empty); 130 } 131 const hirpc1 = hirpc.relabel("domain"); 132 { 133 const sender = hirpc1.action("action"); 134 assert(sender.method.name == "action"); 135 assert(sender.method.full_name == "domain.action"); 136 assert(sender.method.domain == "domain"); 137 } 138 const hirpc2 = hirpc.relabel("newdomain"); 139 { 140 const sender = hirpc2.action("action"); 141 assert(sender.method.name == "action"); 142 assert(sender.method.full_name == "newdomain.action"); 143 assert(sender.method.domain == "newdomain"); 144 } 145 }
HiRPC handler