Commit fd925a0448722fe310ee7ee8bec7417c387a3ac7

Authored by Perry Werneck
1 parent dae54685
Exists in master and in 1 other branch develop

Refactoring convenience object.

client/src/core/host.cc
... ... @@ -49,13 +49,12 @@
49 49  
50 50 namespace TN3270 {
51 51  
52   - /*
53 52 Host::Host(const char *id, const char *url, time_t timeout) {
54 53  
55 54 debug("Creating host id=\"", id);
56 55  
57 56 this->timeout = timeout;
58   - this->session = Session::create(id);
  57 + this->session = Session::getInstance(id);
59 58 if(url) {
60 59 this->connect(url);
61 60 }
... ... @@ -104,14 +103,7 @@
104 103  
105 104 }
106 105  
107   - Host & Host::push(const Action action) {
108   - session->push(action);
109   - sync();
110   - return *this;
111   - }
112   -
113 106 bool Host::isReady() const {
114   -// this->session->waitForReady(this->timeout);
115 107 return getProgramMessage() == MESSAGE_NONE;
116 108 }
117 109  
... ... @@ -194,28 +186,13 @@
194 186 return *this;
195 187 }
196 188  
197   - /// @brief Send PF.
198   - Host & Host::pfkey(unsigned short value) {
199   - this->session->pfkey(value);
200   - return *this;
201   - }
202   -
203   - /// @brief Send PA.
204   - Host & Host::pakey(unsigned short value) {
205   - this->session->pakey(value);
206   - return *this;
207   - }
208   -
209   - /// @brief Request print
210   - Host & Host::print(LIB3270_CONTENT_OPTION option) {
211   - throw std::system_error(ENOTSUP, std::system_category());
212   - }
213   -
  189 + /*
214 190 Host & Host::input(const char *text, size_t sz) {
215 191 this->session->input(text, sz);
216 192 return *this;
217 193 }
218 194  
  195 +
219 196 Host & Host::input(const char *text, const char control_char) {
220 197  
221 198 for(const char * ptr = strchr(text,control_char); ptr; ptr = strchr(text,control_char)) {
... ... @@ -467,6 +444,5 @@
467 444 }
468 445 */
469 446  
470   -
471 447 }
472 448  
... ...
client/src/testprogram/testprogram.cc
... ... @@ -44,6 +44,7 @@
44 44  
45 45 /*---[ Implement ]----------------------------------------------------------------------------------*/
46 46  
  47 +/*
47 48 // Test "Session" object
48 49 static void chkSession() {
49 50  
... ... @@ -81,37 +82,10 @@
81 82 delete hSession;
82 83  
83 84 }
  85 + */
84 86  
85   - int main(int argc, char **argv) {
86   -
87   - const char * session = ""; // "pw3270:a";
88   -
89   - #pragma GCC diagnostic push
90   - #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
91   - static struct option options[] = {
92   - { "session", required_argument, 0, 's' },
93   - { 0, 0, 0, 0}
94   -
95   - };
96   - #pragma GCC diagnostic pop
97   -
98   - int long_index =0;
99   - int opt;
100   - while((opt = getopt_long(argc, argv, "s:", options, &long_index )) != -1) {
101   -
102   - switch(opt) {
103   - case 's':
104   - session = optarg;
105   - break;
106   -
107   - }
108   -
109   - }
110   -
111   - chkSession();
112   -
113   - /*
114   - cout << "Session: " << session << endl;
  87 + // test host object
  88 + static void testHost(const char *session) {
115 89  
116 90 TN3270::Host host{session};
117 91  
... ... @@ -143,8 +117,37 @@
143 117 cerr << std::endl << e.what() << std::endl << std::endl;
144 118  
145 119 }
146   - */
147 120  
  121 + }
  122 +
  123 + int main(int argc, char **argv) {
  124 +
  125 + const char * session = ""; // "pw3270:a";
  126 +
  127 + #pragma GCC diagnostic push
  128 + #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  129 + static struct option options[] = {
  130 + { "session", required_argument, 0, 's' },
  131 + { 0, 0, 0, 0}
  132 +
  133 + };
  134 + #pragma GCC diagnostic pop
  135 +
  136 + int long_index =0;
  137 + int opt;
  138 + while((opt = getopt_long(argc, argv, "s:", options, &long_index )) != -1) {
  139 +
  140 + switch(opt) {
  141 + case 's':
  142 + session = optarg;
  143 + break;
  144 +
  145 + }
  146 +
  147 + }
  148 +
  149 + cout << "Session: " << session << endl;
  150 + testHost(session);
148 151  
149 152 return 0;
150 153 }
... ...
common/src/include/lib3270/ipc.h
... ... @@ -255,14 +255,18 @@
255 255 virtual void push(int baddr, const char *text, int length) = 0;
256 256 virtual void push(int row, int col, const char *text, int length) = 0;
257 257  
258   - inline void push(int row, int col, const std::string &text) {
259   - push(row,col,text.c_str(),text.size());
  258 + inline void push(const std::string &text) {
  259 + push(-1,text.c_str(),text.size());
260 260 }
261 261  
262 262 inline void push(int baddr, const std::string &text) {
263 263 push(baddr,text.c_str(),text.size());
264 264 }
265 265  
  266 + inline void push(int row, int col, const std::string &text) {
  267 + push(row,col,text.c_str(),text.size());
  268 + }
  269 +
266 270 void push(const PFKey key);
267 271 void push(const PAKey key);
268 272 virtual void push(const Action action) = 0;
... ... @@ -354,7 +358,6 @@
354 358 };
355 359  
356 360 /// @brief TN3270 Host
357   - /*
358 361 class TN3270_PUBLIC Host : public std::basic_streambuf<char, std::char_traits<char> > {
359 362 private:
360 363  
... ... @@ -389,9 +392,32 @@
389 392 return session->getConnectionState() == state;
390 393 }
391 394  
  395 + // Contents
  396 + Host & pop(int baddr, std::string &text);
  397 + Host & pop(int row, int col, std::string &text);
  398 + Host & pop(std::string &text);
  399 +
  400 + std::string toString() const;
  401 + std::string toString(int baddr, size_t len = -1, char lf = '\n') const;
  402 + std::string toString(int row, int col, size_t len = -1, char lf = '\n') const;
  403 +
  404 + template<typename T>
  405 + Host & push(T value) {
  406 + session->push(value);
  407 + sync();
  408 + return *this;
  409 + }
  410 +
  411 + template<typename T>
  412 + Host & pop(T value) {
  413 + sync();
  414 + session->pop(value);
  415 + return *this;
  416 + }
  417 +
  418 + // Actions
392 419 Host & connect(const char *url = nullptr, bool sync = true);
393 420 Host & disconnect();
394   -
395 421 Host & waitForReady(time_t timeout = DEFAULT_TIMEOUT);
396 422  
397 423 /// @brief Execute action by name.
... ... @@ -424,17 +450,16 @@
424 450 }
425 451  
426 452 /// @brief Set cursor address.
427   - inline void setCursor(unsigned short addr) {
  453 + inline Host & setCursor(unsigned short addr) {
428 454 session->setCursor(addr);
  455 + return *this;
429 456 }
430 457  
431   - inline void setCursorAddress(unsigned short addr) {
432   - session->setCursor(addr);
433   - }
434 458  
435 459 /// @brief Set cursor position.
436   - inline void setCursor(unsigned short row, unsigned short col) {
  460 + inline Host & setCursor(unsigned short row, unsigned short col) {
437 461 session->setCursor(row,col);
  462 + return *this;
438 463 }
439 464  
440 465 /// @brief Get cursor address
... ... @@ -470,13 +495,22 @@
470 495 // Actions
471 496  
472 497 /// @brief Send PF.
473   - Host & pfkey(unsigned short value);
  498 + inline Host & pfkey(unsigned short value) {
  499 + session->pfkey(value);
  500 + return *this;
  501 + }
474 502  
475 503 /// @brief Send PA.
476   - Host & pakey(unsigned short value);
  504 + inline Host & pakey(unsigned short value) {
  505 + session->pakey(value);
  506 + return *this;
  507 + }
477 508  
478 509 /// @brief Request print
479   - Host & print(LIB3270_CONTENT_OPTION option = LIB3270_CONTENT_ALL);
  510 + inline Host & print(LIB3270_CONTENT_OPTION option = LIB3270_CONTENT_ALL) {
  511 + session->print(option);
  512 + return *this;
  513 + }
480 514  
481 515 /// @brief Wait.
482 516 inline Host & wait(unsigned short seconds) {
... ... @@ -499,73 +533,26 @@
499 533 ///
500 534 /// @param text String with the text to input.
501 535 /// @param control_char Control character used to identify commands.
502   - Host & input(const char *text, const char control_char = '@');
  536 + // Host & input(const char *text, const char control_char = '@');
503 537  
504   - Host & input(const char *text, size_t sz);
  538 + // Host & input(const char *text, size_t sz);
505 539  
506   - /// @brief Set field at current posicion, jumps to next writable field.
  540 + /// @brief Set field at current position, jumps to next writable field.
507 541 inline Host & push(const char *text) {
508   - session->push(text);
  542 + session->push(text,-1);
509 543 return *this;
510 544 };
511 545  
512   - inline Host & push(const std::string &text) {
513   - session->push(text);
514   - return *this;
515   -
516   - }
517   -
518   - inline Host & push(int baddr, const std::string &text) {
519   - session->push(baddr,text);
520   - return *this;
521   - }
522   -
523   - inline Host & push(int baddr, const char *text, int length = -1) {
524   - session->push(baddr,text,length);
525   - return *this;
526   - }
527   -
528   - inline Host & push(int row, int col, const std::string &text) {
529   - session->push(row,col,text);
530   - return *this;
531   - }
532   -
533   - inline Host & push(int row, int col, const char *text, int length = -1) {
534   - session->push(row,col,text,length);
535   - return *this;
536   - }
537   -
538   - inline Host & push(const PFKey key) {
539   - session->push(key);
540   - return *this;
541   - }
542   -
543   - inline Host & push(const PAKey key) {
544   - session->push(key);
545   - return *this;
546   - }
547   -
548   - Host & push(const Action action);
549   -
550   - // Get contents.
551   -
552   - Host & pop(int baddr, std::string &text);
553   - Host & pop(int row, int col, std::string &text);
554   - Host & pop(std::string &text);
555   -
556   - std::string toString() const;
557   - std::string toString(int baddr, size_t len = -1, char lf = '\n') const;
558   - std::string toString(int row, int col, size_t sz, char lf = '\n') const;
559   -
  546 + /*
560 547 // Event listeners
561 548 inline Host & insert(Event::Type type, std::function <void(const Event &event)> listener) noexcept {
562 549 session->insert(type, listener);
563 550 return *this;
564 551 }
  552 + */
565 553  
566 554  
567 555 };
568   - */
569 556  
570 557 }
571 558  
... ... @@ -573,7 +560,6 @@
573 560 TN3270_PUBLIC const char * toCharString(const TN3270::ConnectionState connectionState);
574 561 TN3270_PUBLIC const char * toCharString(const TN3270::Action action);
575 562  
576   - /*
577 563 template <typename T>
578 564 inline TN3270_PUBLIC TN3270::Session & operator<<(TN3270::Session& session, const T value) {
579 565 return session.push(value);
... ... @@ -593,7 +579,6 @@
593 579 stream << host.toString();
594 580 return stream;
595 581 }
596   - */
597 582  
598 583  
599 584 #endif
... ...