1 module tagion.testbench.hirpcclient;
2 
3 import core.thread;
4 import std.conv : to;
5 import std.format;
6 import std.socket : AddressFamily, InternetAddress, ProtocolType, Socket, SocketException, SocketType, TcpSocket, getAddress;
7 import std.stdio;
8 import tagion.communication.HiRPC;
9 import tagion.gossip.GossipNet;
10 import tagion.hibon.Document : Document;
11 import tagion.hibon.HiBON : HiBON;
12 import tagion.tools.Basic;
13 import tagion.utils.Random;
14 
15 version (none) class HRPCNet : StdSecureNet {
16     import tagion.hashgraph.HashGraph;
17 
18     override void request(HashGraph hashgraph, immutable(ubyte[]) fingerprint) {
19         assert(0, format("Not implemented %s", __PRETTY_FUNCTION__));
20     }
21 
22     this(string passphrase) {
23         import tagion.crypto.secp256k1.NativeSecp256k1;
24 
25         super(new NativeSecp256k1(NativeSecp256k1.Format.AUTO, NativeSecp256k1.Format.COMPACT));
26         generateKeyPair(passphrase);
27         import tagion.Base;
28         import tagion.utils.Miscellaneous;
29 
30         writefln("public=%s", (cast(Buffer) pubkey).toHexString);
31     }
32 }
33 
34 mixin Main!(_main, "hirpc");
35 
36 int _main(string[] args) {
37     version (none) {
38         ushort port;
39         enum BUFFER_SIZE = 1024;
40         if (args.length >= 2) {
41             port = to!ushort(args[1]);
42         }
43         else {
44             port = 4444;
45         }
46 
47         auto addresses = getAddress("localhost", port);
48         auto socket = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
49         scope (exit)
50             socket.close();
51 
52         socket.connect(addresses[0]);
53 
54         //    auto buffer = new ubyte[2056];
55         ptrdiff_t amountRead;
56 
57         string test = "Hello";
58         //immutable buffer=cast(immutable(ubyte[]))(test);
59 
60         HRPC hrpc;
61         immutable passphrase = "Very secret password for the client";
62         hrpc.net = new HRPCNet(passphrase);
63 
64         auto randtime = Random!uint(5678);
65 
66         foreach (num; 0 .. 10) {
67             {
68                 auto bson = new HBSON;
69 
70                 bson["test"] = "Hello";
71                 bson["value"] = randtime.value(1000);
72                 auto sender = hrpc.action("read", bson);
73 
74                 immutable buffer = hrpc.toBSON(sender).serialize;
75                 writefln("\nSend %s bytes", buffer.length);
76                 socket.send(buffer);
77 
78                 ubyte[1024] buf;
79 
80                 auto buf_size = socket.receive(buf);
81                 if (buf_size > 0) {
82                     auto doc_received = Document(buf[0 .. buf_size].idup);
83                     auto received = hrpc.receive(doc_received);
84                     writeln("Received :");
85                     writeln(doc_received.toText);
86                     if (received.verified) {
87                         writeln("Message is verified and signed");
88                     }
89                     else {
90                         writeln("Message is not signed");
91                     }
92                 }
93                 else {
94                     writefln("Buffer %d", buf_size);
95                 }
96             }
97             Thread.sleep(randtime.value(200).msecs);
98 
99             {
100                 auto sender = hrpc.action("noarg", null);
101 
102                 immutable buffer = hrpc.toBSON(sender).serialize;
103                 writefln("\nSend %s bytes", buffer.length);
104                 socket.send(buffer);
105 
106                 ubyte[1024] buf;
107 
108                 auto buf_size = socket.receive(buf);
109                 if (buf_size > 0) {
110                     auto doc_received = Document(buf[0 .. buf_size].idup);
111                     auto received = hrpc.receive(doc_received);
112                     writeln("Received :");
113                     writeln(doc_received.toText);
114                     if (received.verified) {
115                         writeln("Message is verified and signed");
116                     }
117                     else {
118                         writeln("Message is not signed");
119                     }
120                 }
121                 else {
122                     writefln("Biffer %d", buf_size);
123                 }
124 
125             }
126             Thread.sleep(randtime.value(200).msecs);
127         }
128         //buffer[0..test.length]=
129         // while((amountRead = socket.receive(buffer)) != 0) {
130         //     enforce(amountRead > 0, lastSocketError);
131 
132         //     // Do stuff with buffer
133         // }
134     }
135     return 0;
136 }