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_COMMUNICATOR_BASE_H_ 18 #define MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_COMMUNICATOR_BASE_H_ 19 20 #include <string> 21 #include <memory> 22 #include <unordered_map> 23 #include <functional> 24 #include <thread> 25 26 #include "ps/core/communicator/message_handler.h" 27 #include "utils/log_adapter.h" 28 #include "ps/core/communicator/http_message_handler.h" 29 #include "ps/core/communicator/tcp_server.h" 30 #include "ps/core/node_info.h" 31 #include "ps/constants.h" 32 33 namespace mindspore { 34 namespace ps { 35 namespace core { 36 enum class TcpUserCommand { 37 kPush, 38 kPull, 39 kCount, 40 kReachThreshold, 41 kResetCount, 42 kGetMetadata, 43 kUpdateMetadata, 44 kCounterEvent, 45 kPullWeight, 46 kPushWeight, 47 kSyncIteration, 48 kNotifyLeaderToNextIter, 49 kPrepareForNextIter, 50 kProceedToNextIter, 51 kEndLastIter, 52 kStartFLJob, 53 kUpdateModel, 54 kGetModel, 55 kPushMetrics, 56 kNewInstance, 57 kQueryInstance, 58 kEnableFLS, 59 kDisableFLS 60 }; 61 62 // CommunicatorBase is used to receive request and send response for server. 63 // It is the base class of HttpCommunicator and TcpCommunicator. 64 class CommunicatorBase { 65 public: 66 using MessageCallback = std::function<void(std::shared_ptr<MessageHandler>)>; 67 using HttpMsgCallback = std::function<void(std::shared_ptr<HttpMessageHandler>)>; 68 using OnNodeEventCallback = std::function<void(const ClusterEvent &)>; 69 using TcpMsgCallback = std::function<void(std::shared_ptr<core::TcpConnection> conn, 70 std::shared_ptr<core::MessageMeta> meta, DataPtr data, size_t size)>; CommunicatorBase()71 CommunicatorBase() : running_(false) {} 72 73 virtual ~CommunicatorBase(); 74 75 virtual bool Start() = 0; 76 virtual bool Stop() = 0; 77 // You need to call the Start() function before calling the Join() function, it will block server's main thread. 78 // if you want to exit the Join() function, then you should call the Stop() function in another thread. 79 void Join(); 80 81 virtual void RegisterMsgCallBack(const std::string &msg_type, const MessageCallback &cb) = 0; 82 83 bool SendResponse(const void *rsp_data, size_t rsp_len, const std::shared_ptr<MessageHandler> &msg_handler); 84 85 bool running() const; 86 87 protected: 88 std::unordered_map<std::string, MessageCallback> msg_callbacks_; 89 std::thread running_thread_; 90 bool running_; 91 }; 92 } // namespace core 93 } // namespace ps 94 } // namespace mindspore 95 #endif // MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_COMMUNICATOR_BASE_H_ 96