srpc_rpc_controller.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. Copyright (c) 2022 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string>
  16. #include <string.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. #include <fcntl.h>
  21. #include "srpc_controller.h"
  22. RPCController::RPCController()
  23. {
  24. this->config.type = COMMAND_RPC;
  25. struct file_info info;
  26. info = { "rpc/CMakeLists.txt", "CMakeLists.txt", rpc_cmake_transform };
  27. this->default_files.push_back(info);
  28. info = { "common/config.json", "client.conf", nullptr };
  29. this->default_files.push_back(info);
  30. info = { "common/config.json", "server.conf", nullptr };
  31. this->default_files.push_back(info);
  32. info = { "common/GNUmakefile", "GNUmakefile", nullptr };
  33. this->default_files.push_back(info);
  34. info = { "config/Json.h", "config/Json.h", nullptr };
  35. this->default_files.push_back(info);
  36. info = { "config/Json.cc", "config/Json.cc", nullptr };
  37. this->default_files.push_back(info);
  38. info = { "config/config_full.h", "config/config.h", nullptr };
  39. this->default_files.push_back(info);
  40. info = { "config/config_full.cc", "config/config.cc", nullptr };
  41. this->default_files.push_back(info);
  42. info = { "common/config.json", "full.conf", nullptr };
  43. this->default_files.push_back(info);
  44. }
  45. void RPCController::print_usage(const char *name) const
  46. {
  47. printf(COLOR_PINK"Usage:\n"
  48. COLOR_INFO" %s " COLOR_COMMAND "rpc "
  49. COLOR_INFO"<PROJECT_NAME> " COLOR_FLAG "[FLAGS]\n\n"
  50. COLOR_PINK"Example:\n"
  51. COLOR_PURPLE" %s rpc my_rpc_project\n\n"
  52. COLOR_PINK"Available Flags:\n"
  53. COLOR_FLAG" -r "
  54. COLOR_WHITE": rpc type [ SRPC | SRPCHttp | BRPC | Thrift | "
  55. "ThriftHttp | TRPC | TRPCHttp ] (default: SRPC)\n"
  56. COLOR_FLAG" -o "
  57. COLOR_WHITE": project output path (default: CURRENT_PATH)\n"
  58. COLOR_FLAG" -s "
  59. COLOR_WHITE": service name (default: PROJECT_NAME)\n"
  60. COLOR_FLAG" -i "
  61. COLOR_WHITE": idl type [ protobuf | thrift ] (default: protobuf)\n"
  62. COLOR_FLAG" -x "
  63. COLOR_WHITE": data type [ protobuf | thrift | json ] "
  64. "(default: idl type. json for http)\n"
  65. COLOR_FLAG" -c "
  66. COLOR_WHITE": compress type [ gzip | zlib | snappy | lz4 ] "
  67. "(default: no compression)\n"
  68. COLOR_FLAG" -d "
  69. COLOR_WHITE": path of dependencies (default: COMPILE_PATH)\n"
  70. COLOR_FLAG" -f "
  71. COLOR_WHITE": specify the idl_file to generate codes "
  72. "(default: templates/rpc/IDL_FILE)\n"
  73. COLOR_FLAG" -p "
  74. COLOR_WHITE": specify the path for idl_file to depend "
  75. "(default: templates/rpc/)\n"
  76. COLOR_OFF, name, name);
  77. }
  78. bool RPCController::copy_files()
  79. {
  80. struct srpc_config *config = &this->config;
  81. int ret = true;
  82. if (config->specified_idl_file == NULL) // fill the default rpc files
  83. {
  84. this->fill_rpc_default_files();
  85. }
  86. else
  87. {
  88. // copy specified idl file and generate rpc files
  89. struct GeneratorParams params;
  90. params.out_dir = config->output_path;
  91. params.input_dir = config->specified_idl_path;
  92. if (params.input_dir[params.input_dir.length() - 1] != '/')
  93. params.input_dir += "/";
  94. params.idl_file = config->specified_idl_file;
  95. auto pos = params.idl_file.find_last_of('/');
  96. if (pos != std::string::npos)
  97. params.idl_file = params.idl_file.substr(pos + 1);
  98. std::string out_file = std::string(config->output_path) +
  99. std::string("/") + params.idl_file;
  100. if (CommandController::copy_single_file(config->specified_idl_file,
  101. out_file, nullptr) == false)
  102. return false;
  103. ControlGenerator gen(config);
  104. printf(COLOR_PURPLE"Info: srpc generator begin.\n" COLOR_OFF);
  105. ret = gen.generate(params);
  106. if (ret == false)
  107. {
  108. printf(COLOR_RED"Error: srpc generator error.\n\n" COLOR_OFF);
  109. return false;
  110. }
  111. printf(COLOR_PURPLE"Info: srpc generator done.\n\n" COLOR_OFF);
  112. }
  113. return CommandController::copy_files();
  114. }
  115. static bool rpc_get_opt(int argc, const char **argv, struct srpc_config *config)
  116. {
  117. int c;
  118. while ((c = getopt(argc, (char * const *)argv,
  119. "o:r:i:x:c:s:d:f:p:")) >= 0)
  120. {
  121. switch (c)
  122. {
  123. case 'o':
  124. if (sscanf(optarg, "%s", config->output_path) != 1)
  125. return false;
  126. break;
  127. case 'r':
  128. config->set_rpc_type(optarg);
  129. break;
  130. case 'i':
  131. config->set_idl_type(optarg);
  132. break;
  133. case 'x':
  134. config->set_data_type(optarg);
  135. break;
  136. case 'c':
  137. config->set_compress_type(optarg);
  138. break;
  139. case 's':
  140. config->service_name = optarg;
  141. break;
  142. case 'd':
  143. config->specified_depend_path = true;
  144. memset(config->depend_path, 0, MAXPATHLEN);
  145. if (sscanf(optarg, "%s", config->depend_path) != 1)
  146. return false;
  147. break;
  148. case 'f':
  149. config->specified_idl_file = optarg;
  150. break;
  151. case 'p':
  152. config->specified_idl_path = optarg;
  153. break;
  154. default:
  155. printf(COLOR_RED "Error:\n Unknown args : "
  156. COLOR_BLUE "%s\n\n" COLOR_OFF, argv[optind - 1]);
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. bool RPCController::get_opt(int argc, const char **argv)
  163. {
  164. optind = 2;
  165. if (rpc_get_opt(argc, argv, &this->config) == false)
  166. return false;
  167. if (optind == argc)
  168. {
  169. printf(COLOR_RED "Missing: PROJECT_NAME\n\n" COLOR_OFF);
  170. return false;
  171. }
  172. this->config.project_name = argv[optind];
  173. optind++;
  174. if (rpc_get_opt(argc, argv, &this->config) == false)
  175. return false;
  176. if (this->config.project_name == NULL)
  177. {
  178. printf(COLOR_RED "Missing: PROJECT_NAME\n\n" COLOR_OFF);
  179. return false;
  180. }
  181. return true;
  182. }
  183. bool RPCController::check_args()
  184. {
  185. if (CommandController::check_args() == false)
  186. return false;
  187. struct srpc_config *config = &this->config;
  188. if (config->rpc_type == PROTOCOL_TYPE_MAX ||
  189. config->idl_type == IDL_TYPE_MAX ||
  190. config->data_type == DATA_TYPE_MAX ||
  191. config->compress_type == COMPRESS_TYPE_MAX)
  192. {
  193. printf(COLOR_RED"Error:\n Invalid rpc args : -r | -i | -c | -d .\n\n" COLOR_OFF);
  194. return false;
  195. }
  196. switch (config->rpc_type)
  197. {
  198. case PROTOCOL_TYPE_THRIFT:
  199. case PROTOCOL_TYPE_THRIFT_HTTP:
  200. if (config->idl_type == IDL_TYPE_PROTOBUF ||
  201. config->data_type == DATA_TYPE_PROTOBUF)
  202. {
  203. printf(COLOR_RED"Error:\n "
  204. COLOR_BLUE"\" %s \" "
  205. COLOR_RED"does NOT support protobuf as idl or data type.\n\n" COLOR_OFF,
  206. config->rpc_type_string());
  207. return false;
  208. }
  209. if (config->idl_type == IDL_TYPE_DEFAULT)
  210. config->idl_type = IDL_TYPE_THRIFT; // as default;
  211. break;
  212. case PROTOCOL_TYPE_BRPC:
  213. case PROTOCOL_TYPE_TRPC:
  214. case PROTOCOL_TYPE_TRPC_HTTP:
  215. if (config->idl_type == IDL_TYPE_THRIFT ||
  216. config->data_type == DATA_TYPE_THRIFT)
  217. {
  218. printf(COLOR_RED "Error:\n "
  219. COLOR_BLUE "\" %s \" "
  220. COLOR_RED "does NOT support thrift as idl or data type.\n" COLOR_OFF,
  221. config->rpc_type_string());
  222. return false;
  223. }
  224. default:
  225. if (config->idl_type == IDL_TYPE_DEFAULT)
  226. config->idl_type = IDL_TYPE_PROTOBUF; // as default;
  227. break;
  228. }
  229. if (config->prepare_specified_idl_file() == false)
  230. return false;
  231. if (config->service_name == NULL)
  232. config->service_name = config->project_name;
  233. return true;
  234. }
  235. APIController::APIController()
  236. {
  237. this->config.type = COMMAND_API;
  238. this->config.idl_type = IDL_TYPE_PROTOBUF;
  239. }
  240. void APIController::print_usage(const char *name) const
  241. {
  242. printf(COLOR_PINK"Usage:\n"
  243. COLOR_INFO" %s " COLOR_COMMAND "api "
  244. COLOR_INFO"<FILE_NAME> " COLOR_FLAG "[FLAGS]\n\n"
  245. COLOR_PINK"Example:\n"
  246. COLOR_PURPLE" %s api my_api\n\n"
  247. COLOR_PINK"Available Flags:\n"
  248. COLOR_FLAG" -o "
  249. COLOR_WHITE": file output path (default: CURRENT_PATH)\n"
  250. COLOR_FLAG" -i "
  251. COLOR_WHITE": idl type [ protobuf | thrift ] (default: protobuf)\n"
  252. COLOR_OFF, name, name);
  253. }
  254. static bool api_get_opt(int argc, const char **argv, struct srpc_config *config)
  255. {
  256. char c;
  257. while ((c = getopt(argc, (char * const *)argv, "o:i:")) != -1)
  258. {
  259. switch (c)
  260. {
  261. case 'o':
  262. memset(config->output_path, 0, MAXPATHLEN);
  263. if (sscanf(optarg, "%s", config->output_path) != 1)
  264. return false;
  265. break;
  266. case 'i':
  267. config->set_idl_type(optarg);
  268. break;
  269. default:
  270. printf(COLOR_RED "Error:\n Unknown args : "
  271. COLOR_BLUE "%s\n\n" COLOR_OFF, argv[optind - 1]);
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. bool APIController::get_opt(int argc, const char **argv)
  278. {
  279. optind = 2;
  280. getcwd(this->config.output_path, MAXPATHLEN);
  281. if (api_get_opt(argc, argv, &this->config) == false)
  282. return false;
  283. if (optind == argc)
  284. {
  285. printf(COLOR_RED "Missing: FILE_NAME\n\n" COLOR_OFF);
  286. return false;
  287. }
  288. this->config.project_name = argv[optind];
  289. optind++;
  290. if (api_get_opt(argc, argv, &this->config) == false)
  291. return false;
  292. if (this->config.project_name == NULL)
  293. {
  294. printf(COLOR_RED "Missing: FILE_NAME\n\n" COLOR_OFF);
  295. return false;
  296. }
  297. this->config.service_name = this->config.project_name;
  298. return true;
  299. }
  300. bool APIController::dependencies_and_dir()
  301. {
  302. std::string idl_file_name;
  303. std::string out_file_name = this->config.project_name;
  304. if (this->config.idl_type == IDL_TYPE_PROTOBUF)
  305. {
  306. out_file_name += ".proto";
  307. idl_file_name = "rpc/rpc.proto";
  308. }
  309. else if (this->config.idl_type == IDL_TYPE_THRIFT)
  310. {
  311. out_file_name += ".thrift";
  312. idl_file_name = "rpc/rpc.thrift";
  313. }
  314. std::string abs_file_name = this->config.output_path;
  315. if (abs_file_name.at(abs_file_name.length() - 1) != '/')
  316. abs_file_name += "/";
  317. abs_file_name += out_file_name;
  318. DIR *dir;
  319. dir = opendir(this->config.output_path);
  320. if (dir == NULL)
  321. {
  322. if (mkdir_p(this->config.output_path, 0755) != 0)
  323. {
  324. perror("Error:\n failed to make output_path ");
  325. return false;
  326. }
  327. }
  328. else
  329. {
  330. closedir(dir);
  331. struct stat st;
  332. if (stat(abs_file_name.c_str(), &st) >= 0)
  333. {
  334. printf(COLOR_RED"Error:\n file"
  335. COLOR_BLUE" %s " COLOR_RED "EXISTED in path"
  336. COLOR_BLUE" %s " COLOR_RED ".\n" COLOR_OFF,
  337. out_file_name.c_str(), this->config.output_path);
  338. return false;
  339. }
  340. }
  341. dir = opendir(this->config.template_path);
  342. if (dir == NULL)
  343. {
  344. printf(COLOR_RED"Error:\n template path "
  345. COLOR_BLUE" %s " COLOR_RED "does NOT exist.\n" COLOR_OFF,
  346. this->config.template_path);
  347. return false;
  348. }
  349. struct file_info info;
  350. info = { idl_file_name , out_file_name, rpc_idl_transform };
  351. this->default_files.push_back(info);
  352. closedir(dir);
  353. return true;
  354. }
  355. void APIController::print_success_info() const
  356. {
  357. std::string file_name = this->config.project_name;
  358. if (this->config.idl_type == IDL_TYPE_PROTOBUF)
  359. file_name += ".proto";
  360. else if (this->config.idl_type == IDL_TYPE_THRIFT)
  361. file_name += ".thrift";
  362. printf(COLOR_GREEN"Success:\n Create api file"
  363. COLOR_BLUE" %s " COLOR_GREEN "at path"
  364. COLOR_BLUE" %s " COLOR_GREEN "done.\n\n" COLOR_OFF,
  365. file_name.c_str(), this->config.output_path);
  366. printf(COLOR_PINK"Suggestions:\n"
  367. COLOR_WHITE" Modify the api file as you needed.\n"
  368. " And make rpc project base on this file with the following command:\n\n"
  369. COLOR_GREEN" ./srpc rpc my_rpc_project -f %s -p %s\n\n" COLOR_OFF,
  370. file_name.c_str(), this->config.output_path);
  371. }