main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Created by zx on 2020/6/18.
  3. //
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include <glog/logging.h>
  7. #include "TaskQueue/TQFactory.h"
  8. #include "./communication/communication_socket_base.h"
  9. #include "StoreProcessTask.h"
  10. #include "system_communicator.h"
  11. #include "Locate_communicator.h"
  12. #include "threadSafeQueue.h"
  13. #include "pathcreator.h"
  14. #include <glog/logging.h>
  15. #include <Parkspace_communicator.h>
  16. //using google::protobuf::io::FileInputStream;
  17. //using google::protobuf::io::FileOutputStream;
  18. using google::protobuf::io::ZeroCopyInputStream;
  19. using google::protobuf::io::CodedInputStream;
  20. using google::protobuf::io::ZeroCopyOutputStream;
  21. using google::protobuf::io::CodedOutputStream;
  22. using google::protobuf::Message;
  23. ///线程池
  24. tq::IQueue* g_pthread_queue = nullptr;
  25. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size);
  26. void init_glog();
  27. Error_manager Init_communicators();
  28. int main(int argc,char* argv[])
  29. {
  30. init_glog();
  31. Error_manager code=Init_communicators();
  32. if(code!=SUCCESS)
  33. {
  34. LOG(ERROR)<<code.to_string();
  35. }
  36. g_pthread_queue= tq::TQFactory::CreateDefaultQueue();
  37. g_pthread_queue->Start(12);
  38. //std::thread* pthread=new std::thread(delete_thread);
  39. StoreProcessTask* task=new StoreProcessTask();
  40. int N=0;
  41. while(1)
  42. {
  43. StoreProcessTask* task=new StoreProcessTask();
  44. char license[255]={0};
  45. int rd=rand()%900000+100000;
  46. sprintf(license,"鄂A%d",rd);
  47. task->init_task(rand(),rand()%6,license);
  48. if(g_pthread_queue->TaskCount()<1000)
  49. {
  50. g_pthread_queue->AddTask(task);
  51. N++;
  52. printf("task size / pushed size : %04d / %08d license : %s\n",g_pthread_queue->TaskCount(),N,license);
  53. }
  54. else {
  55. delete task;
  56. }
  57. usleep(1);
  58. }
  59. g_pthread_queue->WaitForFinish();
  60. delete g_pthread_queue;
  61. return 0;
  62. }
  63. Error_manager Init_communicators()
  64. {
  65. Error_manager code;
  66. if(Locate_communicator::get_instance_pointer()== nullptr)
  67. return FAILED;
  68. code=Locate_communicator::get_instance_pointer()->communication_connect("tcp://127.0.0.1:4444");
  69. if(code!=SUCCESS)
  70. {
  71. return code;
  72. }
  73. Locate_communicator::get_instance_pointer()->communication_run();
  74. //
  75. if(Parkspace_communicator::get_instance_pointer()== nullptr)
  76. return FAILED;
  77. code=Parkspace_communicator::get_instance_pointer()->communication_connect("tcp://192.168.2.139:7001");
  78. if(code!=SUCCESS)
  79. {
  80. return code;
  81. }
  82. Parkspace_communicator::get_instance_pointer()->communication_run();
  83. ///最后初始化与终端通讯的对象
  84. if(System_communicator::get_instance_pointer()== nullptr)
  85. return FAILED;
  86. usleep(1000*3000);
  87. return SUCCESS;
  88. }
  89. GOOGLE_GLOG_DLL_DECL void shut_down_logging(const char* data, int size)
  90. {
  91. time_t tt;
  92. time( &tt );
  93. tt = tt + 8*3600; // transform the time zone
  94. tm* t= gmtime( &tt );
  95. char buf[255]={0};
  96. sprintf(buf,"./%d%02d%02d-%02d%02d%02d-dump.txt",
  97. t->tm_year + 1900,
  98. t->tm_mon + 1,
  99. t->tm_mday,
  100. t->tm_hour,
  101. t->tm_min,
  102. t->tm_sec);
  103. FILE* tp_file=fopen(buf,"w");
  104. fprintf(tp_file,data,strlen(data));
  105. fclose(tp_file);
  106. }
  107. void init_glog()
  108. {
  109. time_t tt = time(0);//时间cuo
  110. struct tm* t = localtime(&tt);
  111. char strYear[255]={0};
  112. char strMonth[255]={0};
  113. char strDay[255]={0};
  114. sprintf(strYear,"%04d", t->tm_year+1900);
  115. sprintf(strMonth,"%02d", t->tm_mon+1);
  116. sprintf(strDay,"%02d", t->tm_mday);
  117. char buf[255]={0};
  118. getcwd(buf,255);
  119. char strdir[255]={0};
  120. sprintf(strdir,"%s/log/%s/%s/%s", buf,strYear,strMonth,strDay);
  121. PathCreator creator;
  122. creator.Mkdir(strdir);
  123. char logPath[255] = { 0 };
  124. sprintf(logPath, "%s/", strdir);
  125. FLAGS_max_log_size = 100;
  126. FLAGS_logbufsecs = 0;
  127. google::InitGoogleLogging("LidarMeasurement");
  128. google::SetStderrLogging(google::INFO);
  129. google::SetLogDestination(0, logPath);
  130. google::SetLogFilenameExtension("zxlog");
  131. google::InstallFailureSignalHandler();
  132. google::InstallFailureWriter(&shut_down_logging);
  133. FLAGS_colorlogtostderr = true; // Set log color
  134. FLAGS_logbufsecs = 0; // Set log output speed(s)
  135. FLAGS_max_log_size = 1024; // Set max log file size(GB)
  136. FLAGS_stop_logging_if_full_disk = true;
  137. }