switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / ModuleManager.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 ModuleManager non-inline non-template implementation */
30
31 #include "ModuleManager.hh"
32 //#include "ModuleManager.ih"
33
34 // Custom includes
35 #include <senf/Utils/membind.hh>
36 #include <senf/Utils/Console/ParsedCommand.hh>
37 #include <senf/Utils/Console/Sysdir.hh>
38 #include "Module.hh"
39
40 //#include "ModuleManager.mpp"
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 // senf::ppi::ModuleManager
46
47 prefix_ void senf::ppi::ModuleManager::init()
48 {
49     while (! initQueue_.empty()) {
50         initQueue_.front()->v_init();
51         initQueue_.pop_front();
52     }
53     initRunner_.disable();
54 }
55
56 #ifndef DOXYGEN
57
58 struct senf::ppi::ModuleManager::RunGuard
59 {
60     RunGuard(ModuleManager & m) : manager(m) { manager.running_ = true; }
61     ~RunGuard() { manager.running_ = false; }
62     ModuleManager & manager;
63 };
64
65 #endif
66
67 prefix_ void senf::ppi::ModuleManager::run()
68 {
69     init();
70     RunGuard guard (*this);
71     scheduler::process();
72 }
73
74 //-/////////////////////////////////////////////////////////////////////////////////////////////////
75 // private members
76
77 prefix_ senf::ppi::ModuleManager::ModuleManager()
78     : running_(false), terminate_(false),
79       initRunner_ ("senf::ppi::init", membind(&ModuleManager::init, this),
80                    scheduler::EventHook::PRE, false)
81 {
82     console::sysdir().add("ppi", consoleDir_);
83
84     consoleDir_
85         .add("dump", console::factory::Command(
86                  senf::membind(&ModuleManager::dumpModules, this))
87              .doc("Dump complete PPI structure\n"
88                   "The dump will contain one paragraph for each module. The first line gives module\n"
89                   "information, additional lines list all connectors and their peers (if connected).\n"
90                   "\n"
91                   "This information can be processed by 'tools/drawmodules.py' and 'dot' (from the\n"
92                   "graphviz package) to generate a graphic representation of the module structure:\n"
93                   "\n"
94                   "    $ echo /sys/ppi/dump | nc -q1 <host> <port> \\\n"
95                   "          | python tools/drawmodules.py | dot -Tpng /dev/fd/0 >modules.png\n")
96             );
97 }
98
99 prefix_ void senf::ppi::ModuleManager::dumpModules(std::ostream & os)
100     const
101 {
102     for (ModuleRegistry::const_iterator i (moduleRegistry_.begin()), i_end (moduleRegistry_.end());
103          i != i_end; ++i) {
104         os << *i << " " << prettyName(typeid(**i)) << "\n";
105         for (module::Module::ConnectorRegistry::iterator j ((*i)->connectorRegistry_.begin()),
106                  j_end ((*i)->connectorRegistry_.end()); j != j_end; ++j) {
107             os << "  " << *j << " " << prettyName(typeid(**j));
108             if ((**j).connected()) {
109                 os << " " << & (*j)->peer();
110                 connector::PassiveConnector * pc (dynamic_cast<connector::PassiveConnector *>(*j));
111                 if (pc && pc->throttled())
112                     os << " throttled";
113             }
114             os << "\n";
115         }
116         os << "\n";
117     }
118 }
119
120 //-/////////////////////////////////////////////////////////////////////////////////////////////////
121 #undef prefix_
122 //#include "ModuleManager.mpp"
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: