| 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 | #include <boost/lexical_cast.hpp>
|
|---|
| 10 |
|
|---|
| 11 | #include "Dim.h"
|
|---|
| 12 | #include "Time.h"
|
|---|
| 13 | #include "MainImp.h"
|
|---|
| 14 | #include "Readline.h"
|
|---|
| 15 | #include "WindowLog.h"
|
|---|
| 16 | #include "MessageImp.h"
|
|---|
| 17 | #include "Configuration.h"
|
|---|
| 18 |
|
|---|
| 19 | namespace Main
|
|---|
| 20 | {
|
|---|
| 21 | using namespace std;
|
|---|
| 22 |
|
|---|
| 23 | void SetupConfiguration(Configuration &conf)
|
|---|
| 24 | {
|
|---|
| 25 | const string n = conf.GetName()+".log";
|
|---|
| 26 |
|
|---|
| 27 | po::options_description config("Program options");
|
|---|
| 28 | config.add_options()
|
|---|
| 29 | ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
|
|---|
| 30 | ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
|
|---|
| 31 | ("log,l", var<string>(n), "Name of local log-file")
|
|---|
| 32 | ("append-log", po_bool(), "Append log information to local log-file")
|
|---|
| 33 | ("null", po_switch(), "Suppresses almost all console output - including errors (only available without --console option)")
|
|---|
| 34 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
|---|
| 35 | ("cmd", vars<string>(), "Execute one or more commands at startup")
|
|---|
| 36 | ("exec,e", vars<string>(), "Execute one or more scrips at startup ('file:N' - start at label N)")
|
|---|
| 37 | ("arg:*", var<string>(), "Arguments for script execution, e.g. --val:ra='12.5436'")
|
|---|
| 38 | ("quit", po_switch(), "Quit after startup");
|
|---|
| 39 | ;
|
|---|
| 40 |
|
|---|
| 41 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
|---|
| 42 | conf.AddEnv("host", "DIM_HOST_NODE");
|
|---|
| 43 |
|
|---|
| 44 | conf.AddOptions(config);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void PrintUsage()
|
|---|
| 48 | {
|
|---|
| 49 | cout <<
|
|---|
| 50 | "Files:\n"
|
|---|
| 51 | "The following files are written by each program by default\n"
|
|---|
| 52 | " program.evt: A log of all executed of skipped events\n"
|
|---|
| 53 | " program.his: The history accessible by Pg-up/dn\n"
|
|---|
| 54 | " program.log: All output piped to the log-stream\n"
|
|---|
| 55 | << endl;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | template<class T>
|
|---|
| 59 | void PrintHelp()
|
|---|
| 60 | {
|
|---|
| 61 | Dim::Setup();
|
|---|
| 62 |
|
|---|
| 63 | ofstream fout("/dev/null");
|
|---|
| 64 |
|
|---|
| 65 | T io_service(fout);
|
|---|
| 66 |
|
|---|
| 67 | io_service.PrintListOfStates(cout);
|
|---|
| 68 | cout << "\nList of available commands:\n";
|
|---|
| 69 | io_service.PrintListOfEvents(cout);
|
|---|
| 70 | cout << "\n";
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | void Thread(MainImp *io_service, bool dummy, int &rc)
|
|---|
| 74 | {
|
|---|
| 75 | // This is necessary so that the StateMachien Thread can signal the
|
|---|
| 76 | // Readline to exit
|
|---|
| 77 | rc = io_service->Run(dummy);
|
|---|
| 78 | Readline::Stop();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | template<class T, class S>
|
|---|
| 82 | int execute(Configuration &conf, bool dummy=false)
|
|---|
| 83 | {
|
|---|
| 84 | Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"));
|
|---|
| 85 |
|
|---|
| 86 | // -----------------------------------------------------------------
|
|---|
| 87 |
|
|---|
| 88 | static T shell(conf.GetName().c_str(),
|
|---|
| 89 | conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
|
|---|
| 90 |
|
|---|
| 91 | WindowLog &win = shell.GetStreamIn();
|
|---|
| 92 | WindowLog &wout = shell.GetStreamOut();
|
|---|
| 93 |
|
|---|
| 94 | // Switching off buffering is not strictly necessary, since
|
|---|
| 95 | // the destructor of shell should flush everything still buffered,
|
|---|
| 96 | // nevertheless it helps to debug problems in the initialization
|
|---|
| 97 | // sequence.
|
|---|
| 98 | const bool backlog = wout.GetBacklog();
|
|---|
| 99 | const bool null = wout.GetNullOutput();
|
|---|
| 100 | if (conf.Has("console") || !conf.Get<bool>("null"))
|
|---|
| 101 | {
|
|---|
| 102 | wout.SetBacklog(false);
|
|---|
| 103 | wout.SetNullOutput(false);
|
|---|
| 104 | wout.Display(true);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (conf.Has("log"))
|
|---|
| 108 | if (!wout.OpenLogFile(conf.Get<string>("log"), conf.Get<bool>("append-log")))
|
|---|
| 109 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 110 |
|
|---|
| 111 | S io_service(wout);
|
|---|
| 112 |
|
|---|
| 113 | const boost::filesystem::path path(conf.GetName());
|
|---|
| 114 |
|
|---|
| 115 | const string pname = path.parent_path().string();
|
|---|
| 116 | #if BOOST_VERSION < 104600
|
|---|
| 117 | const string fname = path.filename();
|
|---|
| 118 | #else
|
|---|
| 119 | const string fname = path.filename().string();
|
|---|
| 120 | #endif
|
|---|
| 121 | const Time now;
|
|---|
| 122 | io_service.Write(now, "/----------------------- Program ------------------------");
|
|---|
| 123 | io_service.Write(now, "| Program: "PACKAGE_STRING" ("+fname+":"+boost::lexical_cast<string>(getpid())+")");
|
|---|
| 124 | io_service.Write(now, "| CallPath: "+pname);
|
|---|
| 125 | io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
|
|---|
| 126 | io_service.Write(now, "| Revision: "REVISION);
|
|---|
| 127 | io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
|
|---|
| 128 | io_service.Write(now, "| URL: "PACKAGE_URL);
|
|---|
| 129 | io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
|
|---|
| 130 | io_service.Write(now, "\\----------------------- Options ------------------------");
|
|---|
| 131 | const multimap<string,string> mmap = conf.GetOptions();
|
|---|
| 132 | for (auto it=mmap.begin(); it!=mmap.end(); it++)
|
|---|
| 133 | io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
|
|---|
| 134 |
|
|---|
| 135 | const map<string,string> &args = conf.GetOptions<string>("arg:");
|
|---|
| 136 | if (args.size()>0)
|
|---|
| 137 | {
|
|---|
| 138 | io_service.Write(now, "------------------------ Arguments ----------------------", MessageImp::kMessage);
|
|---|
| 139 |
|
|---|
| 140 | for (auto it=args.begin(); it!=args.end(); it++)
|
|---|
| 141 | {
|
|---|
| 142 | ostringstream str;
|
|---|
| 143 | str.setf(ios_base::left);
|
|---|
| 144 | str << ": " << it->first << " = " << it->second;
|
|---|
| 145 | io_service.Write(now, str.str(), MessageImp::kMessage);
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | io_service.Write(now, "\\------------------- Evaluating options -----------------");
|
|---|
| 150 | const int rc = io_service.EvalOptions(conf);
|
|---|
| 151 | if (rc>=0)
|
|---|
| 152 | {
|
|---|
| 153 | ostringstream str;
|
|---|
| 154 | str << "Exit triggered by EvalOptions with rc=" << rc;
|
|---|
| 155 | io_service.Write(now, str.str(), MessageImp::kError);
|
|---|
| 156 | return rc;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | const map<string,string> &wco = conf.GetWildcardOptions();
|
|---|
| 160 | if (wco.size()>0)
|
|---|
| 161 | {
|
|---|
| 162 | io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
|
|---|
| 163 |
|
|---|
| 164 | size_t max = 0;
|
|---|
| 165 | for (auto it=wco.begin(); it!=wco.end(); it++)
|
|---|
| 166 | if (it->second.length()>max)
|
|---|
| 167 | max = it->second.length();
|
|---|
| 168 |
|
|---|
| 169 | for (auto it=wco.begin(); it!=wco.end(); it++)
|
|---|
| 170 | {
|
|---|
| 171 | ostringstream str;
|
|---|
| 172 | str.setf(ios_base::left);
|
|---|
| 173 | str << setw(max+1) << it->second << " : " << it->first;
|
|---|
| 174 | io_service.Write(now, str.str(), MessageImp::kWarn);
|
|---|
| 175 | }
|
|---|
| 176 | io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
|
|---|
| 177 | return 127;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | io_service.Message("==================== Starting main loop =================");
|
|---|
| 181 |
|
|---|
| 182 | if (conf.Has("console") || !conf.Get<bool>("null"))
|
|---|
| 183 | {
|
|---|
| 184 | wout.SetNullOutput(null);
|
|---|
| 185 | wout.SetBacklog(backlog);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | shell.SetReceiver(io_service);
|
|---|
| 189 |
|
|---|
| 190 | // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
|
|---|
| 191 | int ret = 0;
|
|---|
| 192 | thread t(bind(Main::Thread, &io_service, dummy, ref(ret)));
|
|---|
| 193 |
|
|---|
| 194 | const vector<string> v1 = conf.Vec<string>("cmd");
|
|---|
| 195 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
|---|
| 196 | shell.ProcessLine(*it);
|
|---|
| 197 |
|
|---|
| 198 | const vector<string> v2 = conf.Vec<string>("exec");
|
|---|
| 199 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
|---|
| 200 | shell.Execute(*it, args);
|
|---|
| 201 |
|
|---|
| 202 | if (!conf.Get<bool>("quit"))
|
|---|
| 203 | shell.Run(); // Run the shell
|
|---|
| 204 |
|
|---|
| 205 | io_service.Stop(); // Signal Loop-thread to stop
|
|---|
| 206 | // io_service.Close(); // Obsolete, done by the destructor
|
|---|
| 207 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
|---|
| 208 |
|
|---|
| 209 | // Wait until the StateMachine has finished its thread
|
|---|
| 210 | // before returning and destroying the dim objects which might
|
|---|
| 211 | // still be in use.
|
|---|
| 212 | t.join();
|
|---|
| 213 |
|
|---|
| 214 | return ret;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | #endif
|
|---|