RemoteAPI

Simple wrapper to deal with tuples Vibe.d might emit a pragma(msg) when T.length == 0

final
class RemoteAPI : API(
API
Implementation : API
) {}

Constructors

this
this(CtorParams args)

Instantiate a node node and start it

this
this(Tid tid)

Create a reference to an already existing Tid

Members

Functions

tid
Tid tid()
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

Simple usage example

static interface API
{
    @safe:
    public @property ulong pubkey ();
    public Json getValue (ulong idx);
    public Json getQuorumSet ();
    public string recv (Json data);
}

static class MockAPI : API
{
    @safe:
    public override @property ulong pubkey ()
    { return 42; }
    public override Json getValue (ulong idx)
    { assert(0); }
    public override Json getQuorumSet ()
    { assert(0); }
    public override string recv (Json data)
    { assert(0); }
}

scope test = new RemoteAPI!(API, MockAPI)();
assert(test.pubkey() == 42);

In a real world usage, users will most likely need to use the registry

1 import std.conv;
2 static import std.concurrency;
3 
4 static interface API
5 {
6     @safe:
7     public @property ulong pubkey ();
8     public Json getValue (ulong idx);
9     public string recv (Json data);
10     public string recv (ulong index, Json data);
11 
12     public string last ();
13 }
14 
15 static class Node : API
16 {
17     @safe:
18     public this (bool isByzantine) { this.isByzantine = isByzantine; }
19     public override @property ulong pubkey ()
20     { lastCall = `pubkey`; return this.isByzantine ? 0 : 42; }
21     public override Json getValue (ulong idx)
22     { lastCall = `getValue`; return Json.init; }
23     public override string recv (Json data)
24     { lastCall = `recv@1`; return null; }
25     public override string recv (ulong index, Json data)
26     { lastCall = `recv@2`; return null; }
27 
28     public override string last () { return this.lastCall; }
29 
30     private bool isByzantine;
31     private string lastCall;
32 }
33 
34 static API factory (string type, ulong hash)
35 {
36     const name = hash.to!string;
37     auto tid = std.concurrency.locate(name);
38     if (tid != tid.init)
39         return new RemoteAPI!(API, Node)(tid);
40 
41     switch (type)
42     {
43     case "normal":
44         auto ret =  new RemoteAPI!(API, Node)(false);
45         std.concurrency.register(name, ret.tid());
46         return ret;
47     case "byzantine":
48         auto ret =  new RemoteAPI!(API, Node)(true);
49         std.concurrency.register(name, ret.tid());
50         return ret;
51     default:
52         assert(0);
53     }
54 }
55 
56 auto node1 = factory("normal", 1);
57 auto node2 = factory("byzantine", 2);
58 
59 static void testFunc()
60 {
61     auto node1 = factory("this does not matter", 1);
62     auto node2 = factory("neither does this", 2);
63     assert(node1.pubkey() == 42);
64     assert(node1.last() == "pubkey");
65     assert(node2.pubkey() == 0);
66     assert(node2.last() == "pubkey");
67 
68     node1.recv(42, Json.init);
69     assert(node1.last() == "recv@2");
70     node1.recv(Json.init);
71     assert(node1.last() == "recv@1");
72     assert(node2.last() == "pubkey");
73 }
74 
75 auto testerFiber = spawn(&testFunc);

Meta