| 1 | #include <iostream>
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/bind.hpp>
|
|---|
| 4 |
|
|---|
| 5 | #include "EventImp.h"
|
|---|
| 6 | #include "WindowLog.h"
|
|---|
| 7 | #include "Configuration.h"
|
|---|
| 8 | #include "StateMachineDim.h"
|
|---|
| 9 |
|
|---|
| 10 | using namespace std;
|
|---|
| 11 |
|
|---|
| 12 | void SetupConfiguration(Configuration &conf)
|
|---|
| 13 | {
|
|---|
| 14 | const string n = conf.GetName()+".log";
|
|---|
| 15 |
|
|---|
| 16 | po::options_description config("Program options");
|
|---|
| 17 | config.add_options()
|
|---|
| 18 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
|---|
| 19 | ("log,l", var<string>(n), "Write log-file")
|
|---|
| 20 | ;
|
|---|
| 21 |
|
|---|
| 22 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
|---|
| 23 |
|
|---|
| 24 | conf.AddOptions(config);
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | /*
|
|---|
| 28 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 29 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 30 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 31 | (GNU coreutils) which contains both strings:
|
|---|
| 32 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 33 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 34 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 35 | */
|
|---|
| 36 | void PrintUsage()
|
|---|
| 37 | {
|
|---|
| 38 | cout <<
|
|---|
| 39 | "The ftmctrl controls the FTM (FACT Trigger Master) board.\n"
|
|---|
| 40 | "\n"
|
|---|
| 41 | "The default is that the program is started without user intercation. "
|
|---|
| 42 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
|---|
| 43 | "option, a local shell can be initialized. With h or help a short "
|
|---|
| 44 | "help message about the usuage can be brought to the screen.\n"
|
|---|
| 45 | "\n"
|
|---|
| 46 | "Usage: ftmctrl [-c type] [OPTIONS]\n"
|
|---|
| 47 | " or: ftmctrl [OPTIONS]\n"
|
|---|
| 48 | "\n"
|
|---|
| 49 | "Options:\n"
|
|---|
| 50 | "The following describes the available commandline options. "
|
|---|
| 51 | "For further details on how command line option are parsed "
|
|---|
| 52 | "and in which order which configuration sources are accessed "
|
|---|
| 53 | "please refer to the class reference of the Configuration class.";
|
|---|
| 54 | cout << endl;
|
|---|
| 55 |
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void PrintHelp()
|
|---|
| 59 | {
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | The first line of the --version information is assumed to be in one
|
|---|
| 64 | of the following formats:
|
|---|
| 65 |
|
|---|
| 66 | <version>
|
|---|
| 67 | <program> <version>
|
|---|
| 68 | {GNU,Free} <program> <version>
|
|---|
| 69 | <program> ({GNU,Free} <package>) <version>
|
|---|
| 70 | <program> - {GNU,Free} <package> <version>
|
|---|
| 71 |
|
|---|
| 72 | and separated from any copyright/author details by a blank line.
|
|---|
| 73 |
|
|---|
| 74 | Handle multi-line bug reporting sections of the form:
|
|---|
| 75 |
|
|---|
| 76 | Report <program> bugs to <addr>
|
|---|
| 77 | GNU <package> home page: <url>
|
|---|
| 78 | ...
|
|---|
| 79 | */
|
|---|
| 80 | void PrintVersion(const char *name)
|
|---|
| 81 | {
|
|---|
| 82 | cout <<
|
|---|
| 83 | name << " - FACT++ 1.0 - Trigger master control\n"
|
|---|
| 84 | "\n"
|
|---|
| 85 | "Written by Thomas Bretz <thomas.bretz@epfl.ch> et al.\n"
|
|---|
| 86 | "\n"
|
|---|
| 87 | "Report bugs to Thomas Bretz <thomas.bretz@epfl.ch>\n"
|
|---|
| 88 | "FACT++ home page: http://www.xxx.com\n"
|
|---|
| 89 | "\n"
|
|---|
| 90 | "Copyright (C) 2011 by the FACT Collaboration.\n"
|
|---|
| 91 | "This is free software; see the source for copying conditions.\n"
|
|---|
| 92 | << endl;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | class ChatServer : public StateMachineDim
|
|---|
| 96 | {
|
|---|
| 97 | private:
|
|---|
| 98 | int HandleMsg(const EventImp &evt)
|
|---|
| 99 | {
|
|---|
| 100 | Message(evt.GetString());
|
|---|
| 101 | return GetCurrentState();
|
|---|
| 102 | }
|
|---|
| 103 | public:
|
|---|
| 104 | ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
|
|---|
| 105 | {
|
|---|
| 106 | AddConfiguration("MSG", "C")
|
|---|
| 107 | (boost::bind(&ChatServer::HandleMsg, this, _1))
|
|---|
| 108 | ("|msg[string]:message to be distributed");
|
|---|
| 109 | }
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | int main(int argc, const char *argv[])
|
|---|
| 113 | {
|
|---|
| 114 | Configuration conf(argv[0]);
|
|---|
| 115 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 116 | SetupConfiguration(conf);
|
|---|
| 117 |
|
|---|
| 118 | po::variables_map vm;
|
|---|
| 119 | try
|
|---|
| 120 | {
|
|---|
| 121 | vm = conf.Parse(argc, argv);
|
|---|
| 122 | }
|
|---|
| 123 | #if BOOST_VERSION > 104000
|
|---|
| 124 | catch (po::multiple_occurrences &e)
|
|---|
| 125 | {
|
|---|
| 126 | cout << "Error: " << e.what() << " of '" << e.get_option_name() << "' option." << endl;
|
|---|
| 127 | cout << endl;
|
|---|
| 128 | return -1;
|
|---|
| 129 | }
|
|---|
| 130 | #endif
|
|---|
| 131 | catch (std::exception &e)
|
|---|
| 132 | {
|
|---|
| 133 | cout << "Error: " << e.what() << endl;
|
|---|
| 134 | cout << endl;
|
|---|
| 135 |
|
|---|
| 136 | return -1;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | if (conf.HasPrint())
|
|---|
| 140 | return -1;
|
|---|
| 141 |
|
|---|
| 142 | if (conf.HasVersion())
|
|---|
| 143 | {
|
|---|
| 144 | PrintVersion(argv[0]);
|
|---|
| 145 | return -1;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | if (conf.HasHelp())
|
|---|
| 149 | {
|
|---|
| 150 | PrintHelp();
|
|---|
| 151 | return -1;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | // To allow overwriting of DIM_DNS_NODE set 0 to 1
|
|---|
| 155 | setenv("DIM_DNS_NODE", conf.Get<string>("dns").c_str(), 1);
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | WindowLog log;
|
|---|
| 159 |
|
|---|
| 160 | if (conf.Has("log"))
|
|---|
| 161 | if (!log.OpenLogFile(conf.Get<string>("log")))
|
|---|
| 162 | cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 163 |
|
|---|
| 164 | ChatServer serv(log);
|
|---|
| 165 | serv.Run(true);
|
|---|
| 166 |
|
|---|
| 167 | return 0;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | // **************************************************************************
|
|---|
| 171 | /** @example chatserv.cc
|
|---|
| 172 |
|
|---|
| 173 | The program is stopped by CTRL-C
|
|---|
| 174 |
|
|---|
| 175 | */
|
|---|
| 176 | // **************************************************************************
|
|---|