pip 0.4.2 alpha
git-svn-id: svn://db.shs.com.ru/pip@2 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
281
main.cpp
281
main.cpp
@@ -1,188 +1,129 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Test program
|
||||
Copyright (C) 2014 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
//#define PIP_DEBUG
|
||||
/*#include "pip.h"
|
||||
|
||||
|
||||
class ElementA: public PIObject {
|
||||
PIOBJECT(ElementA)
|
||||
// ...
|
||||
};
|
||||
ADD_NEW_TO_COLLECTION(ab_group, ElementA)
|
||||
|
||||
class ElementB: public PIObject {
|
||||
PIOBJECT(ElementB)
|
||||
// ...
|
||||
};
|
||||
ADD_NEW_TO_COLLECTION(ab_group, ElementB)
|
||||
|
||||
class ElementC: public PIObject {
|
||||
PIOBJECT(ElementC)
|
||||
// ...
|
||||
};
|
||||
ADD_NEW_TO_COLLECTION(c_group, ElementC)
|
||||
|
||||
class ElementD: public PIObject {
|
||||
PIOBJECT(ElementD)
|
||||
// ...
|
||||
};
|
||||
*/
|
||||
#include "pip.h"
|
||||
#include "pivariant.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include "picodeparser.h"
|
||||
#include "pidir.h"
|
||||
#include "piconnection.h"
|
||||
#include <QList>
|
||||
|
||||
bool readed(void*, uchar * data, int size) {
|
||||
piCout << Hex << "readed" << PIByteArray(data, size);
|
||||
//piCout << PIString((char*)data, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
class A: public PIObject {
|
||||
PIOBJECT(A)
|
||||
class Ob: public PIObject {
|
||||
PIOBJECT(Ob)
|
||||
public:
|
||||
EVENT_HANDLER2(void, cr, const PIString &, from, const PIByteArray &, data) {
|
||||
piCout << "A readed" << from << Hex << data;
|
||||
Ob() {
|
||||
sft.setName("sft");
|
||||
sft.setDirectory(sft.directory().cd("..\\"));
|
||||
piCout << "Send File Transfer DIrectory" << sft.directory().absolutePath();
|
||||
CONNECTU(&sft, sendRequest, this, ssend);
|
||||
|
||||
rft.setName("rft");
|
||||
rft.setDirectory(rft.directory().cd("..\\1"));
|
||||
piCout << "Receive File Transfer DIrectory" << rft.directory().absolutePath();
|
||||
CONNECTU(&rft, sendRequest, this, rsend);
|
||||
}
|
||||
EVENT_HANDLER2(void, per, uchar *, data, int, size) {
|
||||
piCout << "A readed" << size << ":\"" << PIString((const char *)data, size) << "\"" << NewLine;
|
||||
//piCout << "A readed \"";
|
||||
|
||||
void startsend() {
|
||||
PIDir dir = PIDir::current();
|
||||
dir.cd("..\\");
|
||||
piCout << dir.absolutePath();
|
||||
PIVector<PIDir::DirEntry> des = dir.allEntries();
|
||||
piCout << "all entries" << des.size();
|
||||
PIDir::DirEntry sde;
|
||||
piForeachC(PIDir::DirEntry de, des) {
|
||||
//piCout << (de.isDir() ? "dir:" : "file") << de.name << de.size;
|
||||
if (de.name == "0") sde = de;
|
||||
}
|
||||
//sft.setPacketSize(64096);
|
||||
sft.send(sde);
|
||||
}
|
||||
|
||||
private:
|
||||
EVENT_HANDLER1(void, ssend, PIByteArray &, data) {
|
||||
// piCout << "[sender]" << sft.stateString() << ". datasize =" << data.size()
|
||||
// << "(" << PIString::readableSize(sft.bytesFileCur()) << "/" << PIString::readableSize(sft.bytesFileAll()) << ", "
|
||||
// << PIString::readableSize(sft.bytesTotalCur()) << "/" << PIString::readableSize(sft.bytesTotalAll()) << ")";
|
||||
if(rand()%100 != 90) rft.received(data);
|
||||
}
|
||||
|
||||
EVENT_HANDLER1(void, rsend, PIByteArray &, data) {
|
||||
// piCout << "[receiver]" << rft.stateString() << ". datasize =" << data.size()
|
||||
// << "(" << PIString::readableSize(rft.bytesFileCur()) << "/" << PIString::readableSize(rft.bytesFileAll()) << ", "
|
||||
// << PIString::readableSize(rft.bytesTotalCur()) << "/" << PIString::readableSize(rft.bytesTotalAll()) << ")";
|
||||
if(rand()%100 != 90) sft.received(data);
|
||||
}
|
||||
|
||||
PIFileTransfer sft;
|
||||
PIFileTransfer rft;
|
||||
};
|
||||
|
||||
class TC: public PIConnection {
|
||||
|
||||
class UDPFileTransfer: public PITimer {
|
||||
PIOBJECT_SUBCLASS(UDPFileTransfer, PITimer)
|
||||
public:
|
||||
TC() {
|
||||
PIPacketExtractor * pe = addFilter("h&f", addDevice("file://piiodevice.h", PIIODevice::ReadOnly, false), PIPacketExtractor::HeaderAndFooter);
|
||||
pe->setHeader(PIString("//!").toByteArray());
|
||||
pe->setFooter(PIString("\n").toByteArray());
|
||||
pe = addFilter(" h ", "file://piiodevice.h", PIPacketExtractor::Header);
|
||||
addChannel(pe, addDevice("file://out.txt", PIIODevice::WriteOnly));
|
||||
pe->setHeader(PIString("PI").toByteArray());
|
||||
pe->setPayloadSize(3);
|
||||
startAllThreadedReads();
|
||||
UDPFileTransfer(const PIString &src_ip_port, const PIString &dst_ip_port) {
|
||||
eth.setReadAddress(src_ip_port);
|
||||
eth.setSendAddress(dst_ip_port);
|
||||
//ft.setPacketSize(65000);
|
||||
CONNECTU(&ft, sendRequest, this, ftsend);
|
||||
CONNECTU(&ft, startSend, this, ftevent);
|
||||
CONNECTU(&ft, finishSend, this, ftevent);
|
||||
CONNECTU(&ft, startReceive, this, ftevent);
|
||||
CONNECTU(&ft, finishReceive, this, ftevent);
|
||||
CONNECTU(ð, threadedReadEvent, this, received);
|
||||
start(50);
|
||||
eth.open();
|
||||
eth.startThreadedRead();
|
||||
}
|
||||
virtual void dataReceived(const PIString & from, const PIByteArray & data) {
|
||||
piCout << "dataReceived" << from << (data.size());
|
||||
|
||||
void startSend(const PIString &file) {
|
||||
ft.send(file);
|
||||
}
|
||||
virtual void packetReceived(const PIString & from, const PIByteArray & data) {
|
||||
piCout << "packetReceived" << from << (data.size()) << PIString(data);
|
||||
|
||||
PIFileTransfer ft;
|
||||
|
||||
private:
|
||||
PIEthernet eth;
|
||||
|
||||
void tick(void *, int) {
|
||||
if (ft.isSending() || ft.isReceiving()) ftevent();
|
||||
}
|
||||
virtual bool filterValidatePayload(const PIString & filter_name, uchar * rec, int size) {
|
||||
//piCout << "filterValidatePayload" << filter_name << PIString((char*)rec, size);
|
||||
if (filter_name == " h ") return PIString((char*)rec, size) == "IOD";
|
||||
return false;
|
||||
|
||||
EVENT_HANDLER(void, ftevent) {
|
||||
piCout << ft.stateString()
|
||||
<< "(" << PIString::readableSize(ft.bytesFileCur()) << "/" << PIString::readableSize(ft.bytesFileAll()) << ", "
|
||||
<< PIString::readableSize(ft.bytesTotalCur()) << "/" << PIString::readableSize(ft.bytesTotalAll()) << ")";
|
||||
}
|
||||
|
||||
EVENT_HANDLER1(void, ftsend, PIByteArray &, data) {
|
||||
eth.send(data);
|
||||
}
|
||||
|
||||
EVENT_HANDLER2(void, received, uchar * , readed, int, size) {
|
||||
PIByteArray ba(readed, size);
|
||||
ft.received(ba);
|
||||
}
|
||||
};
|
||||
|
||||
int main (int argc, char * argv[]) {
|
||||
/*A a_;
|
||||
PIFile file("piiodevice.h", PIIODevice::ReadOnly);
|
||||
PIByteArray header = PIString("PI").toByteArray();
|
||||
PIByteArray footer = PIString("}").toByteArray();
|
||||
PIPacketExtractor pe(&file);
|
||||
//pe.setPacketData(header.data(), header.size_s(), 10);
|
||||
pe.setSplitMode(PIPacketExtractor::Footer);
|
||||
pe.setHeader(header);
|
||||
pe.setFooter(footer);
|
||||
pe.setPayloadSize(3);
|
||||
pe.setThreadedReadBufferSize(40);
|
||||
//pe.setBufferSize(256);
|
||||
//pe.setPacketData(0, 0, 20);
|
||||
CONNECT2(void, uchar * , int , &pe, packetReceived, &a_, per)
|
||||
pe.startThreadedRead();
|
||||
piMSleep(500);*/
|
||||
|
||||
TC tc;
|
||||
piMSleep(500);
|
||||
piCout << tc.makeConfig();
|
||||
return 0;
|
||||
|
||||
/*tm = PISystemTime::current();
|
||||
for (int i = 0; i < 10000000; ++i) {
|
||||
ql.append(i*10);
|
||||
ql.prepend(i*10 + 1);
|
||||
if (!(argc == 3 || argc == 4)) {
|
||||
piCout << "UDPFileTransfer";
|
||||
piCout << "USE: piptest [src_ip_port] [dst_ip_port] {filename}";
|
||||
return 0;
|
||||
}
|
||||
piCout << (PISystemTime::current() - tm).toMicroseconds();
|
||||
*/
|
||||
//tm = PISystemTime::current();
|
||||
/*for (int i = 0; i < 100000000; ++i) {
|
||||
pl.append(i*10);
|
||||
pl.prepend(i*10 + 1);
|
||||
}*/
|
||||
//PICodeParser cd_;
|
||||
//cd.includeDirectory("../qpicalculator");
|
||||
//cd_.parseFile("piincludes.h");
|
||||
//piCout << (PISystemTime::current() - tm).toMilliseconds();
|
||||
/*piCout << NewLine;
|
||||
piForeachCA (i, pl)
|
||||
piCout << i;
|
||||
|
||||
pl.remove(1, 2).prepend(111).prepend(222);
|
||||
pl.remove(1, 1);
|
||||
piCout << NewLine;
|
||||
piForeachCA (i, pl)
|
||||
piCout << i;*/
|
||||
/*piCout << NewLine;
|
||||
for (int i = 0; i < pl.size_s(); ++i)
|
||||
piCout << pl[i];
|
||||
*/
|
||||
|
||||
/*PIEthernet eth(PIEthernet::UDP);
|
||||
eth.setReadAddress("192.168.0.30:4001");
|
||||
eth.setSendAddress("192.168.0.50:4001");
|
||||
eth.startThreadedRead(readed);
|
||||
piCout << "Connected";
|
||||
//eth.send(PIString("This is test string!\n").toByteArray());
|
||||
FOREVER_WAIT*/
|
||||
|
||||
if (argc < 2) return 0;
|
||||
PICodeParser cd;
|
||||
//cd.includeDirectory("../qpicalculator");
|
||||
cd.parseFile(argv[1]);
|
||||
piForeachC (PICodeParser::Enum & e, cd.enums)
|
||||
piCout << e.name << e.members;
|
||||
|
||||
|
||||
//piCout << v.toType<float>();
|
||||
//piCout << v.toType<float>().toType<PIString>();
|
||||
//PIFile::remove("ki");
|
||||
/*PIConfig conf("protocols_commod.conf");
|
||||
piCout << conf.allTree();
|
||||
conf.setValue("rmd.123", 456);*/
|
||||
|
||||
/*PITimer tm;
|
||||
piCout << tm.debug() << tm.properties();
|
||||
tm.setDebug(false);
|
||||
piCout << tm.debug() << tm.properties();
|
||||
tm.setDebug(true);
|
||||
piCout << tm.debug() << tm.properties();*/
|
||||
|
||||
//PIObject * ser = (PIObject * )PIIODevice::createFromFullPath("file://OM2:38400:7");
|
||||
//piCout << ser << NewLine << ser->properties();
|
||||
PIKbdListener kbd;
|
||||
kbd.enableExitCapture();
|
||||
PIString src = argv[1];
|
||||
PIString dst = argv[2];
|
||||
UDPFileTransfer f(src, dst);
|
||||
piCout << "work directory" << f.ft.directory().absolutePath() << ", listen on" << src << ",send to" << dst;
|
||||
if (argc == 4) {
|
||||
PIString file = argv[3];
|
||||
piCout << "send file" << file;
|
||||
f.startSend(file);
|
||||
return 0;
|
||||
} else {
|
||||
piCout << "wait for receiving";
|
||||
}
|
||||
WAIT_FOR_EXIT
|
||||
return 0;
|
||||
// Ob o;
|
||||
// ft.setPacketSize(65536);
|
||||
// PITimeMeasurer tm;
|
||||
// o.startsend();
|
||||
// piCout << tm.elapsed_s();
|
||||
// return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user