1 | #ifndef FACT_Main
|
---|
2 | #define FACT_Main
|
---|
3 |
|
---|
4 | #include <map>
|
---|
5 | #include <thread>
|
---|
6 | #include <functional>
|
---|
7 |
|
---|
8 | #include <boost/filesystem.hpp>
|
---|
9 |
|
---|
10 | #include "dim.h"
|
---|
11 |
|
---|
12 | #include "Dim.h"
|
---|
13 | #include "Time.h"
|
---|
14 | #include "MainImp.h"
|
---|
15 | #include "Readline.h"
|
---|
16 | #include "WindowLog.h"
|
---|
17 | #include "MessageImp.h"
|
---|
18 | #include "Configuration.h"
|
---|
19 |
|
---|
20 | namespace Main
|
---|
21 | {
|
---|
22 | using namespace std;
|
---|
23 | namespace fs = boost::filesystem;
|
---|
24 |
|
---|
25 | void SetupConfiguration(Configuration &conf)
|
---|
26 | {
|
---|
27 | const string n = conf.GetName()+".log";
|
---|
28 |
|
---|
29 | po::options_description config("Program options");
|
---|
30 | config.add_options()
|
---|
31 | ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
|
---|
32 | ("port", var<uint16_t>(DNS_PORT), "Port to connect to dim nameserver.")
|
---|
33 | ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
|
---|
34 | ("log,l", var<string>(n), "Name of local log-file")
|
---|
35 | ("logpath", var<string>(), "Absolute path to log-files (default: excutable's directory)")
|
---|
36 | ("no-log", po_switch(), "Supress log-file")
|
---|
37 | ("append-log", po_bool(), "Append log information to local log-file")
|
---|
38 | ("null", po_switch(), "Suppresses almost all console output - including errors (only available without --console option)")
|
---|
39 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
40 | ("cmd", vars<string>(), "Execute one or more commands at startup")
|
---|
41 | ("exec,e", vars<string>(), "Execute one or more scrips at startup ('file:N' - start at label N)")
|
---|
42 | ("arg:*", var<string>(), "Arguments for script execution with --exc, e.g. --arg:ra='12.5436'")
|
---|
43 | ("home", var<string>(), "Path to home directory (used as default for logpath if standard log files not writable)")
|
---|
44 | ("quit", po_switch(), "Quit after startup");
|
---|
45 | ;
|
---|
46 |
|
---|
47 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
48 | conf.AddEnv("port", "DIM_DNS_PORT");
|
---|
49 | conf.AddEnv("host", "DIM_HOST_NODE");
|
---|
50 | conf.AddEnv("home", "HOME");
|
---|
51 |
|
---|
52 | conf.AddOptions(config);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void PrintUsage()
|
---|
56 | {
|
---|
57 | cout <<
|
---|
58 | "Files:\n"
|
---|
59 | "The following files are written by each program by default\n"
|
---|
60 | " program.evt: A log of all executed of skipped events\n"
|
---|
61 | " program.his: The history accessible by Pg-up/dn\n"
|
---|
62 | " program.log: All output piped to the log-stream\n"
|
---|
63 | << endl;
|
---|
64 | }
|
---|
65 |
|
---|
66 | template<class T>
|
---|
67 | void PrintHelp()
|
---|
68 | {
|
---|
69 | Dim::Setup();
|
---|
70 |
|
---|
71 | ofstream fout("/dev/null");
|
---|
72 |
|
---|
73 | T io_service(fout);
|
---|
74 |
|
---|
75 | io_service.PrintListOfStates(cout);
|
---|
76 | cout << "\nList of available commands:\n";
|
---|
77 | io_service.PrintListOfEvents(cout);
|
---|
78 | cout << "\n";
|
---|
79 | }
|
---|
80 |
|
---|
81 | void Thread(MainImp *io_service, bool dummy, int &rc)
|
---|
82 | {
|
---|
83 | // This is necessary so that the StateMachien Thread can signal the
|
---|
84 | // Readline to exit
|
---|
85 | rc = io_service->Run(dummy);
|
---|
86 | Readline::Stop();
|
---|
87 | }
|
---|
88 |
|
---|
89 | template<class T, class S>
|
---|
90 | int execute(Configuration &conf, bool dummy=false)
|
---|
91 | {
|
---|
92 | Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"), conf.Get<uint16_t>("port"));
|
---|
93 |
|
---|
94 | // -----------------------------------------------------------------
|
---|
95 | const fs::path program(conf.GetName());
|
---|
96 |
|
---|
97 | // Split path to program into path and filename
|
---|
98 | const string prgpath = program.parent_path().string();
|
---|
99 |
|
---|
100 | #if BOOST_VERSION < 104600
|
---|
101 | const string prgname = program.filename();
|
---|
102 | #else
|
---|
103 | const string prgname = program.filename().string();
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | fs::path path = conf.Has("logpath") ? conf.Get<string>("logpath") : "";
|
---|
107 |
|
---|
108 | // No explicit path given
|
---|
109 | if (path.empty())
|
---|
110 | {
|
---|
111 | path = prgpath.empty() ? "." : prgpath;
|
---|
112 |
|
---|
113 | // default path not accessible
|
---|
114 | if (access(prgpath.c_str(), W_OK))
|
---|
115 | {
|
---|
116 | path = ".";
|
---|
117 |
|
---|
118 | if (conf.Has("home"))
|
---|
119 | {
|
---|
120 | path = conf.Get<string>("home");
|
---|
121 | path /= ".fact++";
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | // Create directories if necessary
|
---|
127 | fs::create_directories(path);
|
---|
128 |
|
---|
129 | // -----------------------------------------------------------------
|
---|
130 |
|
---|
131 | static T shell((path/prgname).string().c_str(),
|
---|
132 | conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
|
---|
133 |
|
---|
134 | WindowLog &win = shell.GetStreamIn();
|
---|
135 | WindowLog &wout = shell.GetStreamOut();
|
---|
136 |
|
---|
137 | // Switching off buffering is not strictly necessary, since
|
---|
138 | // the destructor of shell should flush everything still buffered,
|
---|
139 | // nevertheless it helps to debug problems in the initialization
|
---|
140 | // sequence.
|
---|
141 | const bool backlog = wout.GetBacklog();
|
---|
142 | const bool null = wout.GetNullOutput();
|
---|
143 | if (conf.Has("console") || !conf.Get<bool>("null"))
|
---|
144 | {
|
---|
145 | wout.SetBacklog(false);
|
---|
146 | wout.SetNullOutput(false);
|
---|
147 | wout.Display(true);
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (conf.Has("log") && !conf.Get<bool>("no-log"))
|
---|
151 | {
|
---|
152 | #if BOOST_VERSION < 104600
|
---|
153 | const fs::path file = fs::path(conf.Get<string>("log")).filename();
|
---|
154 | #else
|
---|
155 | const fs::path file = fs::path(conf.Get<string>("log")).filename();
|
---|
156 | #endif
|
---|
157 | if (!wout.OpenLogFile((path/file).string(), conf.Get<bool>("append-log")))
|
---|
158 | win << kYellow << "WARNING - Couldn't open log-file " << (path/file).string() << ": " << strerror(errno) << endl;
|
---|
159 | }
|
---|
160 |
|
---|
161 | S io_service(wout);
|
---|
162 |
|
---|
163 | const Time now;
|
---|
164 | io_service.Write(now, "/----------------------- Program ------------------------");
|
---|
165 | io_service.Write(now, "| Program: " PACKAGE_STRING " ("+prgname+":"+to_string(getpid())+")");
|
---|
166 | io_service.Write(now, "| CallPath: "+prgpath);
|
---|
167 | io_service.Write(now, "| Compiled: " __DATE__ " " __TIME__ );
|
---|
168 | io_service.Write(now, "| Revision: " REVISION);
|
---|
169 | io_service.Write(now, "| DIM: v"+to_string(DIM_VERSION_NUMBER/100)+"r"+to_string(DIM_VERSION_NUMBER%100)+" ("+io_service.GetName()+")");
|
---|
170 | io_service.Write(now, "| Contact: " PACKAGE_BUGREPORT);
|
---|
171 | io_service.Write(now, "| URL: " PACKAGE_URL);
|
---|
172 | io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
|
---|
173 | io_service.Write(now, "\\----------------------- Options ------------------------");
|
---|
174 | const multimap<string,string> mmap = conf.GetOptions();
|
---|
175 | for (auto it=mmap.begin(); it!=mmap.end(); it++)
|
---|
176 | io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
|
---|
177 |
|
---|
178 | const map<string,string> &args = conf.GetOptions<string>("arg:");
|
---|
179 | if (!args.empty())
|
---|
180 | {
|
---|
181 | io_service.Write(now, "------------------------ Arguments ----------------------", MessageImp::kMessage);
|
---|
182 |
|
---|
183 | for (auto it=args.begin(); it!=args.end(); it++)
|
---|
184 | {
|
---|
185 | ostringstream str;
|
---|
186 | str.setf(ios_base::left);
|
---|
187 | str << ": " << it->first << " = " << it->second;
|
---|
188 | io_service.Write(now, str.str(), MessageImp::kMessage);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | io_service.Write(now, "\\------------------- Evaluating options -----------------");
|
---|
193 | const int rc = io_service.EvalOptions(conf);
|
---|
194 | if (rc>=0)
|
---|
195 | {
|
---|
196 | ostringstream str;
|
---|
197 | str << "Exit triggered by EvalOptions with rc=" << rc;
|
---|
198 | io_service.Write(now, str.str(), rc==0?MessageImp::kInfo:MessageImp::kError);
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|
202 | const map<string,string> &wco = conf.GetWildcardOptions();
|
---|
203 | if (!wco.empty())
|
---|
204 | {
|
---|
205 | io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
|
---|
206 |
|
---|
207 | size_t max = 0;
|
---|
208 | for (auto it=wco.begin(); it!=wco.end(); it++)
|
---|
209 | if (it->second.length()>max)
|
---|
210 | max = it->second.length();
|
---|
211 |
|
---|
212 | for (auto it=wco.begin(); it!=wco.end(); it++)
|
---|
213 | {
|
---|
214 | ostringstream str;
|
---|
215 | str.setf(ios_base::left);
|
---|
216 | str << setw(max+1) << it->second << " : " << it->first;
|
---|
217 | io_service.Write(now, str.str(), MessageImp::kWarn);
|
---|
218 | }
|
---|
219 | io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
|
---|
220 | return 127;
|
---|
221 | }
|
---|
222 |
|
---|
223 | io_service.Message("==================== Starting main loop =================");
|
---|
224 |
|
---|
225 | if (conf.Has("console") || !conf.Get<bool>("null"))
|
---|
226 | {
|
---|
227 | wout.SetNullOutput(null);
|
---|
228 | wout.SetBacklog(backlog);
|
---|
229 | }
|
---|
230 |
|
---|
231 | shell.SetReceiver(io_service);
|
---|
232 |
|
---|
233 | int ret = 0;
|
---|
234 | thread t(bind(Main::Thread, &io_service, dummy, ref(ret)));
|
---|
235 |
|
---|
236 | // Wait until state machine is ready (The only case I can imagine
|
---|
237 | // in which the state will never chane is when DIM triggers
|
---|
238 | // an exit before the state machine has been started at all.
|
---|
239 | // Hopefully checking the readline (see Threed) should fix
|
---|
240 | // that -- difficult to test.)
|
---|
241 | while ((io_service.GetCurrentState()<StateMachineImp::kSM_Ready ||
|
---|
242 | !io_service.MessageQueueEmpty()) && !shell.IsStopped())
|
---|
243 | usleep(1);
|
---|
244 |
|
---|
245 | // Execute command line commands
|
---|
246 | const vector<string> v1 = conf.Vec<string>("cmd");
|
---|
247 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
---|
248 | shell.ProcessLine(*it);
|
---|
249 |
|
---|
250 | const vector<string> v2 = conf.Vec<string>("exec");
|
---|
251 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
---|
252 | shell.Execute(*it, args);
|
---|
253 |
|
---|
254 | // Run the shell if no immediate exit was requested
|
---|
255 | if (!conf.Get<bool>("quit"))
|
---|
256 | shell.Run();
|
---|
257 |
|
---|
258 | io_service.Stop(); // Signal Loop-thread to stop
|
---|
259 | // io_service.Close(); // Obsolete, done by the destructor
|
---|
260 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
---|
261 |
|
---|
262 | // Wait until the StateMachine has finished its thread
|
---|
263 | // before returning and destroying the dim objects which might
|
---|
264 | // still be in use.
|
---|
265 | t.join();
|
---|
266 |
|
---|
267 | return ret;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | #endif
|
---|