git-svn-id: svn://db.shs.com.ru/libs@123 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
#include "cdutils_core.h"
|
||||
#include "cdutils_parser.h"
|
||||
#include "piconfig.h"
|
||||
#include "cdutils_protocol.h"
|
||||
#include "piiodevice.h"
|
||||
#include "piiobytearray.h"
|
||||
#include "piiostring.h"
|
||||
#include "pifile.h"
|
||||
|
||||
@@ -74,15 +73,20 @@ __Core_Initializer__::~__Core_Initializer__() {
|
||||
|
||||
|
||||
|
||||
CDCore::CDCore(const char * conf) {
|
||||
CDCore::CDCore() {
|
||||
setName("CDCore");
|
||||
CONNECTU(&connection, dataReceivedEvent, this, dataReceived);
|
||||
PIString s(conf);
|
||||
PIString s(app_config);
|
||||
connection.configureFromString(&s);
|
||||
datatr.setPacketSize(960);
|
||||
connection.start();
|
||||
k_.name = "__root__";
|
||||
k_.alias = "root";
|
||||
|
||||
CONNECTU(&datatr, sendRequest, this, dtSendRequest)
|
||||
CONNECTU(&datatr, receiveFinished, this, dtReceiveFinished)
|
||||
CONNECTU(&sendt, started, this, sendThread)
|
||||
|
||||
/*k_[1] = KType(1, "123", "120+3", "comment");
|
||||
k_[2] = KType(2, "1", "2", "comm");
|
||||
k_[4] = KType(4, "-0.6", "-6/10", "mment");
|
||||
@@ -118,11 +122,18 @@ void CDCore::k_write(PIIODevice * d) {
|
||||
void CDCore::k_read(PIIODevice * d) {
|
||||
PIConfig conf(d, PIIODevice::ReadOnly);
|
||||
k_.read(&(conf.rootEntry()));
|
||||
PIVector<PIIODevice * > ds = connection.allDevices();
|
||||
// piForeach(PIIODevice * d, ds) {
|
||||
// if (d)
|
||||
// piCoutObj << d->constructFullPath() << d->isOpened();
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
void CDCore::k_parse(PIIODevice * d) {
|
||||
k_ = CDParser::parse(d, CDType::cdK);
|
||||
k_.name = "__root__";
|
||||
k_.alias = "root";
|
||||
}
|
||||
|
||||
|
||||
@@ -140,25 +151,47 @@ void CDCore::k_update(PIIODevice * d) {
|
||||
k_ = uk;
|
||||
}
|
||||
|
||||
void CDCore::reinitConnection(const PIString &configuration) {
|
||||
PIString c = configuration;
|
||||
connection.stop();
|
||||
connection.removeAllDevices();
|
||||
connection.configureFromString(&c);
|
||||
connection.start();
|
||||
}
|
||||
|
||||
|
||||
void CDUtils::CDCore::dataReceived(const PIString & from, const PIByteArray & data) {
|
||||
piCout << from << data.size();
|
||||
if (from == "cd") {
|
||||
// piCoutObj << "dataReceived" << from << data.size();
|
||||
PIIODevice * d = connection.deviceByName("cd");
|
||||
if (d && d == connection.deviceByFullPath(from)) {
|
||||
datatr.received(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CDCore::K_Send() {
|
||||
piCoutObj << "K_Send";
|
||||
PIString s;
|
||||
PIIOString ios(&s);
|
||||
k_write(&ios);
|
||||
PIByteArray ba;
|
||||
PIIOByteArray iob(&ba, PIIODevice::ReadWrite);
|
||||
// PIIOByteArray iob;
|
||||
// iob.open(&ba, PIIODevice::ReadWrite);
|
||||
k_write(&iob);
|
||||
piCoutObj << PIString(ba);
|
||||
if (sendt.isRunning()) {
|
||||
piCoutObj << "send in process...";
|
||||
return;
|
||||
}
|
||||
wheader.session_id = 0;
|
||||
wheader.type = CD_KSend;
|
||||
send_data.clear();
|
||||
send_data << wheader << ba;
|
||||
sendt.startOnce();
|
||||
// ios.close();
|
||||
piCout << s;
|
||||
// piCout << s;
|
||||
//PIByteArray ba = PIByteArray::fromString(s);
|
||||
PIFile pf("k.txt", PIIODevice::ReadWrite);
|
||||
pf.resize(0);
|
||||
pf.write(s.toUTF8());
|
||||
// PIFile pf("k.txt", PIIODevice::ReadWrite);
|
||||
// pf.resize(0);
|
||||
// pf.write(s.toUTF8());
|
||||
//k_write(&pf);
|
||||
//piCout << pf.readLine();
|
||||
}
|
||||
@@ -166,5 +199,62 @@ void CDCore::K_Send() {
|
||||
|
||||
void CDCore::K_Request() {
|
||||
piCoutObj << "K_Request";
|
||||
|
||||
if (sendt.isRunning()) {
|
||||
piCoutObj << "send in process...";
|
||||
return;
|
||||
}
|
||||
wheader.session_id = 0;
|
||||
wheader.type = CD_KQuery;
|
||||
send_data.clear();
|
||||
send_data << wheader;
|
||||
sendt.startOnce();
|
||||
}
|
||||
|
||||
|
||||
void CDCore::sendThread() {
|
||||
bool ok = datatr.send(send_data);
|
||||
CDPacketType pt = (CDPacketType)wheader.type;
|
||||
switch (pt) {
|
||||
case CD_KSend:
|
||||
if (ok) K_Sended();
|
||||
else K_SendFail();
|
||||
break;
|
||||
case CD_KQuery:
|
||||
if (!ok) K_ReceiveFail();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CDCore::dtSendRequest(PIByteArray &data) {
|
||||
connection.writeByName("cd", data);
|
||||
// piCoutObj << "send" << data.size() << ret;
|
||||
}
|
||||
|
||||
|
||||
void CDCore::dtReceiveFinished(bool ok) {
|
||||
if (ok) {
|
||||
PIByteArray ba = datatr.data();
|
||||
PacketHeader p;
|
||||
ba >> p;
|
||||
CDPacketType pt = (CDPacketType)p.type;
|
||||
switch (pt) {
|
||||
case CD_KQuery:
|
||||
K_Send();
|
||||
break;
|
||||
case CD_KSend: {
|
||||
piCoutObj << "K received";
|
||||
PIByteArray k;
|
||||
ba >> k;
|
||||
k << uchar(0);
|
||||
PIString s = PIString::fromUTF8((const char *)k.data());
|
||||
PIIOString ios(&s);
|
||||
k_read(&ios);
|
||||
K_Received();
|
||||
} break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user