1 | #include "StateMachineDimControl.h"
|
---|
2 | #include "RemoteControl.h"
|
---|
3 |
|
---|
4 | using namespace std;
|
---|
5 |
|
---|
6 | // ------------------------------------------------------------------------
|
---|
7 |
|
---|
8 | #include "Main.h"
|
---|
9 |
|
---|
10 | template<class T>
|
---|
11 | int RunShell(Configuration &conf)
|
---|
12 | {
|
---|
13 | const boost::filesystem::path path(conf.GetName());
|
---|
14 | const string fname = path.filename().string();
|
---|
15 |
|
---|
16 | StateMachineDimControl::fIsServer = fname=="dimserver";
|
---|
17 | return Main::execute<T, StateMachineDimControl>(conf);
|
---|
18 | }
|
---|
19 |
|
---|
20 | void SetupConfiguration(Configuration &conf)
|
---|
21 | {
|
---|
22 | const boost::filesystem::path path(conf.GetName());
|
---|
23 | const string fname = path.filename().string();
|
---|
24 |
|
---|
25 | po::options_description control("Options ("+fname+")");
|
---|
26 | control.add_options()
|
---|
27 | ("force-console", po_switch(), "Forces console mode in server-mode.")
|
---|
28 | ("debug", po_bool(false), "Print the labels for debugging purpose")
|
---|
29 | ("user,u", var<string>(""), "A user name - just for logging purposes (default is ${USER})")
|
---|
30 | ("JavaScript.*", var<string>(), "Additional arguments which are provided to JavaScripts started in a dimctrl server via the START command")
|
---|
31 | ;
|
---|
32 |
|
---|
33 | if (fname!="dimserver")
|
---|
34 | {
|
---|
35 | control.add_options()
|
---|
36 | ("start", var<string>(), "Start a java script with the given name on the dimctrl-server")
|
---|
37 | ("batch", var<string>(), "Start a batch script with the given name at the given label (script.dim[:N]) on the dimctrl-server")
|
---|
38 | ("stop", po_switch(), "Stop a currently running script on the dimctrl-server")
|
---|
39 | ("restart", var<string>(), "Send 'EXIT 126' to the given server")
|
---|
40 | ("msg", var<string>(), "Send a message to the chat server.")
|
---|
41 | ;
|
---|
42 | }
|
---|
43 |
|
---|
44 | conf.AddEnv("user", "USER");
|
---|
45 |
|
---|
46 | conf.AddOptions(control);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /*
|
---|
50 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
51 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
52 | are used to match the usage synopsis in program output. An example from cp
|
---|
53 | (GNU coreutils) which contains both strings:
|
---|
54 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
55 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
56 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
57 | */
|
---|
58 | void PrintUsage()
|
---|
59 | {
|
---|
60 | cout <<
|
---|
61 | "The dim control is a central master for the dim network.\n"
|
---|
62 | "\n"
|
---|
63 | "The program can be started as a dim server, so that it is visible "
|
---|
64 | "in the dim network to other clients. If started as a client (dimctrl), "
|
---|
65 | "it can only interact passively with the dim network. The usual case "
|
---|
66 | "should be to have one server running (dimserver) and control it from "
|
---|
67 | "a dimctrl started.\n"
|
---|
68 | "\n"
|
---|
69 | "Usage: dimctrl [-c type] [OPTIONS]\n"
|
---|
70 | " or: dimctrl [OPTIONS]\n"
|
---|
71 | " or: dimserver [OPTIONS]\n";
|
---|
72 | cout << endl;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void PrintHelp()
|
---|
76 | {
|
---|
77 | Main::PrintHelp<StateMachineDimControl>();
|
---|
78 |
|
---|
79 | /* Additional help text which is printed after the configuration
|
---|
80 | options goes here */
|
---|
81 |
|
---|
82 | /*
|
---|
83 | cout << "bla bla bla" << endl << endl;
|
---|
84 | cout << endl;
|
---|
85 | cout << "Environment:" << endl;
|
---|
86 | cout << "environment" << endl;
|
---|
87 | cout << endl;
|
---|
88 | cout << "Examples:" << endl;
|
---|
89 | cout << "test exam" << endl;
|
---|
90 | cout << endl;
|
---|
91 | cout << "Files:" << endl;
|
---|
92 | cout << "files" << endl;
|
---|
93 | cout << endl;
|
---|
94 | */
|
---|
95 | }
|
---|
96 |
|
---|
97 | int main(int argc, const char* argv[])
|
---|
98 | {
|
---|
99 | Configuration conf(argv[0]);
|
---|
100 | conf.SetPrintUsage(PrintUsage);
|
---|
101 | Main::SetupConfiguration(conf);
|
---|
102 | SetupConfiguration(conf);
|
---|
103 |
|
---|
104 | if (!conf.DoParse(argc, argv, PrintHelp))
|
---|
105 | return 127;
|
---|
106 |
|
---|
107 | if (conf.Get<bool>("force-console") && !conf.Has("console"))
|
---|
108 | throw runtime_error("--force-console must be used with --console/-c");
|
---|
109 |
|
---|
110 | const boost::filesystem::path path(conf.GetName());
|
---|
111 | const string fname = path.filename().string();
|
---|
112 |
|
---|
113 | if (fname=="dimserver" && !conf.Get<bool>("force-console"))
|
---|
114 | conf.Remove("console");
|
---|
115 |
|
---|
116 | if (!conf.Has("console"))
|
---|
117 | return RunShell<RemoteStream>(conf);
|
---|
118 |
|
---|
119 | if (conf.Get<int>("console")==0)
|
---|
120 | return RunShell<RemoteShell>(conf);
|
---|
121 | else
|
---|
122 | return RunShell<RemoteConsole>(conf);
|
---|
123 | }
|
---|