FACT++  1.0
chatserv.cc
Go to the documentation of this file.
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 
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 (overwites DIM_DNS_NODE environment variable)")
19  ("host", var<string>(""), "Address with which the Dim nameserver can connect to this host (overwites DIM_HOST_NODE environment variable)")
20  ("log,l", var<string>(n), "Write log-file")
21  ;
22 
23  conf.AddEnv("dns", "DIM_DNS_NODE");
24  conf.AddEnv("host", "DIM_HOST_NODE");
25 
26  conf.AddOptions(config);
27 }
28 
29 /*
30  Extract usage clause(s) [if any] for SYNOPSIS.
31  Translators: "Usage" and "or" here are patterns (regular expressions) which
32  are used to match the usage synopsis in program output. An example from cp
33  (GNU coreutils) which contains both strings:
34  Usage: cp [OPTION]... [-T] SOURCE DEST
35  or: cp [OPTION]... SOURCE... DIRECTORY
36  or: cp [OPTION]... -t DIRECTORY SOURCE...
37  */
38 void PrintUsage()
39 {
40  cout <<
41  "The chatserv is a Dim-based chat server.\n"
42  "\n"
43  "It is a non-interactive program which acts as a relay of messages "
44  "sent via a Dim command CHAT/MSG and which are redirected to the "
45  "logging service CHAT/MESSAGE.\n"
46  "\n"
47  "Usage: chatserv [OPTIONS]\n"
48  " or: chatserv [OPTIONS]\n";
49  cout << endl;
50 }
51 
52 void PrintHelp()
53 {
54  /* Additional help text which is printed after the configuration
55  options goes here */
56 }
57 
59 {
60 private:
61  int HandleMsg(const EventImp &evt)
62  {
63  Comment(evt.GetString());
64  return GetCurrentState();
65  }
66 public:
67  ChatServer(ostream &lout) : StateMachineDim(lout, "CHAT")
68  {
69  AddEvent("MSG", "C")
70  (bind(&ChatServer::HandleMsg, this, placeholders::_1))
71  ("|msg[string]:message to be distributed");
72  }
73 };
74 
75 int main(int argc, const char *argv[])
76 {
77  Configuration conf(argv[0]);
79  SetupConfiguration(conf);
80 
81  if (!conf.DoParse(argc, argv, PrintHelp))
82  return 127;
83 
84  Dim::Setup(conf.Get<string>("dns"), conf.Get<string>("host"));
85 
86  WindowLog log;
87 
88  ReadlineColor::PrintBootMsg(log, conf.GetName(), false);
89 
90  if (conf.Has("log"))
91  if (!log.OpenLogFile(conf.Get<string>("log")))
92  cerr << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
93 
94  return ChatServer(log).Run();
95 }
96 
97 // **************************************************************************
103 // **************************************************************************
A general base-class describing events issues in a state machine.
Definition: EventImp.h:11
A C++ ostream to an ncurses window supporting attributes and colors.
Definition: WindowLog.h:50
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
STL namespace.
void AddEnv(const std::string &conf, const std::string &env)
std::string GetString() const
Definition: EventImp.cc:194
void Setup(const std::string &dns="", const std::string &host="")
Definition: DimSetup.cc:160
bool Has(const std::string &var)
void PrintUsage()
Definition: chatserv.cc:38
void AddOptions(const po::options_description &opt, bool visible=true)
Definition: Configuration.h:92
int HandleMsg(const EventImp &evt)
Definition: chatserv.cc:61
ChatServer(ostream &lout)
Definition: chatserv.cc:67
void SetupConfiguration(Configuration &conf)
Definition: chatserv.cc:12
bool PrintBootMsg(std::ostream &out, const std::string &name, bool interactive=true)
Commandline parsing, resource file parsing and database access.
Definition: Configuration.h:9
int main(int argc, const char *argv[])
Definition: chatserv.cc:75
Class for a state machine implementation within a DIM network.
virtual int Run(bool dummy)
Start the mainloop.
bool OpenLogFile(const std::string &filename, bool append=false)
Open a log-file.
Definition: WindowLog.cc:111
void PrintHelp()
Definition: chatserv.cc:52
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
const std::string & GetName() const