Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : #include "console/AsyncConsole.h"
4 :
5 : #include "console/SyncCommandWrapper.h"
6 : #include "logger/ConsoleLogger.h"
7 :
8 : #include <etl/singleton_base.h>
9 : #include <etl/string.h>
10 : #include <util/command/HelpCommand.h>
11 : #include <util/command/ParentCommand.h>
12 : #include <util/format/SharedStringWriter.h>
13 :
14 : namespace
15 : {
16 1 : ::util::command::ParentCommand& getParentCommand()
17 : {
18 1 : static ::util::command::ParentCommand parentCommand("root", "root");
19 1 : return parentCommand;
20 : }
21 : } // namespace
22 :
23 : namespace console
24 : {
25 : using ::util::logger::CONSOLE;
26 : using ::util::logger::Logger;
27 :
28 0 : AsyncConsole::AsyncConsole()
29 0 : : ::etl::singleton_base<AsyncConsole>(*this), fpOutputStream(nullptr), fOnLineProcessed()
30 : {
31 0 : static ::util::command::HelpCommand helpCommand(getParentCommand());
32 0 : static ::console::SyncCommandWrapper syncCommandWrapper(helpCommand);
33 0 : }
34 :
35 1 : void AsyncConsole::addCommand(::util::command::ICommand& command)
36 : {
37 1 : getParentCommand().addCommand(command);
38 1 : }
39 :
40 0 : void AsyncConsole::commandExecuted(::util::command::ICommand::ExecuteResult result)
41 : {
42 0 : AsyncConsole::instance().terminate(result);
43 0 : }
44 :
45 0 : void AsyncConsole::onLineReceived(
46 : ::util::stream::ISharedOutputStream& outputStream,
47 : ::etl::istring const& line,
48 : OnLineProcessed const& onLineProcessed)
49 : {
50 0 : auto const cmd = ::util::string::ConstString(line.c_str(), line.length());
51 0 : Logger::info(CONSOLE, "Received console command \"%.*s\"", cmd.length(), cmd.data());
52 :
53 0 : fOnLineProcessed = onLineProcessed;
54 0 : fpOutputStream = &outputStream;
55 :
56 : ::util::command::ICommand::ExecuteResult const result
57 0 : = getParentCommand().execute(cmd, &outputStream);
58 :
59 0 : if (result.getResult() == ::util::command::ICommand::Result::OK)
60 : { // in this case, we terminate since commandExecuted is called
61 0 : return;
62 : }
63 :
64 0 : Logger::info(CONSOLE, "Console command failed");
65 :
66 0 : ::util::format::SharedStringWriter sharedStringWriter(outputStream);
67 0 : sharedStringWriter.printf("error\n");
68 :
69 0 : onLineProcessed();
70 0 : }
71 :
72 0 : void AsyncConsole::terminate(::util::command::ICommand::ExecuteResult result)
73 : {
74 0 : if (fpOutputStream != nullptr)
75 : {
76 0 : ::util::format::StringWriter stringWriter(fpOutputStream->startOutput(nullptr));
77 :
78 0 : switch (result.getResult())
79 : {
80 0 : case ::util::command::ICommand::Result::OK:
81 : {
82 0 : Logger::info(CONSOLE, "Console command succeeded");
83 0 : stringWriter.printf("ok\n");
84 0 : break;
85 : }
86 0 : default:
87 : {
88 0 : Logger::info(CONSOLE, "Console command failed");
89 0 : stringWriter.printf("error\n");
90 0 : break;
91 : }
92 : }
93 0 : fpOutputStream = nullptr;
94 : }
95 :
96 0 : OnLineProcessed onLineProcessed = fOnLineProcessed;
97 0 : fOnLineProcessed = OnLineProcessed();
98 0 : onLineProcessed();
99 0 : }
100 :
101 : } /* namespace console */
|