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