main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Created by zx on 2020/6/18.
  3. //
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include <glog/logging.h>
  7. #include "system_communicator.h"
  8. #include "Locate_communicator.h"
  9. #include "command_manager.h"
  10. #include "exception_solver.h"
  11. #include "proto_tool.h"
  12. #include "pathcreator.h"
  13. #include <Parkspace_communicator.h>
  14. #include <dispatch_communicator.h>
  15. //using google::protobuf::io::FileInputStream;
  16. //using google::protobuf::io::FileOutputStream;
  17. using google::protobuf::io::ZeroCopyInputStream;
  18. using google::protobuf::io::CodedInputStream;
  19. using google::protobuf::io::ZeroCopyOutputStream;
  20. using google::protobuf::io::CodedOutputStream;
  21. using google::protobuf::Message;
  22. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size);
  23. void init_glog();
  24. Error_manager init_communicators();
  25. int main(int argc,char* argv[])
  26. {
  27. init_glog();
  28. Error_manager code=init_communicators();
  29. if(code!=SUCCESS)
  30. {
  31. LOG(ERROR)<<code.to_string();
  32. return 0;
  33. }
  34. getchar();
  35. return 0;
  36. }
  37. Error_manager init_communicators()
  38. {
  39. setting::System_setting system_setting;
  40. if(! proto_tool::read_proto_param("./setting/system_setting.prototxt",system_setting) )
  41. {
  42. return Error_manager(COMMUNICATION_READ_PROTOBUF_ERROR,MINOR_ERROR,
  43. "Communication_socket_base read_proto_param failed");
  44. }
  45. ///初始化故障处理通讯对象
  46. if(Exception_solver::get_instance_pointer()== nullptr)
  47. return FAILED;
  48. char bind_string[64]={0};
  49. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.exception_handle_port());
  50. Exception_solver::get_instance_pointer()->communication_bind(bind_string);
  51. ///初始化与终端通讯的对象
  52. if(System_communicator::get_instance_pointer()== nullptr)
  53. return FAILED;
  54. memset(bind_string,0,64);
  55. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.terminor_port());
  56. System_communicator::get_instance_pointer()->communication_bind(bind_string);
  57. Error_manager code;
  58. /*
  59. * 初始化各个通讯模块,
  60. */
  61. if(Locate_communicator::get_instance_pointer()== nullptr)
  62. return FAILED;
  63. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.measurer_port());
  64. code=Locate_communicator::get_instance_pointer()->communication_bind(bind_string);
  65. if(code!=SUCCESS)
  66. {
  67. return code;
  68. }
  69. Locate_communicator::get_instance_pointer()->communication_run();
  70. //初始化车位分配通讯模块
  71. if(Parkspace_communicator::get_instance_pointer()== nullptr)
  72. return FAILED;
  73. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.park_space_port());
  74. code=Parkspace_communicator::get_instance_pointer()->communication_bind(bind_string);
  75. if(code!=SUCCESS)
  76. {
  77. return code;
  78. }
  79. Parkspace_communicator::get_instance_pointer()->communication_run();
  80. /*
  81. * 初始化调度模块
  82. */
  83. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.dispatcher_port());
  84. Dispatch_communicator::get_instance_pointer()->communication_bind(bind_string);
  85. Dispatch_communicator::get_instance_pointer()->communication_run();
  86. /*
  87. * 初始化指令执行模块
  88. */
  89. if(Command_manager::get_instance_pointer()== nullptr)
  90. return Error_manager(FAILED,CRITICAL_ERROR,"创建指令执行模块失败");
  91. code=Command_manager::get_instance_pointer()->init(system_setting);
  92. if(code!=SUCCESS)
  93. {
  94. return code;
  95. }
  96. usleep(1000*1000);
  97. //运行接收指令通讯块,开始接收指令
  98. System_communicator::get_instance_pointer()->communication_run();
  99. LOG(INFO)<<"系统初始化完成 --------------------------------------------- !!!";
  100. return SUCCESS;
  101. }
  102. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  103. {
  104. time_t tt;
  105. time( &tt );
  106. tt = tt + 8*3600; // transform the time zone
  107. tm* t= gmtime( &tt );
  108. char buf[255]={0};
  109. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  110. t->tm_year + 1900,
  111. t->tm_mon + 1,
  112. t->tm_mday,
  113. t->tm_hour,
  114. t->tm_min,
  115. t->tm_sec);
  116. FILE* tp_file=fopen(buf,"w");
  117. fprintf(tp_file,data,strlen(data));
  118. fclose(tp_file);
  119. }
  120. void init_glog()
  121. {
  122. time_t tt = time(0);//时间cuo
  123. struct tm* t = localtime(&tt);
  124. char strYear[255]={0};
  125. char strMonth[255]={0};
  126. char strDay[255]={0};
  127. sprintf(strYear,"%04d", t->tm_year+1900);
  128. sprintf(strMonth,"%02d", t->tm_mon+1);
  129. sprintf(strDay,"%02d", t->tm_mday);
  130. char buf[255]={0};
  131. getcwd(buf,255);
  132. char strdir[255]={0};
  133. sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
  134. PathCreator creator;
  135. creator.Mkdir(strdir);
  136. char logPath[255] = { 0 };
  137. sprintf(logPath, "%s/", strdir);
  138. FLAGS_max_log_size = 100;
  139. FLAGS_logbufsecs = 0;
  140. google::InitGoogleLogging("LidarMeasurement");
  141. google::SetStderrLogging(google::INFO);
  142. google::SetLogDestination(0, logPath);
  143. google::SetLogFilenameExtension("zxlog");
  144. google::InstallFailureSignalHandler();
  145. google::InstallFailureWriter(&shut_down_logging);
  146. FLAGS_colorlogtostderr = true; // Set log color
  147. FLAGS_logbufsecs = 0; // Set log output speed(s)
  148. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  149. FLAGS_stop_logging_if_full_disk = true;
  150. }