Commit 64bf19bb6e30ce7bffe24e763fac9e0d7c564d73

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

Fixing memory leaks, working on C++ API.

host.cc
... ... @@ -55,8 +55,15 @@
55 55 this->session = nullptr;
56 56 }
57 57  
  58 + void Host::connect(const char *url) {
  59 + this->session->connect(url);
  60 + sync();
  61 + }
  62 +
  63 +
58 64 /// @brief Writes characters to the associated file from the put area
59 65 int Host::sync() {
  66 + this->session->waitForReady();
60 67 return 0;
61 68 }
62 69  
... ... @@ -74,5 +81,20 @@
74 81  
75 82 }
76 83  
  84 + Host & Host::push(const Action action) {
  85 + session->push(action);
  86 + sync();
  87 + return *this;
  88 + }
  89 +
  90 + std::string Host::toString() const {
  91 +
  92 + if(this->session->getConnectionState() == TN3270::DISCONNECTED) {
  93 + throw std::system_error(ENOTCONN, std::system_category());
  94 + }
  95 +
  96 + return this->session->toString();
  97 + }
  98 +
77 99 }
78 100  
... ...
lib3270++.cbp
... ... @@ -8,7 +8,7 @@
8 8 <Option compiler="gcc" />
9 9 <Build>
10 10 <Target title="Debug">
11   - <Option output=".bin/Debug/C++ Bindings for lib3270" prefix_auto="1" extension_auto="1" />
  11 + <Option output="../../.bin/Debug/lib3270++" prefix_auto="1" extension_auto="1" />
12 12 <Option object_output=".obj/Debug/" />
13 13 <Option type="1" />
14 14 <Option compiler="gcc" />
... ... @@ -42,6 +42,8 @@
42 42 </Linker>
43 43 <Unit filename="../include/lib3270++.h" />
44 44 <Unit filename="../include/lib3270.h" />
  45 + <Unit filename="../include/lib3270/action_table.h" />
  46 + <Unit filename="../include/lib3270/actions.h" />
45 47 <Unit filename="../include/lib3270/popup.h" />
46 48 <Unit filename="../include/lib3270/session.h" />
47 49 <Unit filename="abstract.cc" />
... ...
local/session.cc
... ... @@ -37,6 +37,7 @@
37 37 */
38 38  
39 39 #include "../private.h"
  40 + #include <lib3270/actions.h>
40 41  
41 42 extern "C" {
42 43 #include <lib3270/actions.h>
... ... @@ -82,6 +83,8 @@
82 83  
83 84 void Local::Session::wait(time_t timeout) {
84 85  
  86 + std::lock_guard<std::mutex> lock(sync);
  87 +
85 88 int rc = lib3270_wait_for_ready(this->hSession, timeout);
86 89  
87 90 if(rc) {
... ... @@ -98,9 +101,7 @@
98 101 throw std::system_error(rc, std::system_category());
99 102 }
100 103  
101   - wait();
102   -
103   - }
  104 + }
104 105  
105 106 void Local::Session::disconnect() {
106 107 std::lock_guard<std::mutex> lock(sync);
... ... @@ -109,23 +110,42 @@
109 110  
110 111 // Wait for session state.
111 112 void Local::Session::waitForReady(time_t timeout) throw() {
  113 + this->wait(timeout);
  114 + }
112 115  
113   - std::lock_guard<std::mutex> lock(sync);
114   - wait(timeout);
  116 + std::string Local::Session::toString(int baddr, size_t len, char lf) const {
  117 +
  118 + std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync);
  119 +
  120 + char * text = lib3270_get_text(hSession, baddr, len, lf);
  121 +
  122 + if(!text) {
  123 + throw std::runtime_error("Can't get screen contents");
  124 + }
  125 +
  126 + string rc = convertFromHost(text);
  127 +
  128 + lib3270_free(text);
  129 +
  130 + return rc;
115 131  
116 132 }
117 133  
118   - // Gets
119   - std::string Local::Session::toString() const {
  134 + std::string Local::Session::toString(int row, int col, size_t sz, char lf) const {
  135 +
120 136 std::lock_guard<std::mutex> lock(const_cast<Local::Session *>(this)->sync);
121   - }
122 137  
123   - std::string Local::Session::toString(int baddr, size_t len, bool lf) {
124   - std::lock_guard<std::mutex> lock(sync);
125   - }
  138 + char * text = lib3270_get_text_at(hSession, row, col, sz, lf);
126 139  
127   - std::string Local::Session::toString(int row, int col, size_t sz, bool lf) {
128   - std::lock_guard<std::mutex> lock(sync);
  140 + if(!text) {
  141 + throw std::runtime_error("Can't get screen contents");
  142 + }
  143 +
  144 + string rc = convertFromHost(text);
  145 +
  146 + lib3270_free(text);
  147 +
  148 + return rc;
129 149 }
130 150  
131 151 ProgramMessage Local::Session::getProgramMessage() const {
... ... @@ -168,6 +188,30 @@
168 188  
169 189 TN3270::Session & Local::Session::push(const Action action) {
170 190 std::lock_guard<std::mutex> lock(sync);
  191 +
  192 + switch(action) {
  193 + case ENTER:
  194 + lib3270_enter(hSession);
  195 + break;
  196 +
  197 + case ERASE:
  198 + lib3270_erase(hSession);
  199 + break;
  200 +
  201 + case ERASE_EOF:
  202 + lib3270_eraseeof(hSession);
  203 + break;
  204 +
  205 + case ERASE_EOL:
  206 + lib3270_eraseeol(hSession);
  207 + break;
  208 +
  209 + case ERASE_INPUT:
  210 + lib3270_eraseinput(hSession);
  211 + break;
  212 +
  213 + }
  214 +
171 215 return *this;
172 216 }
173 217  
... ...
private.h
... ... @@ -45,6 +45,7 @@
45 45 #include <lib3270++.h>
46 46 #include <lib3270/popup.h>
47 47 #include <system_error>
  48 + #include <stdexcept>
48 49  
49 50  
50 51 #ifdef HAVE_ICONV
... ... @@ -133,9 +134,8 @@
133 134 void waitForReady(time_t timeout = 5) throw() override;
134 135  
135 136 // Gets
136   - std::string toString() const override;
137   - std::string toString(int baddr = 0, size_t len = -1, bool lf = false) override;
138   - std::string toString(int row, int col, size_t sz, bool lf = false) override;
  137 + std::string toString(int baddr, size_t len, char lf) const override;
  138 + std::string toString(int row, int col, size_t sz, char lf) const override;
139 139  
140 140 ProgramMessage getProgramMessage() const override;
141 141  
... ...
testprogram/testprogram.cc
... ... @@ -39,6 +39,8 @@
39 39 #include <cstdlib>
40 40 #include <lib3270++.h>
41 41  
  42 + using namespace std;
  43 +
42 44 /*---[ Implement ]----------------------------------------------------------------------------------*/
43 45  
44 46 int main(int argc, const char *argv[]) {
... ... @@ -46,6 +48,11 @@
46 48 TN3270::Host host;
47 49  
48 50 host.connect(getenv("TN3270URL"));
  51 + cout << host << endl;
  52 +
  53 + host << TN3270::ENTER;
  54 +
  55 +
49 56  
50 57 return 0;
51 58 }
... ...