• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "host/commands/cvd/server_command/cmd_list.h"
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include <android-base/strings.h>
23 
24 #include "common/libs/fs/shared_buf.h"
25 #include "common/libs/utils/json.h"
26 #include "host/commands/cvd/server_command/server_handler.h"
27 #include "host/commands/cvd/server_command/utils.h"
28 #include "host/commands/cvd/types.h"
29 
30 namespace cuttlefish {
31 
32 class CvdCmdlistHandler : public CvdServerHandler {
33  public:
INJECT(CvdCmdlistHandler (CommandSequenceExecutor & executor))34   INJECT(CvdCmdlistHandler(CommandSequenceExecutor& executor))
35       : executor_(executor) {}
36 
CanHandle(const RequestWithStdio & request) const37   Result<bool> CanHandle(const RequestWithStdio& request) const override {
38     auto invocation = ParseInvocation(request.Message());
39     return (invocation.command == "cmd-list");
40   }
41 
Handle(const RequestWithStdio & request)42   Result<cvd::Response> Handle(const RequestWithStdio& request) override {
43     std::lock_guard interrupt_lock(interruptible_);
44     CF_EXPECT(!interrupted_, "Interrupted");
45 
46     cvd::Response response;
47     response.mutable_command_response();  // Sets oneof member
48     response.mutable_status()->set_code(cvd::Status::OK);
49 
50     CF_EXPECT(CanHandle(request));
51 
52     auto [subcmd, subcmd_args] = ParseInvocation(request.Message());
53     const auto subcmds = executor_.CmdList();
54 
55     std::vector<std::string> subcmds_vec{subcmds.begin(), subcmds.end()};
56     const auto subcmds_str = android::base::Join(subcmds_vec, ",");
57     Json::Value subcmd_info;
58     subcmd_info["subcmd"] = subcmds_str;
59     WriteAll(request.Out(), subcmd_info.toStyledString());
60     return response;
61   }
62 
Interrupt()63   Result<void> Interrupt() override {
64     std::scoped_lock interrupt_lock(interruptible_);
65     interrupted_ = true;
66     CF_EXPECT(executor_.Interrupt());
67     return {};
68   }
69 
70   // not intended to be used by the user
CmdList() const71   cvd_common::Args CmdList() const override { return {}; }
72 
73  private:
74   std::mutex interruptible_;
75   bool interrupted_ = false;
76   CommandSequenceExecutor& executor_;
77 };
78 
79 fruit::Component<fruit::Required<CommandSequenceExecutor>>
CvdCmdlistComponent()80 CvdCmdlistComponent() {
81   return fruit::createComponent()
82       .addMultibinding<CvdServerHandler, CvdCmdlistHandler>();
83 }
84 
85 }  // namespace cuttlefish
86