1 #include "hmbdc/app/LoggerT.hpp" 2 #include "hmbdc/app/Context.hpp" 4 #include <boost/format.hpp> 11 namespace hmbdc {
namespace app {
namespace utils {
23 template <
typename NetCtx>
25 :
Client<ConsoleClient<NetCtx>, JustBytes> {
36 , istream& myCin = cin
37 , ostream& myCout = cout
38 , ostream& myCerr = cerr
39 ,
string initCmd =
"")
48 thread stdinThread([
this]() {
49 stdinThreadEntrance();
51 stdinThread_ = move(stdinThread);
54 char const* hmbdcName()
const {
63 static char const*
help() {
65 "\nCONSOLE LANGUAGE:\n" 66 "topic <topic>, set current topic\nexample: topic console\n\n" 67 "ohex, output message in hex format\nexample: ohex\n\n" 68 "sendhex <tag> <hex bytes>, send a binary message with a message tag\nexample: sendhex 1001 01 00 0f ef\n\n" 69 "ostr, output message in string format\nexample: ostr\n\n" 70 "sendstr <tag> <string>, send a string message with a message tag\nexample: sendstr 1001 hello\n\n" 71 "listen <topic>, console start to listen to the topic\nexample: listen my_topic\n\n" 72 "stoplisten <topic>, console stop listen to the topic\nexample: stoplisten john\n\n" 76 void stoppedCb(exception
const& e){
77 myCout_ << (e.what()) << endl;
90 myCout_ << dec << tag;
92 for (
auto p = bytes; p != bytes + ctx_.maxMessageSize(); ++p) {
93 myCout_ <<
' ' << boost::format(
"%02x") % (uint16_t)*p;
96 myCout_ <<
' ' << string((
char*)bytes, strnlen((
char*)bytes, ctx_.maxMessageSize()));
109 void stdinThreadEntrance() {
110 processCmd(initCmd_);
114 void processCmd(istream& is) {
116 while(getline(is, line)) {
117 if (!line.size())
continue;
118 istringstream iss{line};
124 Topic tmp(topic.c_str());
125 auto sender = NetCtx::instance().getSender(tmp);
130 myCerr_ <<
" cannot find transport for " << topic << endl;
132 }
else if (op ==
"sendstr") {
134 myCerr_ <<
" topic is not set yet" << endl;
142 sender_->sendBytes(tag, msg.c_str(),
143 min(msg.size() + 1u, ctx_.maxMessageSize())
146 myCerr_ <<
" sendstr grammar error" << endl;
148 }
else if (op ==
"sendhex") {
150 myCerr_ <<
" topic is not set yet" << endl;
156 vector<uint8_t> bytes;
159 bytes.push_back((uint8_t)tmp);
163 sender_->sendBytes(tag, &bytes[0],
164 min(bytes.size(), ctx_.maxMessageSize())
167 myCerr_ <<
" sendhex grammar error" << endl;
169 }
else if (op ==
"ohex") {
171 }
else if (op ==
"ostr") {
173 }
else if (op ==
"listen") {
176 NetCtx::instance().listenTo(
Topic(topic));
177 }
else if (op ==
"stoplisten") {
180 NetCtx::instance().stopListenTo(
Topic(topic));
182 myCerr_ <<
" unknown command " << op << endl;
194 typename NetCtx::Sender* sender_;
195 istringstream initCmd_;
static char const * help()
documentation for all commands the console interprets
Definition: ConsoleClient.hpp:63
void handleJustBytesCb(uint16_t tag, uint8_t *bytes)
callback for JustBytes
Definition: ConsoleClient.hpp:89
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
a Client that work as a console to send and receive network messages
Definition: ConsoleClient.hpp:24
Definition: TypedString.hpp:74
ConsoleClient(Context<> &ctx, istream &myCin=cin, ostream &myCout=cout, ostream &myCerr=cerr, string initCmd="")
construct
Definition: ConsoleClient.hpp:35
Definition: Context.hpp:384
void waitUntilFinish()
wait until the console closes
Definition: ConsoleClient.hpp:104
Definition: Client.hpp:39
Definition: Client.hpp:11