start switching to BusHandler and MessageMap, move raw logging and dumping to Port and avoid permanent open/close on ofstream
This commit is contained in:
@@ -6,8 +6,8 @@ AM_CXXFLAGS = -fpic \
|
|||||||
|
|
||||||
bin_PROGRAMS = ebusd
|
bin_PROGRAMS = ebusd
|
||||||
|
|
||||||
ebusd_SOURCES = busloop.cpp \
|
ebusd_SOURCES = bushandler.cpp \
|
||||||
busloop.h \
|
bushandler.h \
|
||||||
network.cpp \
|
network.cpp \
|
||||||
network.h \
|
network.h \
|
||||||
baseloop.cpp \
|
baseloop.cpp \
|
||||||
|
|||||||
+103
-35
@@ -18,9 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "baseloop.h"
|
#include "baseloop.h"
|
||||||
#include "configfile.h"
|
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "appl.h"
|
#include "appl.h"
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -30,15 +30,42 @@ extern Appl& A;
|
|||||||
BaseLoop::BaseLoop()
|
BaseLoop::BaseLoop()
|
||||||
{
|
{
|
||||||
// create commands DB
|
// create commands DB
|
||||||
m_commands = ConfigCommands(A.getOptVal<const char*>("ebusconfdir"), ft_csv).getCommands();
|
m_templates = new DataFieldTemplates();
|
||||||
L.log(bas, trace, "ebus configuration dir: %s", A.getOptVal<const char*>("ebusconfdir"));
|
m_messages = new MessageMap();
|
||||||
L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB());
|
|
||||||
L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB());
|
|
||||||
L.log(bas, event, " polling DB: %d ", m_commands->sizePollDB());
|
|
||||||
|
|
||||||
// create busloop
|
string confdir = A.getOptVal<const char*>("ebusconfdir");
|
||||||
m_busloop = new BusLoop(m_commands);
|
L.log(bas, trace, "ebus configuration dir: %s", confdir.c_str());
|
||||||
m_busloop->start("busloop");
|
result_t result = m_templates->readFromFile(confdir+"/_types.csv");
|
||||||
|
if (result == RESULT_OK)
|
||||||
|
L.log(bas, trace, "read templates");
|
||||||
|
else
|
||||||
|
L.log(bas, error, "error reading templates: %s", getResultCode(result));
|
||||||
|
result = readConfigFiles(confdir, ".csv");
|
||||||
|
if (result == RESULT_OK)
|
||||||
|
L.log(bas, trace, "read config files");
|
||||||
|
else
|
||||||
|
L.log(bas, error, "error reading config files: %s", getResultCode(result));
|
||||||
|
|
||||||
|
/*L.log(bas, event, "commands DB: %d ", m_commands->sizeCmdDB());
|
||||||
|
L.log(bas, event, " cycle DB: %d ", m_commands->sizeCycDB());
|
||||||
|
L.log(bas, event, " polling DB: %d ", m_commands->sizePollDB());*/
|
||||||
|
|
||||||
|
const bool logRaw = A.getOptVal<bool>("lograwdata");
|
||||||
|
|
||||||
|
const bool dumpRaw = A.getOptVal<bool>("dump");
|
||||||
|
const char* dumpRawFile = A.getOptVal<const char*>("dumpfile");
|
||||||
|
const long dumpRawMaxSize = A.getOptVal<long>("dumpsize");
|
||||||
|
|
||||||
|
// create Port
|
||||||
|
m_port = new Port(A.getOptVal<const char*>("device"), A.getOptVal<bool>("nodevicecheck"), logRaw, &L, dumpRaw, dumpRawFile, dumpRawMaxSize);
|
||||||
|
m_port->open();
|
||||||
|
|
||||||
|
if (m_port->isOpen() == false)
|
||||||
|
L.log(bus, error, "can't open %s", A.getOptVal<const char*>("device"));
|
||||||
|
|
||||||
|
// create BusHandler
|
||||||
|
m_busHandler = new BusHandler(m_port, m_messages, SYN, SYN); // TODO
|
||||||
|
m_busHandler->start("bushandler");
|
||||||
|
|
||||||
// create network
|
// create network
|
||||||
m_network = new Network(A.getOptVal<bool>("localhost"), &m_netQueue);
|
m_network = new Network(A.getOptVal<bool>("localhost"), &m_netQueue);
|
||||||
@@ -47,22 +74,63 @@ BaseLoop::BaseLoop()
|
|||||||
|
|
||||||
BaseLoop::~BaseLoop()
|
BaseLoop::~BaseLoop()
|
||||||
{
|
{
|
||||||
// free network
|
|
||||||
if (m_network != NULL)
|
if (m_network != NULL)
|
||||||
delete m_network;
|
delete m_network;
|
||||||
|
|
||||||
// free busloop
|
if (m_busHandler != NULL) {
|
||||||
if (m_busloop != NULL) {
|
m_busHandler->stop();
|
||||||
m_busloop->stop();
|
m_busHandler->join();
|
||||||
m_busloop->join();
|
delete m_busHandler;
|
||||||
delete m_busloop;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// free commands DB
|
if (m_port != NULL)
|
||||||
if (m_commands != NULL)
|
delete m_port;
|
||||||
delete m_commands;
|
|
||||||
|
if (m_messages != NULL)
|
||||||
|
delete m_messages;
|
||||||
|
|
||||||
|
if (m_templates != NULL)
|
||||||
|
delete m_templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result_t BaseLoop::readConfigFiles(const string path, const string extension)
|
||||||
|
{
|
||||||
|
DIR* dir = opendir(path.c_str());
|
||||||
|
|
||||||
|
if (dir == NULL)
|
||||||
|
return RESULT_ERR_FILENOTFOUND;
|
||||||
|
|
||||||
|
dirent* d = readdir(dir);
|
||||||
|
|
||||||
|
while (d != NULL) {
|
||||||
|
if (d->d_type == DT_DIR) {
|
||||||
|
string fn = d->d_name;
|
||||||
|
//std::cout << "found dir " << fn << endl;
|
||||||
|
if (fn != "." && fn != "..") {
|
||||||
|
const string p = path + "/" + d->d_name;
|
||||||
|
result_t result = readConfigFiles(p, extension);
|
||||||
|
if (result != RESULT_OK)
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
} else if (d->d_type == DT_REG) {
|
||||||
|
string fn = d->d_name;
|
||||||
|
//std::cout << "found file " << fn << endl;
|
||||||
|
if (fn.find(extension, (fn.length() - extension.length())) != string::npos
|
||||||
|
&& fn != "_types" + extension) {
|
||||||
|
const string p = path + "/" + d->d_name;
|
||||||
|
result_t result = m_messages->readFromFile(p, m_templates);
|
||||||
|
if (result != RESULT_OK)
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
d = readdir(dir);
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
|
|
||||||
|
return RESULT_OK;
|
||||||
|
};
|
||||||
|
|
||||||
void BaseLoop::start()
|
void BaseLoop::start()
|
||||||
{
|
{
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -100,7 +168,7 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
{
|
{
|
||||||
ostringstream result;
|
ostringstream result;
|
||||||
string cycdata, polldata;
|
string cycdata, polldata;
|
||||||
int index;
|
Message* message;
|
||||||
|
|
||||||
// prepare data
|
// prepare data
|
||||||
string token;
|
string token;
|
||||||
@@ -118,15 +186,15 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
result << "command not found";
|
result << "command not found";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ct_get:
|
/*case ct_get:
|
||||||
if (cmd.size() < 3 || cmd.size() > 4) {
|
if (cmd.size() < 3 || cmd.size() > 4) {
|
||||||
result << "usage: 'get class cmd (sub)'";
|
result << "usage: 'get class cmd (sub)'";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
index = m_commands->findCommand(data);
|
message = m_messages->find(cmd[1], cmd[2], true, false);
|
||||||
|
|
||||||
if (index >= 0) {
|
if (message != NULL) {
|
||||||
|
|
||||||
// polling data
|
// polling data
|
||||||
if (strcasecmp(m_commands->getCmdType(index).c_str(), "P") == 0) {
|
if (strcasecmp(m_commands->getCmdType(index).c_str(), "P") == 0) {
|
||||||
@@ -176,9 +244,9 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
result << "ebus command not found";
|
result << "ebus command not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;*/
|
||||||
|
|
||||||
case ct_set:
|
/*case ct_set:
|
||||||
if (cmd.size() != 4) {
|
if (cmd.size() != 4) {
|
||||||
result << "usage: 'set class cmd value'";
|
result << "usage: 'set class cmd value'";
|
||||||
break;
|
break;
|
||||||
@@ -231,9 +299,9 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
result << "ebus command not found";
|
result << "ebus command not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;*/
|
||||||
|
|
||||||
case ct_cyc:
|
/*case ct_cyc:
|
||||||
if (cmd.size() < 3 || cmd.size() > 4) {
|
if (cmd.size() < 3 || cmd.size() > 4) {
|
||||||
result << "usage: 'cyc class cmd (sub)'";
|
result << "usage: 'cyc class cmd (sub)'";
|
||||||
break;
|
break;
|
||||||
@@ -259,9 +327,9 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
result << "ebus command not found";
|
result << "ebus command not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;*/
|
||||||
|
|
||||||
case ct_hex:
|
/*case ct_hex:
|
||||||
if (cmd.size() != 2) {
|
if (cmd.size() != 2) {
|
||||||
result << "usage: 'hex value' (value: ZZPBSBNNDx)";
|
result << "usage: 'hex value' (value: ZZPBSBNNDx)";
|
||||||
break;
|
break;
|
||||||
@@ -289,9 +357,9 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
delete message;
|
delete message;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;*/
|
||||||
|
|
||||||
case ct_scan:
|
/*case ct_scan:
|
||||||
if (cmd.size() == 1) {
|
if (cmd.size() == 1) {
|
||||||
m_busloop->scan();
|
m_busloop->scan();
|
||||||
result << "done";
|
result << "done";
|
||||||
@@ -315,7 +383,7 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
result << "usage: 'scan'" << endl
|
result << "usage: 'scan'" << endl
|
||||||
<< " 'scan full'" << endl
|
<< " 'scan full'" << endl
|
||||||
<< " 'scan result'";
|
<< " 'scan result'";
|
||||||
break;
|
break;*/
|
||||||
|
|
||||||
case ct_log:
|
case ct_log:
|
||||||
if (cmd.size() != 3 ) {
|
if (cmd.size() != 3 ) {
|
||||||
@@ -348,7 +416,7 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_busloop->raw();
|
m_port->setLogRaw(!m_port->getLogRaw());
|
||||||
result << "done";
|
result << "done";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -358,11 +426,11 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_busloop->dump();
|
m_port->setDumpRaw(!m_port->getDumpRaw());
|
||||||
result << "done";
|
result << "done";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ct_reload:
|
/*case ct_reload:
|
||||||
if (cmd.size() != 1) {
|
if (cmd.size() != 1) {
|
||||||
result << "usage: 'reload'";
|
result << "usage: 'reload'";
|
||||||
break;
|
break;
|
||||||
@@ -382,7 +450,7 @@ string BaseLoop::decodeMessage(const string& data)
|
|||||||
|
|
||||||
result << "done";
|
result << "done";
|
||||||
break;
|
break;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
case ct_help:
|
case ct_help:
|
||||||
result << "commands:" << endl
|
result << "commands:" << endl
|
||||||
|
|||||||
+16
-8
@@ -20,9 +20,9 @@
|
|||||||
#ifndef BASELOOP_H_
|
#ifndef BASELOOP_H_
|
||||||
#define BASELOOP_H_
|
#define BASELOOP_H_
|
||||||
|
|
||||||
#include "commands.h"
|
#include "message.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
#include "busloop.h"
|
#include "bushandler.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ class BaseLoop
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief construct the baseloop and creates commads, network and busloop subsystems.
|
* @brief construct the baseloop and creates messaging, network and busloop subsystems.
|
||||||
*/
|
*/
|
||||||
BaseLoop();
|
BaseLoop();
|
||||||
|
|
||||||
@@ -60,6 +60,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
~BaseLoop();
|
~BaseLoop();
|
||||||
|
|
||||||
|
result_t readConfigFiles(const string path, const string extension);
|
||||||
/**
|
/**
|
||||||
* @brief start baseloop instance.
|
* @brief start baseloop instance.
|
||||||
*/
|
*/
|
||||||
@@ -72,13 +73,20 @@ public:
|
|||||||
void addMessage(NetMessage* message) { m_netQueue.add(message); }
|
void addMessage(NetMessage* message) { m_netQueue.add(message); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** the commands instance */
|
|
||||||
Commands* m_commands;
|
|
||||||
|
|
||||||
/** the busloop instance */
|
/** the @a DataFieldTemplates instance. */
|
||||||
BusLoop* m_busloop;
|
DataFieldTemplates* m_templates;
|
||||||
|
|
||||||
/** the network instance */
|
/** the @a MessageMap instance. */
|
||||||
|
MessageMap* m_messages;
|
||||||
|
|
||||||
|
/** the @a Port instance. */
|
||||||
|
Port* m_port;
|
||||||
|
|
||||||
|
/** the @a BusHandler instance. */
|
||||||
|
BusHandler* m_busHandler;
|
||||||
|
|
||||||
|
/** the @a Network instance. */
|
||||||
Network* m_network;
|
Network* m_network;
|
||||||
|
|
||||||
/** queue for network messages */
|
/** queue for network messages */
|
||||||
|
|||||||
Reference in New Issue
Block a user