1
2
3
4
5
6 #include <cmajor/build/ServerCommand.hpp>
7 #include <cmajor/build/ServerExecutionContext.hpp>
8 #include <cmajor/build/BuildOption.hpp>
9 #include <cmajor/build/ServerConfig.hpp>
10 #include <cmajor/build/BuildLangLexer.hpp>
11 #include <cmajor/build/BuildLangServerParser.hpp>
12 #include <soulng/util/Log.hpp>
13 #include <soulng/util/Unicode.hpp>
14 #include <iostream>
15
16 namespace cmajor { namespace build {
17
18 using namespace soulng::util;
19 using namespace soulng::unicode;
20
21 std::std::unique_ptr<ExecutionContext>CreateServerExecutionContext(conststd::string&serverName)
22 {
23 if (serverName == "local")
24 {
25 throw std::runtime_error("cannot run 'local' server in server execution context");
26 }
27 return std::unique_ptr<ExecutionContext>(new ServerExecutionContext(serverName));
28 }
29
30 ServerCommand::ServerCommand()
31 {
32 }
33
34 ServerCommand::~ServerCommand()
35 {
36 }
37
38 AddServerServerCommand::AddServerServerCommand(const std::string& serverName_, int port_) : serverName(serverName_), port(port_)
39 {
40 }
41
42 void AddServerServerCommand::Execute()
43 {
44 bool force = false;
45 if (GetBuildOption(BuildOptions::force))
46 {
47 force = true;
48 }
49 ServerConfig::Instance().Add(serverName, std::string(), port, std::string(), force, true, true);
50 LogMessage(-1, "server '" + serverName + "' added");
51 }
52
53 RemoveServerServerCommand::RemoveServerServerCommand(const std::string& serverName_) : serverName(serverName_)
54 {
55 }
56
57 void RemoveServerServerCommand::Execute()
58 {
59 ServerConfig::Instance().Remove(serverName);
60 LogMessage(-1, "server '" + serverName + "' removed");
61 }
62
63 RunServerServerCommand::RunServerServerCommand(const std::string& serverName_) : serverName(serverName_)
64 {
65 }
66
67 void RunServerServerCommand::Execute()
68 {
69 std::unique_ptr<ExecutionContext> context = CreateServerExecutionContext(serverName);
70 while (true)
71 {
72 std::string commandStr;
73 std::getline(std::cin, commandStr);
74 if (commandStr == "exit")
75 {
76 context->ExitServer();
77 std::cout << "exit." << std::endl;
78 return;
79 }
80 else
81 {
82 std::cerr << "unknown command '" << commandStr << "', type 'exit' <ENTER> to stop and exit server." << std::endl;
83 }
84 }
85 }
86
87 ShowConfigurationServerCommand::ShowConfigurationServerCommand()
88 {
89 }
90
91 void ShowConfigurationServerCommand::Execute()
92 {
93 ServerConfig::Instance().Show();
94 }
95
96 std::std::unique_ptr<ServerCommand>ParseServerCommand(conststd::string&command)
97 {
98 BuildLangLexer lexer(ToUtf32(command), "", 0);
99 BuildOptionSetter optionSetter;
100 return BuildLangServerParser::Parse(lexer, &optionSetter);
101 }
102
103 } }