| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <iostream> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
#include "absl/flags/flag.h" |
| 24 |
|
| 25 |
#include <grpc/support/log.h> |
| 26 |
#include <grpcpp/server.h> |
| 27 |
#include <grpcpp/server_builder.h> |
| 28 |
#include <grpcpp/server_context.h> |
| 29 |
|
| 30 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 31 |
#include "test/cpp/util/test_config.h" |
| 32 |
|
| 33 |
ABSL_FLAG(std::string, address, "", "Address to bind to"); |
| 34 |
|
| 35 |
using grpc::testing::EchoRequest; |
| 36 |
using grpc::testing::EchoResponse; |
| 37 |
|
| 38 |
namespace grpc { |
| 39 |
namespace testing { |
| 40 |
|
| 41 |
class ServiceImpl final : public grpc::testing::EchoTestService::Service { |
| 42 |
Status BidiStream( |
| 43 |
ServerContext* /*context*/, |
| 44 |
ServerReaderWriter<EchoResponse, EchoRequest>* stream) override { |
| 45 |
EchoRequest request; |
| 46 |
EchoResponse response; |
| 47 |
while (stream->Read(&request)) { |
| 48 |
gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 49 |
response.set_message(request.message()); |
| 50 |
stream->Write(response); |
| 51 |
} |
| 52 |
return Status::OK; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
void RunServer() { |
| 57 |
ServiceImpl service; |
| 58 |
|
| 59 |
ServerBuilder builder; |
| 60 |
builder.AddListeningPort(absl::GetFlag(FLAGS_address), |
| 61 |
grpc::InsecureServerCredentials()); |
| 62 |
builder.RegisterService(&service); |
| 63 |
std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 64 |
std::cout << "Server listening on " << absl::GetFlag(FLAGS_address) |
| 65 |
<< std::endl; |
| 66 |
server->Wait(); |
| 67 |
} |
| 68 |
} // namespace testing |
| 69 |
} // namespace grpc |
| 70 |
|
| 71 |
int main(int argc, char** argv) { |
| 72 |
grpc::testing::InitTest(&argc, &argv, true); |
| 73 |
grpc::testing::RunServer(); |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |