Commit 09f7edb3def3e459c58886c5962acd5e9d2614d6
1 parent
833d8b5c
Exists in
master
and in
3 other branches
Working on new C++ API.
Showing
7 changed files
with
117 additions
and
12 deletions
Show diff stats
src/include/lib3270++.h
| @@ -33,6 +33,8 @@ | @@ -33,6 +33,8 @@ | ||
| 33 | 33 | ||
| 34 | #include <iostream> | 34 | #include <iostream> |
| 35 | #include <cstdarg> | 35 | #include <cstdarg> |
| 36 | + #include <vector> | ||
| 37 | + #include <functional> | ||
| 36 | #include <lib3270.h> | 38 | #include <lib3270.h> |
| 37 | 39 | ||
| 38 | #if defined(_WIN32) | 40 | #if defined(_WIN32) |
| @@ -63,8 +65,9 @@ | @@ -63,8 +65,9 @@ | ||
| 63 | namespace TN3270 { | 65 | namespace TN3270 { |
| 64 | 66 | ||
| 65 | class Host; | 67 | class Host; |
| 68 | + class Controller; | ||
| 66 | 69 | ||
| 67 | - class Event { | 70 | + class TN3270_PUBLIC Event { |
| 68 | public: | 71 | public: |
| 69 | enum Type : uint8_t { | 72 | enum Type : uint8_t { |
| 70 | All, ///< @brief All events (undefined). | 73 | All, ///< @brief All events (undefined). |
| @@ -77,9 +80,7 @@ | @@ -77,9 +80,7 @@ | ||
| 77 | Type type; | 80 | Type type; |
| 78 | 81 | ||
| 79 | protected: | 82 | protected: |
| 80 | - Event(enum Type type) { | ||
| 81 | - this->type = type; | ||
| 82 | - } | 83 | + Event(enum Type type); |
| 83 | 84 | ||
| 84 | public: | 85 | public: |
| 85 | virtual ~Event(); | 86 | virtual ~Event(); |
| @@ -89,6 +90,15 @@ | @@ -89,6 +90,15 @@ | ||
| 89 | return this->type == type; | 90 | return this->type == type; |
| 90 | } | 91 | } |
| 91 | 92 | ||
| 93 | + /// @brief Check event type | ||
| 94 | + inline bool operator==(Event::Type type) const noexcept { | ||
| 95 | + return this->type == type; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + inline operator Event::Type() const noexcept { | ||
| 99 | + return this->type; | ||
| 100 | + } | ||
| 101 | + | ||
| 92 | /// @brief Get event description. | 102 | /// @brief Get event description. |
| 93 | virtual std::string toString() const = 0; | 103 | virtual std::string toString() const = 0; |
| 94 | 104 | ||
| @@ -174,6 +184,7 @@ | @@ -174,6 +184,7 @@ | ||
| 174 | /// @brief Write error to log file. | 184 | /// @brief Write error to log file. |
| 175 | void error(const char *fmt, ...) const; | 185 | void error(const char *fmt, ...) const; |
| 176 | 186 | ||
| 187 | + /// @brief Fire event. | ||
| 177 | void fire(const Event &event); | 188 | void fire(const Event &event); |
| 178 | 189 | ||
| 179 | public: | 190 | public: |
| @@ -228,6 +239,9 @@ | @@ -228,6 +239,9 @@ | ||
| 228 | virtual Session & pop(int row, int col, std::string &text) = 0; | 239 | virtual Session & pop(int row, int col, std::string &text) = 0; |
| 229 | virtual Session & pop(std::string &text) = 0; | 240 | virtual Session & pop(std::string &text) = 0; |
| 230 | 241 | ||
| 242 | + /// @brief Insert event listener. | ||
| 243 | + void insert(Event::Type type, std::function <void(const Event &event)> listener); | ||
| 244 | + | ||
| 231 | }; | 245 | }; |
| 232 | 246 | ||
| 233 | /// @brief TN3270 Host | 247 | /// @brief TN3270 Host |
| @@ -334,6 +348,13 @@ | @@ -334,6 +348,13 @@ | ||
| 334 | return *this; | 348 | return *this; |
| 335 | } | 349 | } |
| 336 | 350 | ||
| 351 | + // Event listeners | ||
| 352 | + inline Host & insert(Event::Type type, std::function <void(const Event &event)> listener) noexcept { | ||
| 353 | + session->insert(type, listener); | ||
| 354 | + return *this; | ||
| 355 | + } | ||
| 356 | + | ||
| 357 | + | ||
| 337 | }; | 358 | }; |
| 338 | 359 | ||
| 339 | } | 360 | } |
| @@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin | ||
| 19 | + * St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como lib3270++.h e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | +/** | ||
| 31 | + * @file src/lib3270++/events.cc | ||
| 32 | + * | ||
| 33 | + * @brief | ||
| 34 | + * | ||
| 35 | + * @author perry.werneck@gmail.com | ||
| 36 | + * | ||
| 37 | + */ | ||
| 38 | + | ||
| 39 | + #include "private.h" | ||
| 40 | + | ||
| 41 | + | ||
| 42 | +/*---[ Implement ]----------------------------------------------------------------------------------*/ | ||
| 43 | + | ||
| 44 | + namespace TN3270 { | ||
| 45 | + | ||
| 46 | + Event::Event(enum Event::Type type) { | ||
| 47 | + this->type = type; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + Event::~Event() { | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + | ||
| 56 | + |
src/lib3270++/lib3270++.cbp
| @@ -44,6 +44,7 @@ | @@ -44,6 +44,7 @@ | ||
| 44 | <Unit filename="../include/lib3270/popup.h" /> | 44 | <Unit filename="../include/lib3270/popup.h" /> |
| 45 | <Unit filename="../include/lib3270/session.h" /> | 45 | <Unit filename="../include/lib3270/session.h" /> |
| 46 | <Unit filename="abstract.cc" /> | 46 | <Unit filename="abstract.cc" /> |
| 47 | + <Unit filename="events.cc" /> | ||
| 47 | <Unit filename="host.cc" /> | 48 | <Unit filename="host.cc" /> |
| 48 | <Unit filename="local.cc" /> | 49 | <Unit filename="local.cc" /> |
| 49 | <Unit filename="private.h" /> | 50 | <Unit filename="private.h" /> |
src/lib3270++/local.cc
| @@ -86,6 +86,13 @@ | @@ -86,6 +86,13 @@ | ||
| 86 | free(buffer); | 86 | free(buffer); |
| 87 | } | 87 | } |
| 88 | 88 | ||
| 89 | +#ifdef DEBUG | ||
| 90 | + std::cerr << "Popup:" << std::endl | ||
| 91 | + << "\t" << title << std::endl | ||
| 92 | + << "\t" << msg << std::endl | ||
| 93 | + << "\t" << description << std::endl; | ||
| 94 | +#endif // DEBUG | ||
| 95 | + | ||
| 89 | } | 96 | } |
| 90 | 97 | ||
| 91 | virtual ~PopupEvent() { | 98 | virtual ~PopupEvent() { |
| @@ -113,14 +120,26 @@ | @@ -113,14 +120,26 @@ | ||
| 113 | this->hSession = nullptr; | 120 | this->hSession = nullptr; |
| 114 | } | 121 | } |
| 115 | 122 | ||
| 123 | + void LocalSession::wait(time_t timeout) { | ||
| 124 | + | ||
| 125 | + int rc = lib3270_wait_for_ready(this->hSession, timeout); | ||
| 126 | + | ||
| 127 | + if(rc) { | ||
| 128 | + throw std::system_error(rc, std::system_category()); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + } | ||
| 132 | + | ||
| 116 | void LocalSession::connect(const char *url) { | 133 | void LocalSession::connect(const char *url) { |
| 117 | std::lock_guard<std::mutex> lock(sync); | 134 | std::lock_guard<std::mutex> lock(sync); |
| 118 | int rc = lib3270_connect_url(hSession,url,0); | 135 | int rc = lib3270_connect_url(hSession,url,0); |
| 119 | 136 | ||
| 120 | - if(!rc) { | 137 | + if(rc) { |
| 121 | throw std::system_error(rc, std::system_category()); | 138 | throw std::system_error(rc, std::system_category()); |
| 122 | } | 139 | } |
| 123 | 140 | ||
| 141 | + wait(); | ||
| 142 | + | ||
| 124 | } | 143 | } |
| 125 | 144 | ||
| 126 | void LocalSession::disconnect() { | 145 | void LocalSession::disconnect() { |
| @@ -132,12 +151,7 @@ | @@ -132,12 +151,7 @@ | ||
| 132 | void LocalSession::waitForReady(time_t timeout) throw() { | 151 | void LocalSession::waitForReady(time_t timeout) throw() { |
| 133 | 152 | ||
| 134 | std::lock_guard<std::mutex> lock(sync); | 153 | std::lock_guard<std::mutex> lock(sync); |
| 135 | - | ||
| 136 | - int rc = lib3270_wait_for_ready(this->hSession, timeout); | ||
| 137 | - | ||
| 138 | - if(rc) { | ||
| 139 | - throw std::system_error(rc, std::system_category()); | ||
| 140 | - } | 154 | + wait(timeout); |
| 141 | 155 | ||
| 142 | } | 156 | } |
| 143 | 157 |
src/lib3270++/private.h
| @@ -112,6 +112,9 @@ | @@ -112,6 +112,9 @@ | ||
| 112 | /// @brief Popup Handler. | 112 | /// @brief Popup Handler. |
| 113 | static int popupHandler(H3270 *session, LIB3270_NOTIFY type, const char *title, const char *msg, const char *fmt, va_list arg); | 113 | static int popupHandler(H3270 *session, LIB3270_NOTIFY type, const char *title, const char *msg, const char *fmt, va_list arg); |
| 114 | 114 | ||
| 115 | + /// @brief Wait for network events | ||
| 116 | + void wait(time_t timeout = 5); | ||
| 117 | + | ||
| 115 | public: | 118 | public: |
| 116 | LocalSession(); | 119 | LocalSession(); |
| 117 | virtual ~LocalSession(); | 120 | virtual ~LocalSession(); |
src/lib3270++/session.cc
src/lib3270++/testprogram/testprogram.cc
| @@ -36,6 +36,7 @@ | @@ -36,6 +36,7 @@ | ||
| 36 | * | 36 | * |
| 37 | */ | 37 | */ |
| 38 | 38 | ||
| 39 | + #include <cstdlib> | ||
| 39 | #include <lib3270++.h> | 40 | #include <lib3270++.h> |
| 40 | 41 | ||
| 41 | /*---[ Implement ]----------------------------------------------------------------------------------*/ | 42 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
| @@ -44,7 +45,7 @@ | @@ -44,7 +45,7 @@ | ||
| 44 | 45 | ||
| 45 | TN3270::Host host; | 46 | TN3270::Host host; |
| 46 | 47 | ||
| 47 | - host.connect(nullptr); | 48 | + host.connect(getenv("TN3270URL")); |
| 48 | 49 | ||
| 49 | return 0; | 50 | return 0; |
| 50 | } | 51 | } |