|
@@ -0,0 +1,140 @@
|
|
|
|
+#include "groupqueryclient.h"
|
|
|
|
+
|
|
|
|
+groupqueryclient::groupqueryclient()
|
|
|
|
+{
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void groupqueryclient::run()
|
|
|
|
+{
|
|
|
|
+ int ninterval = 100;
|
|
|
|
+ // if(ninterval<=0)ninterval = 1000;
|
|
|
|
+
|
|
|
|
+ QTime xTime;
|
|
|
|
+ xTime.start();
|
|
|
|
+ int nlastsend = xTime.elapsed();
|
|
|
|
+
|
|
|
|
+ std::string target_str = mstrserver;
|
|
|
|
+ auto cargs = grpc::ChannelArguments();
|
|
|
|
+ cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
|
|
|
|
+ cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
|
|
|
|
+
|
|
|
|
+ std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
|
|
|
|
+ target_str, grpc::InsecureChannelCredentials(),cargs);
|
|
|
|
+
|
|
|
|
+ std::unique_ptr<iv::group::groupservice::Stub> stub_ = iv::group::groupservice::NewStub(channel);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ iv::group::groupRequest request;
|
|
|
|
+
|
|
|
|
+ int nid = 0;
|
|
|
|
+
|
|
|
|
+ // Container for the data we expect from the server.
|
|
|
|
+ iv::group::groupReply reply;
|
|
|
|
+
|
|
|
|
+ gpr_timespec timespec;
|
|
|
|
+ timespec.tv_sec = 30;//设置阻塞时间为2秒
|
|
|
|
+ timespec.tv_nsec = 0;
|
|
|
|
+ timespec.clock_type = GPR_TIMESPAN;
|
|
|
|
+
|
|
|
|
+ // ClientContext context;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ while(!QThread::isInterruptionRequested())
|
|
|
|
+ {
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
+ if(abs(xTime.elapsed()-nlastsend)<ninterval)
|
|
|
|
+ {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ ClientContext context ;
|
|
|
|
+ context.set_deadline(timespec);
|
|
|
|
+
|
|
|
|
+ request.set_strvehid("testid");
|
|
|
|
+ request.set_ngroup(-1);
|
|
|
|
+ request.set_msgtime(QDateTime::currentMSecsSinceEpoch());
|
|
|
|
+ nid++;
|
|
|
|
+
|
|
|
|
+ nlastsend = xTime.elapsed();
|
|
|
|
+ // The actual RPC.
|
|
|
|
+ Status status = stub_->queryallgroup(&context, request, &reply);
|
|
|
|
+ if (status.ok()) {
|
|
|
|
+ std::cout<<nid<<" query successfully"<<std::endl;
|
|
|
|
+ if(reply.nres() == 1)
|
|
|
|
+ {
|
|
|
|
+ std::cout<<"reply size is "<<reply.xdata().size()<<std::endl;
|
|
|
|
+ iv::group::groupinfo xinfo;
|
|
|
|
+ if(reply.xdata().size() > 0)
|
|
|
|
+ {
|
|
|
|
+ if(xinfo.ParseFromArray(reply.xdata().data(),reply.xdata().size()))
|
|
|
|
+ {
|
|
|
|
+ if(xinfo.mvehinfo_size() > 0)
|
|
|
|
+ {
|
|
|
|
+ unsigned int j;
|
|
|
|
+ for(j=0;j<xinfo.mvehinfo_size();j++)
|
|
|
|
+ {
|
|
|
|
+ iv::group::vehicleinfo * pvehinfo = xinfo.mutable_mvehinfo(j);
|
|
|
|
+ mMutexInfo.lock();
|
|
|
|
+ bool bExist = false;
|
|
|
|
+ unsigned int i;
|
|
|
|
+ for(i=0;i<mvectorvehinfo.size();i++)
|
|
|
|
+ {
|
|
|
|
+ if(mvectorvehinfo[i].strvehid == pvehinfo->vehicleid())
|
|
|
|
+ {
|
|
|
|
+ bExist = true;
|
|
|
|
+ mvectorvehinfo[i].mnLastUpdateTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
+ mvectorvehinfo[i].mvehinfo.CopyFrom(*pvehinfo);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(bExist == false)
|
|
|
|
+ {
|
|
|
|
+ iv::vehinfo xveh;
|
|
|
|
+ xveh.mnLastUpdateTime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
+ xveh.strvehid = pvehinfo->vehicleid();
|
|
|
|
+ xveh.mvehinfo.CopyFrom(*pvehinfo);
|
|
|
|
+ mvectorvehinfo.push_back(xveh);
|
|
|
|
+ }
|
|
|
|
+ mMutexInfo.unlock();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ std::cout << status.error_code() << ": " << status.error_message()
|
|
|
|
+ << std::endl;
|
|
|
|
+ std::cout<<"RPC failed"<<std::endl;
|
|
|
|
+ if(status.error_code() == 4)
|
|
|
|
+ {
|
|
|
|
+ std::cout<<" RPC Exceed Time, Create New stub_"<<std::endl;
|
|
|
|
+ channel = grpc::CreateCustomChannel(
|
|
|
|
+ target_str, grpc::InsecureChannelCredentials(),cargs);
|
|
|
|
+
|
|
|
|
+ stub_ = iv::group::groupservice::NewStub(channel);
|
|
|
|
+ }
|
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(900));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+std::vector<iv::vehinfo> groupqueryclient::GetVehinfo()
|
|
|
|
+{
|
|
|
|
+ std::vector<iv::vehinfo> xrtn;
|
|
|
|
+ mMutexInfo.lock();
|
|
|
|
+ xrtn = mvectorvehinfo;
|
|
|
|
+ mMutexInfo.unlock();
|
|
|
|
+ return xrtn;
|
|
|
|
+}
|