1 module grpc.channel;
2 import interop.headers;
3 import grpc.common.cq;
4 import grpc.core.utils;
5 
6 class Channel {
7 	private {
8 		grpc_channel* channel;
9 	}
10 
11     @property grpc_channel* ptr() {
12         return channel;
13     }
14 
15     @property string target() {
16         char* target_ = grpc_channel_get_target(channel);
17         if(target != null) {
18             import std.string;
19             return target_.fromStringz.idup;
20         }
21         else {
22             return "";
23         }
24     }
25 
26     void getInfo(ref grpc_channel_info info) {
27         assert(&info != null, "ptr was null");
28 
29         grpc_channel_get_info(channel, &info);
30     }
31 
32     void notifyOnStateChange(grpc_connectivity_state pre, Duration timeout) {
33         assert(0, "Unimplemented");
34     }
35 
36 	bool waitForStateChange(grpc_connectivity_state pre, Duration timeout) {
37         auto time = MonoTime.currTime(); 
38         while(state() == pre) {
39             if(MonoTime.currTime - time > timeout) {
40                 return false;
41             }
42             import core.thread;
43             Thread.sleep(1.msecs);
44         }
45 		return true;
46 	}
47 
48     grpc_connectivity_state state(int try_to_connect = 0) {
49         return grpc_channel_check_connectivity_state(channel, try_to_connect);
50     }
51 
52     bool waitForConnect(Duration timeout) {
53         auto time = MonoTime.currTime();
54         import std.stdio;
55 
56         state(1);
57         while(state() != GRPC_CHANNEL_READY) {
58             if((MonoTime.currTime - time) > timeout) {
59                 return false;
60             }
61 
62             import core.thread;
63             Thread.sleep(1.msecs);
64         }
65 
66         writeln(state());
67         return true;
68     }
69 
70 	this(string target) {
71         import std.string;
72         channel = grpc_insecure_channel_create(target.toStringz, null, null);
73         assert(channel != null, "channel was null");
74 	}
75 
76     import std.variant;
77 
78     this(string target, Variant[] channelArgs) {
79         assert(0, "Unimplemented");
80     }
81 
82     ~this() {
83         grpc_channel_destroy(channel);
84     }
85 
86 }
87