Commit e975323c authored by Matthieu Kermagoret's avatar Matthieu Kermagoret
Browse files

Connector SSH: made commander a singleton.

parent 184e701d
......@@ -173,6 +173,14 @@ if (WITH_TESTING)
target_link_libraries("${TEST_NAME}" "${CONNECTORLIB}")
add_test("${TEST_NAME}" "${TEST_NAME}")
# commander tests.
# Check singleton.
set(TEST_NAME "commander_singleton")
add_executable("${TEST_NAME}"
"${TEST_DIR}/commander/singleton.cc")
target_link_libraries("${TEST_NAME}" "${CONNECTORLIB}")
add_test("${TEST_NAME}" "${TEST_NAME}")
else()
set(BUILD_UNIT_TEST "No")
endif ()
......
......@@ -21,6 +21,7 @@
#ifndef CCCS_COMMANDER_HH
# define CCCS_COMMANDER_HH
# include <memory>
# include <string>
# include "com/centreon/connector/ssh/namespace.hh"
# include "com/centreon/handle_listener.hh"
......@@ -35,30 +36,33 @@ CCCS_BEGIN()
*
* Handle check execution command from the monitoring engine.
*/
class commander : public handle_listener {
class commander : public handle_listener {
public:
commander();
~commander() throw ();
void close(handle& h);
void error(handle& h);
void read(handle& h);
void reg();
void unreg(bool all = true);
bool want_read(handle& h);
bool want_write(handle& h);
void write(handle& h);
~commander() throw ();
void close(handle& h);
void error(handle& h);
static commander& instance();
static void load();
void read(handle& h);
void reg();
static void unload();
void unreg(bool all = true);
bool want_read(handle& h);
bool want_write(handle& h);
void write(handle& h);
private:
commander(commander const& c);
commander& operator=(commander const& c);
void _parse(std::string const& cmd);
commander();
commander(commander const& c);
commander& operator=(commander const& c);
void _parse(std::string const& cmd);
std::string _rbuffer;
io::standard_input
_si;
io::standard_output
_so;
std::string _wbuffer;
static std::auto_ptr<commander>
_instance;
std::string _rbuffer;
io::standard_input _si;
io::standard_output _so;
std::string _wbuffer;
};
CCCS_END()
......
......@@ -32,23 +32,19 @@
using namespace com::centreon;
using namespace com::centreon::connector::ssh;
// Class instance.
std::auto_ptr<commander> commander::_instance;
/**************************************
* *
* Public Methods *
* *
**************************************/
/**
* Default constructor.
*/
commander::commander() {}
/**
* Destructor.
*/
commander::~commander() throw () {
unreg();
}
commander::~commander() throw () {}
/**
* Close callback.
......@@ -82,6 +78,23 @@ void commander::error(handle& h) {
return ;
}
/**
* Get class instance.
*
* @return Class instance.
*/
commander& commander::instance() {
return (*_instance);
}
/**
* Load singleton.
*/
void commander::load() {
_instance.reset(new commander);
return ;
}
/**
* Read callback.
*
......@@ -134,6 +147,14 @@ void commander::reg() {
return ;
}
/**
* Unload singleton.
*/
void commander::unload() {
_instance.reset();
return ;
}
/**
* Unregister commander with multiplexer.
*
......@@ -183,6 +204,11 @@ void commander::write(handle& h) {
* *
**************************************/
/**
* Default constructor.
*/
commander::commander() {}
/**
* @brief Copy constructor.
*
......
......@@ -61,6 +61,7 @@ int main() {
// Initializations.
logging::engine::load();
multiplexer::load();
commander::load();
#if LIBSSH2_VERSION_NUM >= 0x010205
// Initialize libssh2.
......@@ -74,8 +75,7 @@ int main() {
signal(SIGTERM, term_handler);
// Listener of commands.
commander c;
c.reg();
commander::instance().reg();
// Multiplexing loop.
logging::info(logging::medium) << "starting multiplexing loop";
......@@ -86,14 +86,14 @@ int main() {
// Remove command listener on input.
logging::debug(logging::high)
<< "commander will stop listening on input";
c.unreg(false);
commander::instance().unreg(false);
// Wait for remaining sessions.
// XXX : multiplexer.remaining() > 1 || multiplexer.want_write()
// Remove command listener totally.
logging::debug(logging::high) << "removing command listener";
c.unreg();
commander::instance().unreg();
// Set return value.
retval = EXIT_SUCCESS;
......@@ -108,6 +108,7 @@ int main() {
#endif /* libssh2 version >= 1.2.5 */
// Deinitializations.
commander::unload();
multiplexer::unload();
logging::engine::unload();
......
/*
** Copyright 2011 Merethis
**
** This file is part of Centreon Connector SSH.
**
** Centreon Connector SSH is free software: you can redistribute it
** and/or modify it under the terms of the GNU Affero General Public
** License as published by the Free Software Foundation, either version
** 3 of the License, or (at your option) any later version.
**
** Centreon Connector SSH is distributed in the hope that it will be
** useful, but WITHOUT ANY WARRANTY; without even the implied warranty
** of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public
** License along with Centreon Connector SSH. If not, see
** <http://www.gnu.org/licenses/>.
*/
#include "com/centreon/connector/ssh/commander.hh"
using namespace com::centreon::connector::ssh;
/**
* Check that the commander singleton works properly.
*
* @return 0 on success.
*/
int main() {
// Return value.
int retval(0);
// By default, commander is not loaded.
retval |= (&commander::instance() != NULL);
// Load commander.
commander::load();
retval |= (&commander::instance() == NULL);
// Unload commander.
commander::unload();
retval |= (&commander::instance() != NULL);
// Return check result.
return (retval);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment