• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_COMMUNICATOR_H_
18 #define MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_COMMUNICATOR_H_
19 
20 #include <string>
21 #include <memory>
22 #include <unordered_map>
23 #include "ps/core/communicator/http_server.h"
24 #include "ps/core/communicator/http_message_handler.h"
25 #include "ps/core/communicator/task_executor.h"
26 #include "ps/core/communicator/communicator_base.h"
27 #include "ps/core/communicator/http_msg_handler.h"
28 #include "utils/ms_exception.h"
29 
30 namespace mindspore {
31 namespace ps {
32 namespace core {
33 class HttpCommunicator : public CommunicatorBase {
34  public:
HttpCommunicator(const std::string & ip,uint16_t port,const std::shared_ptr<TaskExecutor> & task_executor)35   explicit HttpCommunicator(const std::string &ip, uint16_t port, const std::shared_ptr<TaskExecutor> &task_executor)
36       : task_executor_(task_executor), http_server_(nullptr), ip_(ip), port_(port) {
37     http_server_ = std::make_shared<HttpServer>(ip_, port_, kThreadNum);
38   }
39 
40   ~HttpCommunicator() = default;
41 
42   bool Start() override;
43   bool Stop() override;
44   void RegisterMsgCallBack(const std::string &msg_type, const MessageCallback &cb) override;
45 
46  private:
47   std::shared_ptr<TaskExecutor> task_executor_;
48   std::shared_ptr<HttpServer> http_server_;
49   std::unordered_map<std::string, HttpMsgCallback> http_msg_callbacks_;
50 
51   std::string ip_;
52   uint16_t port_;
53 };
54 }  // namespace core
55 }  // namespace ps
56 }  // namespace mindspore
57 #endif  // MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_COMMUNICATOR_H_
58