#include "hmbdc/app/tcpcast/NetContext.hpp"
#include <iostream>
#include <string>
#include <memory>
#include <unistd.h>
using namespace std;
using namespace hmbdc::app;
struct ChatMessage
char id[16];
char msg[1000];
};
static_assert(sizeof(ChatMessage) == 1016, "good idea to check message size remain consistent");
struct Chatter
:
Client<Chatter, ChatMessage> {
Chatter(char const* id)
: id_(id){}
void handleMessageCb(ChatMessage const& m) {
if (id_ != m.id) {
cout << m.id << ": " << m.msg << endl;
}
}
private:
string id_;
};
int main(int argc, char** argv) {
using namespace std;
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("loopback", true);
auto& net = tcpcast::NetContext::instance();
ctx.start(1
, 0x02);
Chatter chatter(myId);
ctx.addToPool(chatter);
net.listenTo(t);
auto s = net.getSender(t);
string line;
ChatMessage m;
strncpy(m.id, myId, sizeof(m.id));
cout << "start type a message" << endl;
cout << "ctrl-d to terminate" << endl;
while(getline(cin, line)) {
strncpy(m.msg, line.c_str(), sizeof(m.msg));
s->send(m);
}
net.stopListenTo(t);
}