main.cpp 5.1 KB

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