Move random port generating to util.cpp

This commit is contained in:
HanzZ 2011-12-09 13:51:50 +01:00
parent 1dafed2e6b
commit 4c334e9f1c
7 changed files with 24 additions and 18 deletions

View file

@ -40,6 +40,8 @@ std::string serializeGroups(const std::vector<std::string> &groups);
std::vector<std::string> deserializeGroups(std::string &groups);
int getRandomPort(const std::string &s);
}
}

View file

@ -24,7 +24,9 @@ port = 5222
backend_host = localhost
# Port on which Spectrum listens for backends.
backend_port=10001
# By default Spectrum chooses random backend port and there's
# no need to change it normally
#backend_port=10001
# Full path to PKCS#12 cetficiate used for TLS in server mode.
#cert=

View file

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 2.6)
FILE(GLOB SRC *.cpp)
ADD_EXECUTABLE(spectrum2_manager ${SRC} ../../src/config.cpp)
ADD_EXECUTABLE(spectrum2_manager ${SRC} ../../src/config.cpp ../../src/util.cpp)
target_link_libraries(spectrum2_manager ${SWIFTEN_LIBRARY})

View file

@ -19,6 +19,7 @@
*/
#include "transport/config.h"
#include "transport/util.h"
#include <fstream>
#ifdef _MSC_VER
#include <direct.h>
@ -120,13 +121,7 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
else if (opt.string_key == "service.backend_port") {
found_backend_port = true;
if (opt.value[0] == "0") {
unsigned long r = 0;
BOOST_FOREACH(char c, _jid) {
r += (int) c;
}
srand(time(NULL) + r);
int randomPort = 30000 + rand() % 10000;
opt.value[0] = boost::lexical_cast<std::string>(randomPort);
opt.value[0] = boost::lexical_cast<std::string>(Util::getRandomPort(_jid.empty() ? jid : _jid));
}
}
else if (opt.string_key == "service.working_dir") {
@ -148,14 +143,9 @@ bool Config::load(std::istream &ifs, boost::program_options::options_description
parsed.options.push_back(boost::program_options::basic_option<char>("service.pidfile", value));
}
if (!found_backend_port) {
unsigned long r = 0;
BOOST_FOREACH(char c, _jid) {
r += (int) c;
}
srand(time(NULL) + r);
int randomPort = 30000 + rand() % 10000;
std::vector<std::string> value;
value.push_back(boost::lexical_cast<std::string>(randomPort));
std::string p = boost::lexical_cast<std::string>(Util::getRandomPort(_jid.empty() ? jid : _jid));
value.push_back(p);
parsed.options.push_back(boost::program_options::basic_option<char>("service.backend_port", value));
}

View file

@ -19,6 +19,7 @@
*/
#include "transport/presenceoracle.h"
#include "Swiften/Swiften.h"
#include <boost/bind.hpp>
@ -46,9 +47,10 @@ void PresenceOracle::handleStanzaChannelAvailableChanged(bool available) {
void PresenceOracle::handleIncomingPresence(Presence::ref presence) {
// ignore presences for some contact, we're checking only presences for the transport itself here.
if (!presence->getTo().getNode().empty()) {
bool isMUC = presence->getPayload<MUCPayload>() != NULL || *presence->getTo().getNode().c_str() == '#';
// filter out login/logout presence spam
if (!presence->getTo().getNode().empty() && isMUC == false)
return;
}
JID bareJID(presence->getFrom().toBare());
if (presence->getType() == Presence::Subscribe) {

View file

@ -45,6 +45,7 @@
#include "log4cxx/consoleappender.h"
#include "log4cxx/patternlayout.h"
#include "log4cxx/propertyconfigurator.h"
#include "Swiften/Swiften.h"
using namespace Swift;
using namespace boost;

View file

@ -128,6 +128,15 @@ std::vector<std::string> deserializeGroups(std::string &groups) {
return ret;
}
int getRandomPort(const std::string &s) {
unsigned long r = 0;
BOOST_FOREACH(char c, s) {
r += (int) c;
}
srand(time(NULL) + r);
return 30000 + rand() % 10000;
}
}
}