service_manager.h 3.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
W
wangguibao 已提交
14

W
wangguibao 已提交
15 16 17
#pragma once
#include <string>
#include <utility>
W
wangguibao 已提交
18 19 20 21 22 23
#include "common/inner_common.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#define REGIST_FORMAT_SERVICE(svr_name, svr)                                 \
  do {                                                                       \
    int ret =                                                                \
        ::baidu::paddle_serving::predictor::FormatServiceManager::instance() \
            .regist_service(svr_name, svr);                                  \
    if (ret != 0) {                                                          \
      LOG(ERROR) << "Failed regist service[" << svr_name << "]"              \
                 << "[" << typeid(svr).name() << "]"                         \
                 << "!";                                                     \
    } else {                                                                 \
      LOG(INFO) << "Success regist service[" << svr_name << "]["             \
                << typeid(svr).name() << "]"                                 \
                << "!";                                                      \
    }                                                                        \
  } while (0)
W
wangguibao 已提交
39 40

class FormatServiceManager {
W
wangguibao 已提交
41 42
 public:
  typedef google::protobuf::Service Service;
W
wangguibao 已提交
43

W
wangguibao 已提交
44 45 46 47 48
  int regist_service(const std::string& svr_name, Service* svr) {
    if (_service_map.find(svr_name) != _service_map.end()) {
      LOG(ERROR) << "Service[" << svr_name << "][" << typeid(svr).name() << "]"
                 << " already exist!";
      return -1;
W
wangguibao 已提交
49 50
    }

W
wangguibao 已提交
51 52 53 54 55 56
    std::pair<boost::unordered_map<std::string, Service*>::iterator, bool> ret;
    ret = _service_map.insert(std::make_pair(svr_name, svr));
    if (ret.second == false) {
      LOG(ERROR) << "Service[" << svr_name << "][" << typeid(svr).name() << "]"
                 << " insert failed!";
      return -1;
W
wangguibao 已提交
57 58
    }

W
wangguibao 已提交
59 60 61 62 63 64 65 66 67 68 69
    LOG(INFO) << "Service[" << svr_name << "] insert successfully!";
    return 0;
  }

  Service* get_service(const std::string& svr_name) {
    boost::unordered_map<std::string, Service*>::iterator res;
    if ((res = _service_map.find(svr_name)) == _service_map.end()) {
      LOG(WARNING) << "Service[" << svr_name << "] "
                   << "not found in service manager"
                   << "!";
      return NULL;
W
wangguibao 已提交
70
    }
W
wangguibao 已提交
71 72
    return (*res).second;
  }
W
wangguibao 已提交
73

W
wangguibao 已提交
74 75 76 77
  static FormatServiceManager& instance() {
    static FormatServiceManager service_;
    return service_;
  }
W
wangguibao 已提交
78

W
wangguibao 已提交
79 80 81
 private:
  boost::unordered_map<std::string, Service*> _service_map;
};
W
wangguibao 已提交
82

W
wangguibao 已提交
83 84 85
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu