for usage.
#include "hmbdc/app/tcpcast/NetContext.hpp"
#include "hmbdc/app/Context.hpp"
#include <iostream>
#include <string>
#include <memory>
#include <unistd.h>
struct ChatMessage
char id[16];
char msg[1000];
};
struct Attachement
char id[16];
};
struct Chatter
:
Client<Chatter, ChatMessage, Attachement> {
Chatter(char const* id)
: id_(id){}
void handleMessageCb(ChatMessage const& m) {
cout << m.id << ": " << m.msg << endl;
}
void handleMessageCb(Attachement const& m) {
cout << "received " << m.file << " from " << m.id << ' ' << m.len << " bytes" << endl;
}
private:
string id_;
};
int main(int argc, char** argv) {
if (argc != 4) {
cerr << argv[0] << " local-ip chat-group-name my-name" << endl;
cerr << "multicast should be enabled on local-ip network" << endl;
return -1;
}
auto ifaceAddr = argv[1];
auto chatGroup = argv[2];
auto myId = argv[3];
config.put("ifaceAddr", ifaceAddr);
config.put("downloadDir", "/tmp");
config.put("loopback", true);
MyContext ctx;
SingletonGuardian<NetCtx> g;
auto& net = NetCtx::instance();
auto sengine = net.createSendTransportEngine(config
, sizeof(ChatMessage));
auto rengine = net.createRecvTransportEngine(config
, ctx.buffer());
ctx.start(1
, 0x01);
ctx.addToPool(*rengine);
ctx.addToPool(*sengine);
Chatter chatter(myId);
ctx.addToPool(chatter);
Topic t(chatGroup);
net.listenTo(t);
auto s = net.getSender(t);
string line;
ChatMessage m;
strncpy(m.id, myId, sizeof(m.id));
cout << "start type a message or send a file. \n\\<file> to send a file" << endl;
cout << "ctrl-d to terminate" << endl;
while(getline(cin, line)) {
if (line[0] == '\\') {
Attachement attach;
strncpy(attach.id, myId, sizeof(attach.id));
if (attach.map(line.c_str() + 1)) {
s->send(attach);
} else {
cout << "wrong file!" << endl;
}
} else {
strncpy(m.msg, line.c_str(), sizeof(m.msg));
s->send(m);
}
}
net.stopListenTo(t);
ctx.stop();
ctx.join();
}