srpc_controller.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <fcntl.h>
  10. #include "srpc_controller.h"
  11. static constexpr const char *DEPENDENCIES_ERROR = R"(Warning:
  12. Default dependencies path : %s does not have third_party dependencies.
  13. This may cause link error in project : %s
  14. Please check or specify srpc path with '-d'
  15. Or use the following command to pull srpc:
  16. "git clone --recursive https://github.com/sogou/srpc.git"
  17. "cd srpc && make"
  18. )";
  19. static constexpr const char *CMAKE_PROTOC_CODES = R"(
  20. find_program(PROTOC "protoc")
  21. if(${PROTOC} STREQUAL "PROTOC-NOTFOUND")
  22. message(FATAL_ERROR "Protobuf compiler is missing!")
  23. endif ()
  24. protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${IDL_FILE}))";
  25. int mkdir_p(const char *name, mode_t mode)
  26. {
  27. int ret = mkdir(name, mode);
  28. if (ret == 0 || errno != ENOENT)
  29. return ret;
  30. size_t len = strlen(name);
  31. if (len > MAXPATHLEN)
  32. {
  33. errno = ENAMETOOLONG;
  34. return -1;
  35. }
  36. char path[MAXPATHLEN + 1] = {};
  37. memcpy(path, name, len);
  38. if (name[len - 1] != '/')
  39. {
  40. path[len] = '/';
  41. len++;
  42. }
  43. bool has_valid = false;
  44. for (int i = 0; i < len; i++)
  45. {
  46. if (path[i] != '/' && path[i] != '.') // simple check of valid path
  47. {
  48. has_valid = true;
  49. continue;
  50. }
  51. if (path[i] == '/' && has_valid == true)
  52. {
  53. path[i] = '\0';
  54. ret = mkdir(path, mode);
  55. if (ret != 0 && errno != EEXIST)
  56. return ret;
  57. path[i] = '/';
  58. has_valid = false;
  59. }
  60. }
  61. return ret;
  62. }
  63. static const char *get_proxy_rpc_string(const char *type)
  64. {
  65. if (strcasecmp(type, "SRPCHttp") == 0)
  66. return "SRPCHttp";
  67. if (strcasecmp(type, "SRPC") == 0)
  68. return "SRPC";
  69. if (strcasecmp(type, "BRPC") == 0)
  70. return "BRPC";
  71. if (strcasecmp(type, "ThriftHttp") == 0)
  72. return "ThriftHttp";
  73. if (strcasecmp(type, "Thrift") == 0)
  74. return "Thrift";
  75. if (strcasecmp(type, "TRPCHttp") == 0)
  76. return "TRPCHttp";
  77. if (strcasecmp(type, "TRPC") == 0)
  78. return "TRPC";
  79. return "Unknown type";
  80. }
  81. // path=/root/a/
  82. // file=b/c/d.txt
  83. // make the middle path and return the full file name
  84. static std::string make_file_path(const char *path, const std::string& file)
  85. {
  86. DIR *dir;
  87. auto pos = file.find_last_of('/');
  88. std::string file_name;
  89. if (pos != std::string::npos)
  90. {
  91. file_name = file.substr(pos + 1);
  92. std::string dir_name = std::string(path) + std::string("/") +
  93. file.substr(0, pos + 1);
  94. dir = opendir(dir_name.c_str());
  95. if (dir == NULL)
  96. {
  97. if (mkdir_p(dir_name.c_str(), 0755) != 0)
  98. return "";
  99. }
  100. else
  101. closedir(dir);
  102. }
  103. file_name = path;
  104. file_name += "/";
  105. file_name += file;
  106. return file_name;
  107. }
  108. static std::string rpc_client_file_codes(const struct srpc_config *config)
  109. {
  110. std::string ret;
  111. if (config->compress_type != COMPRESS_TYPE_NONE)
  112. {
  113. ret += "\tparams.task_params.compress_type = ";
  114. ret += config->rpc_compress_string();
  115. ret += ";\n";
  116. }
  117. if (config->data_type != DATA_TYPE_DEFAULT)
  118. {
  119. ret += "\tparams.task_params.data_type = ";
  120. ret += config->rpc_data_string();
  121. ret += ";\n";
  122. }
  123. return ret;
  124. }
  125. static std::string rpc_server_file_codes(const struct srpc_config *config)
  126. {
  127. std::string ret;
  128. if (config->compress_type != COMPRESS_TYPE_NONE)
  129. {
  130. ret += " ctx->set_compress_type(";
  131. ret += config->rpc_compress_string();
  132. ret += ");\n";
  133. }
  134. if (config->data_type != DATA_TYPE_DEFAULT)
  135. {
  136. ret += " ctx->set_data_type(";
  137. ret += config->rpc_data_string();
  138. ret += ");\n";
  139. }
  140. ret += " ";
  141. return ret;
  142. }
  143. bool rpc_idl_transform(const std::string& format, FILE *out,
  144. const struct srpc_config *config)
  145. {
  146. size_t len = fprintf(out, format.c_str(), config->service_name);
  147. return len > 0;
  148. }
  149. bool rpc_client_transform(const std::string& format, FILE *out,
  150. const struct srpc_config *config)
  151. {
  152. std::string prepare_params = rpc_client_file_codes(config);
  153. const char *rpc_type;
  154. if (config->type == COMMAND_PROXY)
  155. rpc_type = get_proxy_rpc_string(config->proxy_client_type_string());
  156. else
  157. rpc_type = config->rpc_type_string();
  158. const char *thrift_namespace = "";
  159. if (config->idl_type == IDL_TYPE_THRIFT)
  160. thrift_namespace = config->project_name;
  161. size_t len = fprintf(out, format.c_str(),
  162. config->service_name, prepare_params.c_str(),
  163. config->service_name, rpc_type,
  164. thrift_namespace, thrift_namespace);
  165. return len > 0;
  166. }
  167. bool rpc_server_transform(const std::string& format, FILE *out,
  168. const struct srpc_config *config)
  169. {
  170. std::string prepare_ctx = rpc_server_file_codes(config);
  171. const char *rpc_type;
  172. if (config->type == COMMAND_PROXY)
  173. rpc_type = get_proxy_rpc_string(config->proxy_server_type_string());
  174. else
  175. rpc_type = config->rpc_type_string();
  176. size_t len = fprintf(out, format.c_str(),
  177. config->service_name, config->service_name,
  178. prepare_ctx.c_str(), rpc_type,
  179. config->project_name, rpc_type);
  180. return len > 0;
  181. }
  182. bool CommandController::parse_args(int argc, const char **argv)
  183. {
  184. if (argc < 3)
  185. {
  186. if (argc == 2 && strncmp(argv[1], "api", strlen(argv[1])) == 0)
  187. printf(COLOR_RED "Missing: FILE_NAME\n\n" COLOR_OFF);
  188. else
  189. printf(COLOR_RED "Missing: PROJECT_NAME\n\n" COLOR_OFF);
  190. return false;
  191. }
  192. memset(this->config.output_path, 0, MAXPATHLEN);
  193. if (get_path(__FILE__, this->config.depend_path, 2) == false)
  194. return false;
  195. if (get_path(__FILE__, this->config.template_path, 1) == false)
  196. return false;
  197. snprintf(this->config.template_path + strlen(this->config.template_path),
  198. MAXPATHLEN - strlen(this->config.template_path), "templates/");
  199. if (this->get_opt(argc, argv) == false)
  200. return false;
  201. if (this->check_args() == false)
  202. return false;
  203. return true;
  204. }
  205. bool CommandController::check_args()
  206. {
  207. size_t path_len = strlen(this->config.output_path);
  208. if (strlen(this->config.project_name) >= MAXPATHLEN - path_len - 2)
  209. {
  210. printf(COLOR_RED"Error:\n project name" COLOR_BLUE " %s "
  211. COLOR_RED"or path" COLOR_BLUE " %s "
  212. COLOR_RED" is too long. Total limit : %d.\n\n" COLOR_OFF,
  213. this->config.project_name, this->config.output_path, MAXPATHLEN);
  214. return false;
  215. }
  216. if (path_len != 0 && this->config.output_path[path_len - 1] != '/')
  217. {
  218. this->config.output_path[path_len] = '/';
  219. path_len++;
  220. }
  221. snprintf(this->config.output_path + path_len, MAXPATHLEN - path_len, "%s/",
  222. this->config.project_name);
  223. return true;
  224. }
  225. static inline bool is_with_srpc(const struct srpc_config *config)
  226. {
  227. if (config->type == COMMAND_RPC ||
  228. (config->type == COMMAND_PROXY &&
  229. check_proxy_type(config->proxy_server_type) != PROXY_BASIC_TYPE &&
  230. check_proxy_type(config->proxy_client_type) != PROXY_BASIC_TYPE))
  231. {
  232. return true;
  233. }
  234. return false;
  235. }
  236. static bool check_dependencies_and_init(const struct srpc_config *config)
  237. {
  238. int status;
  239. bool with_srpc = is_with_srpc(config);
  240. // the specified_depend_path must for workflow
  241. if (config->specified_depend_path == true && with_srpc == false)
  242. return true;
  243. std::string check_file = config->depend_path;
  244. if (with_srpc == false)
  245. check_file += "workflow/workflow-config.cmake.in";
  246. else
  247. check_file += "third_party/snappy/cmake";
  248. if (access(check_file.c_str(), 0) != 0)
  249. {
  250. std::string cmd = "cd ";
  251. cmd += config->depend_path;
  252. cmd += "&& git submodule update --init";
  253. if (with_srpc == false)
  254. cmd += " workflow";
  255. status = system(cmd.c_str());
  256. if (status == -1 || status == 127)
  257. {
  258. perror("git submodule update failed");
  259. return false;
  260. }
  261. if (access(check_file.c_str(), 0) != 0)
  262. {
  263. printf(DEPENDENCIES_ERROR, config->depend_path,
  264. config->project_name);
  265. return false;
  266. }
  267. }
  268. return true;
  269. }
  270. static bool check_libraries_and_compile(const struct srpc_config *config)
  271. {
  272. int status;
  273. std::string library_path;
  274. std::string compile_path = config->depend_path;
  275. if (is_with_srpc(config) == false && config->specified_depend_path == false)
  276. compile_path += "/workflow";
  277. library_path = compile_path;
  278. library_path += "/_lib";
  279. if (access(library_path.c_str(), 0) != 0)
  280. {
  281. std::string cmd = "cd ";
  282. cmd += compile_path;
  283. cmd += " && make -j4";
  284. status = system(cmd.c_str());
  285. if (status == -1 || status == 127)
  286. {
  287. perror("execute command make failed");
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293. bool CommandController::dependencies_and_dir()
  294. {
  295. struct srpc_config *config = &this->config;
  296. DIR *dir;
  297. dir = opendir(config->output_path);
  298. if (dir != NULL)
  299. {
  300. printf(COLOR_RED "Error:\n project path "
  301. COLOR_BLUE " %s " COLOR_RED " EXISTS.\n\n" COLOR_OFF,
  302. config->output_path);
  303. closedir(dir);
  304. return false;
  305. }
  306. dir = opendir(config->template_path);
  307. if (dir == NULL)
  308. {
  309. printf(COLOR_RED "Error:\n template path "
  310. COLOR_BLUE " %s " COLOR_RED "does NOT exist.\n" COLOR_OFF,
  311. config->template_path);
  312. return false;
  313. }
  314. closedir(dir);
  315. if (check_dependencies_and_init(config) == false)
  316. return false;
  317. if (check_libraries_and_compile(config) == false)
  318. return false;
  319. if (mkdir_p(config->output_path, 0755) != 0)
  320. {
  321. perror("Error:\n failed to make project ");
  322. return false;
  323. }
  324. std::string config_path = config->output_path;
  325. config_path += "/config";
  326. if (mkdir(config_path.c_str(), 0755) != 0)
  327. {
  328. perror("Error:\n failed to make project config path ");
  329. return false;
  330. }
  331. return true;
  332. }
  333. // get the depth-th upper path of file
  334. bool CommandController::get_path(const char *file, char *path, int depth)
  335. {
  336. size_t len = strlen(file);
  337. size_t i;
  338. memset(path, 0, MAXPATHLEN);
  339. if (len == 0 || depth <= 0)
  340. return false;
  341. int state = 0;
  342. for (i = len - 1; i >= 0; i--)
  343. {
  344. if (file[i] == '/')
  345. {
  346. state++;
  347. if (state >= depth)
  348. break;
  349. }
  350. }
  351. if (state != depth)
  352. return false;
  353. memcpy(path, file, i + 1);
  354. return true;
  355. }
  356. bool CommandController::copy_single_file(const std::string& in_file,
  357. const std::string& out_file,
  358. transform_function_t transform)
  359. {
  360. FILE *read_fp;
  361. FILE *write_fp;
  362. size_t size;
  363. char *buf;
  364. bool ret = false;
  365. read_fp = fopen(in_file.c_str(), "r");
  366. if (read_fp)
  367. {
  368. write_fp = fopen(out_file.c_str(), "w");
  369. if (write_fp)
  370. {
  371. fseek(read_fp, 0, SEEK_END);
  372. size = ftell(read_fp);
  373. buf = (char *)malloc(size);
  374. if (buf)
  375. {
  376. fseek(read_fp, 0, SEEK_SET);
  377. if (fread(buf, size, 1, read_fp) == 1)
  378. {
  379. if (transform != nullptr)
  380. {
  381. std::string format = std::string(buf, size);
  382. ret = transform(format, write_fp, &this->config);
  383. }
  384. else
  385. ret = fwrite(buf, size, 1, write_fp) >= 0 ? true : false;
  386. }
  387. else
  388. {
  389. printf(COLOR_RED"Error:\n read " COLOR_WHITE
  390. " %s " COLOR_RED "failed\n\n" COLOR_OFF,
  391. in_file.c_str());
  392. }
  393. free(buf);
  394. }
  395. else
  396. printf(COLOR_RED"Error:\n system error.\n\n" COLOR_OFF);
  397. fclose(write_fp);
  398. }
  399. else
  400. {
  401. printf(COLOR_RED"Error:\n write" COLOR_WHITE " %s "
  402. COLOR_RED"failed\n\n" COLOR_OFF, out_file.c_str());
  403. }
  404. }
  405. else
  406. {
  407. printf(COLOR_RED"Error:\n open" COLOR_WHITE " %s "
  408. COLOR_RED " failed.\n\n" COLOR_OFF, in_file.c_str());
  409. }
  410. return ret;
  411. }
  412. bool CommandController::copy_files()
  413. {
  414. std::string read_file;
  415. std::string write_file;
  416. bool ret = true;
  417. for (auto it = this->default_files.begin();
  418. it != this->default_files.end() && ret == true; it++)
  419. {
  420. read_file = this->config.template_path;
  421. read_file += it->in_file;
  422. write_file = make_file_path(this->config.output_path, it->out_file);
  423. if (write_file.empty())
  424. ret = false;
  425. else
  426. ret = this->copy_single_file(read_file, write_file, it->transform);
  427. }
  428. return ret;
  429. }
  430. void CommandController::print_success_info() const
  431. {
  432. printf(COLOR_GREEN"Success!\n make project path"
  433. COLOR_BLUE" %s " COLOR_GREEN " done.\n\n",
  434. this->config.output_path);
  435. printf(COLOR_PINK"Commands:\n " COLOR_BLUE "cd %s\n make -j\n\n",
  436. this->config.output_path);
  437. printf(COLOR_PINK"Execute:\n "
  438. COLOR_GREEN "./server\n ./client\n\n" COLOR_OFF);
  439. }
  440. bool common_cmake_transform(const std::string& format, FILE *out,
  441. const struct srpc_config *config)
  442. {
  443. std::string path = config->depend_path;
  444. if (config->specified_depend_path == false)
  445. path += "workflow";
  446. std::string codes_str;
  447. std::string executors_str;
  448. if (config->type == COMMAND_FILE)
  449. codes_str = " file_service.cc";
  450. if (config->type != COMMAND_FILE && config->type != COMMAND_COMPUTE)
  451. executors_str = " client";
  452. if (config->type == COMMAND_PROXY)
  453. executors_str += " proxy";
  454. size_t len = fprintf(out, format.c_str(), config->project_name,
  455. path.c_str(), codes_str.c_str(), executors_str.c_str());
  456. return len > 0;
  457. }
  458. void CommandController::fill_rpc_default_files()
  459. {
  460. struct file_info info;
  461. std::string idl_file_name, client_file_name, server_file_name;
  462. std::string out_file_name = this->config.project_name;
  463. if (this->config.idl_type == IDL_TYPE_PROTOBUF)
  464. {
  465. out_file_name += ".proto";
  466. idl_file_name = "rpc/rpc.proto";
  467. client_file_name = "rpc/client_protobuf.cc";
  468. server_file_name = "rpc/server_protobuf.cc";
  469. }
  470. else
  471. {
  472. out_file_name += ".thrift";
  473. idl_file_name = "rpc/rpc.thrift";
  474. client_file_name = "rpc/client_thrift.cc";
  475. server_file_name = "rpc/server_thrift.cc";
  476. }
  477. info = { idl_file_name , out_file_name, rpc_idl_transform };
  478. this->default_files.push_back(info);
  479. info = { client_file_name, "client_main.cc", rpc_client_transform };
  480. this->default_files.push_back(info);
  481. info = { server_file_name, "server_main.cc", rpc_server_transform };
  482. this->default_files.push_back(info);
  483. info = { "rpc/server.conf", "server.conf", nullptr };
  484. this->default_files.push_back(info);
  485. if (this->config.type == COMMAND_RPC)
  486. info = { "rpc/client.conf", "client.conf", nullptr };
  487. else
  488. info = { "proxy/client_rpc.conf", "client.conf", nullptr };
  489. this->default_files.push_back(info);
  490. }
  491. bool rpc_cmake_transform(const std::string& format, FILE *out,
  492. const struct srpc_config *config)
  493. {
  494. std::string idl_file_name;
  495. std::string srpc_path = config->depend_path;
  496. std::string workflow_path = config->depend_path;
  497. workflow_path += "workflow";
  498. if (config->specified_idl_file != NULL)
  499. {
  500. idl_file_name = config->specified_idl_file;
  501. size_t pos = idl_file_name.find_last_of('/');
  502. if (pos != std::string::npos)
  503. idl_file_name = idl_file_name.substr(pos + 1);
  504. }
  505. else if (config->idl_type == IDL_TYPE_PROTOBUF)
  506. {
  507. idl_file_name = config->project_name;
  508. idl_file_name += ".proto";
  509. }
  510. else
  511. {
  512. idl_file_name = config->project_name;
  513. idl_file_name += ".thrift";
  514. }
  515. std::string is_proxy_str = config->type == COMMAND_PROXY ? " proxy" : "";
  516. size_t len = fprintf(out, format.c_str(), config->project_name,
  517. workflow_path.c_str(), srpc_path.c_str(),
  518. idl_file_name.c_str(),
  519. config->idl_type == IDL_TYPE_PROTOBUF ?
  520. CMAKE_PROTOC_CODES : "",
  521. is_proxy_str.c_str());
  522. return len > 0;
  523. }