dispatchers.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include <boost/format.hpp>
  2. #include <cctype>
  3. #include "dispatchers.h"
  4. #include "logging.h"
  5. Dispatch::Dispatch(Session *s) : sess{s} {};
  6. Dispatch::~Dispatch(){};
  7. void Dispatch::to_server(const std::string &send) { sess->to_server(send); }
  8. void Dispatch::to_client(const std::string &send) { sess->to_client(send); }
  9. const std::string &Dispatch::get_prompt(void) { return sess->get_prompt(); }
  10. void Dispatch::setNotify(notifyFunc nf) { notify_ = nf; }
  11. void Dispatch::notify(void) {
  12. if (notify_) {
  13. sess->post(notify_);
  14. notify_ = nullptr;
  15. }
  16. }
  17. MainDispatch::MainDispatch(Session *s) : Dispatch{s}, id{s} {
  18. BUGZ_LOG(warning) << "MainDispatch()";
  19. }
  20. MainDispatch::~MainDispatch() { BUGZ_LOG(warning) << "~MainDispatch()"; }
  21. void MainDispatch::activate(void) {
  22. // how to set this event to our method?
  23. sess->emit_server_line = [this](const std::string &s) { server_line(s); };
  24. sess->emit_server_prompt = [this](const std::string &s) { server_prompt(s); };
  25. sess->emit_client_input = nullptr;
  26. // sess->emit_client_input = [this](const std::string &s) { client_input(s);
  27. // };
  28. sess->show_client = false; // don not auto-send server to client
  29. sess->talk_direct = false; // do not auto-send client to server
  30. count = 0;
  31. old_prompt = sess->get_prompt();
  32. to_client("\n\r\x1b[1;34mWELCOME! You are now in the proxy zone...\n\r");
  33. id.prompt = "Proxy Main :=> ";
  34. id.max_length = 25;
  35. id.setNotify([this]() { this->have_input(); });
  36. id.activate();
  37. }
  38. void MainDispatch::have_input(void) {
  39. ++count;
  40. std::string output =
  41. str(boost::format("Your Input (%2%): [%1%]\n\r") % id.input % count);
  42. to_client(output);
  43. if (count >= 5) {
  44. to_client("Returning you to the game...\n\r");
  45. deactivate();
  46. } else {
  47. // prompt it again, sam.
  48. id.setNotify([this]() { this->have_input(); });
  49. id.activate();
  50. }
  51. }
  52. void MainDispatch::deactivate(void) {
  53. // Since we're the main thing there --
  54. sess->emit_server_line = nullptr;
  55. sess->emit_server_prompt = nullptr;
  56. sess->emit_client_input = nullptr;
  57. sess->show_client = true;
  58. sess->talk_direct = true;
  59. sess->set_prompt(old_prompt);
  60. notify();
  61. }
  62. void MainDispatch::server_line(const std::string &line) {
  63. BUGZ_LOG(info) << "MDSL: " << line;
  64. to_client("SL: ");
  65. to_client(line);
  66. to_client("\n\r");
  67. }
  68. void MainDispatch::server_prompt(const std::string &prompt) {
  69. BUGZ_LOG(info) << "MDSP: " << prompt;
  70. }
  71. #ifdef NEVERMORE
  72. void MainDispatch::client_input(const std::string &input) {
  73. // I don't care what the old prompt looked liked at this point.
  74. BUGZ_LOG(warning) << "Got: " << input; // << " prompt=" << get_prompt();
  75. // Serious problem when the input = "\x1b" ESC. The output gets gummed/locked
  76. // up.
  77. if (input == "\x1b") {
  78. return;
  79. }
  80. ++count;
  81. std::string output = str(boost::format("MSG %1%: [%2%]\n\r") % count % input);
  82. to_client(output);
  83. if (count >= 5) {
  84. to_client("And we're outta here!\n\r");
  85. deactivate();
  86. }
  87. }
  88. #endif
  89. InputDispatch::InputDispatch(Session *s) : Dispatch(s) {
  90. BUGZ_LOG(warning) << "InputDispatch()";
  91. }
  92. void InputDispatch::activate(void) {
  93. ds = sess->save_settings();
  94. sess->emit_server_line = [this](const std::string &s) { server_line(s); };
  95. sess->emit_server_prompt =
  96. nullptr; // [this](const std::string &s) { server_prompt(s); };
  97. sess->emit_client_input = [this](const std::string &s) { client_input(s); };
  98. sess->show_client = false; // don not auto-send server to client
  99. sess->talk_direct = false; // do not auto-send client to server
  100. input.clear();
  101. to_client(prompt);
  102. }
  103. void InputDispatch::deactivate(void) {
  104. sess->restore_settings(ds);
  105. notify();
  106. }
  107. void InputDispatch::server_line(const std::string &line) {}
  108. // void InputDispatch::server_prompt(const std::string &prompt) {}
  109. void InputDispatch::client_input(const std::string &cinput) {
  110. for (const char ch : cinput) {
  111. if (isprint(ch)) {
  112. // Ok!
  113. if (input.length() < max_length) {
  114. to_client(std::string(1, ch));
  115. input += ch;
  116. }
  117. } else if ((ch == '\b') || (ch == 0x7f)) {
  118. // Backspace or rubout
  119. if (input.length() > 0) {
  120. to_client("\b \b");
  121. input.erase(input.size() - 1);
  122. }
  123. } else if (ch == '\r') {
  124. // Ok, we're done!
  125. BUGZ_LOG(info) << "InputDispatch done: " << input;
  126. to_client("\n\r");
  127. deactivate();
  128. }
  129. }
  130. }
  131. /*
  132. * CoreDispatch: This is an example class that does dispatch.
  133. * Copy this and make changes from there...
  134. */
  135. CoreDispatch::CoreDispatch(Session *s) : Dispatch{s} {
  136. BUGZ_LOG(warning) << "CoreDispatch()";
  137. }
  138. void CoreDispatch::activate(void) {
  139. // save things, set things
  140. }
  141. void CoreDispatch::deactivate(void) {
  142. // restore things
  143. notify();
  144. }
  145. void CoreDispatch::server_line(const std::string &line) {}
  146. void CoreDispatch::server_prompt(const std::string &prompt) {}
  147. void CoreDispatch::client_input(const std::string &input) {
  148. BUGZ_LOG(warning) << "Got: " << input << " prompt=" << get_prompt();
  149. }