1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_DATA_SERVICE_GRPC_WORKER_IMPL_H_
17 #define TENSORFLOW_CORE_DATA_SERVICE_GRPC_WORKER_IMPL_H_
18 
19 #include "grpcpp/server_builder.h"
20 #include "tensorflow/core/data/service/worker.grpc.pb.h"
21 #include "tensorflow/core/data/service/worker_impl.h"
22 #include "tensorflow/core/protobuf/service_config.pb.h"
23 
24 namespace tensorflow {
25 namespace data {
26 
27 // This class is a wrapper that handles communication for gRPC.
28 class GrpcWorkerImpl : public WorkerService::Service {
29  public:
30   // Constructs a GrpcWorkerImpl with the given config, and registers it with
31   // `server_builder`.
32   explicit GrpcWorkerImpl(const experimental::WorkerConfig& config,
33                           ::grpc::ServerBuilder& server_builder);
~GrpcWorkerImpl()34   ~GrpcWorkerImpl() override {}
35 
36   Status Start(const std::string& worker_address,
37                const std::string& transfer_address);
38 
39   std::function<Status(const GetElementRequest*, GetElementResponse*)>
get_element_getter()40   get_element_getter() {
41     return
42         [this](const GetElementRequest* request, GetElementResponse* response) {
43           return impl_.GetElement(request, response);
44         };
45   }
46 
47 #define HANDLER(method)                                 \
48   ::grpc::Status method(::grpc::ServerContext* context, \
49                         const method##Request* request, \
50                         method##Response* response) override;
51   HANDLER(ProcessTask);
52   HANDLER(GetElement);
53   HANDLER(GetWorkerTasks);
54 #undef HANDLER
55 
56  private:
57   DataServiceWorkerImpl impl_;
58 
59   TF_DISALLOW_COPY_AND_ASSIGN(GrpcWorkerImpl);
60 };
61 
62 }  // namespace data
63 }  // namespace tensorflow
64 
65 #endif  // TENSORFLOW_CORE_DATA_SERVICE_GRPC_WORKER_IMPL_H_
66