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_command_builder.h" 17#include "common_types.pb.h" 18#include "logging.h" 19#include "plugin_service_types.pb.h" 20 21PluginCommandBuilder::PluginCommandBuilder() 22{ 23 cmdIdAutoIncrease_ = 1; 24} 25 26PluginCommandBuilder::~PluginCommandBuilder() {} 27 28GetCommandResponsePtr PluginCommandBuilder::BuildCreateSessionCmd(const ProfilerPluginConfig& config, 29 uint32_t bufferSize) 30{ 31 auto cmd = std::make_shared<GetCommandResponse>(); 32 cmd->set_status(ResponseStatus::OK); 33 cmd->set_has_more(false); 34 cmd->set_command_id(cmdIdAutoIncrease_); 35 CreateSessionCmd* csc = cmd->mutable_create_session_cmd(); 36 csc->add_buffer_sizes(bufferSize); 37 ProfilerPluginConfig* ppc = csc->add_plugin_configs(); 38 *ppc = config; 39 commandHistory_[cmdIdAutoIncrease_] = cmd; 40 cmdIdAutoIncrease_++; 41 return cmd; 42} 43 44GetCommandResponsePtr PluginCommandBuilder::BuildDestroySessionCmd(uint32_t pluginId) 45{ 46 auto cmd = std::make_shared<GetCommandResponse>(); 47 cmd->set_status(ResponseStatus::OK); 48 cmd->set_has_more(false); 49 cmd->set_command_id(cmdIdAutoIncrease_); 50 51 DestroySessionCmd* dsc = cmd->mutable_destroy_session_cmd(); 52 dsc->add_plugin_ids(pluginId); 53 54 commandHistory_[cmdIdAutoIncrease_] = cmd; 55 cmdIdAutoIncrease_++; 56 return cmd; 57} 58 59GetCommandResponsePtr PluginCommandBuilder::BuildStartSessionCmd(const ProfilerPluginConfig& config, uint32_t pluginId) 60{ 61 auto cmd = std::make_shared<GetCommandResponse>(); 62 cmd->set_status(ResponseStatus::OK); 63 cmd->set_has_more(false); 64 cmd->set_command_id(cmdIdAutoIncrease_); 65 66 StartSessionCmd* ssc = cmd->mutable_start_session_cmd(); 67 ssc->add_plugin_ids(pluginId); 68 ProfilerPluginConfig* ppc = ssc->add_plugin_configs(); 69 *ppc = config; 70 71 commandHistory_[cmdIdAutoIncrease_] = cmd; 72 cmdIdAutoIncrease_++; 73 return cmd; 74} 75 76GetCommandResponsePtr PluginCommandBuilder::BuildStopSessionCmd(uint32_t pluginId) 77{ 78 auto cmd = std::make_shared<GetCommandResponse>(); 79 cmd->set_status(ResponseStatus::OK); 80 cmd->set_has_more(false); 81 cmd->set_command_id(cmdIdAutoIncrease_); 82 83 StopSessionCmd* ssc = cmd->mutable_stop_session_cmd(); 84 ssc->add_plugin_ids(pluginId); 85 86 commandHistory_[cmdIdAutoIncrease_] = cmd; 87 cmdIdAutoIncrease_++; 88 return cmd; 89} 90 91GetCommandResponsePtr PluginCommandBuilder::BuildRefreshSessionCmd(uint32_t pluginId) 92{ 93 auto cmd = std::make_shared<GetCommandResponse>(); 94 cmd->set_status(ResponseStatus::OK); 95 cmd->set_has_more(false); 96 cmd->set_command_id(cmdIdAutoIncrease_); 97 98 RefreshSessionCmd* rsc = cmd->mutable_refresh_session_cmd(); 99 rsc->add_plugin_ids(pluginId); 100 101 commandHistory_[cmdIdAutoIncrease_] = cmd; 102 cmdIdAutoIncrease_++; 103 return cmd; 104} 105 106bool PluginCommandBuilder::GetedCommandResponse(uint32_t cmdId) 107{ 108 if (commandHistory_.find(cmdId) == commandHistory_.end()) { 109 return false; 110 } 111 commandHistory_.erase(cmdId); 112 return true; 113}