Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <thread>
35 :
36 : #include <gflags/gflags.h>
37 : #include <grpc/grpc.h>
38 : #include <grpc/support/alloc.h>
39 : #include <grpc/support/host_port.h>
40 : #include <grpc/support/log.h>
41 : #include <grpc++/server.h>
42 : #include <grpc++/server_builder.h>
43 : #include <grpc++/server_context.h>
44 : #include <grpc++/security/server_credentials.h>
45 :
46 : #include "test/cpp/qps/qpstest.grpc.pb.h"
47 : #include "test/cpp/qps/server.h"
48 : #include "test/cpp/qps/timer.h"
49 :
50 : namespace grpc {
51 : namespace testing {
52 :
53 4 : class TestServiceImpl GRPC_FINAL : public TestService::Service {
54 : public:
55 58803 : Status UnaryCall(ServerContext* context, const SimpleRequest* request,
56 : SimpleResponse* response) GRPC_OVERRIDE {
57 58803 : if (request->response_size() > 0) {
58 58803 : if (!Server::SetPayload(request->response_type(),
59 : request->response_size(),
60 58803 : response->mutable_payload())) {
61 0 : return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
62 : }
63 : }
64 58803 : return Status::OK;
65 : }
66 1 : Status StreamingCall(
67 : ServerContext* context,
68 : ServerReaderWriter<SimpleResponse, SimpleRequest>* stream) GRPC_OVERRIDE {
69 1 : SimpleRequest request;
70 53150 : while (stream->Read(&request)) {
71 53148 : SimpleResponse response;
72 53148 : if (request.response_size() > 0) {
73 53148 : if (!Server::SetPayload(request.response_type(),
74 : request.response_size(),
75 53148 : response.mutable_payload())) {
76 0 : return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
77 : }
78 : }
79 53148 : stream->Write(response);
80 53148 : }
81 1 : return Status::OK;
82 : }
83 : };
84 :
85 4 : class SynchronousServer GRPC_FINAL : public grpc::testing::Server {
86 : public:
87 2 : SynchronousServer(const ServerConfig& config, int port)
88 2 : : impl_(MakeImpl(port)) {}
89 :
90 : private:
91 2 : std::unique_ptr<grpc::Server> MakeImpl(int port) {
92 2 : ServerBuilder builder;
93 :
94 2 : char* server_address = NULL;
95 2 : gpr_join_host_port(&server_address, "::", port);
96 2 : builder.AddListeningPort(server_address, InsecureServerCredentials());
97 2 : gpr_free(server_address);
98 :
99 2 : builder.RegisterService(&service_);
100 :
101 2 : return builder.BuildAndStart();
102 : }
103 :
104 : TestServiceImpl service_;
105 : std::unique_ptr<grpc::Server> impl_;
106 : };
107 :
108 2 : std::unique_ptr<grpc::testing::Server> CreateSynchronousServer(
109 : const ServerConfig& config, int port) {
110 2 : return std::unique_ptr<Server>(new SynchronousServer(config, port));
111 : }
112 :
113 : } // namespace testing
114 : } // namespace grpc
|