123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include <QCoreApplication>
- #include <QMutex>
- #include <iostream>
- #include <QFile>
- #include "xmlparam.h"
- #include "modulecomm.h"
- #include "rawpic.pb.h"
- #include "CivetServer.h"
- #include <cstring>
- #ifdef _WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #endif
- iv::vision::rawpic mRawPic;
- QMutex gMutex;
- qint64 gLastUpdate = 0;
- void * gpa;
- #define DOCUMENT_ROOT "./frontend/dist"
- #define PORT "8081"
- void Listenpic(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
- {
- if(nSize<1000)return;
- iv::vision::rawpic pic;
- if(false == pic.ParseFromArray(strdata,nSize))
- {
- std::cout<<"picview Listenpic fail."<<std::endl;
- return;
- }
- gMutex.lock();
- mRawPic.CopyFrom(pic);
- gMutex.unlock();
- gLastUpdate = QDateTime::currentMSecsSinceEpoch();
- }
- 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 Web UI</title>\n");
- QFile xFile;
- xFile.setFileName("./frontend/indexpic.html");
- if(xFile.open(QIODevice::ReadOnly))
- {
- QByteArray ba = xFile.readAll();
- mg_printf(conn,ba.data());
- }
- return 1;
- }
- };
- class PicHandler : public CivetHandler
- {
- public:
- bool
- handleGet(CivetServer *server, struct mg_connection *conn)
- {
- static int ncount;
- // return false;
- // mg_printf(conn,
- // "HTTP/1.1 200 OK\r\nContent-Type: "
- // "text/html\r\nConnection: close\r\n\r\n");
- // mg_printf(conn, "<html><body>");
- // mg_printf(conn, "<h2>This is the Pic handler!!!</h2>");
- // mg_printf(conn, "</body></html>\n");
- 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;
- 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("./picview_civetweb.xml");
- std::string strmsgname = xp.GetParam("imagemsgname","picfront");
- gpa = iv::modulecomm::RegisterRecv(strmsgname.data(),Listenpic);
- mg_init_library(0);
- const char *options[] = {
- "document_root", DOCUMENT_ROOT, "listening_ports", PORT, 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);
- PicHandler h_pic;
- server.addHandler("/pic", h_pic);
- return a.exec();
- }
|