| 1 | #ifndef FACT_MAIN
|
|---|
| 2 | #define FACT_MAIN
|
|---|
| 3 |
|
|---|
| 4 | #include <map>
|
|---|
| 5 | #include <thread>
|
|---|
| 6 | #include <functional>
|
|---|
| 7 |
|
|---|
| 8 | #include "LocalControl.h"
|
|---|
| 9 | #include "Configuration.h"
|
|---|
| 10 |
|
|---|
| 11 | namespace Main
|
|---|
| 12 | {
|
|---|
| 13 | void SetupConfiguration(Configuration &conf)
|
|---|
| 14 | {
|
|---|
| 15 | const string n = conf.GetName()+".log";
|
|---|
| 16 |
|
|---|
| 17 | po::options_description config("Program options");
|
|---|
| 18 | config.add_options()
|
|---|
| 19 | ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
|
|---|
| 20 | ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
|
|---|
| 21 | ("log,l", var<string>(n), "Name of local log-file")
|
|---|
| 22 | ("append-log", po_bool(), "Append log information to local log-file")
|
|---|
| 23 | ("null", po_switch(), "Suppresses almost all console output - including errors (only available without --console option)")
|
|---|
| 24 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
|---|
| 25 | ("cmd", vars<string>(), "Execute one or more commands at startup")
|
|---|
| 26 | ("exec,e", vars<string>(), "Execute one or more scrips at startup")
|
|---|
| 27 | ("quit", po_switch(), "Quit after startup");
|
|---|
| 28 | ;
|
|---|
| 29 |
|
|---|
| 30 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
|---|
| 31 | conf.AddEnv("host", "DIM_HOST_NODE");
|
|---|
| 32 |
|
|---|
| 33 | conf.AddOptions(config);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | void Thread(StateMachineImp *io_service, bool dummy)
|
|---|
| 37 | {
|
|---|
| 38 | // This is necessary so that the StateMachien Thread can signal the
|
|---|
| 39 | // Readline to exit
|
|---|
| 40 | io_service->Run(dummy);
|
|---|
| 41 | Readline::Stop();
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | template<class T, class S>
|
|---|
| 45 | int execute(Configuration &conf, bool dummy=false)
|
|---|
| 46 | {
|
|---|
| 47 | Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"));
|
|---|
| 48 |
|
|---|
| 49 | // -----------------------------------------------------------------
|
|---|
| 50 |
|
|---|
| 51 | static T shell(conf.GetName().c_str(),
|
|---|
| 52 | conf.Has("console") ? conf.Get<int>("console")!=1 : conf.Get<bool>("null"));
|
|---|
| 53 |
|
|---|
| 54 | WindowLog &win = shell.GetStreamIn();
|
|---|
| 55 | WindowLog &wout = shell.GetStreamOut();
|
|---|
| 56 |
|
|---|
| 57 | // Switching off buffering is not strictly necessary, since
|
|---|
| 58 | // the destructor of shell should flush everything still buffered,
|
|---|
| 59 | // nevertheless it helps to debug problems in the initialization
|
|---|
| 60 | // sequence.
|
|---|
| 61 | const bool backlog = wout.GetBacklog();
|
|---|
| 62 | const bool null = wout.GetNullOutput();
|
|---|
| 63 | if (conf.Has("console") || !conf.Get<bool>("null"))
|
|---|
| 64 | {
|
|---|
| 65 | wout.SetBacklog(false);
|
|---|
| 66 | wout.SetNullOutput(false);
|
|---|
| 67 | wout.Display(true);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if (conf.Has("log"))
|
|---|
| 71 | if (!wout.OpenLogFile(conf.Get<string>("log"), conf.Get<bool>("append-log")))
|
|---|
| 72 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 73 |
|
|---|
| 74 | S io_service(wout);
|
|---|
| 75 |
|
|---|
| 76 | const boost::filesystem::path path(conf.GetName());
|
|---|
| 77 |
|
|---|
| 78 | const string pname = path.parent_path().string();
|
|---|
| 79 | const string fname = path.filename();
|
|---|
| 80 |
|
|---|
| 81 | const Time now;
|
|---|
| 82 | io_service.Write(now, "/----------------------- Program ------------------------");
|
|---|
| 83 | io_service.Write(now, "| Program: "PACKAGE_STRING" ("+fname+")");
|
|---|
| 84 | io_service.Write(now, "| CallPath: "+pname);
|
|---|
| 85 | io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
|
|---|
| 86 | io_service.Write(now, "| Revision: "REVISION);
|
|---|
| 87 | io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
|
|---|
| 88 | io_service.Write(now, "| URL: "PACKAGE_URL);
|
|---|
| 89 | io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
|
|---|
| 90 | io_service.Write(now, "\\----------------------- Options ------------------------");
|
|---|
| 91 | const multimap<string,string> mmap = conf.GetOptions();
|
|---|
| 92 | for (multimap<string,string>::const_iterator it=mmap.begin(); it!=mmap.end(); it++)
|
|---|
| 93 | io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
|
|---|
| 94 | io_service.Write(now, "\\------------------- Evaluating options -----------------");
|
|---|
| 95 | const int rc = io_service.EvalOptions(conf);
|
|---|
| 96 | if (rc>=0)
|
|---|
| 97 | {
|
|---|
| 98 | ostringstream str;
|
|---|
| 99 | str << "Exit triggered by EvalOptions with rc=" << rc;
|
|---|
| 100 | io_service.Write(now, str.str(), MessageImp::kError);
|
|---|
| 101 | return rc;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | const map<string,string> &wco = conf.GetWildcardOptions();
|
|---|
| 105 | if (wco.size()>0)
|
|---|
| 106 | {
|
|---|
| 107 | io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
|
|---|
| 108 |
|
|---|
| 109 | size_t max = 0;
|
|---|
| 110 | for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
|
|---|
| 111 | if (it->second.length()>max)
|
|---|
| 112 | max = it->second.length();
|
|---|
| 113 |
|
|---|
| 114 | for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
|
|---|
| 115 | {
|
|---|
| 116 | ostringstream str;
|
|---|
| 117 | str.setf(ios_base::left);
|
|---|
| 118 | str << setw(max+1) << it->second << " : " << it->first;
|
|---|
| 119 | io_service.Write(now, str.str(), MessageImp::kWarn);
|
|---|
| 120 | }
|
|---|
| 121 | io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
|
|---|
| 122 | return 127;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | io_service.Message("==================== Starting main loop =================");
|
|---|
| 126 |
|
|---|
| 127 | if (conf.Has("console") || !conf.Get<bool>("null"))
|
|---|
| 128 | {
|
|---|
| 129 | wout.SetNullOutput(null);
|
|---|
| 130 | wout.SetBacklog(backlog);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | shell.SetReceiver(io_service);
|
|---|
| 134 |
|
|---|
| 135 | // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
|
|---|
| 136 | thread t(bind(Main::Thread, &io_service, dummy));
|
|---|
| 137 |
|
|---|
| 138 | const vector<string> v1 = conf.Vec<string>("cmd");
|
|---|
| 139 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
|---|
| 140 | shell.ProcessLine(*it);
|
|---|
| 141 |
|
|---|
| 142 | const vector<string> v2 = conf.Vec<string>("exec");
|
|---|
| 143 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
|---|
| 144 | shell.Execute(*it);
|
|---|
| 145 |
|
|---|
| 146 | if (conf.Get<bool>("quit"))
|
|---|
| 147 | shell.Stop();
|
|---|
| 148 |
|
|---|
| 149 | shell.Run(); // Run the shell
|
|---|
| 150 | io_service.Stop(); // Signal Loop-thread to stop
|
|---|
| 151 | // io_service.Close(); // Obsolete, done by the destructor
|
|---|
| 152 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
|---|
| 153 |
|
|---|
| 154 | // Wait until the StateMachine has finished its thread
|
|---|
| 155 | // before returning and destroying the dim objects which might
|
|---|
| 156 | // still be in use.
|
|---|
| 157 | t.join();
|
|---|
| 158 |
|
|---|
| 159 | return 0;
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | #endif
|
|---|