1 module IDL.IdlInerfaceCreateCode; 2 3 import std.array : appender; 4 import std.format; 5 import std.regex; 6 import std.stdio; 7 8 import IDL.IdlParseInterface; 9 import IDL.IdlStructCreateCode; 10 import IDL.IdlUnit; 11 import IDL.IdlSymbol; 12 import IDL.IdlParseStruct; 13 14 class IdlFunctionArgCode 15 { 16 static string createServerCode(MemberAttr functionInerface) 17 { 18 auto strings = appender!string(); 19 20 auto dlangVarName = idlDlangVariable.get(functionInerface.typeName, null); 21 22 if(dlangVarName is null) 23 { 24 auto dlangStructName = idlStructList.get(functionInerface.typeName, null); 25 26 if(dlangStructName is null) 27 { 28 throw new Exception("not parse symbol for struct name: " ~ functionInerface.typeName); 29 } 30 } 31 32 formattedWrite(strings, "\t\t%s %s;\n", functionInerface.typeName, functionInerface.getVarName); 33 34 return strings.data; 35 } 36 37 38 static string createClientCode(MemberAttr functionInerface) 39 { 40 auto strings = appender!string(); 41 42 auto dlangVarName = idlDlangVariable.get(functionInerface.typeName, null); 43 44 if(dlangVarName == null) 45 { 46 auto dlangStructName = idlStructList.get(functionInerface.typeName, null); 47 48 if(dlangStructName is null) 49 { 50 throw new Exception("not parse symbol for struct name: " ~ functionInerface.typeName); 51 } 52 } 53 54 formattedWrite(strings, "\t\t %s %s;\n", functionInerface.typeName, functionInerface.getVarName); 55 56 return strings.data; 57 } 58 } 59 60 class IdlFunctionAttrCode 61 { 62 static string createServerInterfaceCode(FunctionAttr FunctionAttrInterface, string inerfaceName) 63 { 64 auto strings = appender!string(); 65 66 formattedWrite(strings, "\tvoid %sInterface(RpcRequest req){\n\n", FunctionAttrInterface.funcName); 67 68 formattedWrite(strings, "\t\tubyte[] flatBufBytes;\n\n"); 69 70 formattedWrite(strings, "\t\tauto resp = new RpcResponse(req);\n"); 71 formattedWrite(strings, "\t\treq.pop(flatBufBytes);\n\n"); 72 formattedWrite(strings, "\t\tauto %sFB = %sFB.getRootAs%sFB(new ByteBuffer(flatBufBytes));\n", FunctionAttrInterface.funcArgMap.getVarName, FunctionAttrInterface.funcArgMap.getTypeName, FunctionAttrInterface.funcArgMap.getTypeName); 73 74 formattedWrite(strings, "\t\t%s %s;\n\n", FunctionAttrInterface.funcArgMap.getTypeName, FunctionAttrInterface.funcArgMap.getVarName); 75 76 formattedWrite(strings, "\t\t//input flatbuffer code for %sFB class\n\n\n\n", FunctionAttrInterface.funcArgMap.getTypeName); 77 formattedWrite(strings, "\t\t%s\n\n", IdlParseStruct.createDeserializeCodeForFlatbuffer(idlStructList[FunctionAttrInterface.funcArgMap.getTypeName], FunctionAttrInterface.funcArgMap.getVarName, FunctionAttrInterface.funcArgMap.getVarName~"FB")); 78 79 80 formattedWrite(strings, "\t\tauto %s = (cast(%sService)this).%s(%s);\n\n", FunctionAttrInterface.retValue.getVarName, inerfaceName, FunctionAttrInterface.getFuncName, FunctionAttrInterface.funcArgMap.getVarName); 81 82 formattedWrite(strings, "\t\tauto builder = new FlatBufferBuilder(512);\n"); 83 84 formattedWrite(strings, "\t\t//input flatbuffer code for %sFB class\n\n", FunctionAttrInterface.retValue.getTypeName); 85 formattedWrite(strings, "\t\t%s\n\n", IdlParseStruct.createSerializeCodeForFlatbuffer(idlStructList[FunctionAttrInterface.retValue.getTypeName], FunctionAttrInterface.retValue.getVarName)); 86 87 formattedWrite(strings, "\t\tbuilder.finish(%sPos);\n\n", FunctionAttrInterface.retValue.getVarName); 88 89 formattedWrite(strings, "\t\tresp.push(builder.sizedByteArray);\n\n"); 90 formattedWrite(strings, "\t\trpImpl.response(resp);\n"); 91 92 formattedWrite(strings, "\t}\n\n\n\n"); 93 94 return strings.data; 95 } 96 97 static string createServerServiceCode(FunctionAttr FunctionAttrInterface) 98 { 99 auto strings = appender!string(); 100 101 auto funcArgsStrirngs = appender!string(); 102 103 auto v = FunctionAttrInterface.funcArgMap; 104 105 formattedWrite(funcArgsStrirngs, "%s %s", v.getTypeName, v.getVarName); 106 107 formattedWrite(strings, "\t%s %s(%s){\n\n", FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.funcName, funcArgsStrirngs.data); 108 formattedWrite(strings, "\t\t%s %sRet;\n", FunctionAttrInterface.retValue.getTypeName, stringToLower(FunctionAttrInterface.retValue.getTypeName, 0)); 109 formattedWrite(strings, "\t\t//input service code for %s class\n\n\n\n", FunctionAttrInterface.retValue.getTypeName); 110 111 formattedWrite(strings, "\t\treturn %sRet;\n\t}\n\n\n\n", stringToLower(FunctionAttrInterface.retValue.getTypeName, 0)); 112 113 return strings.data; 114 } 115 116 117 static string createClientServiceCode(FunctionAttr FunctionAttrInterface) 118 { 119 auto strings = appender!string(); 120 121 auto funcArgsStrirngs = appender!string(); 122 123 auto v = FunctionAttrInterface.funcArgMap; 124 formattedWrite(funcArgsStrirngs, "%s %s", v.getTypeName, v.getVarName); 125 126 127 auto funcValuesArgsStrirngs = appender!string(); 128 129 v = FunctionAttrInterface.funcArgMap; 130 131 formattedWrite(funcValuesArgsStrirngs, "%s", v.getVarName); 132 133 134 formattedWrite(strings, "\t%s %s(%s, const RPC_PACKAGE_COMPRESS_TYPE compressType = RPC_PACKAGE_COMPRESS_TYPE.RPCT_NO, const int secondsTimeOut = RPC_REQUEST_TIMEOUT_SECONDS){\n\n", 135 FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.funcName, funcArgsStrirngs.data); 136 137 formattedWrite(strings, "\t\t%s ret = super.%sInterface(%s, compressType, secondsTimeOut);\n", 138 FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.funcName, funcValuesArgsStrirngs.data); 139 140 formattedWrite(strings, "\t\treturn ret;\n"); 141 formattedWrite(strings, "\t}\n\n\n"); 142 143 144 formattedWrite(strings, "\tvoid %s(%s, Rpc%sCallback rpcCallback, const RPC_PACKAGE_COMPRESS_TYPE compressType = RPC_PACKAGE_COMPRESS_TYPE.RPCT_NO, const int secondsTimeOut = RPC_REQUEST_TIMEOUT_SECONDS){\n\n", 145 FunctionAttrInterface.funcName, funcArgsStrirngs.data, FunctionAttrInterface.funcName); 146 formattedWrite(strings, "\t\tsuper.%sInterface(%s, rpcCallback, compressType, secondsTimeOut);\n", FunctionAttrInterface.funcName, funcValuesArgsStrirngs.data); 147 formattedWrite(strings, "\t}\n\n\n"); 148 149 return strings.data; 150 } 151 152 153 154 static string createClientInterfaceCode(FunctionAttr FunctionAttrInterface, string inerfaceName) 155 { 156 auto strings = appender!string(); 157 158 auto funcArgsStrirngs = appender!string(); 159 160 auto v = FunctionAttrInterface.funcArgMap; 161 formattedWrite(funcArgsStrirngs, "%s %s", v.getTypeName, v.getVarName); 162 163 auto funcArgsStructStrirngs = appender!string(); 164 v = FunctionAttrInterface.funcArgMap; 165 formattedWrite(funcArgsStructStrirngs, "%s", v.getVarName); 166 167 formattedWrite(strings, "\t%s %sInterface(const %s, const RPC_PACKAGE_COMPRESS_TYPE compressType, const int secondsTimeOut, const size_t funcId = %s){\n\n", 168 FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.funcName, funcArgsStrirngs.data, FunctionAttrInterface.funcHash); 169 170 formattedWrite(strings, "\t\tauto builder = new FlatBufferBuilder(512);\n\n"); 171 formattedWrite(strings, "\t\t//input flatbuffer code for %sFB class\n\n\n\n\n", v.typeName); 172 formattedWrite(strings, "\t\t%s\n\n", IdlParseStruct.createSerializeCodeForFlatbuffer(idlStructList[v.typeName], v.varName)); 173 formattedWrite(strings, "\t\tbuilder.finish(%sPos);\n\n", v.varName); 174 175 176 formattedWrite(strings, "\t\tauto req = new RpcRequest(compressType, secondsTimeOut);\n\n"); 177 formattedWrite(strings, "\t\treq.push(builder.sizedByteArray);\n\n"); 178 formattedWrite(strings, "\t\tRpcResponse resp = rpImpl.syncCall(req, RPC_PACKAGE_PROTOCOL.TPP_FLAT_BUF, funcId);\n\n"); 179 formattedWrite(strings, "\t\tif(resp.getStatus == RESPONSE_STATUS.RS_OK){\n\n"); 180 formattedWrite(strings, "\t\t\tubyte[] flatBufBytes;\n"); 181 formattedWrite(strings, "\t\t\tresp.pop(flatBufBytes);\n\n"); 182 formattedWrite(strings, "\t\t\tauto %sFB = %sFB.getRootAs%sFB(new ByteBuffer(flatBufBytes));\n", FunctionAttrInterface.retValue.getVarName, FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.retValue.getTypeName); 183 184 formattedWrite(strings, "\t\t\t%s %s;\n\n", FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.retValue.getVarName); 185 formattedWrite(strings, "\t\t\t//input flatbuffer code for %sFB class\n\n\n\n\n", FunctionAttrInterface.retValue.getTypeName); 186 formattedWrite(strings, "\t\t%s\n\n", IdlParseStruct.createDeserializeCodeForFlatbuffer(idlStructList[FunctionAttrInterface.retValue.getTypeName], FunctionAttrInterface.retValue.getVarName, FunctionAttrInterface.retValue.getVarName~"FB")); 187 formattedWrite(strings, "\t\t\treturn %s;\n\t\t}else{\n", FunctionAttrInterface.retValue.getVarName); 188 formattedWrite(strings, "\t\t\tthrow new Exception(\"rpc sync call error, function:\" ~ RpcBindFunctionMap[funcId]);\n\t\t}\n"); 189 formattedWrite(strings, "\t}\n\n\n"); 190 191 192 193 formattedWrite(strings, "\talias Rpc%sCallback = void delegate(%s);\n\n", FunctionAttrInterface.funcName, FunctionAttrInterface.retValue.getTypeName); 194 formattedWrite(strings, "\tvoid %sInterface(const %s, Rpc%sCallback rpcCallback, const RPC_PACKAGE_COMPRESS_TYPE compressType, const int secondsTimeOut, const size_t funcId = %s){\n\n", 195 FunctionAttrInterface.funcName, funcArgsStrirngs.data, FunctionAttrInterface.funcName, FunctionAttrInterface.funcHash); 196 197 formattedWrite(strings, "\t\tauto builder = new FlatBufferBuilder(512);\n"); 198 formattedWrite(strings, "\t\t//input flatbuffer code for %sFB class\n\n\n\n\n", v.typeName); 199 formattedWrite(strings, "\t\t%s\n\n", IdlParseStruct.createSerializeCodeForFlatbuffer(idlStructList[v.typeName], v.varName)); 200 formattedWrite(strings, "\t\tbuilder.finish(%sPos);\n", v.varName); 201 202 203 formattedWrite(strings, "\t\tauto req = new RpcRequest(compressType, secondsTimeOut);\n\n"); 204 formattedWrite(strings, "\t\treq.push(builder.sizedByteArray);\n\n"); 205 formattedWrite(strings, "\t\trpImpl.asyncCall(req, delegate(RpcResponse resp){\n\n"); 206 formattedWrite(strings, "\t\t\tif(resp.getStatus == RESPONSE_STATUS.RS_OK){\n\n"); 207 formattedWrite(strings, "\t\t\t\tubyte[] flatBufBytes;\n"); 208 formattedWrite(strings, "\t\t\t\t%s %s;\n\n", FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.retValue.getVarName); 209 formattedWrite(strings, "\t\t\t\tresp.pop(flatBufBytes);\n\n"); 210 formattedWrite(strings, "\t\t\t\tauto %sFB = %sFB.getRootAs%sFB(new ByteBuffer(flatBufBytes));\n", FunctionAttrInterface.retValue.getVarName, FunctionAttrInterface.retValue.getTypeName, FunctionAttrInterface.retValue.getTypeName); 211 212 formattedWrite(strings, "\t\t\t\t//input flatbuffer code for %sFB class\n\n\n\n\n", FunctionAttrInterface.retValue.getTypeName); 213 formattedWrite(strings, "\t\t%s\n\n", IdlParseStruct.createDeserializeCodeForFlatbuffer(idlStructList[FunctionAttrInterface.retValue.getTypeName], FunctionAttrInterface.retValue.getVarName, FunctionAttrInterface.retValue.getVarName~"FB")); 214 formattedWrite(strings, "\t\t\t\trpcCallback(%s);\n", FunctionAttrInterface.retValue.getVarName); 215 formattedWrite(strings, "\t\t\t}else{\n\t\t\t\tthrow new Exception(\"rpc sync call error, function:\" ~ RpcBindFunctionMap[funcId]);\n\t\t\t}}, RPC_PACKAGE_PROTOCOL.TPP_FLAT_BUF, funcId);\n", inerfaceName); 216 formattedWrite(strings, "\t}\n\n\n"); 217 218 return strings.data; 219 } 220 } 221 222 223 class idl_inerface_dlang_code 224 { 225 static string createServerCodeForInterface(IdlParseInterface idlInterface) 226 { 227 auto strings = appender!string(); 228 229 formattedWrite(strings, "abstract class %sInterface{ \n\n", idlInterface.interfaceName); 230 formattedWrite(strings, "\tthis(RpcServer rpServer){ \n"); 231 formattedWrite(strings, "\t\trpImpl = new RpcServerImpl!(%sService)(rpServer); \n", idlInterface.interfaceName); 232 233 foreach(k,v; idlInterface.functionList) 234 { 235 formattedWrite(strings, "\t\trpImpl.bindRequestCallback(%s, &this.%sInterface); \n\n", v.funcHash, v.getFuncName); 236 } 237 238 formattedWrite(strings, "\t}\n\n"); 239 240 foreach(k,v; idlInterface.functionList) 241 { 242 formattedWrite(strings, IdlFunctionAttrCode.createServerInterfaceCode(v, idlInterface.interfaceName)); 243 } 244 245 formattedWrite(strings, "\tRpcServerImpl!(%sService) rpImpl;\n}\n\n\n", idlInterface.interfaceName); 246 247 return strings.data; 248 } 249 250 251 static string createServerCodeForService(IdlParseInterface idlInterface) 252 { 253 auto strings = appender!string(); 254 255 256 formattedWrite(strings, "\nclass %sService: %sInterface{\n\n", idlInterface.interfaceName, idlInterface.interfaceName); 257 formattedWrite(strings, "\tthis(RpcServer rpServer){\n"); 258 259 foreach(k,v; idlInterface.functionList) 260 { 261 formattedWrite(strings, "\t\tRpcBindFunctionMap[%s] = typeid(&%sService.%s).toString();\n", v.funcHash, idlInterface.interfaceName, v.funcName); 262 } 263 264 formattedWrite(strings, "\t\tsuper(rpServer);\n"); 265 formattedWrite(strings, "\t}\n\n"); 266 267 foreach(k,v; idlInterface.functionList) 268 { 269 formattedWrite(strings, IdlFunctionAttrCode.createServerServiceCode(v)); 270 } 271 272 formattedWrite(strings,"}\n\n\n\n"); 273 274 return strings.data; 275 } 276 277 278 static string createClientCodeForInterface(IdlParseInterface idlInterface) 279 { 280 auto strings = appender!string(); 281 282 formattedWrite(strings, "abstract class %sInterface{ \n\n", idlInterface.interfaceName); 283 formattedWrite(strings, "\tthis(RpcClient rpClient){ \n"); 284 formattedWrite(strings, "\t\trpImpl = new RpcClientImpl!(%sService)(rpClient); \n", idlInterface.interfaceName); 285 formattedWrite(strings, "\t}\n\n"); 286 287 288 foreach(k,v; idlInterface.functionList) 289 { 290 formattedWrite(strings, IdlFunctionAttrCode.createClientInterfaceCode(v, idlInterface.interfaceName)); 291 } 292 293 formattedWrite(strings, "\tRpcClientImpl!(%sService) rpImpl;\n}\n\n\n", idlInterface.interfaceName); 294 295 return strings.data; 296 } 297 298 299 static string createClientCodeForService(IdlParseInterface idlInterface) 300 { 301 auto strings = appender!string(); 302 303 formattedWrite(strings, "\nclass %sService: %sInterface{\n\n", idlInterface.interfaceName, idlInterface.interfaceName); 304 formattedWrite(strings, "\tthis(RpcClient rpClient){\n"); 305 306 foreach(k,v; idlInterface.functionList) 307 { 308 formattedWrite(strings, "\t\tRpcBindFunctionMap[%s] = typeid(&%sService.%s).toString();\n", v.funcHash, idlInterface.interfaceName, v.funcName); 309 } 310 311 312 formattedWrite(strings, "\t\tsuper(rpClient);\n"); 313 formattedWrite(strings, "\t}\n\n"); 314 315 316 foreach(k,v; idlInterface.functionList) 317 { 318 formattedWrite(strings, IdlFunctionAttrCode.createClientServiceCode(v)); 319 } 320 321 322 formattedWrite(strings, "}\n\n\n\n"); 323 324 return strings.data; 325 } 326 327 } 328 329