main.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include <QCoreApplication>
  2. #include <QMutex>
  3. #include <iostream>
  4. #include <QFile>
  5. #include "xmlparam.h"
  6. #include "rawpic.pb.h"
  7. #include "CivetServer.h"
  8. #include <cstring>
  9. #ifdef _WIN32
  10. #include <windows.h>
  11. #else
  12. #include <unistd.h>
  13. #endif
  14. #include "grpccivet.h"
  15. #define DOCUMENT_ROOT "./frontend/dist"
  16. QByteArray gbasnow;
  17. grpcpc * ggrpc;
  18. class WsStartHandler : public CivetHandler
  19. {
  20. public:
  21. bool
  22. handleGet(CivetServer *server, struct mg_connection *conn)
  23. {
  24. mg_printf(conn,
  25. "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
  26. "close\r\n\r\n");
  27. mg_printf(conn, "<!DOCTYPE html>\n");
  28. mg_printf(conn, "<html>\n<head>\n");
  29. mg_printf(conn, "<meta charset=\"UTF-8\">\n");
  30. mg_printf(conn, "<title>ADC IV RemoteCtrl UI</title>\n");
  31. QFile xFile;
  32. xFile.setFileName("./frontend/index.html");
  33. if(xFile.open(QIODevice::ReadOnly))
  34. {
  35. QByteArray ba = xFile.readAll();
  36. mg_printf(conn,ba.data());
  37. }
  38. return 1;
  39. }
  40. };
  41. class PicFrontHandler : public CivetHandler
  42. {
  43. public:
  44. bool
  45. handleGet(CivetServer *server, struct mg_connection *conn)
  46. {
  47. static int ncount;
  48. mg_printf(conn,
  49. "HTTP/1.1 200 OK\r\n"
  50. "Connection: close\r\n"
  51. "Max-Age: 0\r\n"
  52. "Expires: 0\r\n"
  53. "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
  54. "Pragma: no-cache\r\n"
  55. "Content-Type: multipart/x-mixed-replace; "
  56. "boundary=--BoundaryString\r\n"
  57. "\r\n");
  58. mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
  59. mg_printf(conn,
  60. "<script type=\"text/javascript\">\r\n"
  61. "function myrefresh() {\r\n"
  62. "window.location.reload();\r\n"
  63. "}\r\n"
  64. "setTimeout('myrefresh()', 1000);\r\n"
  65. "</script>\r\n");
  66. QByteArray ba;
  67. iv::vision::rawpic xrawpic;
  68. if(ggrpc->GetRawPic(0,xrawpic) == 1)
  69. {
  70. ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  71. }
  72. else
  73. {
  74. ba = gbasnow;
  75. }
  76. // if(gLastUpdate > 0)
  77. // {
  78. // iv::vision::rawpic xrawpic;
  79. // gMutex.lock();
  80. // xrawpic.CopyFrom(mRawPic);
  81. // gMutex.unlock();
  82. // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  83. // }
  84. mg_printf(conn,
  85. "--BoundaryString\r\n"
  86. "Content-type: image/jpeg\r\n"
  87. "Content-Length: %zu\r\n"
  88. "\r\n",
  89. ba.size());
  90. mg_write(conn, ba.data(), ba.size());
  91. mg_printf(conn, "\r\n\r\n");
  92. ncount++;
  93. printf("send pic. %d\n",ncount);
  94. return true;
  95. }
  96. };
  97. class PicRearHandler : public CivetHandler
  98. {
  99. public:
  100. bool
  101. handleGet(CivetServer *server, struct mg_connection *conn)
  102. {
  103. static int ncount;
  104. mg_printf(conn,
  105. "HTTP/1.1 200 OK\r\n"
  106. "Connection: close\r\n"
  107. "Max-Age: 0\r\n"
  108. "Expires: 0\r\n"
  109. "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
  110. "Pragma: no-cache\r\n"
  111. "Content-Type: multipart/x-mixed-replace; "
  112. "boundary=--BoundaryString\r\n"
  113. "\r\n");
  114. mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
  115. mg_printf(conn,
  116. "<script type=\"text/javascript\">\r\n"
  117. "function myrefresh() {\r\n"
  118. "window.location.reload();\r\n"
  119. "}\r\n"
  120. "setTimeout('myrefresh()', 1000);\r\n"
  121. "</script>\r\n");
  122. QByteArray ba;
  123. iv::vision::rawpic xrawpic;
  124. if(ggrpc->GetRawPic(1,xrawpic) == 1)
  125. {
  126. ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  127. }
  128. else
  129. {
  130. ba = gbasnow;
  131. }
  132. // if(gLastUpdate > 0)
  133. // {
  134. // iv::vision::rawpic xrawpic;
  135. // gMutex.lock();
  136. // xrawpic.CopyFrom(mRawPic);
  137. // gMutex.unlock();
  138. // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  139. // }
  140. mg_printf(conn,
  141. "--BoundaryString\r\n"
  142. "Content-type: image/jpeg\r\n"
  143. "Content-Length: %zu\r\n"
  144. "\r\n",
  145. ba.size());
  146. mg_write(conn, ba.data(), ba.size());
  147. mg_printf(conn, "\r\n\r\n");
  148. ncount++;
  149. printf("send pic. %d\n",ncount);
  150. return true;
  151. }
  152. };
  153. class PicLeftHandler : public CivetHandler
  154. {
  155. public:
  156. bool
  157. handleGet(CivetServer *server, struct mg_connection *conn)
  158. {
  159. static int ncount;
  160. mg_printf(conn,
  161. "HTTP/1.1 200 OK\r\n"
  162. "Connection: close\r\n"
  163. "Max-Age: 0\r\n"
  164. "Expires: 0\r\n"
  165. "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
  166. "Pragma: no-cache\r\n"
  167. "Content-Type: multipart/x-mixed-replace; "
  168. "boundary=--BoundaryString\r\n"
  169. "\r\n");
  170. mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
  171. mg_printf(conn,
  172. "<script type=\"text/javascript\">\r\n"
  173. "function myrefresh() {\r\n"
  174. "window.location.reload();\r\n"
  175. "}\r\n"
  176. "setTimeout('myrefresh()', 1000);\r\n"
  177. "</script>\r\n");
  178. QByteArray ba;
  179. iv::vision::rawpic xrawpic;
  180. if(ggrpc->GetRawPic(2,xrawpic) == 1)
  181. {
  182. ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  183. }
  184. else
  185. {
  186. ba = gbasnow;
  187. }
  188. // if(gLastUpdate > 0)
  189. // {
  190. // iv::vision::rawpic xrawpic;
  191. // gMutex.lock();
  192. // xrawpic.CopyFrom(mRawPic);
  193. // gMutex.unlock();
  194. // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  195. // }
  196. mg_printf(conn,
  197. "--BoundaryString\r\n"
  198. "Content-type: image/jpeg\r\n"
  199. "Content-Length: %zu\r\n"
  200. "\r\n",
  201. ba.size());
  202. mg_write(conn, ba.data(), ba.size());
  203. mg_printf(conn, "\r\n\r\n");
  204. ncount++;
  205. printf("send pic. %d\n",ncount);
  206. return true;
  207. }
  208. };
  209. class PicRightHandler : public CivetHandler
  210. {
  211. public:
  212. bool
  213. handleGet(CivetServer *server, struct mg_connection *conn)
  214. {
  215. static int ncount;
  216. mg_printf(conn,
  217. "HTTP/1.1 200 OK\r\n"
  218. "Connection: close\r\n"
  219. "Max-Age: 0\r\n"
  220. "Expires: 0\r\n"
  221. "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
  222. "Pragma: no-cache\r\n"
  223. "Content-Type: multipart/x-mixed-replace; "
  224. "boundary=--BoundaryString\r\n"
  225. "\r\n");
  226. mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
  227. mg_printf(conn,
  228. "<script type=\"text/javascript\">\r\n"
  229. "function myrefresh() {\r\n"
  230. "window.location.reload();\r\n"
  231. "}\r\n"
  232. "setTimeout('myrefresh()', 1000);\r\n"
  233. "</script>\r\n");
  234. QByteArray ba;
  235. iv::vision::rawpic xrawpic;
  236. if(ggrpc->GetRawPic(3,xrawpic) == 1)
  237. {
  238. ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  239. }
  240. else
  241. {
  242. ba = gbasnow;
  243. }
  244. // if(gLastUpdate > 0)
  245. // {
  246. // iv::vision::rawpic xrawpic;
  247. // gMutex.lock();
  248. // xrawpic.CopyFrom(mRawPic);
  249. // gMutex.unlock();
  250. // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
  251. // }
  252. mg_printf(conn,
  253. "--BoundaryString\r\n"
  254. "Content-type: image/jpeg\r\n"
  255. "Content-Length: %zu\r\n"
  256. "\r\n",
  257. ba.size());
  258. mg_write(conn, ba.data(), ba.size());
  259. mg_printf(conn, "\r\n\r\n");
  260. ncount++;
  261. printf("send pic. %d\n",ncount);
  262. return true;
  263. }
  264. };
  265. int main(int argc, char *argv[])
  266. {
  267. QCoreApplication a(argc, argv);
  268. iv::xmlparam::Xmlparam xp("./driver_cloud_grpc_civetweb.xml");
  269. // std::string strmsgname = xp.GetParam("imagemsgname","picfront");
  270. std::string strport = xp.GetParam("Port","6101");
  271. // gpa = iv::modulecomm::RegisterRecv(strmsgname.data(),Listenpic);
  272. QFile xFile;
  273. xFile.setFileName("./frontend/tvsnow.jpg");
  274. if(xFile.open(QIODevice::ReadOnly))
  275. {
  276. gbasnow = xFile.readAll();
  277. }
  278. xFile.close();
  279. mg_init_library(0);
  280. const char *options[] = {
  281. "document_root", DOCUMENT_ROOT, "listening_ports", strport.data(), 0};
  282. std::vector<std::string> cpp_options;
  283. for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
  284. cpp_options.push_back(options[i]);
  285. }
  286. // CivetServer server(options); // <-- C style start
  287. CivetServer server(cpp_options); // <-- C++ style start
  288. WsStartHandler h_ws;
  289. server.addHandler("/", h_ws);
  290. PicFrontHandler h_picfront;
  291. server.addHandler("/picfront", h_picfront);
  292. PicRearHandler h_picrear;
  293. server.addHandler("/picrear", h_picrear);
  294. PicLeftHandler h_picleft;
  295. server.addHandler("/picleft", h_picleft);
  296. PicRightHandler h_picright;
  297. server.addHandler("/picright", h_picright);
  298. ggrpc = new grpcpc("test");
  299. ggrpc->start();
  300. return a.exec();
  301. }