mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
8.4 KiB
253 lines
8.4 KiB
10 years ago
|
/*
|
||
|
*
|
||
|
* Copyright 2015, Google Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* Redistribution and use in source and binary forms, with or without
|
||
|
* modification, are permitted provided that the following conditions are
|
||
|
* met:
|
||
|
*
|
||
|
* * Redistributions of source code must retain the above copyright
|
||
|
* notice, this list of conditions and the following disclaimer.
|
||
|
* * Redistributions in binary form must reproduce the above
|
||
|
* copyright notice, this list of conditions and the following disclaimer
|
||
|
* in the documentation and/or other materials provided with the
|
||
|
* distribution.
|
||
|
* * Neither the name of Google Inc. nor the names of its
|
||
|
* contributors may be used to endorse or promote products derived from
|
||
|
* this software without specific prior written permission.
|
||
|
*
|
||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*
|
||
|
*/
|
||
|
|
||
10 years ago
|
#include <chrono>
|
||
10 years ago
|
#include <iostream>
|
||
|
#include <memory>
|
||
10 years ago
|
#include <random>
|
||
10 years ago
|
#include <string>
|
||
10 years ago
|
#include <thread>
|
||
10 years ago
|
|
||
|
#include <grpc/grpc.h>
|
||
9 years ago
|
#include <grpc++/channel.h>
|
||
10 years ago
|
#include <grpc++/client_context.h>
|
||
|
#include <grpc++/create_channel.h>
|
||
10 years ago
|
#include <grpc++/credentials.h>
|
||
10 years ago
|
#include "helper.h"
|
||
10 years ago
|
#include "route_guide.grpc.pb.h"
|
||
10 years ago
|
|
||
9 years ago
|
using grpc::Channel;
|
||
10 years ago
|
using grpc::ChannelArguments;
|
||
|
using grpc::ClientContext;
|
||
10 years ago
|
using grpc::ClientReader;
|
||
|
using grpc::ClientReaderWriter;
|
||
|
using grpc::ClientWriter;
|
||
10 years ago
|
using grpc::Status;
|
||
10 years ago
|
using examples::Point;
|
||
|
using examples::Feature;
|
||
|
using examples::Rectangle;
|
||
|
using examples::RouteSummary;
|
||
|
using examples::RouteNote;
|
||
|
using examples::RouteGuide;
|
||
10 years ago
|
|
||
10 years ago
|
Point MakePoint(long latitude, long longitude) {
|
||
|
Point p;
|
||
|
p.set_latitude(latitude);
|
||
|
p.set_longitude(longitude);
|
||
|
return p;
|
||
|
}
|
||
|
|
||
|
Feature MakeFeature(const std::string& name,
|
||
|
long latitude, long longitude) {
|
||
|
Feature f;
|
||
|
f.set_name(name);
|
||
|
f.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
|
||
|
return f;
|
||
|
}
|
||
|
|
||
|
RouteNote MakeRouteNote(const std::string& message,
|
||
|
long latitude, long longitude) {
|
||
|
RouteNote n;
|
||
|
n.set_message(message);
|
||
|
n.mutable_location()->CopyFrom(MakePoint(latitude, longitude));
|
||
|
return n;
|
||
|
}
|
||
|
|
||
10 years ago
|
class RouteGuideClient {
|
||
10 years ago
|
public:
|
||
9 years ago
|
RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db)
|
||
10 years ago
|
: stub_(RouteGuide::NewStub(channel)) {
|
||
|
examples::ParseDb(db, &feature_list_);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void GetFeature() {
|
||
|
Point point;
|
||
|
Feature feature;
|
||
10 years ago
|
point = MakePoint(409146138, -746188906);
|
||
|
GetOneFeature(point, &feature);
|
||
|
point = MakePoint(0, 0);
|
||
|
GetOneFeature(point, &feature);
|
||
10 years ago
|
}
|
||
|
|
||
|
void ListFeatures() {
|
||
10 years ago
|
examples::Rectangle rect;
|
||
10 years ago
|
Feature feature;
|
||
|
ClientContext context;
|
||
|
|
||
10 years ago
|
rect.mutable_lo()->set_latitude(400000000);
|
||
|
rect.mutable_lo()->set_longitude(-750000000);
|
||
|
rect.mutable_hi()->set_latitude(420000000);
|
||
|
rect.mutable_hi()->set_longitude(-730000000);
|
||
|
std::cout << "Looking for features between 40, -75 and 42, -73"
|
||
|
<< std::endl;
|
||
|
|
||
10 years ago
|
std::unique_ptr<ClientReader<Feature> > reader(
|
||
|
stub_->ListFeatures(&context, rect));
|
||
|
while (reader->Read(&feature)) {
|
||
10 years ago
|
std::cout << "Found feature called "
|
||
|
<< feature.name() << " at "
|
||
|
<< feature.location().latitude()/kCoordFactor_ << ", "
|
||
10 years ago
|
<< feature.location().longitude()/kCoordFactor_ << std::endl;
|
||
10 years ago
|
}
|
||
|
Status status = reader->Finish();
|
||
10 years ago
|
if (status.ok()) {
|
||
10 years ago
|
std::cout << "ListFeatures rpc succeeded." << std::endl;
|
||
|
} else {
|
||
|
std::cout << "ListFeatures rpc failed." << std::endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void RecordRoute() {
|
||
|
Point point;
|
||
10 years ago
|
RouteSummary stats;
|
||
10 years ago
|
ClientContext context;
|
||
10 years ago
|
const int kPoints = 10;
|
||
10 years ago
|
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
|
||
|
|
||
|
std::default_random_engine generator(seed);
|
||
10 years ago
|
std::uniform_int_distribution<int> feature_distribution(
|
||
|
0, feature_list_.size() - 1);
|
||
|
std::uniform_int_distribution<int> delay_distribution(
|
||
|
500, 1500);
|
||
10 years ago
|
|
||
|
std::unique_ptr<ClientWriter<Point> > writer(
|
||
10 years ago
|
stub_->RecordRoute(&context, &stats));
|
||
|
for (int i = 0; i < kPoints; i++) {
|
||
|
const Feature& f = feature_list_[feature_distribution(generator)];
|
||
|
std::cout << "Visiting point "
|
||
|
<< f.location().latitude()/kCoordFactor_ << ", "
|
||
|
<< f.location().longitude()/kCoordFactor_ << std::endl;
|
||
|
if (!writer->Write(f.location())) {
|
||
|
// Broken stream.
|
||
|
break;
|
||
|
}
|
||
|
std::this_thread::sleep_for(std::chrono::milliseconds(
|
||
|
delay_distribution(generator)));
|
||
|
}
|
||
10 years ago
|
writer->WritesDone();
|
||
|
Status status = writer->Finish();
|
||
10 years ago
|
if (status.ok()) {
|
||
10 years ago
|
std::cout << "Finished trip with " << stats.point_count() << " points\n"
|
||
|
<< "Passed " << stats.feature_count() << " features\n"
|
||
|
<< "Travelled " << stats.distance() << " meters\n"
|
||
|
<< "It took " << stats.elapsed_time() << " seconds"
|
||
|
<< std::endl;
|
||
10 years ago
|
} else {
|
||
|
std::cout << "RecordRoute rpc failed." << std::endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void RouteChat() {
|
||
|
ClientContext context;
|
||
|
|
||
10 years ago
|
std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream(
|
||
|
stub_->RouteChat(&context));
|
||
10 years ago
|
|
||
|
std::thread writer([stream]() {
|
||
|
std::vector<RouteNote> notes{
|
||
|
MakeRouteNote("First message", 0, 0),
|
||
|
MakeRouteNote("Second message", 0, 1),
|
||
|
MakeRouteNote("Third message", 1, 0),
|
||
|
MakeRouteNote("Fourth message", 0, 0)};
|
||
|
for (const RouteNote& note : notes) {
|
||
|
std::cout << "Sending message " << note.message()
|
||
|
<< " at " << note.location().latitude() << ", "
|
||
|
<< note.location().longitude() << std::endl;
|
||
|
stream->Write(note);
|
||
|
}
|
||
|
stream->WritesDone();
|
||
|
});
|
||
|
|
||
|
RouteNote server_note;
|
||
10 years ago
|
while (stream->Read(&server_note)) {
|
||
10 years ago
|
std::cout << "Got message " << server_note.message()
|
||
|
<< " at " << server_note.location().latitude() << ", "
|
||
|
<< server_note.location().longitude() << std::endl;
|
||
10 years ago
|
}
|
||
10 years ago
|
writer.join();
|
||
10 years ago
|
Status status = stream->Finish();
|
||
10 years ago
|
if (!status.ok()) {
|
||
10 years ago
|
std::cout << "RouteChat rpc failed." << std::endl;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
private:
|
||
10 years ago
|
|
||
|
bool GetOneFeature(const Point& point, Feature* feature) {
|
||
|
ClientContext context;
|
||
|
Status status = stub_->GetFeature(&context, point, feature);
|
||
10 years ago
|
if (!status.ok()) {
|
||
10 years ago
|
std::cout << "GetFeature rpc failed." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
if (!feature->has_location()) {
|
||
|
std::cout << "Server returns incomplete feature." << std::endl;
|
||
|
return false;
|
||
|
}
|
||
|
if (feature->name().empty()) {
|
||
|
std::cout << "Found no feature at "
|
||
|
<< feature->location().latitude()/kCoordFactor_ << ", "
|
||
|
<< feature->location().longitude()/kCoordFactor_ << std::endl;
|
||
|
} else {
|
||
|
std::cout << "Found feature called " << feature->name() << " at "
|
||
|
<< feature->location().latitude()/kCoordFactor_ << ", "
|
||
|
<< feature->location().longitude()/kCoordFactor_ << std::endl;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
const float kCoordFactor_ = 10000000.0;
|
||
10 years ago
|
std::unique_ptr<RouteGuide::Stub> stub_;
|
||
10 years ago
|
std::vector<Feature> feature_list_;
|
||
10 years ago
|
};
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
10 years ago
|
// Expect only arg: --db_path=path/to/route_guide_db.json.
|
||
10 years ago
|
std::string db = examples::GetDbFileContent(argc, argv);
|
||
10 years ago
|
RouteGuideClient guide(
|
||
10 years ago
|
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(),
|
||
|
ChannelArguments()),
|
||
10 years ago
|
db);
|
||
10 years ago
|
|
||
10 years ago
|
std::cout << "-------------- GetFeature --------------" << std::endl;
|
||
10 years ago
|
guide.GetFeature();
|
||
10 years ago
|
std::cout << "-------------- ListFeatures --------------" << std::endl;
|
||
10 years ago
|
guide.ListFeatures();
|
||
10 years ago
|
std::cout << "-------------- RecordRoute --------------" << std::endl;
|
||
10 years ago
|
guide.RecordRoute();
|
||
10 years ago
|
std::cout << "-------------- RouteChat --------------" << std::endl;
|
||
10 years ago
|
guide.RouteChat();
|
||
|
|
||
10 years ago
|
return 0;
|
||
10 years ago
|
}
|