#include "hmbdc/app/rmcast/NetContext.hpp"
#include "hmbdc/os/Signals.hpp"
#include "hmbdc/os/DownloadFile.hpp"
#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace hmbdc;
using namespace hmbdc::app;
using namespace hmbdc::app::rmcast;
struct MemMappedFile
};
int main(int argc, char** argv) {
bool isSrc = true;
size_t recipientCount = 0;
if (argc == 5) {
isSrc = true;
recipientCount = boost::lexical_cast<size_t>(argv[4]);
} else if (argc == 4) {
isSrc = false;
} else {
cerr << argv[0] << " <iface-addr> <source-dir> <file-name> <recipient-count> | <iface-addr> <dest-dir> <file-name>" << endl;
exit(1);
}
string dir(argv[2]);
string file(argv[3]);
config.put("ifaceAddr", argv[1]);
MyContext ctx;
[&ctx] {
ctx.stop();
}
);
auto& net = NetContext::instance();
if (isSrc) {
config.put("topicRegex", file);
config.put("outBufferSizePower2", 20);
config.put("minRecvToStart", recipientCount);
auto sengine = net.createSendTransportEngine(config
auto sender = net.getSender(
Topic(file));
ctx.start(*sengine, 0);
MemMappedFile m;
m.hasMemoryAttachment::map((dir + "/" + file).c_str());
sender->send(m);
while (sengine->sessionsRemainingActive()) {
sleep(1);
}
ctx.stop();
} else {
config.put("allowRecvMemoryAttachment", MemMappedFile::typeTag);
struct FileWriter {
hasMemoryAttachment::AfterConsumedCleanupFunc afterConsumedCleanupFunc;
string dir_;
MyContext& ctx_;
FileWriter(string dir, MyContext& ctx)
: afterConsumedCleanupFunc(nullptr)
, dir_(dir)
, ctx_(ctx) {
}
void
allocateFor(std::pair<char const*, char const*> topic
, std::string const& senderId
, uint16_t typeTag
, void*& scratchpad) {
df->open(dir_.c_str(), string(topic.first, topic.second).c_str());
scratchpad = df;
}
void
oneSegReady(void*& scratchpad
, void const* seg
, uint16_t segLen
, size_t remainingSegCount) {
df->write(seg, segLen);
if (!remainingSegCount) {
df->close();
delete df;
ctx_.stop();
}
}
};
auto recvEngine = net.createRecvTransportEngine(config
, ctx.buffer()
, RecvTransport::NoOpArb()
, FileWriter(string(dir), ctx)
);
net.listenTo(
Topic(file));
ctx.start(*recvEngine, 0);
}
ctx.join();
}