source: trunk/FACT++/src/dimctrl.cc@ 19685

Last change on this file since 19685 was 19419, checked in by tbretz, 6 years ago
Added an option to send the global shutdown (DIS_DNS/KILL_SERVERS)
File size: 4.5 KB
Line 
1#include "StateMachineDimControl.h"
2
3//#include <sys/stat.h>
4
5#include "RemoteControl.h"
6
7using namespace std;
8
9// ------------------------------------------------------------------------
10
11#include "Main.h"
12
13namespace fs = boost::filesystem;
14
15template<class T>
16int 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
28void 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 ("shutdown", po_switch(), "Shutdown the whole network 'DIS_DNS/KILL_SERVERS'")
53 ("msg", var<string>(), "Send a message to the chat server.")
54 ;
55 }
56
57 conf.AddEnv("user", "USER");
58
59 conf.AddOptions(control);
60}
61
62/*
63 Extract usage clause(s) [if any] for SYNOPSIS.
64 Translators: "Usage" and "or" here are patterns (regular expressions) which
65 are used to match the usage synopsis in program output. An example from cp
66 (GNU coreutils) which contains both strings:
67 Usage: cp [OPTION]... [-T] SOURCE DEST
68 or: cp [OPTION]... SOURCE... DIRECTORY
69 or: cp [OPTION]... -t DIRECTORY SOURCE...
70 */
71void PrintUsage()
72{
73 cout <<
74 "The dim control is a central master for the dim network.\n"
75 "\n"
76 "The program can be started as a dim server, so that it is visible "
77 "in the dim network to other clients. If started as a client (dimctrl), "
78 "it can only interact passively with the dim network. The usual case "
79 "should be to have one server running (dimserver) and control it from "
80 "a dimctrl started.\n"
81 "\n"
82 "Usage: dimctrl [-c type] [OPTIONS]\n"
83 " or: dimctrl [OPTIONS]\n"
84 " or: dimserver [OPTIONS]\n";
85 cout << endl;
86}
87
88void PrintHelp()
89{
90 Main::PrintHelp<StateMachineDimControl>();
91
92 /* Additional help text which is printed after the configuration
93 options goes here */
94
95 /*
96 cout << "bla bla bla" << endl << endl;
97 cout << endl;
98 cout << "Environment:" << endl;
99 cout << "environment" << endl;
100 cout << endl;
101 cout << "Examples:" << endl;
102 cout << "test exam" << endl;
103 cout << endl;
104 cout << "Files:" << endl;
105 cout << "files" << endl;
106 cout << endl;
107 */
108}
109
110int main(int argc, const char* argv[])
111{
112 //chmod(argv[0], 04775);
113
114 Configuration conf(argv[0]);
115 conf.SetPrintUsage(PrintUsage);
116 Main::SetupConfiguration(conf);
117 SetupConfiguration(conf);
118
119 if (!conf.DoParse(argc, argv, PrintHelp))
120 return 127;
121
122 if (conf.Get<bool>("force-console") && !conf.Has("console"))
123 throw runtime_error("--force-console must be used with --console/-c");
124
125#if BOOST_VERSION < 104600
126 const string fname = fs::path(conf.GetName()).filename();
127#else
128 const string fname = fs::path(conf.GetName()).filename().string();
129#endif
130
131 if (fname=="dimserver" && !conf.Get<bool>("force-console"))
132 conf.Remove("console");
133
134 if (!conf.Has("console"))
135 return RunShell<RemoteStream>(conf);
136
137 if (conf.Get<int>("console")==0)
138 return RunShell<RemoteShell>(conf);
139 else
140 return RunShell<RemoteConsole>(conf);
141}
Note: See TracBrowser for help on using the repository browser.