| 1 | #include <iostream> | 
|---|
| 2 |  | 
|---|
| 3 | #include "Dim.h" | 
|---|
| 4 | #include "EventImp.h" | 
|---|
| 5 | #include "WindowLog.h" | 
|---|
| 6 | #include "Configuration.h" | 
|---|
| 7 | #include "StateMachineDim.h" | 
|---|
| 8 | #include "ReadlineColor.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 chatserv is a Dim-based chat server.\n" | 
|---|
| 40 | "\n" | 
|---|
| 41 | "It is a non-interactive program which acts as a relay of messages " | 
|---|
| 42 | "sent via a Dim command CHAT/MSG and which are redirected to the " | 
|---|
| 43 | "logging service CHAT/MESSAGE.\n" | 
|---|
| 44 | "\n" | 
|---|
| 45 | "Usage: chatserv [OPTIONS]\n" | 
|---|
| 46 | "  or:  chatserv [OPTIONS]\n"; | 
|---|
| 47 | cout << endl; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | void PrintHelp() | 
|---|
| 51 | { | 
|---|
| 52 | /* Additional help text which is printed after the configuration | 
|---|
| 53 | options goes here */ | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | class ChatServer : public StateMachineDim | 
|---|
| 57 | { | 
|---|
| 58 | private: | 
|---|
| 59 | int HandleMsg(const EventImp &evt) | 
|---|
| 60 | { | 
|---|
| 61 | Message(evt.GetString()); | 
|---|
| 62 | return GetCurrentState(); | 
|---|
| 63 | } | 
|---|
| 64 | public: | 
|---|
| 65 | ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT") | 
|---|
| 66 | { | 
|---|
| 67 | AddEvent("MSG", "C") | 
|---|
| 68 | (bind(&ChatServer::HandleMsg, this, placeholders::_1)) | 
|---|
| 69 | ("|msg[string]:message to be distributed"); | 
|---|
| 70 | } | 
|---|
| 71 | }; | 
|---|
| 72 |  | 
|---|
| 73 | int main(int argc, const char *argv[]) | 
|---|
| 74 | { | 
|---|
| 75 | Configuration conf(argv[0]); | 
|---|
| 76 | conf.SetPrintUsage(PrintUsage); | 
|---|
| 77 | SetupConfiguration(conf); | 
|---|
| 78 |  | 
|---|
| 79 | po::variables_map vm; | 
|---|
| 80 | try | 
|---|
| 81 | { | 
|---|
| 82 | vm = conf.Parse(argc, argv); | 
|---|
| 83 | } | 
|---|
| 84 | #if BOOST_VERSION > 104000 | 
|---|
| 85 | catch (po::multiple_occurrences &e) | 
|---|
| 86 | { | 
|---|
| 87 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl; | 
|---|
| 88 | return -1; | 
|---|
| 89 | } | 
|---|
| 90 | #endif | 
|---|
| 91 | catch (exception& e) | 
|---|
| 92 | { | 
|---|
| 93 | cerr << "Program options invalid due to: " << e.what() << endl; | 
|---|
| 94 | return -1; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | if (conf.HasVersion() || conf.HasPrint()) | 
|---|
| 98 | return -1; | 
|---|
| 99 |  | 
|---|
| 100 | if (conf.HasHelp()) | 
|---|
| 101 | { | 
|---|
| 102 | PrintHelp(); | 
|---|
| 103 | return -1; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | Dim::Setup(conf.Get<string>("dns")); | 
|---|
| 107 |  | 
|---|
| 108 | WindowLog log; | 
|---|
| 109 |  | 
|---|
| 110 | ReadlineColor::PrintBootMsg(log, conf.GetName(), false); | 
|---|
| 111 |  | 
|---|
| 112 | if (conf.Has("log")) | 
|---|
| 113 | if (!log.OpenLogFile(conf.Get<string>("log"))) | 
|---|
| 114 | cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl; | 
|---|
| 115 |  | 
|---|
| 116 | ChatServer serv(log); | 
|---|
| 117 | serv.Run(true); | 
|---|
| 118 |  | 
|---|
| 119 | return 0; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | // ************************************************************************** | 
|---|
| 123 | /** @example chatserv.cc | 
|---|
| 124 |  | 
|---|
| 125 | The program is stopped by CTRL-C | 
|---|
| 126 |  | 
|---|
| 127 | */ | 
|---|
| 128 | // ************************************************************************** | 
|---|