1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 PLUGIN_SESSION_H
17 #define PLUGIN_SESSION_H
18 
19 #include <mutex>
20 #include "logging.h"
21 #include "nocopyable.h"
22 #include "plugin_service_types.pb.h"
23 #include "profiler_service_types.pb.h"
24 
25 class PluginService;
26 class ProfilerDataRepeater;
27 
28 using PluginServiceWeakPtr = STD_PTR(weak, PluginService);
29 using ProfilerDataRepeaterPtr = STD_PTR(shared, ProfilerDataRepeater);
30 
31 class PluginSession {
32 public:
33     PluginSession(const ProfilerPluginConfig& pluginConfig,
34                   const PluginServiceWeakPtr& pluginService,
35                   const ProfilerDataRepeaterPtr& dataRepeater);
36     PluginSession(const ProfilerPluginConfig& pluginConfig,
37                   const ProfilerSessionConfig::BufferConfig& bufferConfig,
38                   const PluginServiceWeakPtr& pluginService,
39                   const ProfilerDataRepeaterPtr& dataRepeater);
40     ~PluginSession();
41 
42     bool Start();
43     bool Stop();
44     bool Refresh();
45 
46     bool IsAvailable() const;
47 
48     enum State {
49         INVALID = -1,
50         INITIAL = 0,
51         CREATED = 1,
52         STARTED = 2,
53     };
54 
55     void Invalidate();
56 
57     State GetState() const;
58 
59 private:
60     bool Create();
61     bool StopLocked();
62     bool Destroy();
63 
64 private:
65     mutable std::mutex mutex_;
66     State state_;
67     bool withBufferConfig_;
68     ProfilerPluginConfig pluginConfig_;
69     ProfilerSessionConfig::BufferConfig bufferConfig_;
70     PluginServiceWeakPtr pluginService_;
71     ProfilerDataRepeaterPtr dataRepeater_;
72 
73     DISALLOW_COPY_AND_MOVE(PluginSession);
74 };
75 
76 #endif // !PLUGIN_SESSION_H
77