main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Exception_solver::get_instance_pointer()->communication_run();
  52. ///初始化与终端通讯的对象
  53. if(System_communicator::get_instance_pointer()== nullptr)
  54. return FAILED;
  55. memset(bind_string,0,64);
  56. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.terminor_port());
  57. System_communicator::get_instance_pointer()->communication_bind(bind_string);
  58. Error_manager code;
  59. /*
  60. * 初始化各个通讯模块,
  61. */
  62. if(Locate_communicator::get_instance_pointer()== nullptr)
  63. return FAILED;
  64. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.measurer_port());
  65. code=Locate_communicator::get_instance_pointer()->communication_bind(bind_string);
  66. if(code!=SUCCESS)
  67. {
  68. return code;
  69. }
  70. Locate_communicator::get_instance_pointer()->communication_run();
  71. //初始化车位分配通讯模块
  72. if(Parkspace_communicator::get_instance_pointer()== nullptr)
  73. return FAILED;
  74. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.park_space_port());
  75. code=Parkspace_communicator::get_instance_pointer()->communication_bind(bind_string);
  76. if(code!=SUCCESS)
  77. {
  78. return code;
  79. }
  80. Parkspace_communicator::get_instance_pointer()->communication_run();
  81. /*
  82. * 初始化调度模块
  83. */
  84. sprintf(bind_string,"tcp://%s:%d",system_setting.bind_ip().c_str(),system_setting.dispatcher_port());
  85. Dispatch_communicator::get_instance_pointer()->communication_bind(bind_string);
  86. Dispatch_communicator::get_instance_pointer()->communication_run();
  87. /*
  88. * 初始化指令执行模块
  89. */
  90. if(Command_manager::get_instance_pointer()== nullptr)
  91. return Error_manager(FAILED,CRITICAL_ERROR,"创建指令执行模块失败");
  92. code=Command_manager::get_instance_pointer()->init(system_setting);
  93. if(code!=SUCCESS)
  94. {
  95. return code;
  96. }
  97. usleep(1000*1000);
  98. //运行接收指令通讯块,开始接收指令
  99. System_communicator::get_instance_pointer()->communication_run();
  100. LOG(INFO)<<"系统初始化完成 --------------------------------------------- !!!";
  101. return SUCCESS;
  102. }
  103. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  104. {
  105. time_t tt;
  106. time( &tt );
  107. tt = tt + 8*3600; // transform the time zone
  108. tm* t= gmtime( &tt );
  109. char buf[255]={0};
  110. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  111. t->tm_year + 1900,
  112. t->tm_mon + 1,
  113. t->tm_mday,
  114. t->tm_hour,
  115. t->tm_min,
  116. t->tm_sec);
  117. FILE* tp_file=fopen(buf,"w");
  118. fprintf(tp_file,data,strlen(data));
  119. fclose(tp_file);
  120. }
  121. void init_glog()
  122. {
  123. time_t tt = time(0);//时间cuo
  124. struct tm* t = localtime(&tt);
  125. char strYear[255]={0};
  126. char strMonth[255]={0};
  127. char strDay[255]={0};
  128. sprintf(strYear,"%04d", t->tm_year+1900);
  129. sprintf(strMonth,"%02d", t->tm_mon+1);
  130. sprintf(strDay,"%02d", t->tm_mday);
  131. char buf[255]={0};
  132. getcwd(buf,255);
  133. char strdir[255]={0};
  134. sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
  135. PathCreator creator;
  136. creator.Mkdir(strdir);
  137. char logPath[255] = { 0 };
  138. sprintf(logPath, "%s/", strdir);
  139. FLAGS_max_log_size = 100;
  140. FLAGS_logbufsecs = 0;
  141. google::InitGoogleLogging("LidarMeasurement");
  142. google::SetStderrLogging(google::INFO);
  143. google::SetLogDestination(0, logPath);
  144. google::SetLogFilenameExtension("zxlog");
  145. google::InstallFailureSignalHandler();
  146. google::InstallFailureWriter(&shut_down_logging);
  147. FLAGS_colorlogtostderr = true; // Set log color
  148. FLAGS_logbufsecs = 0; // Set log output speed(s)
  149. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  150. FLAGS_stop_logging_if_full_disk = true;
  151. }