1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef TLS_MONITOR_SERVER_H
17 #define TLS_MONITOR_SERVER_H
18 
19 #include <cstdint>
20 #include <napi/native_api.h>
21 #include <queue>
22 #include <set>
23 #include <string>
24 #include <string_view>
25 
26 #include "event_manager.h"
27 #include "singleton.h"
28 #include "socket_remote_info.h"
29 #include "tls.h"
30 #include "tls_socket_server.h"
31 namespace OHOS {
32 namespace NetStack {
33 namespace TlsSocketServer {
34 struct MessagerServerQueue {
35     std::queue<int> clientFdQueue;
36     std::queue<std::string> dataQueue;
37     std::queue<Socket::SocketRemoteInfo> remoteInfoQueue;
38 };
39 
40 class MonitorServer final {
41     DECLARE_DELAYED_SINGLETON(MonitorServer);
42 
43 public:
44     napi_value On(napi_env env, napi_callback_info info);
45     napi_value Off(napi_env env, napi_callback_info info);
46 
47     napi_value ConnectionOn(napi_env env, napi_callback_info info);
48     napi_value ConnectionOff(napi_env env, napi_callback_info info);
49     void TLSServerRegEvent(std::string event, TLSSocketServer *tlsSocketServer, EventManager *eventManager);
50     void TLSConnectionRegEvent(std::string event, TLSSocketServer *tlsSocketServer, int clientId,
51                                EventManager *eventManager);
52     void TLSConnectionUnRegEvent(std::string event, TLSSocketServer *tlsSocketServer, int clientId);
53     class MessageParma {
54     public:
55         int clientID;
56         std::shared_ptr<EventManager> eventManager;
57     };
58 
59     class MessageRecvParma {
60     public:
61         int clientID;
62         std::string data;
63         Socket::SocketRemoteInfo remoteInfo_;
64     };
65 
66 public:
67     int clientFd_ = -1;
68     std::string data_;
69     Socket::SocketRemoteInfo remoteInfo_;
70     MessagerServerQueue messagerServerQueue_;
71     int32_t errorNumber_ = 0;
72     std::string errorString_;
73 
74 private:
75     std::set<std::string_view> monitors_;
76 };
77 } // namespace TlsSocketServer
78 } // namespace NetStack
79 } // namespace OHOS
80 #endif // TLS_MONITOR_SERVER_H
81