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#include "plugin_service_impl.h"
17#include "plugin_service.h"
18
19PluginServiceImpl::PluginServiceImpl(PluginService& p)
20{
21    pluginService = &p;
22}
23
24PluginServiceImpl::~PluginServiceImpl() {}
25
26bool PluginServiceImpl::RegisterPlugin(SocketContext& context,
27                                       ::RegisterPluginRequest& request,
28                                       ::RegisterPluginResponse& response)
29{
30    PluginInfo pluginInfo;
31
32    pluginInfo.name = request.name();
33    pluginInfo.path = request.path();
34    pluginInfo.sha256 = request.sha256();
35    pluginInfo.bufferSizeHint = request.buffer_size_hint();
36    pluginInfo.isStandaloneFileData = request.is_standalone_data();
37    pluginInfo.outFileName = request.out_file_name();
38    pluginInfo.pluginVersion = request.plugin_version();
39    pluginInfo.context = &context;
40
41    int pluginId = pluginService->GetPluginIdByName(pluginInfo.name);
42    if (pluginService->AddPluginInfo(pluginInfo)) {
43        if (pluginId != 0) { // update plugin not need reply response
44            PROFILER_LOG_DEBUG(LOG_CORE, "UpdatePlugin OK");
45            return false;
46        }
47        response.set_status(ResponseStatus::OK);
48        response.set_plugin_id(pluginService->GetPluginIdByName(pluginInfo.name));
49        PROFILER_LOG_DEBUG(LOG_CORE, "RegisterPlugin OK");
50        return true;
51    }
52    response.set_status(ResponseStatus::ERR);
53    PROFILER_LOG_DEBUG(LOG_CORE, "RegisterPlugin FAIL");
54    return false;
55}
56bool PluginServiceImpl::UnregisterPlugin(SocketContext& context,
57                                         ::UnregisterPluginRequest& request,
58                                         ::UnregisterPluginResponse& response)
59{
60    PluginInfo pluginInfo;
61    pluginInfo.id = request.plugin_id();
62
63    if (pluginService->RemovePluginInfo(pluginInfo)) {
64        response.set_status(ResponseStatus::OK);
65        PROFILER_LOG_DEBUG(LOG_CORE, "UnregisterPlugin OK");
66        return true;
67    }
68    response.set_status(ResponseStatus::ERR);
69    PROFILER_LOG_DEBUG(LOG_CORE, "UnregisterPlugin FAIL");
70    return false;
71}
72
73bool PluginServiceImpl::GetCommand(SocketContext& context, ::GetCommandRequest& request, ::GetCommandResponse& response)
74{
75    return false;
76}
77
78bool PluginServiceImpl::NotifyResult(SocketContext& context,
79                                     ::NotifyResultRequest& request,
80                                     ::NotifyResultResponse& response)
81{
82    PROFILER_LOG_DEBUG(LOG_CORE, "NotifyResult");
83    if (pluginService->AppendResult(request)) {
84        response.set_status(ResponseStatus::OK);
85        return true;
86    }
87    response.set_status(ResponseStatus::ERR);
88    return true;
89}
90
91bool PluginServiceImpl::PushCommand(SocketContext& context, GetCommandResponsePtr command)
92{
93    SendResponseGetCommandResponse(context, *command.get());
94    return true;
95}
96
97bool PluginServiceImpl::SendHeartBeat(SocketContext& context)
98{
99    return context.SendHeartBeat();
100}
101