Commit e828a3d8fd8486bc6a226c174b3a2d14a4b106dc

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

Refactoring HLLAPI module.

src/core/calls.cc
@@ -37,7 +37,7 @@ @@ -37,7 +37,7 @@
37 37
38 return atoi(getSession().getRevision().c_str()); 38 return atoi(getSession().getRevision().c_str());
39 39
40 - } catch(std::exception &e) { 40 + } catch(const std::exception &e) {
41 41
42 hllapi_lasterror = e.what(); 42 hllapi_lasterror = e.what();
43 43
@@ -50,15 +50,21 @@ @@ -50,15 +50,21 @@
50 { 50 {
51 try { 51 try {
52 52
53 - getSession().connect((const char *) uri, (wait != 0)); 53 + getSession().connect((const char *) uri);
54 54
55 if(wait) 55 if(wait)
56 return hllapi_wait_for_ready(wait); 56 return hllapi_wait_for_ready(wait);
57 57
58 - } catch(std::exception &e) { 58 + } catch(const std::exception &e) {
59 59
60 hllapi_lasterror = e.what(); 60 hllapi_lasterror = e.what();
61 return HLLAPI_STATUS_SYSTEM_ERROR; 61 return HLLAPI_STATUS_SYSTEM_ERROR;
  62 +
  63 + } catch(...) {
  64 +
  65 + hllapi_lasterror = "Unexpected error";
  66 + return HLLAPI_STATUS_SYSTEM_ERROR;
  67 +
62 } 68 }
63 69
64 return hllapi_get_state(); 70 return hllapi_get_state();
@@ -170,7 +176,7 @@ @@ -170,7 +176,7 @@
170 176
171 return hllapi_get_state(); 177 return hllapi_get_state();
172 178
173 - } catch(std::exception &e) { 179 + } catch(const std::exception &e) {
174 180
175 hllapi_lasterror = e.what(); 181 hllapi_lasterror = e.what();
176 182
src/core/get.cc
@@ -31,8 +31,48 @@ @@ -31,8 +31,48 @@
31 31
32 /*--[ Implement ]------------------------------------------------------------------------------------*/ 32 /*--[ Implement ]------------------------------------------------------------------------------------*/
33 33
  34 + static DWORD get(std::function<void(TN3270::Host &)> worker) noexcept {
  35 +
  36 + try {
  37 +
  38 + TN3270::Host &host = getSession();
  39 +
  40 + if(!host.isConnected())
  41 + return HLLAPI_STATUS_DISCONNECTED;
  42 +
  43 + worker(host);
  44 +
  45 + } catch(std::exception &e) {
  46 +
  47 + hllapi_lasterror = e.what();
  48 + return HLLAPI_STATUS_SYSTEM_ERROR;
  49 +
  50 + } catch(...) {
  51 +
  52 + hllapi_lasterror = "Unexpected error";
  53 + return HLLAPI_STATUS_SYSTEM_ERROR;
  54 +
  55 + }
  56 +
  57 + return HLLAPI_STATUS_SUCCESS;
  58 +
  59 + }
  60 +
34 HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer) { 61 HLLAPI_API_CALL hllapi_get_screen_at(WORD row, WORD col, LPSTR buffer) {
35 62
  63 + if(!(buffer && *buffer))
  64 + return HLLAPI_STATUS_BAD_PARAMETER;
  65 +
  66 + return get([row,col,buffer](TN3270::Host &host) {
  67 +
  68 + size_t length = strlen(buffer);
  69 + string contents = host.toString( (int) row, (int) col, length);
  70 +
  71 + strncpy((char *) buffer, contents.c_str(), std::min(length,contents.size()));
  72 +
  73 + });
  74 +
  75 + /*
36 try { 76 try {
37 77
38 TN3270::Host &host = getSession(); 78 TN3270::Host &host = getSession();
@@ -56,11 +96,29 @@ @@ -56,11 +96,29 @@
56 } 96 }
57 97
58 return HLLAPI_STATUS_SUCCESS; 98 return HLLAPI_STATUS_SUCCESS;
  99 + */
59 100
60 } 101 }
61 102
62 HLLAPI_API_CALL hllapi_get_screen(WORD offset, LPSTR buffer, WORD len) { 103 HLLAPI_API_CALL hllapi_get_screen(WORD offset, LPSTR buffer, WORD len) {
63 104
  105 + if(!(buffer && *buffer))
  106 + return HLLAPI_STATUS_BAD_PARAMETER;
  107 +
  108 + if(len == 0)
  109 + return HLLAPI_STATUS_BAD_PARAMETER;
  110 +
  111 + return get([offset,buffer,len](TN3270::Host &host) {
  112 +
  113 + string contents = host.toString((int) offset, (size_t) len);
  114 +
  115 + memset(buffer,' ',len);
  116 + strncpy((char *) buffer, contents.c_str(), std::min((size_t) len,contents.size()));
  117 +
  118 + });
  119 +
  120 +
  121 + /*
64 try { 122 try {
65 123
66 TN3270::Host &host = getSession(); 124 TN3270::Host &host = getSession();
@@ -87,11 +145,27 @@ @@ -87,11 +145,27 @@
87 } 145 }
88 146
89 return HLLAPI_STATUS_SUCCESS; 147 return HLLAPI_STATUS_SUCCESS;
  148 + */
90 149
91 } 150 }
92 151
93 HLLAPI_API_CALL hllapi_get_lu_name(LPSTR buffer, WORD len) { 152 HLLAPI_API_CALL hllapi_get_lu_name(LPSTR buffer, WORD len) {
94 153
  154 + if(!(buffer && *buffer))
  155 + return HLLAPI_STATUS_BAD_PARAMETER;
  156 +
  157 + if(len == 0)
  158 + return HLLAPI_STATUS_BAD_PARAMETER;
  159 +
  160 + return get([buffer,len](TN3270::Host &host) {
  161 +
  162 + string luname = host.getLUName();
  163 + memset(buffer,' ',len);
  164 + strncpy((char *) buffer, luname.c_str(), std::min((size_t) len,luname.size()));
  165 +
  166 + });
  167 +
  168 + /*
95 try { 169 try {
96 170
97 TN3270::Host &host = getSession(); 171 TN3270::Host &host = getSession();
@@ -107,7 +181,6 @@ @@ -107,7 +181,6 @@
107 return HLLAPI_STATUS_BAD_PARAMETER; 181 return HLLAPI_STATUS_BAD_PARAMETER;
108 182
109 string luname = host.getLUName(); 183 string luname = host.getLUName();
110 -  
111 memset(buffer,' ',len); 184 memset(buffer,' ',len);
112 strncpy((char *) buffer, luname.c_str(), std::min((size_t) len,luname.size())); 185 strncpy((char *) buffer, luname.c_str(), std::min((size_t) len,luname.size()));
113 186
@@ -120,6 +193,8 @@ @@ -120,6 +193,8 @@
120 193
121 return HLLAPI_STATUS_SUCCESS; 194 return HLLAPI_STATUS_SUCCESS;
122 195
  196 + */
  197 +
123 } 198 }
124 199
125 200
src/core/set.cc
@@ -28,70 +28,105 @@ @@ -28,70 +28,105 @@
28 */ 28 */
29 29
30 #include "private.h" 30 #include "private.h"
  31 + #include <lib3270/keyboard.h>
31 32
32 #include <string> 33 #include <string>
  34 + #include <functional>
33 35
34 36
35 /*--[ Implement ]------------------------------------------------------------------------------------*/ 37 /*--[ Implement ]------------------------------------------------------------------------------------*/
36 38
37 - HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text) { 39 + DWORD hllapi_translate_keyboard_state(LIB3270_KEYBOARD_LOCK_STATE state, HLLAPI_STATUS def = HLLAPI_STATUS_SYSTEM_ERROR) {
  40 +
  41 + // Is unlocked.
  42 + if(state == LIB3270_KL_UNLOCKED)
  43 + return def;
  44 +
  45 + // Is connected?
  46 + if((state & LIB3270_KL_NOT_CONNECTED) != 0)
  47 + return HLLAPI_STATUS_DISCONNECTED;
  48 +
  49 + if( (state & (LIB3270_KL_AWAITING_FIRST|LIB3270_KL_OIA_TWAIT)) != 0)
  50 + return HLLAPI_STATUS_WAITING;
  51 +
  52 + return HLLAPI_STATUS_KEYBOARD_LOCKED;
  53 +
  54 + }
38 55
39 - try { 56 + static DWORD set(std::function<void(TN3270::Host &)> worker) noexcept {
  57 +
  58 + LIB3270_KEYBOARD_LOCK_STATE kLock = LIB3270_KL_UNLOCKED;
  59 +
  60 + try {
40 61
41 TN3270::Host &host = getSession(); 62 TN3270::Host &host = getSession();
42 63
43 - if(!host.isConnected())  
44 - return HLLAPI_STATUS_DISCONNECTED; 64 + kLock = host.waitForKeyboardUnlock();
  65 + if(kLock == LIB3270_KL_UNLOCKED) {
45 66
46 - if(!(text && *text))  
47 - return HLLAPI_STATUS_BAD_PARAMETER; 67 + try {
48 68
49 - host.push(row,col,(const char *) text); 69 + worker(host);
  70 + return HLLAPI_STATUS_SUCCESS;
  71 +
  72 + } catch(const std::exception &e) {
50 73
  74 + // Worker has failed!
  75 + hllapi_lasterror = e.what();
  76 + }
51 77
52 - } catch(std::exception &e) { 78 + // Failed! Get lock state.
  79 + kLock = host.getKeyboardLockState();
53 80
  81 + }
  82 +
  83 +
  84 + } catch(const std::exception &e) {
  85 +
  86 + // Error getting session or lock state
54 hllapi_lasterror = e.what(); 87 hllapi_lasterror = e.what();
55 return HLLAPI_STATUS_SYSTEM_ERROR; 88 return HLLAPI_STATUS_SYSTEM_ERROR;
56 89
  90 + } catch(...) {
  91 +
  92 + // Unexpected error getting session or lock state
  93 + hllapi_lasterror = "Unexpected error";
  94 + return HLLAPI_STATUS_SYSTEM_ERROR;
  95 +
57 } 96 }
58 97
59 - return HLLAPI_STATUS_SUCCESS; 98 + return hllapi_translate_keyboard_state(kLock);
60 99
61 } 100 }
62 101
63 - HLLAPI_API_CALL hllapi_input_string(LPSTR text, WORD length)  
64 - { 102 + HLLAPI_API_CALL hllapi_set_text_at(WORD row, WORD col, LPSTR text) {
65 103
66 - try { 104 + if(!(text && *text))
  105 + return HLLAPI_STATUS_BAD_PARAMETER;
67 106
68 - TN3270::Host &host = getSession(); 107 + return set([row,col,text](TN3270::Host &host) {
69 108
70 - if(!host.isConnected())  
71 - return HLLAPI_STATUS_DISCONNECTED; 109 + host.push(row,col,(const char *) text);
72 110
73 - if(!(text && *text))  
74 - return HLLAPI_STATUS_BAD_PARAMETER; 111 + });
75 112
76 - if(!length)  
77 - length = strlen(text);  
78 113
79 - host.input((const char *) text, (size_t) length); 114 + }
80 115
81 - } catch(std::exception &e) { 116 + HLLAPI_API_CALL hllapi_input_string(LPSTR text, WORD length)
  117 + {
82 118
83 - hllapi_lasterror = e.what();  
84 - return HLLAPI_STATUS_SYSTEM_ERROR; 119 + if(!(text && *text))
  120 + return HLLAPI_STATUS_BAD_PARAMETER;
85 121
86 - } 122 + return set([text,length](TN3270::Host &host) {
87 123
88 - return HLLAPI_STATUS_SUCCESS; 124 + host.input((const char *) text, (int) length, '@');
89 125
  126 + });
90 127
91 } 128 }
92 129
93 -  
94 -  
95 /* 130 /*
96 HLLAPI_API_CALL hllapi_emulate_input(const LPSTR buffer, WORD len, WORD pasting) 131 HLLAPI_API_CALL hllapi_emulate_input(const LPSTR buffer, WORD len, WORD pasting)
97 { 132 {