1 module grpc.GrpcCode;
2 
3 alias GrpcReplyHandler(T) = void delegate(Status status, T response);
4 
5 string GetFunc(string funcstr)
6 {
7     import std.string;
8     string[] funcs = funcstr.split(".");
9     string myFunc;
10     if (funcs.length > 0)
11         myFunc = funcs[$ - 1];
12     else
13         myFunc = funcstr;
14     return myFunc;
15 }
16 
17 string CM(O , string service , string funcs = __FUNCTION__)()
18 {
19     string func = GetFunc(funcs);
20     string code = 
21     `auto stream = _channel.createStream("/`~ service ~`/`~func~`");
22     stream.write(request , false);
23     auto response = new `~O.stringof ~ `();
24     while(stream.read(response)){}
25     auto status = stream.finish();
26     return response;`;
27     return code;
28 }
29 
30 //auto response = new `~O.stringof ~ `();
31 /*
32  while(stream.read(response)){}
33     auto status = stream.finish();
34 */
35 
36 string CM1(O , string service , string funcs = __FUNCTION__)()
37 {
38     string func = GetFunc(funcs);
39     string code = 
40     `auto stream = _channel.createStream("/`~ service ~`/`~func~`");
41     stream.write(request , false);
42     auto reader = new ClientReader!`~O.stringof~`(stream);
43     return reader;`;
44     return code;
45 }
46 
47 
48 string CM2(O , string service , string funcs = __FUNCTION__)()
49 {
50     string func = GetFunc(funcs);
51     string code = 
52     `auto stream = _channel.createStream("/`~ service ~`/`~func~`");
53     
54     auto writer = new ClientWriter!`~O.stringof~`(stream ,response);
55     return writer;`;
56     return code;
57 }
58 
59 
60 string CM3(I , O , string service , string funcs = __FUNCTION__)()
61 {
62     string func = GetFunc(funcs);
63     string code = 
64     `auto stream = _channel.createStream("/`~ service ~`/`~func~`");
65     auto readerwriter = new ClientReaderWriter!(`~I.stringof~`,`~O.stringof~`)(stream);
66     return readerwriter;`;
67     return code;
68 }
69 
70 
71 
72 
73 string CMA(O , string service , string funcs = __FUNCTION__)()
74 {
75     string func = GetFunc(funcs);
76     string code = 
77     `auto stream = _channel.createStream("/`~ service ~`/`~func~`");
78      stream.setCallBack((data) {
79          auto response = new `~O.stringof~`();
80          if(dele !is null) {
81             fromProtobuf!`~O.stringof~`(data, response);
82             dele(stream.finish() , response);
83          }
84      });
85      stream.write(request , false);`;
86     return code;
87 }
88 
89 string SM(I , O , string method)()
90 {
91     string code = `case "`~method~`":
92                 auto request = new `~I.stringof~`();
93                 auto response = new `~O.stringof~`();
94                 complete.fromProtobuf! `~I.stringof~`(request);
95                 auto status = `~method~`(request,response);
96                 stream.write(response,false);
97                 return status;`;
98     return code;
99 }
100 
101 
102 /// server stream
103 string SM1(I , O , string method)()
104 {
105     string code = `case "`~method~`":
106                 auto request = new `~I.stringof~`();
107                 auto writer = new ServerWriter!`~O.stringof~`(stream);
108                 complete.fromProtobuf! `~I.stringof~`(request);
109                 auto status = `~method~`(request,writer);
110                 return status;`;
111     return code;
112 }
113 
114 /// client stream
115 string SM2(I , O , string method)()
116 {
117     string code = `case "`~method~`":
118                 auto reader = new ServerReader!`~I.stringof~`(stream);
119                 auto response = new `~O.stringof~`();               
120                 auto status = `~method~`(reader,response);
121                 stream.write(response);
122                 return status;`;
123     return code;
124 }
125 
126 /// client stream & server stream
127 string SM3(I , O , string method)()
128 {
129     string code = `case "`~method~`":
130                 auto readerwriter = new ServerReaderWriter!(`~I.stringof~`,`~O.stringof~`)(stream);                            
131                 auto status = `~method~`(readerwriter);
132                 return status;`;
133     return code;
134 }
135 
136 string NONE()
137 {
138     string code = `default:
139                   return new Status(StatusCode.NOT_FOUND , "The method '" ~ method ~ "' is not found in " ~ SERVICE);`;
140     return code;
141 }