| 1 | #include <iostream>
|
|---|
| 2 |
|
|---|
| 3 | #include "dim.h"
|
|---|
| 4 |
|
|---|
| 5 | #include "Dim.h"
|
|---|
| 6 | #include "EventImp.h"
|
|---|
| 7 | #include "WindowLog.h"
|
|---|
| 8 | #include "Configuration.h"
|
|---|
| 9 | #include "StateMachineDim.h"
|
|---|
| 10 | #include "ReadlineColor.h"
|
|---|
| 11 |
|
|---|
| 12 | using namespace std;
|
|---|
| 13 |
|
|---|
| 14 | void SetupConfiguration(Configuration &conf)
|
|---|
| 15 | {
|
|---|
| 16 | const string n = conf.GetName()+".log";
|
|---|
| 17 |
|
|---|
| 18 | po::options_description config("Program options");
|
|---|
| 19 | config.add_options()
|
|---|
| 20 | ("dns", var<string>("localhost"), "Dim nameserver (overwites DIM_DNS_NODE environment variable)")
|
|---|
| 21 | ("port", var<uint16_t>(DNS_PORT), "Port to connect to dim nameserver.")
|
|---|
| 22 | ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
|
|---|
| 23 | ("log,l", var<string>(n), "Write log-file")
|
|---|
| 24 | ;
|
|---|
| 25 |
|
|---|
| 26 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
|---|
| 27 | conf.AddEnv("port", "DIM_DNS_PORT");
|
|---|
| 28 | conf.AddEnv("host", "DIM_HOST_NODE");
|
|---|
| 29 |
|
|---|
| 30 | conf.AddOptions(config);
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /*
|
|---|
| 34 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 35 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 36 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 37 | (GNU coreutils) which contains both strings:
|
|---|
| 38 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 39 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 40 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 41 | */
|
|---|
| 42 | void PrintUsage()
|
|---|
| 43 | {
|
|---|
| 44 | cout <<
|
|---|
| 45 | "The chatserv is a Dim-based chat server.\n"
|
|---|
| 46 | "\n"
|
|---|
| 47 | "It is a non-interactive program which acts as a relay of messages "
|
|---|
| 48 | "sent via a Dim command CHAT/MSG and which are redirected to the "
|
|---|
| 49 | "logging service CHAT/MESSAGE.\n"
|
|---|
| 50 | "\n"
|
|---|
| 51 | "Usage: chatserv [OPTIONS]\n"
|
|---|
| 52 | " or: chatserv [OPTIONS]\n";
|
|---|
| 53 | cout << endl;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void PrintHelp()
|
|---|
| 57 | {
|
|---|
| 58 | /* Additional help text which is printed after the configuration
|
|---|
| 59 | options goes here */
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | class ChatServer : public StateMachineDim
|
|---|
| 63 | {
|
|---|
| 64 | private:
|
|---|
| 65 | int HandleMsg(const EventImp &evt)
|
|---|
| 66 | {
|
|---|
| 67 | Comment(evt.GetString());
|
|---|
| 68 | return GetCurrentState();
|
|---|
| 69 | }
|
|---|
| 70 | public:
|
|---|
| 71 | ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
|
|---|
| 72 | {
|
|---|
| 73 | AddEvent("MSG", "C")
|
|---|
| 74 | (bind(&ChatServer::HandleMsg, this, placeholders::_1))
|
|---|
| 75 | ("|msg[string]:message to be distributed");
|
|---|
| 76 | }
|
|---|
| 77 | };
|
|---|
| 78 |
|
|---|
| 79 | int main(int argc, const char *argv[])
|
|---|
| 80 | {
|
|---|
| 81 | Configuration conf(argv[0]);
|
|---|
| 82 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 83 | SetupConfiguration(conf);
|
|---|
| 84 |
|
|---|
| 85 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 86 | return 127;
|
|---|
| 87 |
|
|---|
| 88 | Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"), conf.Get<uint16_t>("port"));
|
|---|
| 89 |
|
|---|
| 90 | WindowLog log;
|
|---|
| 91 |
|
|---|
| 92 | ReadlineColor::PrintBootMsg(log, conf.GetName(), false);
|
|---|
| 93 |
|
|---|
| 94 | if (conf.Has("log"))
|
|---|
| 95 | if (!log.OpenLogFile(conf.Get<string>("log")))
|
|---|
| 96 | cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 97 |
|
|---|
| 98 | return ChatServer(log).Run();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | // **************************************************************************
|
|---|
| 102 | /** @example chatserv.cc
|
|---|
| 103 |
|
|---|
| 104 | The program is stopped by CTRL-C
|
|---|
| 105 |
|
|---|
| 106 | */
|
|---|
| 107 | // **************************************************************************
|
|---|