#include #include #include #include #include #include #include #include #include #include #include #include "grpcgroup.pb.h" #include "groupmsgbuf.h" #include "ivbacktrace.h" #include "xmlparam.h" #include 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 gserver_grpc; static std::unique_ptr 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 "<strvehid().data()<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(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(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: "<