main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <QCoreApplication>
  2. #include <QMutex>
  3. #include <iostream>
  4. #include <QFile>
  5. #include "xmlparam.h"
  6. #include "modulecomm.h"
  7. #include "rawpic.pb.h"
  8. #include "CivetServer.h"
  9. #include <cstring>
  10. #ifdef _WIN32
  11. #include <windows.h>
  12. #else
  13. #include <unistd.h>
  14. #endif
  15. iv::vision::rawpic mRawPic;
  16. QMutex gMutex;
  17. qint64 gLastUpdate = 0;
  18. void * gpa;
  19. #define DOCUMENT_ROOT "./frontend/dist"
  20. #define PORT "8081"
  21. void Listenpic(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  22. {
  23. if(nSize<1000)return;
  24. iv::vision::rawpic pic;
  25. if(false == pic.ParseFromArray(strdata,nSize))
  26. {
  27. std::cout<<"picview Listenpic fail."<<std::endl;
  28. return;
  29. }
  30. gMutex.lock();
  31. mRawPic.CopyFrom(pic);
  32. gMutex.unlock();
  33. gLastUpdate = QDateTime::currentMSecsSinceEpoch();
  34. }
  35. class WsStartHandler : public CivetHandler
  36. {
  37. public:
  38. bool
  39. handleGet(CivetServer *server, struct mg_connection *conn)
  40. {
  41. mg_printf(conn,
  42. "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
  43. "close\r\n\r\n");
  44. mg_printf(conn, "<!DOCTYPE html>\n");
  45. mg_printf(conn, "<html>\n<head>\n");
  46. mg_printf(conn, "<meta charset=\"UTF-8\">\n");
  47. mg_printf(conn, "<title>ADC IV Web UI</title>\n");
  48. QFile xFile;
  49. xFile.setFileName("./frontend/indexpic.html");
  50. if(xFile.open(QIODevice::ReadOnly))
  51. {
  52. QByteArray ba = xFile.readAll();
  53. mg_printf(conn,ba.data());
  54. }
  55. return 1;
  56. }
  57. };
  58. class PicHandler : public CivetHandler
  59. {
  60. public:
  61. bool
  62. handleGet(CivetServer *server, struct mg_connection *conn)
  63. {
  64. static int ncount;
  65. // return false;
  66. // mg_printf(conn,
  67. // "HTTP/1.1 200 OK\r\nContent-Type: "
  68. // "text/html\r\nConnection: close\r\n\r\n");
  69. // mg_printf(conn, "<html><body>");
  70. // mg_printf(conn, "<h2>This is the Pic handler!!!</h2>");
  71. // mg_printf(conn, "</body></html>\n");
  72. mg_printf(conn,
  73. "HTTP/1.1 200 OK\r\n"
  74. "Connection: close\r\n"
  75. "Max-Age: 0\r\n"
  76. "Expires: 0\r\n"
  77. "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
  78. "Pragma: no-cache\r\n"
  79. "Content-Type: multipart/x-mixed-replace; "
  80. "boundary=--BoundaryString\r\n"
  81. "\r\n");
  82. mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
  83. mg_printf(conn,
  84. "<script type=\"text/javascript\">\r\n"
  85. "function myrefresh() {\r\n"
  86. "window.location.reload();\r\n"
  87. "}\r\n"
  88. "setTimeout('myrefresh()', 1000);\r\n"
  89. "</script>\r\n");
  90. QByteArray ba;
  91. if(gLastUpdate > 0)
  92. {
  93. iv::vision::rawpic xrawpic;
  94. gMutex.lock();
  95. xrawpic.CopyFrom(mRawPic);
  96. gMutex.unlock();
  97. ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  98. }
  99. mg_printf(conn,
  100. "--BoundaryString\r\n"
  101. "Content-type: image/jpeg\r\n"
  102. "Content-Length: %zu\r\n"
  103. "\r\n",
  104. ba.size());
  105. mg_write(conn, ba.data(), ba.size());
  106. mg_printf(conn, "\r\n\r\n");
  107. ncount++;
  108. printf("send pic. %d\n",ncount);
  109. return true;
  110. }
  111. };
  112. int main(int argc, char *argv[])
  113. {
  114. QCoreApplication a(argc, argv);
  115. iv::xmlparam::Xmlparam xp("./picview_civetweb.xml");
  116. std::string strmsgname = xp.GetParam("imagemsgname","picfront");
  117. gpa = iv::modulecomm::RegisterRecv(strmsgname.data(),Listenpic);
  118. mg_init_library(0);
  119. const char *options[] = {
  120. "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 0};
  121. std::vector<std::string> cpp_options;
  122. for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
  123. cpp_options.push_back(options[i]);
  124. }
  125. // CivetServer server(options); // <-- C style start
  126. CivetServer server(cpp_options); // <-- C++ style start
  127. WsStartHandler h_ws;
  128. server.addHandler("/", h_ws);
  129. PicHandler h_pic;
  130. server.addHandler("/pic", h_pic);
  131. return a.exec();
  132. }