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 ECMASCRIPT_TOOLING_CLIENT_SESSION_H
17#define ECMASCRIPT_TOOLING_CLIENT_SESSION_H
18
19#include <atomic>
20#include <iostream>
21#include <memory>
22#include <map>
23#include <pthread.h>
24#include <thread>
25#include <uv.h>
26
27#include "tooling/client/manager/domain_manager.h"
28#include "tooling/client/manager/breakpoint_manager.h"
29#include "tooling/client/manager/source_manager.h"
30#include "tooling/client/manager/stack_manager.h"
31#include "tooling/client/manager/variable_manager.h"
32#include "tooling/client/manager/watch_manager.h"
33#include "websocket/client/websocket_client.h"
34
35namespace OHOS::ArkCompiler::Toolchain {
36using CmdForAllCB = std::function<void(uint32_t sessionId)>;
37extern uv_async_t* g_socketSignal;
38class Session {
39public:
40    explicit Session(uint32_t sessionId, std::string& sockInfo);
41    ~Session()
42    {
43        Stop();
44    }
45
46    void SocketMessageLoop();
47    int CreateSocket();
48    int Start();
49    int Stop();
50    void CmdForAllSession(CmdForAllCB callback);
51
52    uint32_t GetMessageId()
53    {
54        return messageId_.fetch_add(1);
55    }
56
57    std::string& GetSockInfo()
58    {
59        return sockInfo_;
60    }
61
62    bool ClientSendReq(const std::string &message)
63    {
64        return cliSocket_.SendReply(message);
65    }
66
67    DomainManager& GetDomainManager()
68    {
69        return domainManager_;
70    }
71
72    BreakPointManager& GetBreakPointManager()
73    {
74        return breakpoint_;
75    }
76
77    StackManager& GetStackManager()
78    {
79        return stackManager_;
80    }
81
82    VariableManager& GetVariableManager()
83    {
84        return variableManager_;
85    }
86
87    WebSocketClient& GetWebSocketClient()
88    {
89        return cliSocket_;
90    }
91
92    ProfilerSingleton& GetProfilerSingleton()
93    {
94        return profiler_;
95    }
96
97    std::string GetSocketStateString()
98    {
99        return cliSocket_.GetSocketStateString();
100    }
101
102    void ProcSocketMsg(char* msg)
103    {
104        domainManager_.DispatcherReply(msg);
105    }
106
107    SourceManager& GetSourceManager()
108    {
109        return sourceManager_;
110    }
111
112    WatchManager& GetWatchManager()
113    {
114        return watchManager_;
115    }
116
117private:
118    uint32_t sessionId_;
119    std::string sockInfo_;
120    DomainManager domainManager_;
121    WebSocketClient cliSocket_;
122    uv_thread_t socketTid_ = 0;
123    std::atomic<uint32_t> messageId_ {1};
124    BreakPointManager breakpoint_;
125    StackManager stackManager_;
126    VariableManager variableManager_;
127    ProfilerSingleton profiler_;
128    SourceManager sourceManager_;
129    WatchManager watchManager_;
130};
131
132constexpr uint32_t MAX_SESSION_NUM = 8;
133class SessionManager {
134public:
135    static SessionManager& getInstance()
136    {
137        static SessionManager instance;
138        return instance;
139    }
140
141    uint32_t GetCurrentSessionId()
142    {
143        return currentSessionId_;
144    }
145
146    Session *GetCurrentSession()
147    {
148        if (currentSessionId_ >= MAX_SESSION_NUM || sessions_[currentSessionId_] == nullptr) {
149            return nullptr;
150        }
151        return sessions_[currentSessionId_].get();
152    }
153
154    Session *GetSessionById(uint32_t sessionId)
155    {
156        if (sessionId >= MAX_SESSION_NUM || sessions_[sessionId] == nullptr) {
157            return nullptr;
158        }
159        return sessions_[sessionId].get();
160    }
161
162    int CreateSessionById(uint32_t sessionId, std::string& sockInfo);
163    int CreateNewSession(std::string& sockInfo);
164    int CreateDefaultSession(std::string& sockInfo);
165    int DelSessionById(uint32_t sessionId);
166    int SessionList();
167    int SessionSwitch(uint32_t sessionId);
168    void CmdForAllSessions(CmdForAllCB callback);
169    int CreateTestSession(std::string& sockInfo);
170
171private:
172    SessionManager() = default;
173    SessionManager(const SessionManager&) = delete;
174    SessionManager& operator=(const SessionManager&) = delete;
175
176    std::unique_ptr<Session> sessions_[MAX_SESSION_NUM];
177    uint32_t currentSessionId_ = 0;
178};
179} // namespace OHOS::ArkCompiler::Toolchain
180#endif