/**
 * Copyright 2020 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MINDSPORE_SERVING_MASTER_GRPC_PROCESS_H
#define MINDSPORE_SERVING_MASTER_GRPC_PROCESS_H

#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <memory>
#include <string>
#include "common/serving_common.h"
#include "common/heart_beat.h"
#include "proto/ms_service.pb.h"
#include "proto/ms_service.grpc.pb.h"
#include "proto/ms_master.pb.h"
#include "proto/ms_master.grpc.pb.h"
#include "proto/ms_worker.pb.h"
#include "proto/ms_worker.grpc.pb.h"
#include "master/dispacther.h"

namespace mindspore {
namespace serving {

// Service Implement
class MSServiceImpl {
 public:
  explicit MSServiceImpl(std::shared_ptr<Dispatcher> dispatcher) : dispatcher_(dispatcher) {}
  ~MSServiceImpl() = default;

  void PredictAsync(const proto::PredictRequest *request, proto::PredictReply *reply, PredictOnFinish on_finish);

 private:
  std::shared_ptr<Dispatcher> dispatcher_;
};

// Service Implement
class MSMasterImpl final : public proto::MSMaster::Service {
 public:
  explicit MSMasterImpl(std::shared_ptr<Dispatcher> dispatcher, const std::string server_address)
      : dispatcher_(dispatcher) {
    if (!watcher_) {
      watcher_ = std::make_shared<Watcher<proto::MSWorker, proto::MSWorker>>(server_address);
    }
  }
  ~MSMasterImpl() = default;

  grpc::Status Register(grpc::ServerContext *context, const proto::RegisterRequest *request,
                        proto::RegisterReply *reply) override;
  grpc::Status Exit(grpc::ServerContext *context, const proto::ExitRequest *request, proto::ExitReply *reply) override;
  grpc::Status AddWorker(grpc::ServerContext *context, const proto::AddWorkerRequest *request,
                         proto::AddWorkerReply *reply) override;
  grpc::Status RemoveWorker(grpc::ServerContext *context, const proto::RemoveWorkerRequest *request,
                            proto::RemoveWorkerReply *reply) override;
  grpc::Status Ping(grpc::ServerContext *context, const proto::PingRequest *request, proto::PingReply *reply) override;
  grpc::Status Pong(grpc::ServerContext *context, const proto::PongRequest *request, proto::PongReply *reply) override;

 private:
  std::shared_ptr<Dispatcher> dispatcher_;
  std::shared_ptr<Watcher<proto::MSWorker, proto::MSWorker>> watcher_;
};

}  // namespace serving
}  // namespace mindspore

#endif  // MINDSPORE_SERVING_MASTER_GRPC_PROCESS_H
