HiRPC

HiRPC handler

Members

Enums

Direction
enum Direction

HiRPC Post direction

SignedState
enum SignedState

State of the signature in the HiRPC

Type
enum Type

Message type

isMessage
eponymoustemplate isMessage(T)

Check if is T is a message

Functions

action
immutable(Sender) action(string method, Document params, uint id)
immutable(Sender) action(string method, T params, uint id)
immutable(Sender) action(string method, const(HiBON) params, uint id)

Creates a sender with a runtime method name

error
immutable(Sender) error(uint id, string msg, int code, Document data)

Creates error response sender from a receiver

error
immutable(Sender) error(const(Receiver) receiver, string msg, int code, Document data)

Creates error response sender from a receiver

generateId
const(uint) generateId()

* Generate a random id * Returns: random id

opDispatch
immutable(Sender) opDispatch(T params, uint id)

Creates a sender via opDispatch.method with argument params

receive
immutable(Receiver) receive(Document doc)
immutable(Receiver) receive(T sender)

Creates a receiver from a Document doc

result
immutable(Sender) result(const(Receiver) receiver, Document return_value)
immutable(Sender) result(const(Receiver) receiver, T return_value)
immutable(Sender) result(const(Receiver) receiver, const(HiBON) return_value)

Create a return sender including the return value

Static functions

getId
uint getId(Document doc)

Get the id of the document doc

getType
Type getType(T message)
Type getType(Document doc)

get the message to of the message

Structs

Error
struct Error

HiRPC error response for a method

Method
struct Method

HiRPC call method

Post
struct Post(Direction DIRECTION)

HiRPC Post (Sender,Receiver)

Response
struct Response

HiRPC result from a method

Examples

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