Handle backendConfig

This commit is contained in:
HanzZ 2012-09-12 13:04:39 +02:00
parent 312879b94a
commit c538d9965b
5 changed files with 88 additions and 4 deletions

View file

@ -83,7 +83,8 @@ class Config {
bool reload();
bool hasKey(const std::string &key) {
return m_variables.find(key) != m_variables.end() || m_unregistered.find(key) != m_unregistered.end();
return (m_variables.find(key) != m_variables.end() || m_unregistered.find(key) != m_unregistered.end()
|| m_backendConfig.find(key) != m_backendConfig.end());
}
/// Returns value of variable defined by key.
@ -94,6 +95,9 @@ class Config {
if (m_variables.find(key) != m_variables.end()) {
return m_variables[key];
}
if (m_backendConfig.find(key) != m_backendConfig.end()) {
return m_backendConfig[key];
}
return m_unregistered[key];
}
@ -107,12 +111,15 @@ class Config {
/// This signal is emitted when config is loaded/reloaded.
boost::signal<void ()> onConfigReloaded;
void updateBackendConfig(const std::string &backendConfig);
static Config *createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port);
private:
int m_argc;
char **m_argv;
Variables m_variables;
Variables m_backendConfig;
std::map<std::string, boost::program_options::variable_value> m_unregistered;
std::string m_file;
std::string m_jid;

View file

@ -108,6 +108,7 @@ class NetworkPluginServer {
void handleFTFinishPayload(const std::string &payload);
void handleFTDataPayload(Backend *b, const std::string &payload);
void handleQueryPayload(Backend *b, const std::string &payload);
void handleBackendConfigPayload(const std::string &payload);
void handleUserCreated(User *user);
void handleRoomJoined(User *user, const Swift::JID &who, const std::string &room, const std::string &nickname, const std::string &password);

View file

@ -284,6 +284,20 @@ std::string Config::getCommandLineArgs() const {
return commandLineArgs.str();
}
void Config::updateBackendConfig(const std::string &backendConfig) {
options_description opts("Backend options");
opts.add_options()
("registration.needPassword", value<bool>()->default_value(true), "")
("registration.extraField", value<std::vector<std::string> >()->multitoken(), "")
;
std::stringstream ifs(backendConfig);
parsed_options parsed = parse_config_file(ifs, opts, true);
store(parsed, m_backendConfig);
notify(m_backendConfig);
}
Config *Config::createFromArgs(int argc, char **argv, std::string &error, std::string &host, int &port) {
std::string jid;
std::ostringstream os;

View file

@ -825,17 +825,27 @@ void NetworkPluginServer::handleQueryPayload(Backend *b, const std::string &data
msg->setBody(payload.config());
m_adminInterface->handleQuery(msg);
pbnetwork::BackendConfig vcard;
vcard.set_config(msg->getBody());
pbnetwork::BackendConfig response;
response.set_config(msg->getBody());
std::string message;
vcard.SerializeToString(&message);
response.SerializeToString(&message);
WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_QUERY);
send(b->connection, message);
}
void NetworkPluginServer::handleBackendConfigPayload(const std::string &data) {
pbnetwork::BackendConfig payload;
if (payload.ParseFromString(data) == false) {
// TODO: ERROR
return;
}
m_config->updateBackendConfig(payload.config());
}
void NetworkPluginServer::handleDataRead(Backend *c, boost::shared_ptr<Swift::SafeByteArray> data) {
// Append data to buffer
c->data.insert(c->data.end(), data->begin(), data->end());
@ -930,6 +940,9 @@ void NetworkPluginServer::handleDataRead(Backend *c, boost::shared_ptr<Swift::Sa
case pbnetwork::WrapperMessage_Type_TYPE_QUERY:
handleQueryPayload(c, wrapper.payload());
break;
case pbnetwork::WrapperMessage_Type_TYPE_BACKEND_CONFIG:
handleBackendConfigPayload(wrapper.payload());
break;
default:
return;
}

49
src/tests/config.cpp Normal file
View file

@ -0,0 +1,49 @@
#include "transport/userregistry.h"
#include "transport/config.h"
#include "transport/storagebackend.h"
#include "transport/user.h"
#include "transport/transport.h"
#include "transport/conversation.h"
#include "transport/usermanager.h"
#include "transport/localbuddy.h"
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Swiften/Swiften.h>
#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Server/Server.h>
#include <Swiften/Network/DummyNetworkFactories.h>
#include <Swiften/Network/DummyConnectionServer.h>
#include "Swiften/Server/ServerStanzaChannel.h"
#include "Swiften/Server/ServerFromClientSession.h"
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
#include "basictest.h"
#include "transport/util.h"
using namespace Transport;
class ConfigTest : public CPPUNIT_NS :: TestFixture{
CPPUNIT_TEST_SUITE(ConfigTest);
CPPUNIT_TEST(updateBackendConfig);
CPPUNIT_TEST_SUITE_END();
public:
void setUp (void) {
}
void tearDown (void) {
}
void updateBackendConfig() {
Config cfg;
CPPUNIT_ASSERT(!cfg.hasKey("registration.needPassword"));
cfg.updateBackendConfig("[registration]\nneedPassword=0\n");
CPPUNIT_ASSERT(cfg.hasKey("registration.needPassword"));
CPPUNIT_ASSERT_EQUAL(false, CONFIG_BOOL(&cfg, "registration.needPassword"));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION (ConfigTest);