gRPC-haskell/examples/hellos/hellos-cpp/hellos_server.cc
Joel Stanley c1fa7956c7 "Hellos" C++/Haskell programs for exercising interop for streaming modes (#49)
* Hellos example: skeleton and server-streaming mode use

* Catch IO exceptions in dispatchLoop

* Distinguish decoding errors from other errors; convert error calls to explicit throws of GRPCIOErrors

* instance Exception GRPCIOError

* Add error checks and error messages to hellos cpp client

* Change fixed32 to uint32

* Add prelim hellos-client, hellos-server executables

* Hellos cpp example: add client-streaming mode use

* In unregistered high-level server, aggressively catch all exceptions raised in
handlers and promote then to a new GRPCIOError constructor.

* Hellos hs example: add client-streaming mode use

* Hellos cpp example: add simple bidi mode use

* Hellos hs example: add simple bidi mode use

* wibbles

* Add GRPCIOErrorEq newtype wrapper w/ Eq instance for testing purposes

* Refactoring wibbles

* README wibbles

* DCR

* Fix rebase derp

* Remove libdl dep, update protobuf github link in hellos cpp Makefile.

* Use Data.Coerce.coerce for GRPCIOErrorEq; remove warnings

* Report expected/got errors in Haskell hellos client/server

* Report expected/got errors in cpp hellos client/server

* Add some instructions for running the hellos client/server

* Fix warnings

* Rename logShow to logMsg and use stderr for logging

* Tweak compliation parameters for hellos hs executables; increase constant workload

* Remove unnecessary type annotation

* Simplify handleError in dispatchLoop

* Remove GRPCIOErrorEq and coerce use; change GRPCIOHandlerException type
2016-07-26 15:21:35 -07:00

87 lines
2.4 KiB
C++

#include <iostream>
#include <memory>
#include <string>
#include <grpc++/grpc++.h>
#include "hellos.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerWriter;
using grpc::ServerReader;
using grpc::ServerReaderWriter;
using grpc::Status;
using hellos::SSRqt;
using hellos::SSRpy;
using hellos::CSRqt;
using hellos::CSRpy;
using hellos::BiRqtRpy;
using hellos::Hellos;
static void Die(const std::string& msg) {
std::cerr << "Fatal error: " << msg << std::endl;
exit(1);
}
class HellosImpl final : public Hellos::Service {
Status HelloSS(ServerContext* context,
const SSRqt* rqt,
ServerWriter<SSRpy>* writer) override {
for (unsigned i = 0; i < rqt->num_replies(); ++i) {
SSRpy rpy;
rpy.set_greeting("Hello there, " + rqt->name() + "!");
writer->Write(rpy);
}
return Status::OK;
}
Status HelloCS(ServerContext* context,
ServerReader<CSRqt>* reader,
CSRpy* rpy) override {
CSRqt rqt;
unsigned rqtCnt = 0;
std::string ex("client streaming payload");
while (reader->Read(&rqt)) {
if (rqt.message() != ex)
Die("HelloCS/rpy: expected payload '" + ex +
"', got '" + rqt.message() + "'");
++rqtCnt;
}
rpy->set_num_requests(rqtCnt);
return Status::OK;
}
Status HelloBi(ServerContext* context,
ServerReaderWriter<BiRqtRpy, BiRqtRpy>* strm) override {
BiRqtRpy rqt;
while (strm->Read(&rqt)) {
strm->Write(rqt);
}
return Status::OK;
}
};
void RunServer() {
std::string server_address("0.0.0.0:50051");
HellosImpl service;
ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// 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());
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();
}
int main(int argc, char** argv) {
RunServer();
return 0;
}