76edf09fcd70a5ff40913ed913821cd384f9e531
[senf.git] / senf / Utils / Console / Example.dox
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \page console_testserver Test Server
24
25     \dontinclude testServer.cc
26
27     You can find this code in the \c testServer.cc source file.
28
29     \skip Custom
30     \skip #include
31     \until namespace
32
33     Here we see the necessary include files. For console support only <tt>senf/Console.hh</tt> needs
34     to be included. This will pull in all necessary definitions.
35
36     We declare an alias for the senf::console::kw namespace which we will use further down for the
37     keyword arguments.
38
39     \until echo
40     \until }
41     \until }
42
43     The first callback \c echo utilizes \link console_manualparse manual argument
44     parsing\endlink. You should always refer to the iterator types as shown here since this will be
45     safe from future changes.
46
47     The following \c struct \c TestObject is used to show how member functions and objects are used
48     in the console.
49
50     \until dir
51
52     Important is the definition of the <b>public</b> senf::console::ScopedDirectory member called \c
53     dir. This member can be used later to add the class to the node tree. Here we just register a
54     single function \c vat (with \link console_autoparse automatic argument parsing\endlink) and
55     set some command attributes.
56
57     \until };
58
59     The \c shutdownServer callback terminates the server.
60
61     \until }
62
63     This happens in two steps: First the \c terminate() call tells the scheduler to leave it's
64     main-loop after shutdownServer returns (which is ultimately called via the console server from
65     the scheduler). Throwing a senf::console::Executor::ExitException is like entering the \c exit
66     built-in command at the console.
67
68     The next callback accesses the client instance directly to manipulate the logging:
69
70     \until }
71
72     The senf::console::Client instance can be accessed using the senf::console::Client::get() helper
73     via the output stream. Since every Client is a senf::log::Target, we can route arbitrary log
74     messages to the console instance.
75
76     We now define \c main() which initializes the node tree and starts the console server
77
78     \until route
79
80     Here we just setup more verbose logging and set \c SIGPIPE signals to be ignored. \c SIGPIPE's
81     are a pain and really should be disabled.
82
83     \until settings
84
85     This shows, how to set the top-level documentation and create a new subdirectory.
86
87     \until doc
88
89     Here we create another new directory and save a reference so we can later access the node
90     directly. All the add commands return such a node reference of the correct type (this is a lie,
91     but it works like this anyways and it's an implementation detail that must not concern you
92     here).
93
94     Instead of creating a new directory directly and later sotring a reference, it is better to use
95     \c senf::console::ScopedDirectory<> like this:
96
97     \until functions
98
99     This will automatically remove the node from the tree when the \c senf::console::ScopedDirectory
100     instance is destroyed. It also protects against the problem of dangling references: When using a
101     plain reference, removing the directory from the tree will destroy the node. The reference
102     however will still reference the (now nonexistent) directory and any access via the reference
103     will crash the program.
104
105     The next statements add commands to the various directories declared so far
106
107     \until Example
108
109     We now continue by creating an instance of our test class \c TestObject
110
111     \until Example
112
113     We add that objects directory to the \c test dir. We now have created a directory structure like
114     tis:
115     <pre>
116     /
117       console/
118         showlog
119       server/
120         shutdown
121       test/
122         echo
123         testob/
124           vat
125     </pre>
126
127     We now start the server (giving it a nice descriptive name) and run the scheduler.
128
129     \until }
130  */
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -U doc"
141 // mode: flyspell
142 // mode: auto-fill
143 // End: