main.cpp 4.5 KB

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