* VALUE is a string
*/
-enum Locations{
- LOC_WRITE_1 = RPL_FIRST_OF(RPLMODULE_CONFIG),
- LOC_WRITE_2,
- LOC_READ_1,
- LOC_READ_2,
+enum Locations {
+ LOC_WRITE_1 = RPL_FIRST_OF(RPLMODULE_CONFIG),
+ LOC_WRITE_2,
+ LOC_READ_1,
+ LOC_READ_2,
};
-
/**
* Constructor.
*
* @param logger NULL or a logger
*/
RplConfig::RplConfig(const char* file, bool readOnly, RplLogger* logger) :
- m_file(file),
- m_lineList(),
- m_readOnly(readOnly),
- m_logger(logger),
- m_ownLogger(logger == NULL)
-{
- if (logger == NULL){
- initLogger();
- }
- if (file != NULL)
- read(file);
+ m_file(file),
+ m_lineList(),
+ m_readOnly(readOnly),
+ m_logger(logger),
+ m_ownLogger(logger == NULL){
+ if (logger == NULL){
+ initLogger();
+ }
+ if (file != NULL)
+ read(file);
}
-
/**
* Destructor.
*
* Frees the resources.
*/
RplConfig::~RplConfig(){
- if (m_ownLogger)
- delete m_logger;
- m_logger = NULL;
+ if (m_ownLogger)
+ delete m_logger;
+ m_logger = NULL;
}
/**
* Inititializes a logger.
*/
void RplConfig::initLogger(){
- m_logger = new RplLogger();
- RplMemoryAppender* appender = new RplMemoryAppender();
- appender->setAutoDelete(true);
- m_logger->addAppender(appender);
-
- RplStreamAppender* appender2 = new RplStreamAppender(stdout);
- appender2->setAutoDelete(true);
- m_logger->addAppender(appender2);
+ m_logger = new RplLogger();
+ RplMemoryAppender* appender = new RplMemoryAppender();
+ appender->setAutoDelete(true);
+ m_logger->addAppender(appender);
+
+ RplStreamAppender* appender2 = new RplStreamAppender(stdout);
+ appender2->setAutoDelete(true);
+ m_logger->addAppender(appender2);
}
/**
* otherwise: the value assigned to key
*/
int RplConfig::asInt(const char* key, int defaultValue) const{
- int rc = defaultValue;
- if (contains(key)){
- rc = atoi((*this)[key]);
- }
- return rc;
+ int rc = defaultValue;
+ if (contains(key)){
+ rc = atoi((*this)[key]);
+ }
+ return rc;
}
/**
* otherwise: the value assigned to key
*/
bool RplConfig::asBool(const char* key, bool defaultValue) const{
- bool rc = defaultValue;
- if (contains(key)){
- QByteArray value = (*this)[key].toLower();
- rc = value == "1" || value == "y" || value == "yes" || value == "t" || value == "true";
- }
-
- return rc;
+ bool rc = defaultValue;
+ if (contains(key)){
+ QByteArray value = (*this)[key].toLower();
+ rc = value == "1" || value == "y" || value == "yes" || value == "t"
+ || value == "true";
+ }
+
+ return rc;
}
/**
* @return defaultValue: key does not exist
*/
QByteArray RplConfig::asString(const char* key, const char* defaultValue){
- QByteArray rc = defaultValue;
- if (contains(key)){
- rc = (*this)[key];
- }
- return rc;
+ QByteArray rc = defaultValue;
+ if (contains(key)){
+ rc = (*this)[key];
+ }
+ return rc;
}
/**
* false: error occurred
*/
bool RplConfig::read(const char* file){
- bool rc = true;
- m_lineList.reserve(1024);
- FILE* fp = fopen(file, "r");
- if (fp == NULL){
- m_logger->logv(LOG_ERROR, LOC_READ_1, "cannot read: %s", file);
- rc = false;
- } else{
- char line[64000];
- char* separator;
- int lineNo = 0;
- while(fgets(line, sizeof line, fp) != NULL){
- lineNo++;
- m_lineList.append(line);
- if(isalnum(line[0]) && (separator = strchr(line, '=')) != NULL){
- QByteArray key(line, separator - line);
- QByteArray value(separator + 1);
- key = key.trimmed();
- value = value.trimmed();
- if (contains(key))
- m_logger->logv(LOG_WARNING, LOC_READ_2, "defined more than once: %s-%d: %s",
- file, lineNo, line);
- else
- insert(key, value);
- }
- }
- }
- return rc;
+ bool rc = true;
+ m_lineList.reserve(1024);
+ FILE* fp = fopen(file, "r");
+ if (fp == NULL){
+ m_logger->logv(LOG_ERROR, LOC_READ_1, "cannot read: %s", file);
+ rc = false;
+ }else{
+ char line[64000];
+ char* separator;
+ int lineNo = 0;
+ while (fgets(line, sizeof line, fp) != NULL){
+ lineNo++;
+ m_lineList.append(line);
+ if (isalnum(line[0]) && (separator = strchr(line, '=')) != NULL){
+ QByteArray key(line, separator - line);
+ QByteArray value(separator + 1);
+ key = key.trimmed();
+ value = value.trimmed();
+ if (contains(key))
+ m_logger->logv(LOG_WARNING, LOC_READ_2,
+ "defined more than once: %s-%d: %s", file, lineNo, line);
+ else
+ insert(key, value);
+ }
+ }
+ }
+ return rc;
}
/**
* false: error occurred
*/
bool RplConfig::write(const char* file){
- bool rc = false;
- if (m_readOnly)
- m_logger->log(LOG_ERROR, LOC_WRITE_1, "cannot write: (readonly");
- else{
- m_logger->log(LOG_ERROR, LOC_WRITE_2, "not implemented: write()");
- }
- return rc;
+ bool rc = false;
+ if (m_readOnly)
+ m_logger->log(LOG_ERROR, LOC_WRITE_1, "cannot write: (readonly");
+ else{
+ m_logger->log(LOG_ERROR, LOC_WRITE_2, "not implemented: write()");
+ }
+ return rc;
}
// ------------------
/**
* @brief Unit test of RplConfig.
*/
-class TestRplConfig : public RplTest
-{
+class TestRplConfig: public RplTest {
public:
- TestRplConfig() : RplTest("RplConfig"){}
+ TestRplConfig() :
+ RplTest("RplConfig"){
+ }
public:
- void testBasic(){
- QByteArray fn = getTempFile("test.data", "config");
- RplString::write( fn, "#comment\na=1\nb.1==x\n#=\nB=zzz");
- RplConfig config(fn.constData());
- checkE(3, config.size());
- checkE("1", config["a"]);
- checkE("=x", config["b.1"]);
- checkE("zzz", config["B"]);
- }
- void testAsX(){
- QByteArray fn = getTempFile("test.data", "config");
- RplString::write( fn, "i=123\nb=1\nb2=true\nb3=yes\ns=abc");
- RplConfig config(fn.constData());
- checkE(5, config.size());
- checkE(123, config.asInt("i", -1));
- checkE(-1, config.asInt("I", -1));
- checkT(config.asBool("b", false));
- checkT(config.asBool("b2", false));
- checkT(config.asBool("b3", false));
- checkT(config.asBool("-", true));
- checkF(config.asBool("-", false));
- checkE("abc", config.asString("s", "x"));
- checkE("x", config.asString("S", "x"));
- }
-
- virtual void doIt(){
- testAsX();
- testBasic();
-
- }
+ void testBasic(){
+ QByteArray fn = getTempFile("test.data", "config");
+ RplString::write(fn, "#comment\na=1\nb.1==x\n#=\nB=zzz");
+ RplConfig config(fn.constData());
+ checkE(3, config.size());
+ checkE("1", config["a"]);
+ checkE("=x", config["b.1"]);
+ checkE("zzz", config["B"]);
+ }
+ void testAsX(){
+ QByteArray fn = getTempFile("test.data", "config");
+ RplString::write(fn, "i=123\nb=1\nb2=true\nb3=yes\ns=abc");
+ RplConfig config(fn.constData());
+ checkE(5, config.size());
+ checkE(123, config.asInt("i", -1));
+ checkE(-1, config.asInt("I", -1));
+ checkT(config.asBool("b", false));
+ checkT(config.asBool("b2", false));
+ checkT(config.asBool("b3", false));
+ checkT(config.asBool("-", true));
+ checkF(config.asBool("-", false));
+ checkE("abc", config.asString("s", "x"));
+ checkE("x", config.asString("S", "x"));
+ }
+
+ virtual void doIt(){
+ testAsX();
+ testBasic();
+
+ }
};
#endif
void testRplConfig(){
#ifdef RPL_TEST
- TestRplConfig test;
- test.run();
+ TestRplConfig test;
+ test.run();
#endif
}
enum {
LOC_1 = RPL_FIRST_OF(RPLMODULE_TCPCLIENT), // 701
+ LOC_HANDLE_ERROR_1,
};
/** @class RplTcpClient rpltcpclient.hpp "rplnet/rpltcpclient.hpp"
*/
RplTcpClient::RplTcpClient(const char* ip, int port, QThread* thread,
RplTerminator* terminator, RplLogger* logger) :
- m_peer(new RplTcpPeer(ip, port, thread, terminator, logger))
+ m_peer(new RplTcpPeer(ip, port, thread, terminator, logger)),
+ m_logger(logger)
{
if (ip != NULL && port != 0)
setRemoteAddress(ip, port);
m_peer->setSocket(NULL);
else{
socket = new QTcpSocket();
+ connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
+ this, SLOT(handleError(QAbstractSocket::SocketError)));
m_peer->setSocket(socket);
socket->connectToHost(QString(ip), port);
}
return m_peer;
}
+/**
+ * @brief Handles a network error.
+ *
+ * @param socketError the error code
+ */
+void RplTcpClient::handleError(QAbstractSocket::SocketError socketError){
+ m_logger->logv(LOG_ERROR, LOC_HANDLE_ERROR_1, "Network error %d",
+ socketError);
+}
+
/** @class RplClientThread rpltcpclient.hpp "rplnet/rpltcpclient.hpp"
*
* @brief Implements a thread usable for a tcp client.