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_REQUEST_HANDLER_H_ 18 #define MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_REQUEST_HANDLER_H_ 19 20 #include <event2/event.h> 21 #include <event2/http.h> 22 #include <event2/http_struct.h> 23 #include <event2/bufferevent.h> 24 #include <event2/bufferevent_ssl.h> 25 #include <string> 26 #include <memory> 27 #include <unordered_map> 28 #include "utils/log_adapter.h" 29 #include "ps/core/communicator/http_message_handler.h" 30 #include "ps/core/communicator/ssl_http.h" 31 #include "include/backend/distributed/ps/constants.h" 32 #include "include/backend/distributed/ps/ps_context.h" 33 34 namespace mindspore { 35 namespace ps { 36 namespace core { 37 using OnRequestReceive = std::function<void(std::shared_ptr<HttpMessageHandler>)>; 38 39 /* Each thread corresponds to one HttpRequestHandler, which is used to create one eventbase. All eventbase are listened 40 * on the same fd. Every evhttp_request is executed in one thread. 41 */ 42 class HttpRequestHandler { 43 public: HttpRequestHandler()44 HttpRequestHandler() : evbase_(nullptr) {} 45 virtual ~HttpRequestHandler(); 46 47 bool Initialize(int fd, const std::unordered_map<std::string, OnRequestReceive *> &handlers); 48 void Run(); 49 bool Stop(); 50 static bufferevent *BuffereventCallback(event_base *base, void *arg); 51 52 private: 53 struct event_base *evbase_; 54 }; 55 } // namespace core 56 } // namespace ps 57 } // namespace mindspore 58 #endif // MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_REQUEST_HANDLER_H_ 59