| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include <QCoreApplication>
- #include <QDateTime>
- #include <iostream>
- #include <vector>
- #include <iostream>
- #include <memory>
- #include <string>
- #include <thread>
- #include <grpcpp/grpcpp.h>
- #include <grpcpp/health_check_service_interface.h>
- #include <grpcpp/ext/proto_server_reflection_plugin.h>
- #include "grpcgroup.pb.h"
- #include "groupmsgbuf.h"
- #include "ivbacktrace.h"
- #include "xmlparam.h"
- #include <signal.h>
- using grpc::Server;
- using grpc::ServerBuilder;
- using grpc::ServerContext;
- using grpc::Status;
- #include "./../driver_group_grpc_client/grpcgroup.grpc.pb.h"
- #include "./../driver_db_grpc_server/grpcdb.grpc.pb.h"
- static groupmsgbuf * gpmsgbuf;
- std::string gstrserver;
- bool gbSaveToDB;
- std::string gstrdbpath;
- static std::thread * gptheadgroup;
- static std::thread * gptheaddb;
- static std::unique_ptr<Server> gserver_grpc;
- static std::unique_ptr<Server> gserver_db;
- // Logic and data behind the server's behavior.
- class GroupServiceImpl final : public iv::group::groupservice::Service{
- Status grpcgroup(ServerContext* context, const iv::group::groupRequest* request,
- iv::group::groupReply* reply) override {
- gpmsgbuf->ProcGroupMsg(request,reply);
- return Status::OK;
- }
- Status queryallgroup(ServerContext* context, const iv::group::groupRequest* request,
- iv::group::groupReply* reply) override {
- gpmsgbuf->ProcQueryMsg(request,reply);
- return Status::OK;
- }
- };
- class DBServiceImpl final : public iv::db::dbservice::Service{
- Status querylist(ServerContext* context, const iv::db::listRequest * request,
- iv::db::listReply * reply) override {
- std::cout<<"vehid is "<<request->strvehid().data()<<std::endl;
- // gpmsgbuf->ProcGroupMsg(request,reply);
- gpmsgbuf->ProcDBListMsg(request,reply);
- return Status::OK;
- }
- Status querydata(ServerContext* context, const iv::db::dataRequest * request,
- iv::db::dataReply * reply) override {
- // gpmsgbuf->ProcGroupMsg(request,reply);
- gpmsgbuf->ProcDBFileReqMsg(request,reply);
- return Status::OK;
- }
- Status downdbfile(grpc::ServerContext *context, const iv::db::Filedbreq *request,
- iv::db::FiledbReply *reply) override{
- gpmsgbuf->ProcDBFileDownMsg(request,reply);
- return Status::OK;
- }
- };
- void RunServer() {
- std::string server_address = gstrserver ;//("0.0.0.0:31001");
- GroupServiceImpl service;
- grpc::EnableDefaultHealthCheckService(true);
- // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
- ServerBuilder builder;
- // Listen on the given address without any authentication mechanism.
- builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
- builder.SetMaxReceiveMessageSize(300000000);
- // builder.SetMaxMessageSize(100000000);
- // builder.SetMaxSendMessageSize(100000000);
- // Register "service" as the instance through which we'll communicate with
- // clients. In this case it corresponds to an *synchronous* service.
- builder.RegisterService(&service);
- // Finally assemble the server.
- // std::unique_ptr<Server> server(builder.BuildAndStart());
- gserver_grpc = builder.BuildAndStart();
- std::cout << "Server listening on " << server_address << std::endl;
- // Wait for the server to shutdown. Note that some other thread must be
- // responsible for shutting down the server for this call to ever return.
- // server->Wait();
- gserver_grpc->Wait();
- }
- static std::string getdefdbpath()
- {
- std::string strhome = getenv("HOME");
- std::string strpath = strhome + "/groupdb.db";
- return strpath;
- }
- void RunServerDB() {
- std::string server_address("0.0.0.0:31011");
- DBServiceImpl service;
- grpc::EnableDefaultHealthCheckService(true);
- // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
- ServerBuilder builder;
- // Listen on the given address without any authentication mechanism.
- builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
- builder.SetMaxReceiveMessageSize(300000000);
- // builder.SetMaxMessageSize(100000000);
- // builder.SetMaxSendMessageSize(100000000);
- // Register "service" as the instance through which we'll communicate with
- // clients. In this case it corresponds to an *synchronous* service.
- builder.RegisterService(&service);
- // Finally assemble the server.
- gserver_db = builder.BuildAndStart();
- // std::unique_ptr<Server> server(builder.BuildAndStart());
- std::cout << "Server listening on " << server_address << std::endl;
- // Wait for the server to shutdown. Note that some other thread must be
- // responsible for shutting down the server for this call to ever return.
- // server->Wait();
- gserver_db->Wait();
- // server->Shutdown();
- }
- void threadgroup()
- {
- RunServer();
- }
- void threaddb()
- {
- RunServerDB();
- }
- void signal_handler(int sig)
- {
- if(sig == SIGINT)
- {
- gserver_grpc->Shutdown();
- gserver_db->Shutdown();
- qDebug("shut down grpc.");
- delete gpmsgbuf;
- exit(0);
- }
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- // std::shared_ptr<::google::protobuf::Message> msg_ptr = NULL;
- // msg_ptr =std::shared_ptr<::google::protobuf::Message>(new iv::group::vehicleinfo);
- // iv::group::vehicleinfo xinfo;
- // xinfo.set_groupid(1);
- // ::google::protobuf::Message * pmsg = (::google::protobuf::Message *)&xinfo;
- // ::google::protobuf::Message * pmsg2 = pmsg->New(NULL);
- // iv::group::vehicleinfo *pinfo2 = (iv::group::vehicleinfo *)pmsg2;
- // pinfo2->set_groupid(2);
- RegisterIVBackTrace();
- iv::xmlparam::Xmlparam xp("./driver_group_grpc_server.xml");
- gstrserver = xp.GetParam("server","0.0.0.0:31001");
- gbSaveToDB = xp.GetParam("savetodb",true);
- gstrdbpath = xp.GetParam("dbpath",getdefdbpath());
- std::cout<<"server: "<<gstrserver<<" savetodb:"<<gbSaveToDB<<" db path:"<<gstrdbpath<<std::endl;
- gpmsgbuf = new groupmsgbuf();
- gptheadgroup = new std::thread(threadgroup);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- gptheaddb = new std::thread(threaddb);
- signal(SIGINT,signal_handler);
- return a.exec();
- }
|