Commit b4a8defb6a88da2eef8f4855720529227f4a3cd7
1 parent
dddb96d1
Exists in
master
and in
3 other branches
Implementando componente c++.
Showing
3 changed files
with
76 additions
and
1 deletions
Show diff stats
src/lib3270++/abstract.cc
@@ -37,6 +37,7 @@ | @@ -37,6 +37,7 @@ | ||
37 | */ | 37 | */ |
38 | 38 | ||
39 | #include "private.h" | 39 | #include "private.h" |
40 | + #include <cstring> | ||
40 | 41 | ||
41 | 42 | ||
42 | /*---[ Implement ]----------------------------------------------------------------------------------*/ | 43 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
@@ -68,6 +69,38 @@ | @@ -68,6 +69,38 @@ | ||
68 | 69 | ||
69 | } | 70 | } |
70 | 71 | ||
72 | + /// @brief Setup charsets | ||
73 | + void Abstract::Session::setCharSet(const char *remote, const char *local) { | ||
74 | + | ||
75 | +#ifdef HAVE_ICONV | ||
76 | + | ||
77 | + if(this->iconv.local != (iconv_t) (-1)) | ||
78 | + iconv_close(iconv.local); | ||
79 | + | ||
80 | + if(this->iconv.host != (iconv_t) (-1)) | ||
81 | + iconv_close(iconv.host); | ||
82 | + | ||
83 | + if(strcmp(local,remote)) { | ||
84 | + | ||
85 | + // Local and remote charsets aren't the same, setup conversion | ||
86 | + iconv.local = iconv_open(local, remote); | ||
87 | + iconv.host = iconv_open(remote,local); | ||
88 | + | ||
89 | + } else { | ||
90 | + // Same charset, doesn't convert | ||
91 | + iconv.local = iconv.host = (iconv_t)(-1); | ||
92 | + } | ||
93 | + | ||
94 | +#else | ||
95 | + | ||
96 | + #error No ICONV Support | ||
97 | + | ||
98 | +#endif | ||
99 | + | ||
100 | + | ||
101 | + } | ||
102 | + | ||
103 | + | ||
71 | } | 104 | } |
72 | 105 | ||
73 | 106 |
src/lib3270++/local.cc
@@ -30,13 +30,14 @@ | @@ -30,13 +30,14 @@ | ||
30 | /** | 30 | /** |
31 | * @file src/lib3270++/local.cc | 31 | * @file src/lib3270++/local.cc |
32 | * | 32 | * |
33 | - * @brief | 33 | + * @brief Implement lib3270 direct access layout (NO IPC). |
34 | * | 34 | * |
35 | * @author perry.werneck@gmail.com | 35 | * @author perry.werneck@gmail.com |
36 | * | 36 | * |
37 | */ | 37 | */ |
38 | 38 | ||
39 | #include "private.h" | 39 | #include "private.h" |
40 | + #include <lib3270/actions.h> | ||
40 | 41 | ||
41 | 42 | ||
42 | /*---[ Implement ]----------------------------------------------------------------------------------*/ | 43 | /*---[ Implement ]----------------------------------------------------------------------------------*/ |
@@ -44,26 +45,38 @@ | @@ -44,26 +45,38 @@ | ||
44 | namespace TN3270 { | 45 | namespace TN3270 { |
45 | 46 | ||
46 | LocalSession::LocalSession() : Abstract::Session() { | 47 | LocalSession::LocalSession() : Abstract::Session() { |
48 | + | ||
49 | + std::lock_guard<std::mutex> lock(sync); | ||
50 | + | ||
47 | this->hSession = lib3270_session_new(""); | 51 | this->hSession = lib3270_session_new(""); |
48 | lib3270_set_user_data(this->hSession,(void *) this); | 52 | lib3270_set_user_data(this->hSession,(void *) this); |
53 | + setCharSet(lib3270_get_display_charset(this->hSession)); | ||
54 | + | ||
49 | } | 55 | } |
50 | 56 | ||
51 | LocalSession::~LocalSession() { | 57 | LocalSession::~LocalSession() { |
58 | + | ||
59 | + std::lock_guard<std::mutex> lock(sync); | ||
60 | + | ||
52 | lib3270_session_free(this->hSession); | 61 | lib3270_session_free(this->hSession); |
53 | this->hSession = nullptr; | 62 | this->hSession = nullptr; |
54 | } | 63 | } |
55 | 64 | ||
56 | void LocalSession::connect(const char *url) { | 65 | void LocalSession::connect(const char *url) { |
66 | + std::lock_guard<std::mutex> lock(sync); | ||
57 | lib3270_connect_url(hSession,url,0); | 67 | lib3270_connect_url(hSession,url,0); |
58 | } | 68 | } |
59 | 69 | ||
60 | void LocalSession::disconnect() { | 70 | void LocalSession::disconnect() { |
71 | + std::lock_guard<std::mutex> lock(sync); | ||
61 | lib3270_disconnect(hSession); | 72 | lib3270_disconnect(hSession); |
62 | } | 73 | } |
63 | 74 | ||
64 | // Wait for session state. | 75 | // Wait for session state. |
65 | void LocalSession::waitForReady(time_t timeout) throw() { | 76 | void LocalSession::waitForReady(time_t timeout) throw() { |
66 | 77 | ||
78 | + std::lock_guard<std::mutex> lock(sync); | ||
79 | + | ||
67 | int rc = lib3270_wait_for_ready(this->hSession, timeout); | 80 | int rc = lib3270_wait_for_ready(this->hSession, timeout); |
68 | 81 | ||
69 | if(rc) { | 82 | if(rc) { |
@@ -74,56 +87,72 @@ | @@ -74,56 +87,72 @@ | ||
74 | 87 | ||
75 | // Gets | 88 | // Gets |
76 | std::string LocalSession::toString() const { | 89 | std::string LocalSession::toString() const { |
90 | + std::lock_guard<std::mutex> lock(const_cast<LocalSession *>(this)->sync); | ||
77 | } | 91 | } |
78 | 92 | ||
79 | std::string LocalSession::toString(int baddr, size_t len, bool lf) { | 93 | std::string LocalSession::toString(int baddr, size_t len, bool lf) { |
94 | + std::lock_guard<std::mutex> lock(sync); | ||
80 | } | 95 | } |
81 | 96 | ||
82 | std::string LocalSession::toString(int row, int col, size_t sz, bool lf) { | 97 | std::string LocalSession::toString(int row, int col, size_t sz, bool lf) { |
98 | + std::lock_guard<std::mutex> lock(sync); | ||
83 | } | 99 | } |
84 | 100 | ||
85 | ProgramMessage LocalSession::getProgramMessage() const { | 101 | ProgramMessage LocalSession::getProgramMessage() const { |
102 | + std::lock_guard<std::mutex> lock(const_cast<LocalSession *>(this)->sync); | ||
86 | return (ProgramMessage) lib3270_get_program_message(this->hSession); | 103 | return (ProgramMessage) lib3270_get_program_message(this->hSession); |
87 | } | 104 | } |
88 | 105 | ||
89 | ConnectionState LocalSession::getConnectionState() const { | 106 | ConnectionState LocalSession::getConnectionState() const { |
107 | + std::lock_guard<std::mutex> lock(const_cast<LocalSession *>(this)->sync); | ||
90 | return (ConnectionState) lib3270_get_connection_state(this->hSession); | 108 | return (ConnectionState) lib3270_get_connection_state(this->hSession); |
91 | } | 109 | } |
92 | 110 | ||
93 | /// @brief Set field at current posicion, jumps to next writable field. | 111 | /// @brief Set field at current posicion, jumps to next writable field. |
94 | TN3270::Session & LocalSession::push(const char *text) { | 112 | TN3270::Session & LocalSession::push(const char *text) { |
113 | + std::lock_guard<std::mutex> lock(sync); | ||
95 | return *this; | 114 | return *this; |
96 | } | 115 | } |
97 | 116 | ||
98 | TN3270::Session & LocalSession::push(int baddr, const std::string &text) { | 117 | TN3270::Session & LocalSession::push(int baddr, const std::string &text) { |
118 | + std::lock_guard<std::mutex> lock(sync); | ||
99 | return *this; | 119 | return *this; |
100 | } | 120 | } |
101 | 121 | ||
102 | TN3270::Session & LocalSession::push(int row, int col, const std::string &text) { | 122 | TN3270::Session & LocalSession::push(int row, int col, const std::string &text) { |
123 | + std::lock_guard<std::mutex> lock(sync); | ||
103 | return *this; | 124 | return *this; |
104 | } | 125 | } |
105 | 126 | ||
106 | TN3270::Session & LocalSession::push(const PFKey key) { | 127 | TN3270::Session & LocalSession::push(const PFKey key) { |
128 | + std::lock_guard<std::mutex> lock(sync); | ||
129 | + lib3270_pfkey(hSession,(int) key); | ||
107 | return *this; | 130 | return *this; |
108 | } | 131 | } |
109 | 132 | ||
110 | TN3270::Session & LocalSession::push(const PAKey key) { | 133 | TN3270::Session & LocalSession::push(const PAKey key) { |
134 | + std::lock_guard<std::mutex> lock(sync); | ||
135 | + lib3270_pakey(hSession,(int) key); | ||
111 | return *this; | 136 | return *this; |
112 | } | 137 | } |
113 | 138 | ||
114 | TN3270::Session & LocalSession::push(const Action action) { | 139 | TN3270::Session & LocalSession::push(const Action action) { |
140 | + std::lock_guard<std::mutex> lock(sync); | ||
115 | return *this; | 141 | return *this; |
116 | } | 142 | } |
117 | 143 | ||
118 | TN3270::Session & LocalSession::pop(int baddr, std::string &text) { | 144 | TN3270::Session & LocalSession::pop(int baddr, std::string &text) { |
145 | + std::lock_guard<std::mutex> lock(sync); | ||
119 | return *this; | 146 | return *this; |
120 | } | 147 | } |
121 | 148 | ||
122 | TN3270::Session & LocalSession::pop(int row, int col, std::string &text) { | 149 | TN3270::Session & LocalSession::pop(int row, int col, std::string &text) { |
150 | + std::lock_guard<std::mutex> lock(sync); | ||
123 | return *this; | 151 | return *this; |
124 | } | 152 | } |
125 | 153 | ||
126 | TN3270::Session & LocalSession::pop(std::string &text) { | 154 | TN3270::Session & LocalSession::pop(std::string &text) { |
155 | + std::lock_guard<std::mutex> lock(sync); | ||
127 | return *this; | 156 | return *this; |
128 | } | 157 | } |
129 | 158 |
src/lib3270++/private.h
@@ -41,6 +41,7 @@ | @@ -41,6 +41,7 @@ | ||
41 | #define PRIVATE_H_INCLUDED | 41 | #define PRIVATE_H_INCLUDED |
42 | 42 | ||
43 | #include <config.h> | 43 | #include <config.h> |
44 | + #include <mutex> | ||
44 | #include <lib3270++.h> | 45 | #include <lib3270++.h> |
45 | #include <system_error> | 46 | #include <system_error> |
46 | 47 | ||
@@ -49,6 +50,12 @@ | @@ -49,6 +50,12 @@ | ||
49 | #include <iconv.h> | 50 | #include <iconv.h> |
50 | #endif // HAVE_ICONV | 51 | #endif // HAVE_ICONV |
51 | 52 | ||
53 | +#ifdef WIN32 | ||
54 | + #define SYSTEM_CHARSET "CP1252" | ||
55 | +#else | ||
56 | + #define SYSTEM_CHARSET "UTF-8" | ||
57 | +#endif // WIN32 | ||
58 | + | ||
52 | namespace TN3270 { | 59 | namespace TN3270 { |
53 | 60 | ||
54 | namespace Abstract { | 61 | namespace Abstract { |
@@ -76,6 +83,9 @@ | @@ -76,6 +83,9 @@ | ||
76 | Session(); | 83 | Session(); |
77 | virtual ~Session(); | 84 | virtual ~Session(); |
78 | 85 | ||
86 | + /// @brief Setup charsets | ||
87 | + void setCharSet(const char *remote, const char *local = SYSTEM_CHARSET); | ||
88 | + | ||
79 | }; | 89 | }; |
80 | 90 | ||
81 | } | 91 | } |
@@ -86,6 +96,9 @@ | @@ -86,6 +96,9 @@ | ||
86 | /// @brief Handle of the related instance of lib3270 | 96 | /// @brief Handle of the related instance of lib3270 |
87 | H3270 * hSession; | 97 | H3270 * hSession; |
88 | 98 | ||
99 | + /// @brief Mutex to serialize access to lib3270 | ||
100 | + std::mutex sync; | ||
101 | + | ||
89 | public: | 102 | public: |
90 | LocalSession(); | 103 | LocalSession(); |
91 | virtual ~LocalSession(); | 104 | virtual ~LocalSession(); |