146 lines
4.7 KiB
C++
146 lines
4.7 KiB
C++
#include "pip.h"
|
|
#include "pifiletransfer.h"
|
|
#include "pidatatransfer.h"
|
|
|
|
|
|
using namespace PICoutManipulators;
|
|
|
|
class UDPFileTransfer: public PITimer {
|
|
PIOBJECT_SUBCLASS(UDPFileTransfer, PITimer)
|
|
public:
|
|
UDPFileTransfer(const PIString &src_ip_port, const PIString &dst_ip_port) {
|
|
quet_ = false;
|
|
eth.setReadAddress(src_ip_port);
|
|
eth.setSendAddress(dst_ip_port);
|
|
ft.setPacketSize(8192);
|
|
ft.setName("PIFT");
|
|
CONNECTU(&ft, sendRequest, this, ftsend);
|
|
CONNECTU(&ft, sendFilesStarted, this, ftevent);
|
|
CONNECTU(&ft, receiveFilesStarted, this, ftevent);
|
|
CONNECTU(&ft, sendFilesFinished, this, ftevent);
|
|
CONNECTU(&ft, receiveFilesFinished, this, ftevent);
|
|
CONNECTU(ð, threadedReadEvent, this, received);
|
|
start(50);
|
|
eth.setParameter(PIEthernet::SeparateSockets);
|
|
eth.startThreadedRead();
|
|
}
|
|
|
|
void setQuet(bool quet) {quet_ = quet;}
|
|
|
|
void startSend(const PIStringList &file) {
|
|
ft.send(file);
|
|
}
|
|
|
|
PIFileTransfer ft;
|
|
|
|
private:
|
|
PIEthernet eth;
|
|
bool quet_;
|
|
|
|
void tick(void *, int) {
|
|
if (ft.isStarted()) {
|
|
ftevent();
|
|
if (PIKbdListener::exiting) {
|
|
ft.stopSend();
|
|
ft.stopReceive();
|
|
}
|
|
// piCout << (int)ft.diagnostic().quality();
|
|
}
|
|
}
|
|
|
|
EVENT_HANDLER(void, ftevent) {
|
|
if (quet_) return;
|
|
|
|
#ifdef WINDOWS
|
|
piCout
|
|
#else
|
|
PICout(AddSpaces) << ClearLine
|
|
#endif
|
|
<< ft.stateString() << ft.curFile()
|
|
<< PIString::readableSize(ft.diagnostic().receiveBytesPerSec()) + "/s"
|
|
<< PIString::readableSize(ft.diagnostic().sendBytesPerSec()) + "/s"
|
|
<< "(" << PIString::readableSize(ft.bytesFileCur()) << "/" << PIString::readableSize(ft.bytesFileAll()) << ", "
|
|
<< PIString::readableSize(ft.bytesCur()) << "/" << PIString::readableSize(ft.bytesAll()) << ")"
|
|
<< "ETA" << (ft.diagnostic().receiveBytesPerSec() > 0 ?
|
|
PIString::fromNumber(PISystemTime::fromSeconds((ft.bytesAll() - ft.bytesCur()) /
|
|
ft.diagnostic().receiveBytesPerSec()).toSeconds())
|
|
: PIString("unknown"))
|
|
#ifndef WINDOWS
|
|
<< Flush
|
|
#endif
|
|
;
|
|
}
|
|
|
|
EVENT_HANDLER1(void, ftsend, PIByteArray &, data) {
|
|
eth.send(data);
|
|
}
|
|
|
|
EVENT_HANDLER2(void, received, uchar * , readed, int, size) {
|
|
PIByteArray ba(readed, size);
|
|
ft.received(ba);
|
|
}
|
|
};
|
|
|
|
|
|
void usage() {
|
|
piCout << Bold << "PIP UDP file transfer";
|
|
piCout << Cyan << "Version" << Bold << PIPVersion() << NewLine;
|
|
piCout << Green << Bold << "Usage:" << Default << "\"pift [-hqf] -r <receive_ip> -s <send_ip> [-d <work_dir>] [-p port] [<path1>] [<path2>] [<path3>] [...]\"" << NewLine;
|
|
piCout << Green << Bold << "Details:";
|
|
piCout << "-f " << Green << "- full path in <receive_ip> and <send_ip>";
|
|
piCout << "-h " << Green << "- display this message and exit";
|
|
piCout << "-q " << Green << "- quiet, no debug output to console";
|
|
piCout << "-r " << Green << "- set receive ip address, must be ip address of this computer";
|
|
piCout << "-s " << Green << "- set send ip address, address of computer which communicate with you";
|
|
piCout << "-p " << Green << "- UDP port for transfer, by default 50005";
|
|
piCout << "-d <work_dir> " << Green << "- directory, where place received files";
|
|
piCout << "<path> " << Green << "- add path to send, if no path, then \"pift\" working in receive mode";
|
|
}
|
|
|
|
|
|
int main (int argc, char * argv[]) {
|
|
PICLI cli(argc, argv);
|
|
PIINTROSPECTION_START
|
|
cli.setOptionalArgumentsCount(-1);
|
|
cli.addArgument("send", true);
|
|
cli.addArgument("receive", true);
|
|
cli.addArgument("dir", true);
|
|
cli.addArgument("port", true);
|
|
cli.addArgument("help");
|
|
cli.addArgument("quet");
|
|
cli.addArgument("fullpath");
|
|
if ((!cli.hasArgument("send") || !cli.hasArgument("receive")) || cli.hasArgument("help")) {
|
|
usage();
|
|
return 0;
|
|
}
|
|
PIString src = cli.argumentValue("receive");
|
|
PIString dst = cli.argumentValue("send");
|
|
int port = -1;
|
|
if (cli.hasArgument("port")) port = cli.argumentValue("port").toInt();
|
|
if (port <=0) port = 50005;
|
|
if (!cli.hasArgument("fullpath")) {
|
|
src += ":"+PIString::fromNumber(port);
|
|
dst += ":"+PIString::fromNumber(port);
|
|
}
|
|
PIKbdListener kbd;
|
|
kbd.enableExitCapture();
|
|
UDPFileTransfer f(src, dst);
|
|
if (cli.hasArgument("dir")) f.ft.setDirectory(cli.argumentValue("dir"));
|
|
if (cli.hasArgument("quet")) f.setQuet(true);
|
|
piCout << "work directory" << f.ft.directory().absolutePath() << ", listen on" << src << ", send to" << dst;
|
|
PIStringList fls = cli.optionalArguments();
|
|
if (fls.size() > 0) {
|
|
piCout << "send files" << fls;
|
|
f.startSend(fls);
|
|
PICout(0) << "\n";
|
|
return 0;
|
|
} else {
|
|
piCout << "wait for receiving";
|
|
}
|
|
WAIT_FOR_EXIT
|
|
PICout(0) << "\n";
|
|
return 0;
|
|
|
|
}
|
|
|