main.cpp 5.0 KB

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