-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_main.cpp
100 lines (79 loc) · 2.02 KB
/
client_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// AUTHOR: Zhaoning Kong
// Email: [email protected]
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <cinttypes>
#include <iostream>
#include <ndn-cxx/face.hpp>
#include <ndn-cxx/name.hpp>
#include <string>
#include <thread>
#include <vector>
#include "svs.hpp"
class Options {
public:
Options() : prefix("/ndn/svs") {}
public:
ndn::Name prefix;
uint64_t m_id;
};
namespace ndn {
namespace svs {
class Program {
public:
explicit Program(const Options &options)
: m_options(options),
m_svs(m_options.m_id,
std::bind(&Program::onMsg, this, std::placeholders::_1)) {
printf("SVS client %" PRIu64 " starts\n", m_options.m_id);
// Suppress warning
Interest::setDefaultCanBePrefix(true);
}
void run() {
m_svs.registerPrefix();
// Create other thread to run
std::thread thread_svs([this] { m_svs.run(); });
std::string init_msg = "User " +
boost::lexical_cast<std::string>(m_options.m_id) +
" has joined the groupchat";
m_svs.publishMsg(init_msg);
std::string userInput = "";
while (true) {
// send to Sync
std::getline(std::cin, userInput);
m_svs.publishMsg(userInput);
}
thread_svs.join();
}
private:
/**
* onMsg() - Callback on receiving msg from sync layer.
*/
void onMsg(const std::string &msg) {
// Parse received msg
std::vector<std::string> result;
size_t cursor = msg.find(":");
result.push_back(msg.substr(0, cursor));
if (cursor < msg.length() - 1)
result.push_back(msg.substr(cursor + 1));
else
result.push_back("");
// Print to stdout
printf("User %s>> %s\n\n", result[0].c_str(), result[1].c_str());
}
const Options m_options;
SVS m_svs;
};
} // namespace svs
} // namespace ndn
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: TODO\n");
exit(1);
}
Options opt;
opt.m_id = std::stoll(argv[1]);
ndn::svs::Program program(opt);
program.run();
return 0;
}