123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- #include <QCoreApplication>
- #include <QMutex>
- #include <iostream>
- #include <QFile>
- #include "xmlparam.h"
- #include "rawpic.pb.h"
- #include "CivetServer.h"
- #include <cstring>
- #ifdef _WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #endif
- #include "grpccivet.h"
- #define DOCUMENT_ROOT "./frontend/dist"
- QByteArray gbasnow;
- grpcpc * ggrpc;
- class WsStartHandler : public CivetHandler
- {
- public:
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: "
- "close\r\n\r\n");
- mg_printf(conn, "<!DOCTYPE html>\n");
- mg_printf(conn, "<html>\n<head>\n");
- mg_printf(conn, "<meta charset=\"UTF-8\">\n");
- mg_printf(conn, "<title>ADC IV RemoteCtrl UI</title>\n");
- QFile xFile;
- xFile.setFileName("./frontend/index.html");
- if(xFile.open(QIODevice::ReadOnly))
- {
- QByteArray ba = xFile.readAll();
- mg_printf(conn,ba.data());
- }
- return 1;
- }
- };
- class PicFrontHandler : public CivetHandler
- {
- public:
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- static int ncount;
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\n"
- "Connection: close\r\n"
- "Max-Age: 0\r\n"
- "Expires: 0\r\n"
- "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
- "Pragma: no-cache\r\n"
- "Content-Type: multipart/x-mixed-replace; "
- "boundary=--BoundaryString\r\n"
- "\r\n");
- mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
- mg_printf(conn,
- "<script type=\"text/javascript\">\r\n"
- "function myrefresh() {\r\n"
- "window.location.reload();\r\n"
- "}\r\n"
- "setTimeout('myrefresh()', 1000);\r\n"
- "</script>\r\n");
- QByteArray ba;
- iv::vision::rawpic xrawpic;
- if(ggrpc->GetRawPic(0,xrawpic) == 1)
- {
- ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- }
- else
- {
- ba = gbasnow;
- }
- // if(gLastUpdate > 0)
- // {
- // iv::vision::rawpic xrawpic;
- // gMutex.lock();
- // xrawpic.CopyFrom(mRawPic);
- // gMutex.unlock();
- // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- // }
- mg_printf(conn,
- "--BoundaryString\r\n"
- "Content-type: image/jpeg\r\n"
- "Content-Length: %zu\r\n"
- "\r\n",
- ba.size());
- mg_write(conn, ba.data(), ba.size());
- mg_printf(conn, "\r\n\r\n");
- ncount++;
- printf("send pic. %d\n",ncount);
- return true;
- }
- };
- class PicRearHandler : public CivetHandler
- {
- public:
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- static int ncount;
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\n"
- "Connection: close\r\n"
- "Max-Age: 0\r\n"
- "Expires: 0\r\n"
- "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
- "Pragma: no-cache\r\n"
- "Content-Type: multipart/x-mixed-replace; "
- "boundary=--BoundaryString\r\n"
- "\r\n");
- mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
- mg_printf(conn,
- "<script type=\"text/javascript\">\r\n"
- "function myrefresh() {\r\n"
- "window.location.reload();\r\n"
- "}\r\n"
- "setTimeout('myrefresh()', 1000);\r\n"
- "</script>\r\n");
- QByteArray ba;
- iv::vision::rawpic xrawpic;
- if(ggrpc->GetRawPic(1,xrawpic) == 1)
- {
- ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- }
- else
- {
- ba = gbasnow;
- }
- // if(gLastUpdate > 0)
- // {
- // iv::vision::rawpic xrawpic;
- // gMutex.lock();
- // xrawpic.CopyFrom(mRawPic);
- // gMutex.unlock();
- // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- // }
- mg_printf(conn,
- "--BoundaryString\r\n"
- "Content-type: image/jpeg\r\n"
- "Content-Length: %zu\r\n"
- "\r\n",
- ba.size());
- mg_write(conn, ba.data(), ba.size());
- mg_printf(conn, "\r\n\r\n");
- ncount++;
- printf("send pic. %d\n",ncount);
- return true;
- }
- };
- class PicLeftHandler : public CivetHandler
- {
- public:
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- static int ncount;
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\n"
- "Connection: close\r\n"
- "Max-Age: 0\r\n"
- "Expires: 0\r\n"
- "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
- "Pragma: no-cache\r\n"
- "Content-Type: multipart/x-mixed-replace; "
- "boundary=--BoundaryString\r\n"
- "\r\n");
- mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
- mg_printf(conn,
- "<script type=\"text/javascript\">\r\n"
- "function myrefresh() {\r\n"
- "window.location.reload();\r\n"
- "}\r\n"
- "setTimeout('myrefresh()', 1000);\r\n"
- "</script>\r\n");
- QByteArray ba;
- iv::vision::rawpic xrawpic;
- if(ggrpc->GetRawPic(2,xrawpic) == 1)
- {
- ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- }
- else
- {
- ba = gbasnow;
- }
- // if(gLastUpdate > 0)
- // {
- // iv::vision::rawpic xrawpic;
- // gMutex.lock();
- // xrawpic.CopyFrom(mRawPic);
- // gMutex.unlock();
- // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- // }
- mg_printf(conn,
- "--BoundaryString\r\n"
- "Content-type: image/jpeg\r\n"
- "Content-Length: %zu\r\n"
- "\r\n",
- ba.size());
- mg_write(conn, ba.data(), ba.size());
- mg_printf(conn, "\r\n\r\n");
- ncount++;
- printf("send pic. %d\n",ncount);
- return true;
- }
- };
- class PicRightHandler : public CivetHandler
- {
- public:
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- static int ncount;
- mg_printf(conn,
- "HTTP/1.1 200 OK\r\n"
- "Connection: close\r\n"
- "Max-Age: 0\r\n"
- "Expires: 0\r\n"
- "Cache-Control: no-cache, no-store, must-revalidate, private\r\n"
- "Pragma: no-cache\r\n"
- "Content-Type: multipart/x-mixed-replace; "
- "boundary=--BoundaryString\r\n"
- "\r\n");
- mg_printf(conn,"<meta http-equiv=\"refresh\" content=\"1\">");
- mg_printf(conn,
- "<script type=\"text/javascript\">\r\n"
- "function myrefresh() {\r\n"
- "window.location.reload();\r\n"
- "}\r\n"
- "setTimeout('myrefresh()', 1000);\r\n"
- "</script>\r\n");
- QByteArray ba;
- iv::vision::rawpic xrawpic;
- if(ggrpc->GetRawPic(3,xrawpic) == 1)
- {
- ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- }
- else
- {
- ba = gbasnow;
- }
- // if(gLastUpdate > 0)
- // {
- // iv::vision::rawpic xrawpic;
- // gMutex.lock();
- // xrawpic.CopyFrom(mRawPic);
- // gMutex.unlock();
- // ba.append(xrawpic.picdata().data(),xrawpic.picdata().size());
- // }
- mg_printf(conn,
- "--BoundaryString\r\n"
- "Content-type: image/jpeg\r\n"
- "Content-Length: %zu\r\n"
- "\r\n",
- ba.size());
- mg_write(conn, ba.data(), ba.size());
- mg_printf(conn, "\r\n\r\n");
- ncount++;
- printf("send pic. %d\n",ncount);
- return true;
- }
- };
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- iv::xmlparam::Xmlparam xp("./driver_cloud_grpc_civetweb.xml");
- // std::string strmsgname = xp.GetParam("imagemsgname","picfront");
- std::string strport = xp.GetParam("Port","6101");
- // gpa = iv::modulecomm::RegisterRecv(strmsgname.data(),Listenpic);
- QFile xFile;
- xFile.setFileName("./frontend/tvsnow.jpg");
- if(xFile.open(QIODevice::ReadOnly))
- {
- gbasnow = xFile.readAll();
- }
- xFile.close();
- mg_init_library(0);
- const char *options[] = {
- "document_root", DOCUMENT_ROOT, "listening_ports", strport.data(), 0};
- std::vector<std::string> cpp_options;
- for (int i=0; i<(sizeof(options)/sizeof(options[0])-1); i++) {
- cpp_options.push_back(options[i]);
- }
- // CivetServer server(options); // <-- C style start
- CivetServer server(cpp_options); // <-- C++ style start
- WsStartHandler h_ws;
- server.addHandler("/", h_ws);
- PicFrontHandler h_picfront;
- server.addHandler("/picfront", h_picfront);
- PicRearHandler h_picrear;
- server.addHandler("/picrear", h_picrear);
- PicLeftHandler h_picleft;
- server.addHandler("/picleft", h_picleft);
- PicRightHandler h_picright;
- server.addHandler("/picright", h_picright);
- ggrpc = new grpcpc("test");
- ggrpc->start();
- return a.exec();
- }
|