switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / testServer.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief testServer implementation */
30
31 // Custom includes
32 #include <iostream>
33 #include <senf/Utils/Console.hh>
34 #include <senf/Scheduler/Scheduler.hh>
35
36 namespace kw = senf::console::kw;
37 namespace fty = senf::console::factory;
38
39 void echo(std::ostream & output, senf::console::ParseCommandInfo const & command)
40 {
41     typedef senf::console::ParseCommandInfo::TokensRange::iterator iterator;
42     iterator i (command.tokens().begin());
43     iterator i_end (command.tokens().end());
44     for (; i != i_end; ++i) {
45         output << i->value() << ' ';
46     }
47     output << "\n";
48 }
49
50 struct TestObject
51 {
52     senf::console::ScopedDirectory<TestObject> dir;
53
54     TestObject()
55         : dir(this)
56         {
57             dir.add("vat", fty::Command(&TestObject::vat, this)
58                     .arg("vat", "VAT in %", kw::default_value = 19)
59                     .arg("amount", "Amount including VAT")
60                     .doc("Returns the amount of {vat}-% VAT included in {amount}") );
61         }
62
63     double vat (int vat, double amount)
64         {
65             return amount * vat/(100.0+vat);
66         }
67 };
68
69 void shutdownServer()
70 {
71     senf::scheduler::terminate();
72     throw senf::console::Executor::ExitException();
73 }
74
75 void enableLogging(std::ostream & os)
76 {
77     senf::console::Client::get(os).route<senf::log::NOTICE>();
78 }
79
80 int main(int, char **)
81 {
82     ::signal(SIGPIPE, SIG_IGN);
83     senf::log::ConsoleTarget::instance().route< senf::log::VERBOSE >();
84
85     senf::console::root()
86         .doc("This is the console test application");
87
88     senf::console::root()
89         .add("console",fty::Directory()
90              .doc("Console settings"));
91
92     senf::console::DirectoryNode & serverDir (
93         senf::console::root()
94         .add("server",fty::Directory()
95              .doc("server commands")));
96
97     senf::console::ScopedDirectory<> testDir;
98     senf::console::root()
99         .add("test", testDir)
100         .doc("Test functions");
101
102     senf::console::root()["console"]
103         .add("showlog", fty::Command(&enableLogging)
104              .doc("Enable display of log messages on the current console"));
105
106     senf::console::root().add("sl", fty::Link(senf::console::root()["console"]("showlog")));
107
108     serverDir
109         .add("shutdown", fty::Command(&shutdownServer)
110              .doc("Terminate server application"));
111
112     testDir
113         .add("echo", fty::Command(&echo)
114              .doc("Example of a function utilizing manual argument parsing"));
115
116     TestObject test;
117     testDir
118         .add("extra", test.dir)
119         .doc("Example of an instance directory");
120
121     senf::console::root().add("ex", fty::Link(test.dir));
122
123     senf::console::Server::start( senf::INet4SocketAddress(23232u) )
124         .name("testServer");
125
126     senf::scheduler::process();
127     return 0;
128 }
129
130 \f
131 // Local Variables:
132 // mode: c++
133 // fill-column: 100
134 // comment-column: 40
135 // c-file-style: "senf"
136 // indent-tabs-mode: nil
137 // ispell-local-dictionary: "american"
138 // compile-command: "scons -U testServer"
139 // End: