1 #include "hmbdc/app/Base.hpp" 3 #include <boost/format.hpp> 9 #include <unordered_set> 11 namespace hmbdc {
namespace app {
namespace utils {
13 namespace consoleclient_detail {
25 template <
typename NetCtx>
27 :
Client<ConsoleClient<NetCtx>, JustBytes> {
38 , istream& myCin = cin
39 , ostream& myCout = cout
40 , ostream& myCerr = cerr
42 , std::unordered_set<uint16_t> memoryAttachmentTags = std::unordered_set<uint16_t>()
50 , memoryAttachmentTags_(memoryAttachmentTags) {
51 thread stdinThread([
this]() {
52 stdinThreadEntrance();
54 stdinThread_ = move(stdinThread);
57 char const* hmbdcName()
const {
66 static char const*
help() {
68 "\nCONSOLE LANGUAGE:\n" 69 "topic <topic>, set current topic\nexample: topic console\n\n" 70 "ohex, output message in hex format\nexample: ohex\n\n" 71 "sendhex <tag> <hex bytes>, send a binary message with a message tag\nexample: sendhex 1001 01 00 0f ef\n\n" 72 "ostr, output message in string format\nexample: ostr\n\n" 73 "sendstr <tag> <string>, send a string message with a message tag\nexample: sendstr 1001 hello\n\n" 74 "listen <topic>, console start to listen to the topic\nexample: listen my_topic\n\n" 75 "stoplisten <topic>, console stop listen to the topic\nexample: stoplisten john\n\n" 79 void stoppedCb(exception
const& e)
override {
80 myCout_ << (e.what()) << endl;
93 myCout_ << dec << tag;
95 if (memoryAttachmentTags_.find(tag) != memoryAttachmentTags_.end()) {
100 for (
auto p = bytes; p != bytes + ctx_.maxMessageSize(); ++p) {
101 myCout_ <<
' ' << boost::format(
"%02x") % (uint16_t)*p;
105 for (
auto p = (uint8_t*)att->attachment; p != (uint8_t*)att->attachment + att->len; ++p) {
106 myCout_ << boost::format(
"%02x") % (uint16_t)*p <<
' ';
110 myCout_ <<
' ' << string((
char*)bytes, strnlen((
char*)bytes, ctx_.maxMessageSize()));
113 myCout_ << string((
char*)att->attachment, att->len);
127 void stdinThreadEntrance() {
128 processCmd(initCmd_);
132 void processCmd(istream& is) {
134 while(getline(is, line)) {
135 if (!line.size())
continue;
136 istringstream iss{line};
142 Topic tmp(topic.c_str());
143 auto sender = NetCtx::instance().getSender(tmp);
148 myCerr_ <<
" cannot find transport for " << topic << endl;
150 }
else if (op ==
"sendstr") {
152 myCerr_ <<
" topic is not set yet" << endl;
160 sender_->sendBytes(tag, msg.c_str() + 1,
161 min(msg.size(), ctx_.maxMessageSize())
164 myCerr_ <<
" sendstr syntax error" << endl;
166 }
else if (op ==
"sendstratt") {
168 myCerr_ <<
" topic is not set yet" << endl;
175 if (!iss || !getline(is, line)) {
176 myCerr_ <<
" sendstratt syntax error" << endl;
179 auto att = malloc(line.size());
180 memcpy(att, line.c_str(), line.size());
182 char rawMsg[rawSize];
184 hma->attachment = att;
185 hma->len = line.size();
187 sender_->sendBytes(tag, hma, min(rawSize, ctx_.maxMessageSize()));
188 }
else if (op ==
"sendhex") {
190 myCerr_ <<
" topic is not set yet" << endl;
196 vector<uint8_t> bytes;
199 bytes.push_back((uint8_t)tmp);
203 sender_->sendBytes(tag, &bytes[0],
204 min(bytes.size(), ctx_.maxMessageSize())
207 myCerr_ <<
" sendhex syntax error" << endl;
209 }
else if (op ==
"sendhexatt") {
211 myCerr_ <<
" topic is not set yet" << endl;
218 vector<uint8_t> bytes;
221 bytes.push_back((uint8_t)tmp);
223 if (!iss.eof() || !getline(is, line)) {
224 myCerr_ <<
" sendhexhex syntax error" << endl;
228 istringstream iss{line};
230 vector<uint8_t> attBytes;
232 attBytes.push_back((uint8_t)tmp);
235 auto att = malloc(attBytes.size());
236 memcpy(att, &(attBytes[0]), attBytes.size());
238 char rawMsg[rawSize];
240 hma->attachment = att;
241 hma->len = attBytes.size();
243 sender_->sendBytes(tag, hma, min(rawSize, ctx_.maxMessageSize()));
244 }
else if (op ==
"ohex") {
246 }
else if (op ==
"ostr") {
248 }
else if (op ==
"listen") {
251 NetCtx::instance().listenTo(
Topic(topic));
252 }
else if (op ==
"stoplisten") {
255 NetCtx::instance().stopListenTo(
Topic(topic));
257 myCerr_ <<
" unknown command " << op << endl;
269 typename NetCtx::Sender* sender_;
270 istringstream initCmd_;
271 std::unordered_set<uint16_t> memoryAttachmentTags_;
276 template <
typename NetCtx>
topic as in the publish / subscribe communication paradigm
Definition: Topic.hpp:14
Definition: TypedString.hpp:74
static char const * help()
documentation for all commands the console interprets
Definition: ConsoleClient.hpp:66
void waitUntilFinish()
wait until the console closes
Definition: ConsoleClient.hpp:122
a Client that work as a console to send and receive network messages
Definition: ConsoleClient.hpp:26
A Context is like a media object that facilitates the communications for the Clients that it is holdi...
Definition: Context.hpp:402
ConsoleClient(Context<> &ctx, istream &myCin=cin, ostream &myCout=cout, ostream &myCerr=cerr, string initCmd="", std::unordered_set< uint16_t > memoryAttachmentTags=std::unordered_set< uint16_t >())
constructor
Definition: ConsoleClient.hpp:37
void handleJustBytesCb(uint16_t tag, uint8_t *bytes)
callback for JustBytes
Definition: ConsoleClient.hpp:92
A Client represents a thread of execution/a task. The execution is managed by a Context. a Client object could participate in message dispatching as the receiver of specifed message types.
Definition: Client.hpp:47
if a specific hmbdc network transport (for example tcpcast) supports message with memory attachment...
Definition: Message.hpp:158