code format
This commit is contained in:
@@ -1,43 +1,44 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Complex I/O point
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Complex I/O point
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 Lesser General Public License for more details.
|
||||
This program 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 Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piconnection.h"
|
||||
|
||||
#include "piconfig.h"
|
||||
#include "piiostream.h"
|
||||
|
||||
/** \class PIConnection
|
||||
* \brief Complex Input/Output point
|
||||
*
|
||||
*
|
||||
* \section PIConnection_synopsis Synopsis
|
||||
* %PIConnection provides abstract layer over physical devices,
|
||||
* filtering and connecting data streams. Each %PIConnection
|
||||
* works through Device Pool, so several %PIConnections can
|
||||
* read from single physical device. General scheme:
|
||||
* \image html piconnection.png
|
||||
*
|
||||
*
|
||||
* \section PIConnection_pool Device pool concept
|
||||
* Device pool is static object, single for each application, which
|
||||
* contains unique devices. Each %PIConnection works with real devices
|
||||
* through Device pool. Each device has assosiated thread for read
|
||||
* and it can be started or stopped with %PIConnection functions
|
||||
* \a startThreadedRead() and \a stopThreadedRead().
|
||||
*
|
||||
*
|
||||
* \section PIConnection_filters Filters
|
||||
* %PIConnection filter is a PIPacketExtractor and assosiated
|
||||
* array of devices or other filters. When read thread is successfully read
|
||||
@@ -48,11 +49,11 @@
|
||||
* One filter can receive data from several sources, and can be bounded to
|
||||
* several filters.
|
||||
* \image html piconnection_filters.png
|
||||
*
|
||||
*
|
||||
* \section PIConnection_diag Diagnostics
|
||||
* %PIConnection create PIDiagnostics for each device or filter. You can
|
||||
* access to these objects with functions \a diagnostic().
|
||||
*
|
||||
*
|
||||
* \section PIConnection_sender Senders
|
||||
* %PIConnection can send data to devices with named timers ("senders").
|
||||
* You can create sender or add device to sender with function \a addSender().
|
||||
@@ -61,7 +62,7 @@
|
||||
* You can assign fixed send data to sender with function \a setSenderFixedData().
|
||||
* In this case sender will NOT execute \a senderData(), but send assigned data.
|
||||
* \image html piconnection_senders.png
|
||||
*
|
||||
*
|
||||
* \section PIConnection_config Configuration
|
||||
* You can create %PIConnection from config file section or configure
|
||||
* it later with function \a configureFromConfig(). Devices describes
|
||||
@@ -70,11 +71,11 @@
|
||||
* Also %PIConnection can create PIString with its configuration with
|
||||
* function \a makeConfig(). This string can be directly inserted into the
|
||||
* config file.
|
||||
*
|
||||
*/
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
PIVector<PIConnection * > PIConnection::_connections;
|
||||
PIVector<PIConnection *> PIConnection::_connections;
|
||||
|
||||
|
||||
PIConnection::PIConnection(const PIString & name): PIObject(name) {
|
||||
@@ -123,20 +124,20 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
setName(name_);
|
||||
if (name_.isEmpty()) piCoutObj << "Warning, can't configure connection with empty name";
|
||||
PIConfig::Entry ce(conf.getValue(name_));
|
||||
PIConfig::Branch db(ce.getValue("device").children()), fb(ce.getValue("filter").children()),
|
||||
cb(ce.getValue("channel").children()), sb(ce.getValue("sender").children());
|
||||
PIConfig::Branch db(ce.getValue("device").children()), fb(ce.getValue("filter").children()), cb(ce.getValue("channel").children()),
|
||||
sb(ce.getValue("sender").children());
|
||||
PIStringList dev_list(ce.getValue("device").toString());
|
||||
PIStringList name_list(ce.getValue("device").name());
|
||||
PIStringList flt_list(ce.getValue("filter").toString());
|
||||
for (const PIConfig::Entry * e : db) {
|
||||
for (const PIConfig::Entry * e: db) {
|
||||
dev_list << e->value();
|
||||
name_list << e->name();
|
||||
}
|
||||
for (const PIConfig::Entry * e : fb) {
|
||||
for (const PIConfig::Entry * e: fb) {
|
||||
flt_list << e->name();
|
||||
}
|
||||
PISet<PIString> chk_set = (PISet<PIString>(name_list) & PISet<PIString>(flt_list));
|
||||
//piCout << name_list << flt_list << chk_set;
|
||||
// piCout << name_list << flt_list << chk_set;
|
||||
chk_set.remove("");
|
||||
if (!chk_set.isEmpty()) {
|
||||
piCoutObj << "Error," << chk_set.toVector() << "names assigned to both devices and filters!";
|
||||
@@ -149,8 +150,8 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
PIString & n(name_list[i]);
|
||||
PIIODevice::DeviceMode dm = PIIODevice::ReadWrite;
|
||||
PIIODevice::splitFullPath(fn, &fn, &dm);
|
||||
//piCout << fn;
|
||||
//piCoutObj << "add" << fn << n;
|
||||
// piCout << fn;
|
||||
// piCoutObj << "add" << fn << n;
|
||||
PIIODevice * dev = addDevice(fn, dm);
|
||||
if (!dev) continue;
|
||||
dev_aliases[n] = fn;
|
||||
@@ -167,13 +168,14 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
PIStringList filter_fails;
|
||||
while (added != padded && tries < 100) {
|
||||
padded = added;
|
||||
added = 0;
|
||||
added = 0;
|
||||
++tries;
|
||||
for (const PIConfig::Entry * e : fb) {
|
||||
for (const PIConfig::Entry * e: fb) {
|
||||
PIPacketExtractor::SplitMode sm = PIPacketExtractor::None;
|
||||
PIString sms(e->getValue("splitMode").value());
|
||||
int smi = sms.toInt();
|
||||
if (smi >= 1 && smi <= 5) sm = (PIPacketExtractor::SplitMode)smi;
|
||||
if (smi >= 1 && smi <= 5)
|
||||
sm = (PIPacketExtractor::SplitMode)smi;
|
||||
else {
|
||||
sms = sms.trim().toLowerCase();
|
||||
if (sms.find("header") >= 0 && sms.find("footer") >= 0)
|
||||
@@ -188,8 +190,7 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
if (sms.find("time") >= 0)
|
||||
sm = PIPacketExtractor::Timeout;
|
||||
else {
|
||||
if (sms.find("size") >= 0)
|
||||
sm = PIPacketExtractor::Size;
|
||||
if (sms.find("size") >= 0) sm = PIPacketExtractor::Size;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -198,12 +199,12 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
PIStringList devs(e->value());
|
||||
PIConfig::Branch db(e->getValue("device").children());
|
||||
devs << e->getValue("device", "").value();
|
||||
for (const PIConfig::Entry * e2 : db) {
|
||||
for (const PIConfig::Entry * e2: db) {
|
||||
devs << e2->value();
|
||||
}
|
||||
devs.removeStrings("");
|
||||
if (devs.isEmpty()) continue;
|
||||
PIString dname = dev_aliases.value(devs.front(), devs.front());
|
||||
PIString dname = dev_aliases.value(devs.front(), devs.front());
|
||||
PIPacketExtractor * pe = addFilter(e->name(), dname, sm);
|
||||
if (!pe) {
|
||||
if (!filter_fails.contains(dname)) filter_fails << dname;
|
||||
@@ -218,8 +219,7 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
filter_fails.removeAll(dname);
|
||||
++added;
|
||||
} else {
|
||||
if (!filter_fails.contains(dname))
|
||||
filter_fails << dname;
|
||||
if (!filter_fails.contains(dname)) filter_fails << dname;
|
||||
}
|
||||
}
|
||||
PIDiagnostics * diag = diags_.value(pe, nullptr);
|
||||
@@ -231,24 +231,24 @@ bool PIConnection::configure(PIConfig & conf, const PIString & name_) {
|
||||
}
|
||||
}
|
||||
setDebug(pdebug);
|
||||
for (const PIString & f : filter_fails) {
|
||||
for (const PIString & f: filter_fails) {
|
||||
piCoutObj << "\"addFilter\" error: no such device \"" << f << "\"!";
|
||||
}
|
||||
for (const PIConfig::Entry * e : cb) {
|
||||
for (const PIConfig::Entry * e: cb) {
|
||||
PIString f(e->getValue("from").value()), t(e->getValue("to").value());
|
||||
addChannel(dev_aliases.value(f, f), dev_aliases.value(t, t));
|
||||
}
|
||||
for (const PIConfig::Entry * e : sb) {
|
||||
for (const PIConfig::Entry * e: sb) {
|
||||
PIStringList devs(e->value());
|
||||
PIConfig::Branch db(e->getValue("device").children());
|
||||
devs << e->getValue("device", "").value();
|
||||
for (const PIConfig::Entry * e2 : db) {
|
||||
for (const PIConfig::Entry * e2: db) {
|
||||
devs << e2->value();
|
||||
}
|
||||
devs.removeStrings("");
|
||||
if (devs.isEmpty()) continue;
|
||||
float freq = e->getValue("frequency").toFloat();
|
||||
for (const PIString & d : devs) {
|
||||
for (const PIString & d: devs) {
|
||||
addSender(e->name(), dev_aliases.value(d, d), freq);
|
||||
}
|
||||
PIByteArray fd(PIByteArray::fromUserInput(e->getValue("fixedData").toString()));
|
||||
@@ -262,15 +262,15 @@ PIString PIConnection::makeConfig() const {
|
||||
PIString ret;
|
||||
PIIOTextStream ts(&ret);
|
||||
ts << "[" << name() << "]\n";
|
||||
PIVector<PIIODevice * > devs(boundedDevices());
|
||||
PIVector<PIIODevice *> devs(boundedDevices());
|
||||
int dn(-1);
|
||||
for (const PIIODevice * d : devs) {
|
||||
for (const PIIODevice * d: devs) {
|
||||
PIStringList dnl(deviceNames(d));
|
||||
if (dnl.isEmpty()) dnl << PIString::fromNumber(++dn);
|
||||
for (const PIString & dname : dnl) {
|
||||
for (const PIString & dname: dnl) {
|
||||
ts << "device." << dname << " = " << d->constructFullPath() << " #s\n";
|
||||
ts << "device." << dname << ".bufferSize = " << d->threadedReadBufferSize() << " #n\n";
|
||||
PIDiagnostics * diag = diags_.value(const_cast<PIIODevice * >(d), nullptr);
|
||||
PIDiagnostics * diag = diags_.value(const_cast<PIIODevice *>(d), nullptr);
|
||||
if (diag) ts << "device." << dname << ".disconnectTimeout = " << diag->disconnectTimeout() << " #f\n";
|
||||
}
|
||||
}
|
||||
@@ -301,11 +301,12 @@ PIString PIConnection::makeConfig() const {
|
||||
ts << prefix << ".header = " << ite.value()->extractor->header().toString() << " #s\n";
|
||||
ts << prefix << ".footer = " << ite.value()->extractor->footer().toString() << " #s\n";
|
||||
}
|
||||
dn = 0;
|
||||
dn = 0;
|
||||
auto itc = channels_.makeIterator();
|
||||
while (itc.next()) {
|
||||
for (const PIIODevice * d : itc.value()) {
|
||||
PIString prefix = "channel." + PIString::fromNumber(dn); ++dn;
|
||||
for (const PIIODevice * d: itc.value()) {
|
||||
PIString prefix = "channel." + PIString::fromNumber(dn);
|
||||
++dn;
|
||||
PIString dname = device_names.key(itc.key());
|
||||
if (dname.isEmpty()) dname = devPath(itc.key());
|
||||
ts << prefix << ".from = " << dname << " #s\n";
|
||||
@@ -324,10 +325,8 @@ PIString PIConnection::makeConfig() const {
|
||||
ts << prefix << ".device." << i << " = " << dname << " #s\n";
|
||||
}
|
||||
double int_ = its.value()->int_;
|
||||
if (int_ > 0.)
|
||||
ts << prefix << ".frequency = " << (1000. / int_) << " #f\n";
|
||||
if (!its.value()->sdata.isEmpty())
|
||||
ts << prefix << ".fixedData = " << its.value()->sdata.toString() << " #s\n";
|
||||
if (int_ > 0.) ts << prefix << ".frequency = " << (1000. / int_) << " #f\n";
|
||||
if (!its.value()->sdata.isEmpty()) ts << prefix << ".fixedData = " << its.value()->sdata.toString() << " #s\n";
|
||||
}
|
||||
ts << "[]\n";
|
||||
return ret;
|
||||
@@ -363,7 +362,7 @@ void PIConnection::setDeviceName(PIIODevice * dev, const PIString & name) {
|
||||
PIStringList PIConnection::deviceNames(const PIIODevice * dev) const {
|
||||
PIStringList ret;
|
||||
auto it = device_names.makeIterator();
|
||||
while(it.next()) {
|
||||
while (it.next()) {
|
||||
if (it.value() == dev) ret << it.key();
|
||||
}
|
||||
return ret;
|
||||
@@ -375,7 +374,7 @@ bool PIConnection::removeDevice(const PIString & full_path) {
|
||||
PIIODevice * dev = __device_pool__->device(fp);
|
||||
if (!dev) return false;
|
||||
PIStringList dntd(deviceNames(dev));
|
||||
for (const PIString & n : dntd) {
|
||||
for (const PIString & n: dntd) {
|
||||
device_names.remove(n);
|
||||
}
|
||||
for (auto s = senders.begin(); s != senders.end(); s++) {
|
||||
@@ -406,9 +405,9 @@ bool PIConnection::removeDevice(const PIString & full_path) {
|
||||
|
||||
void PIConnection::removeAllDevices() {
|
||||
device_names.clear();
|
||||
PIVector<PIIODevice * > bdevs(__device_pool__->boundedDevices(this));
|
||||
PIVector<PIIODevice *> bdevs(__device_pool__->boundedDevices(this));
|
||||
__device_pool__->lock();
|
||||
for (PIIODevice * d : bdevs) {
|
||||
for (PIIODevice * d: bdevs) {
|
||||
for (auto s = senders.begin(); s != senders.end(); s++) {
|
||||
if (!s.value()) continue;
|
||||
s.value()->lock();
|
||||
@@ -440,7 +439,7 @@ PIIODevice * PIConnection::deviceByFullPath(const PIString & full_path) const {
|
||||
DevicePool::DeviceData * dd = __device_pool__->devices.value(fp, nullptr);
|
||||
if (!dd) return nullptr;
|
||||
if (!dd->dev) return nullptr;
|
||||
if (!dd->listeners.contains(const_cast<PIConnection * >(this))) return nullptr;
|
||||
if (!dd->listeners.contains(const_cast<PIConnection *>(this))) return nullptr;
|
||||
return dd->dev;
|
||||
}
|
||||
|
||||
@@ -450,16 +449,16 @@ PIIODevice * PIConnection::deviceByName(const PIString & name) const {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIIODevice * > PIConnection::boundedDevices() const {
|
||||
PIVector<PIIODevice *> PIConnection::boundedDevices() const {
|
||||
return __device_pool__->boundedDevices(this);
|
||||
}
|
||||
|
||||
|
||||
PIPacketExtractor * PIConnection::addFilter(const PIString & name_, const PIString & full_path, PIPacketExtractor::SplitMode mode) {
|
||||
PIString fname_ = name_.trimmed();
|
||||
Extractor * e = extractors.value(fname_, nullptr);
|
||||
Extractor * e = extractors.value(fname_, nullptr);
|
||||
if (full_path.isEmpty()) return (e ? e->extractor : nullptr);
|
||||
PIIODevice * dev = devByString(full_path);
|
||||
PIIODevice * dev = devByString(full_path);
|
||||
PIPacketExtractor * pe = nullptr;
|
||||
if (extractors.value(full_path, nullptr)) pe = extractors.value(full_path, nullptr)->extractor;
|
||||
if (pe) dev = pe;
|
||||
@@ -468,7 +467,7 @@ PIPacketExtractor * PIConnection::addFilter(const PIString & name_, const PIStri
|
||||
return nullptr;
|
||||
}
|
||||
if (!e) {
|
||||
e = new Extractor();
|
||||
e = new Extractor();
|
||||
extractors[fname_] = e;
|
||||
}
|
||||
if (!e->extractor) {
|
||||
@@ -482,11 +481,11 @@ PIPacketExtractor * PIConnection::addFilter(const PIString & name_, const PIStri
|
||||
CONNECT2(void, PIDiagnostics::Quality, PIDiagnostics::Quality, d, qualityChanged, this, diagQualityChanged);
|
||||
}
|
||||
__device_pool__->unlock();
|
||||
CONNECT2(void, const uchar * , int, e->extractor, packetReceived, this, packetExtractorReceived)
|
||||
CONNECT2(void, const uchar *, int, e->extractor, packetReceived, this, packetExtractorReceived)
|
||||
}
|
||||
if (!e->devices.contains(dev)) {
|
||||
bounded_extractors[dev] << e->extractor;
|
||||
//if (PIString(dev->className()) == "PIPacketExtractor") dev->setThreadSafe(false);
|
||||
// if (PIString(dev->className()) == "PIPacketExtractor") dev->setThreadSafe(false);
|
||||
e->devices << dev;
|
||||
}
|
||||
return e->extractor;
|
||||
@@ -496,9 +495,9 @@ PIPacketExtractor * PIConnection::addFilter(const PIString & name_, const PIStri
|
||||
PIPacketExtractor * PIConnection::addFilter(PIPacketExtractor * filter, const PIString & full_path) {
|
||||
Extractor * e = nullptr;
|
||||
if (full_path.isEmpty()) return nullptr;
|
||||
PIIODevice * dev = devByString(full_path);
|
||||
PIIODevice * dev = devByString(full_path);
|
||||
PIPacketExtractor * pe = nullptr;
|
||||
e = extractors.value(full_path, nullptr);
|
||||
e = extractors.value(full_path, nullptr);
|
||||
if (e) pe = e->extractor;
|
||||
if (pe) {
|
||||
dev = pe;
|
||||
@@ -507,7 +506,7 @@ PIPacketExtractor * PIConnection::addFilter(PIPacketExtractor * filter, const PI
|
||||
return nullptr;
|
||||
}
|
||||
if (!e) {
|
||||
e = new Extractor();
|
||||
e = new Extractor();
|
||||
extractors[filter->name()] = e;
|
||||
}
|
||||
if (!e->extractor) {
|
||||
@@ -520,7 +519,7 @@ PIPacketExtractor * PIConnection::addFilter(PIPacketExtractor * filter, const PI
|
||||
CONNECT2(void, PIDiagnostics::Quality, PIDiagnostics::Quality, d, qualityChanged, this, diagQualityChanged);
|
||||
}
|
||||
__device_pool__->unlock();
|
||||
CONNECT2(void, const uchar * , int, e->extractor, packetReceived, this, packetExtractorReceived)
|
||||
CONNECT2(void, const uchar *, int, e->extractor, packetReceived, this, packetExtractorReceived)
|
||||
}
|
||||
if (!e->devices.contains(dev)) {
|
||||
bounded_extractors[dev] << e->extractor;
|
||||
@@ -586,8 +585,8 @@ void PIConnection::removeAllFilters() {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIPacketExtractor * > PIConnection::filters() const {
|
||||
PIVector<PIPacketExtractor * > ret;
|
||||
PIVector<PIPacketExtractor *> PIConnection::filters() const {
|
||||
PIVector<PIPacketExtractor *> ret;
|
||||
for (auto i = extractors.begin(); i != extractors.end(); i++) {
|
||||
if (i.value()) {
|
||||
if (i.value()->extractor) ret << i.value()->extractor;
|
||||
@@ -618,8 +617,8 @@ PIPacketExtractor * PIConnection::filter(const PIString & name_) const {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIIODevice * > PIConnection::filterBoundedDevices(const PIString & name_) const {
|
||||
PIVector<PIIODevice * > ret;
|
||||
PIVector<PIIODevice *> PIConnection::filterBoundedDevices(const PIString & name_) const {
|
||||
PIVector<PIIODevice *> ret;
|
||||
Extractor * p = extractors.value(name_.trimmed());
|
||||
if (!p) return ret;
|
||||
return p->devices;
|
||||
@@ -627,12 +626,12 @@ PIVector<PIIODevice * > PIConnection::filterBoundedDevices(const PIString & name
|
||||
|
||||
|
||||
bool PIConnection::addChannel(const PIString & name0, const PIString & name1) {
|
||||
//piCout << "addChannel" << name0 << name1;
|
||||
// piCout << "addChannel" << name0 << name1;
|
||||
if (name0.isEmpty() || name1.isEmpty()) return false;
|
||||
PIIODevice * dev0 = devByString(name0);
|
||||
PIIODevice * dev1 = devByString(name1);
|
||||
Extractor * p0 = extractors.value(name0, nullptr);
|
||||
Extractor * p1 = extractors.value(name1, nullptr);
|
||||
PIIODevice * dev0 = devByString(name0);
|
||||
PIIODevice * dev1 = devByString(name1);
|
||||
Extractor * p0 = extractors.value(name0, nullptr);
|
||||
Extractor * p1 = extractors.value(name1, nullptr);
|
||||
PIPacketExtractor * pe0 = nullptr;
|
||||
PIPacketExtractor * pe1 = nullptr;
|
||||
if (p0) pe0 = p0->extractor;
|
||||
@@ -644,17 +643,16 @@ bool PIConnection::addChannel(const PIString & name0, const PIString & name1) {
|
||||
if (!dev1) piCoutObj << "\"addChannel\" error: no such device \"" << name1 << "\"!";
|
||||
return false;
|
||||
}
|
||||
if (!channels_[dev0].contains(dev1))
|
||||
channels_[dev0] << dev1;
|
||||
if (!channels_[dev0].contains(dev1)) channels_[dev0] << dev1;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool PIConnection::removeChannel(const PIString & name0, const PIString & name1) {
|
||||
PIIODevice * dev0 = devByString(name0);
|
||||
PIIODevice * dev1 = devByString(name1);
|
||||
Extractor * p0 = extractors.value(name0, nullptr);
|
||||
Extractor * p1 = extractors.value(name1, nullptr);
|
||||
PIIODevice * dev0 = devByString(name0);
|
||||
PIIODevice * dev1 = devByString(name1);
|
||||
Extractor * p0 = extractors.value(name0, nullptr);
|
||||
Extractor * p1 = extractors.value(name1, nullptr);
|
||||
PIPacketExtractor * pe0 = nullptr;
|
||||
PIPacketExtractor * pe1 = nullptr;
|
||||
if (p0) pe0 = p0->extractor;
|
||||
@@ -668,8 +666,8 @@ bool PIConnection::removeChannel(const PIString & name0, const PIString & name1)
|
||||
|
||||
|
||||
bool PIConnection::removeChannel(const PIString & name0) {
|
||||
PIIODevice * dev0 = devByString(name0);
|
||||
Extractor * p0 = extractors.value(name0, nullptr);
|
||||
PIIODevice * dev0 = devByString(name0);
|
||||
Extractor * p0 = extractors.value(name0, nullptr);
|
||||
PIPacketExtractor * pe0 = nullptr;
|
||||
if (p0) pe0 = p0->extractor;
|
||||
if (pe0) dev0 = pe0;
|
||||
@@ -710,12 +708,12 @@ PIIODevice * PIConnection::devByString(const PIString & s) const {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIPair<PIString, PIString > > PIConnection::channels() const {
|
||||
PIVector<PIPair<PIString, PIString > > ret;
|
||||
PIVector<PIPair<PIString, PIString>> PIConnection::channels() const {
|
||||
PIVector<PIPair<PIString, PIString>> ret;
|
||||
auto it = channels_.makeIterator();
|
||||
while (it.next()) {
|
||||
PIString fp0(devFPath(it.key()));
|
||||
for (const PIIODevice * d : it.value()) {
|
||||
for (const PIIODevice * d: it.value()) {
|
||||
ret << PIPair<PIString, PIString>(fp0, devFPath(d));
|
||||
}
|
||||
}
|
||||
@@ -730,7 +728,7 @@ void PIConnection::addSender(const PIString & name_, const PIString & full_path_
|
||||
if (!s) {
|
||||
s = new Sender(this);
|
||||
s->setName(fname_);
|
||||
s->int_ = 1000. / frequency;
|
||||
s->int_ = 1000. / frequency;
|
||||
senders[fname_] = s;
|
||||
}
|
||||
PIIODevice * dev = devByString(full_path_name);
|
||||
@@ -739,7 +737,7 @@ void PIConnection::addSender(const PIString & name_, const PIString & full_path_
|
||||
return;
|
||||
}
|
||||
if (!s->isRunning() && start_) {
|
||||
//piCoutObj << name_ << "start" << 1000. / frequency;
|
||||
// piCoutObj << name_ << "start" << 1000. / frequency;
|
||||
if (!__device_pool__->fake) s->start(s->int_);
|
||||
}
|
||||
s->lock();
|
||||
@@ -749,7 +747,7 @@ void PIConnection::addSender(const PIString & name_, const PIString & full_path_
|
||||
|
||||
|
||||
bool PIConnection::removeSender(const PIString & name, const PIString & full_path_name) {
|
||||
Sender * s = senders.value(name, nullptr);
|
||||
Sender * s = senders.value(name, nullptr);
|
||||
PIIODevice * d = devByString(full_path_name);
|
||||
if (!s || !d) return false;
|
||||
s->lock();
|
||||
@@ -880,8 +878,8 @@ void PIConnection::stopAllSenders() {
|
||||
|
||||
|
||||
PIDiagnostics * PIConnection::diagnostic(const PIString & full_path_name) const {
|
||||
PIIODevice * dev = devByString(full_path_name);
|
||||
Extractor * e = extractors.value(full_path_name, nullptr);
|
||||
PIIODevice * dev = devByString(full_path_name);
|
||||
Extractor * e = extractors.value(full_path_name, nullptr);
|
||||
PIPacketExtractor * pe = nullptr;
|
||||
if (e) pe = e->extractor;
|
||||
if (pe) dev = pe;
|
||||
@@ -891,9 +889,9 @@ PIDiagnostics * PIConnection::diagnostic(const PIString & full_path_name) const
|
||||
|
||||
|
||||
int PIConnection::writeByFullPath(const PIString & full_path, const PIByteArray & data) {
|
||||
PIString fp = PIIODevice::normalizeFullPath(full_path);
|
||||
PIString fp = PIIODevice::normalizeFullPath(full_path);
|
||||
PIIODevice * dev = __device_pool__->device(fp);
|
||||
//piCout << "SEND" << full_path << fp;
|
||||
// piCout << "SEND" << full_path << fp;
|
||||
if (!dev) {
|
||||
piCoutObj << "No such full path \"" << full_path << "\"!";
|
||||
return -1;
|
||||
@@ -922,25 +920,25 @@ int PIConnection::write(PIIODevice * dev, const PIByteArray & data) {
|
||||
piCoutObj << "Device \"" << dev->constructFullPath() << "\" can`t write!";
|
||||
return -1;
|
||||
}
|
||||
int ret = dev->write(data);
|
||||
int ret = dev->write(data);
|
||||
PIDiagnostics * diag = diags_.value(dev, nullptr);
|
||||
if (diag && ret > 0) diag->sended(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
PIVector< PIConnection * > PIConnection::allConnections() {
|
||||
PIVector<PIConnection *> PIConnection::allConnections() {
|
||||
return _connections;
|
||||
}
|
||||
|
||||
|
||||
PIVector< PIIODevice * > PIConnection::allDevices() {
|
||||
PIVector<PIIODevice *> PIConnection::allDevices() {
|
||||
return __device_pool__->boundedDevices();
|
||||
}
|
||||
|
||||
|
||||
bool PIConnection::setFakeMode(bool yes) {
|
||||
bool ret = isFakeMode();
|
||||
bool ret = isFakeMode();
|
||||
__device_pool__->fake = yes;
|
||||
return ret;
|
||||
}
|
||||
@@ -951,8 +949,6 @@ bool PIConnection::isFakeMode() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIConnection::DevicePool::DevicePool(): PIThread(false, 10) {
|
||||
setName("PIConnection::DevicePool");
|
||||
needLockRun(true);
|
||||
@@ -960,8 +956,7 @@ PIConnection::DevicePool::DevicePool(): PIThread(false, 10) {
|
||||
}
|
||||
|
||||
|
||||
PIConnection::DevicePool::~DevicePool() {
|
||||
}
|
||||
PIConnection::DevicePool::~DevicePool() {}
|
||||
|
||||
|
||||
void PIConnection::DevicePool::init() {
|
||||
@@ -971,14 +966,14 @@ void PIConnection::DevicePool::init() {
|
||||
|
||||
PIIODevice * PIConnection::DevicePool::addDevice(PIConnection * parent, const PIString & fp, PIIODevice::DeviceMode mode, bool start) {
|
||||
DeviceData * dd = devices[fp];
|
||||
int pmode = 0;
|
||||
int pmode = 0;
|
||||
bool need_start = false;
|
||||
if (!dd) {
|
||||
dd = new DeviceData();
|
||||
dd = new DeviceData();
|
||||
devices[fp] = dd;
|
||||
}
|
||||
if (!dd->dev) {
|
||||
//piCout << "new device" << fp;
|
||||
// piCout << "new device" << fp;
|
||||
dd->dev = PIIODevice::createFromFullPath(fp);
|
||||
if (!dd->dev) {
|
||||
piCoutObj << "Error: can`t create device \"" << fp << "\"!"; //:" << errorString();
|
||||
@@ -1040,7 +1035,7 @@ void PIConnection::DevicePool::unboundConnection(PIConnection * parent) {
|
||||
i.value()->listeners.removeAll(parent);
|
||||
if (i.value()->listeners.isEmpty()) rem << i.key();
|
||||
}
|
||||
for (const PIString & i : rem) {
|
||||
for (const PIString & i: rem) {
|
||||
DeviceData * dd = devices.value(i, nullptr);
|
||||
if (!dd) continue;
|
||||
delete dd;
|
||||
@@ -1067,8 +1062,8 @@ PIConnection::DevicePool::DeviceData * PIConnection::DevicePool::deviceData(PIIO
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIConnection * > PIConnection::DevicePool::boundedConnections() const {
|
||||
PIVector<PIConnection * > ret;
|
||||
PIVector<PIConnection *> PIConnection::DevicePool::boundedConnections() const {
|
||||
PIVector<PIConnection *> ret;
|
||||
auto it = devices.makeIterator();
|
||||
while (it.next()) {
|
||||
if (!it.value()) continue;
|
||||
@@ -1086,8 +1081,8 @@ PIVector<PIConnection * > PIConnection::DevicePool::boundedConnections() const {
|
||||
}
|
||||
|
||||
|
||||
PIVector< PIIODevice * > PIConnection::DevicePool::boundedDevices() const {
|
||||
PIVector<PIIODevice * > ret;
|
||||
PIVector<PIIODevice *> PIConnection::DevicePool::boundedDevices() const {
|
||||
PIVector<PIIODevice *> ret;
|
||||
auto it = devices.makeIterator();
|
||||
while (it.next()) {
|
||||
if (!it.value()) continue;
|
||||
@@ -1098,13 +1093,13 @@ PIVector< PIIODevice * > PIConnection::DevicePool::boundedDevices() const {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIIODevice * > PIConnection::DevicePool::boundedDevices(const PIConnection * parent) const {
|
||||
PIVector<PIIODevice * > ret;
|
||||
PIVector<PIIODevice *> PIConnection::DevicePool::boundedDevices(const PIConnection * parent) const {
|
||||
PIVector<PIIODevice *> ret;
|
||||
auto it = devices.makeIterator();
|
||||
while (it.next()) {
|
||||
if (!it.value()) continue;
|
||||
if (!it.value()->dev) continue;
|
||||
if (it.value()->listeners.contains(const_cast<PIConnection*>(parent))) {
|
||||
if (it.value()->listeners.contains(const_cast<PIConnection *>(parent))) {
|
||||
ret << it.value()->dev;
|
||||
}
|
||||
}
|
||||
@@ -1116,8 +1111,7 @@ PIConnection::DevicePool::DeviceData::~DeviceData() {
|
||||
if (rthread) {
|
||||
rthread->stop();
|
||||
if (dev) dev->interrupt();
|
||||
if (!rthread->waitForFinish(1000))
|
||||
rthread->terminate();
|
||||
if (!rthread->waitForFinish(1000)) rthread->terminate();
|
||||
delete rthread;
|
||||
rthread = nullptr;
|
||||
}
|
||||
@@ -1130,8 +1124,8 @@ PIConnection::DevicePool::DeviceData::~DeviceData() {
|
||||
|
||||
|
||||
void PIConnection::DevicePool::run() {
|
||||
PIVector<PIConnection * > conns(PIConnection::allConnections());
|
||||
for (PIConnection * c : conns) {
|
||||
PIVector<PIConnection *> conns(PIConnection::allConnections());
|
||||
for (PIConnection * c: conns) {
|
||||
for (auto d = c->diags_.begin(); d != c->diags_.end(); d++) {
|
||||
if (!d.value()) continue;
|
||||
d.value()->tick(0, 1);
|
||||
@@ -1141,7 +1135,7 @@ void PIConnection::DevicePool::run() {
|
||||
|
||||
|
||||
void __DevicePool_threadReadDP(void * ddp) {
|
||||
PIConnection::DevicePool::DeviceData * dd((PIConnection::DevicePool::DeviceData * )ddp);
|
||||
PIConnection::DevicePool::DeviceData * dd((PIConnection::DevicePool::DeviceData *)ddp);
|
||||
PIIODevice * dev = dd->dev;
|
||||
if (!dev) {
|
||||
piMSleep(100);
|
||||
@@ -1152,8 +1146,7 @@ void __DevicePool_threadReadDP(void * ddp) {
|
||||
PITimeMeasurer tm;
|
||||
int timeout = dev->reopenTimeout();
|
||||
while (tm.elapsed_m() < timeout) {
|
||||
if (dd->rthread->isStopping())
|
||||
return;
|
||||
if (dd->rthread->isStopping()) return;
|
||||
piMSleep(50);
|
||||
}
|
||||
}
|
||||
@@ -1166,14 +1159,14 @@ void __DevicePool_threadReadDP(void * ddp) {
|
||||
}
|
||||
dev->threadedRead(ba.data(), ba.size_s());
|
||||
dev->threadedReadEvent(ba.data(), ba.size_s());
|
||||
//piCout << "Readed from" << dd->dev->path() << Hex << ba;
|
||||
// piCout << "Readed from" << dd->dev->path() << Hex << ba;
|
||||
__device_pool__->deviceReaded(dd, ba);
|
||||
}
|
||||
|
||||
|
||||
void PIConnection::DevicePool::deviceReaded(PIConnection::DevicePool::DeviceData * dd, const PIByteArray & data) {
|
||||
PIString from = dd->dev->property("__fullPath__").toString();
|
||||
for (PIConnection * ld : dd->listeners) {
|
||||
for (PIConnection * ld: dd->listeners) {
|
||||
ld->rawReceived(dd->dev, from, data);
|
||||
}
|
||||
}
|
||||
@@ -1182,14 +1175,14 @@ void PIConnection::DevicePool::deviceReaded(PIConnection::DevicePool::DeviceData
|
||||
void PIConnection::rawReceived(PIIODevice * dev, const PIString & from, const PIByteArray & data) {
|
||||
dataReceived(from, data);
|
||||
dataReceivedEvent(from, data);
|
||||
PIVector<PIPacketExtractor * > be(bounded_extractors.value(dev));
|
||||
//piCout << be;
|
||||
for (PIPacketExtractor * i : be) {
|
||||
PIVector<PIPacketExtractor *> be(bounded_extractors.value(dev));
|
||||
// piCout << be;
|
||||
for (PIPacketExtractor * i: be) {
|
||||
i->threadedRead(data.data(), data.size_s());
|
||||
}
|
||||
PIVector<PIIODevice * > chd(channels_.value(dev));
|
||||
for (PIIODevice * d : chd) {
|
||||
int ret = d->write(data);
|
||||
PIVector<PIIODevice *> chd(channels_.value(dev));
|
||||
for (PIIODevice * d: chd) {
|
||||
int ret = d->write(data);
|
||||
PIDiagnostics * diag = diags_.value(d, nullptr);
|
||||
if (diag && ret > 0) diag->sended(ret);
|
||||
}
|
||||
@@ -1198,7 +1191,6 @@ void PIConnection::rawReceived(PIIODevice * dev, const PIString & from, const PI
|
||||
}
|
||||
|
||||
|
||||
|
||||
PIByteArray PIConnection::senderData(const PIString & sender_name) {
|
||||
return PIByteArray();
|
||||
}
|
||||
@@ -1212,31 +1204,29 @@ PIConnection::Extractor::~Extractor() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIConnection::Sender::Sender(PIConnection * parent_): parent(parent_), int_(0.f) {
|
||||
setName("__S__.PIConnection.Sender");
|
||||
needLockRun(true);
|
||||
}
|
||||
|
||||
|
||||
void PIConnection::Sender::tick(void * , int) {
|
||||
void PIConnection::Sender::tick(void *, int) {
|
||||
if (!parent) return;
|
||||
PIByteArray data;
|
||||
if (!sdata.isEmpty()) data = sdata;
|
||||
else data = parent->senderData(name());
|
||||
if (!sdata.isEmpty())
|
||||
data = sdata;
|
||||
else
|
||||
data = parent->senderData(name());
|
||||
if (data.isEmpty()) return;
|
||||
//piCoutObj << "write"<<data.size()<<"bytes to"<<devices.size()<<"devices";
|
||||
for (PIIODevice * d : devices) {
|
||||
int ret = d->write(data);
|
||||
// piCoutObj << "write"<<data.size()<<"bytes to"<<devices.size()<<"devices";
|
||||
for (PIIODevice * d: devices) {
|
||||
int ret = d->write(data);
|
||||
PIDiagnostics * diag = parent->diags_.value(d, nullptr);
|
||||
if (diag && ret > 0) diag->sended(ret);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void PIConnection::unboundExtractor(PIPacketExtractor * pe) {
|
||||
if (!pe) return;
|
||||
channels_.remove(pe);
|
||||
@@ -1245,9 +1235,9 @@ void PIConnection::unboundExtractor(PIPacketExtractor * pe) {
|
||||
it.value().removeAll(pe);
|
||||
}
|
||||
bounded_extractors.remove(pe);
|
||||
PIVector<PIIODevice * > k = bounded_extractors.keys();
|
||||
for (PIIODevice * i : k) {
|
||||
PIVector<PIPacketExtractor * > & be(bounded_extractors[i]);
|
||||
PIVector<PIIODevice *> k = bounded_extractors.keys();
|
||||
for (PIIODevice * i: k) {
|
||||
PIVector<PIPacketExtractor *> & be(bounded_extractors[i]);
|
||||
be.removeAll(pe);
|
||||
if (be.isEmpty()) bounded_extractors.remove(i);
|
||||
}
|
||||
@@ -1260,18 +1250,18 @@ void PIConnection::unboundExtractor(PIPacketExtractor * pe) {
|
||||
|
||||
|
||||
void PIConnection::packetExtractorReceived(const uchar * data, int size) {
|
||||
PIString from(emitter()? emitter()->name() : PIString());
|
||||
PIIODevice * cd = (PIIODevice * )emitter();
|
||||
// piCout << "packetExtractorReceived" << from << cd;
|
||||
PIString from(emitter() ? emitter()->name() : PIString());
|
||||
PIIODevice * cd = (PIIODevice *)emitter();
|
||||
// piCout << "packetExtractorReceived" << from << cd;
|
||||
if (cd) {
|
||||
PIVector<PIPacketExtractor * > be(bounded_extractors.value(cd));
|
||||
//piCout << be << (void*)data << size;
|
||||
for (PIPacketExtractor * i : be) {
|
||||
PIVector<PIPacketExtractor *> be(bounded_extractors.value(cd));
|
||||
// piCout << be << (void*)data << size;
|
||||
for (PIPacketExtractor * i: be) {
|
||||
i->threadedRead(data, size);
|
||||
}
|
||||
PIVector<PIIODevice * > chd(channels_.value(cd));
|
||||
for (PIIODevice * d : chd) {
|
||||
int ret = d->write(data, size);
|
||||
PIVector<PIIODevice *> chd(channels_.value(cd));
|
||||
for (PIIODevice * d: chd) {
|
||||
int ret = d->write(data, size);
|
||||
PIDiagnostics * diag = diags_.value(d);
|
||||
if (diag) diag->sended(ret);
|
||||
}
|
||||
@@ -1284,17 +1274,16 @@ void PIConnection::packetExtractorReceived(const uchar * data, int size) {
|
||||
|
||||
|
||||
void PIConnection::diagQualityChanged(PIDiagnostics::Quality new_quality, PIDiagnostics::Quality old_quality) {
|
||||
qualityChanged(diags_.key((PIDiagnostics*)emitter()), new_quality, old_quality);
|
||||
qualityChanged(diags_.key((PIDiagnostics *)emitter()), new_quality, old_quality);
|
||||
}
|
||||
|
||||
|
||||
|
||||
PIConnection::DevicePool * __device_pool__;
|
||||
|
||||
bool __DevicePoolContainer__::inited_(false);
|
||||
|
||||
__DevicePoolContainer__::__DevicePoolContainer__() {
|
||||
if (inited_) return;
|
||||
inited_ = true;
|
||||
inited_ = true;
|
||||
__device_pool__ = new PIConnection::DevicePool();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user