source: trunk/FACT++/src/chatserv.cc@ 10898

Last change on this file since 10898 was 10802, checked in by tbretz, 14 years ago
Simplified program options handling.
File size: 3.1 KB
Line 
1#include <iostream>
2
3#include <boost/bind.hpp>
4
5#include "Dim.h"
6#include "EventImp.h"
7#include "WindowLog.h"
8#include "Configuration.h"
9#include "StateMachineDim.h"
10
11using namespace std;
12
13void 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 host name (Overwites DIM_DNS_NODE environment variable)")
20 ("log,l", var<string>(n), "Write log-file")
21 ;
22
23 conf.AddEnv("dns", "DIM_DNS_NODE");
24
25 conf.AddOptions(config);
26}
27
28/*
29 Extract usage clause(s) [if any] for SYNOPSIS.
30 Translators: "Usage" and "or" here are patterns (regular expressions) which
31 are used to match the usage synopsis in program output. An example from cp
32 (GNU coreutils) which contains both strings:
33 Usage: cp [OPTION]... [-T] SOURCE DEST
34 or: cp [OPTION]... SOURCE... DIRECTORY
35 or: cp [OPTION]... -t DIRECTORY SOURCE...
36 */
37void PrintUsage()
38{
39 cout <<
40 "The chatserv is a Dim-based chat server.\n"
41 "\n"
42 "It is a non-interactive program which acts as a relay of messages "
43 "sent via a Dim command CHAT/MSG and which are redirected to the "
44 "logging service CHAT/MESSAGE.\n"
45 "\n"
46 "Usage: chatserv [OPTIONS]\n"
47 " or: chatserv [OPTIONS]\n";
48 cout << endl;
49}
50
51void PrintHelp()
52{
53 /* Additional help text which is printed after the configuration
54 options goes here */
55}
56
57class ChatServer : public StateMachineDim
58{
59private:
60 int HandleMsg(const EventImp &evt)
61 {
62 Message(evt.GetString());
63 return GetCurrentState();
64 }
65public:
66 ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
67 {
68 AddEvent("MSG", "C")
69 (boost::bind(&ChatServer::HandleMsg, this, _1))
70 ("|msg[string]:message to be distributed");
71 }
72};
73
74int main(int argc, const char *argv[])
75{
76 Configuration conf(argv[0]);
77 conf.SetPrintUsage(PrintUsage);
78 SetupConfiguration(conf);
79
80 po::variables_map vm;
81 try
82 {
83 vm = conf.Parse(argc, argv);
84 }
85#if BOOST_VERSION > 104000
86 catch (po::multiple_occurrences &e)
87 {
88 cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
89 return -1;
90 }
91#endif
92 catch (exception& e)
93 {
94 cerr << "Program options invalid due to: " << e.what() << endl;
95 return -1;
96 }
97
98 if (conf.HasVersion() || conf.HasPrint())
99 return -1;
100
101 if (conf.HasHelp())
102 {
103 PrintHelp();
104 return -1;
105 }
106
107 Dim::Setup(conf.Get<string>("dns"));
108
109 WindowLog log;
110
111 if (conf.Has("log"))
112 if (!log.OpenLogFile(conf.Get<string>("log")))
113 cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
114
115 ChatServer serv(log);
116 serv.Run(true);
117
118 return 0;
119}
120
121// **************************************************************************
122/** @example chatserv.cc
123
124The program is stopped by CTRL-C
125
126*/
127// **************************************************************************
Note: See TracBrowser for help on using the repository browser.