Commit 351a0d1482c53fbc84d96b20b898d9ffb092cfff

Authored by Perry Werneck
1 parent 83e47afa

Adding background tasks support.

src/include/lib3270.h
... ... @@ -809,12 +809,12 @@
809 809 LIB3270_EXPORT void lib3270_update_poll_fd(H3270 *session, int fd, LIB3270_IO_FLAG flag);
810 810  
811 811 /**
812   - * @brief Callback table
  812 + * @brief I/O Controller.
813 813 *
814   - * Structure with GUI unblocking I/O calls, used to replace the lib3270´s internal ones.
  814 + * GUI unblocking I/O calls, used to replace the lib3270´s internal ones.
815 815 *
816 816 */
817   - struct lib3270_callbacks
  817 + typedef struct lib3270_io_controller
818 818 {
819 819 unsigned short sz;
820 820  
... ... @@ -827,8 +827,9 @@
827 827 int (*Wait)(H3270 *hSession, int seconds);
828 828 int (*event_dispatcher)(H3270 *session, int wait);
829 829 void (*ring_bell)(H3270 *session);
  830 + int (*run_task)(int(*callback)(H3270 *, void *), H3270 *session, void *parm);
830 831  
831   - };
  832 + } LIB3270_IO_CONTROLLER;
832 833  
833 834 /**
834 835 * Register application Handlers.
... ... @@ -838,7 +839,7 @@
838 839 * @return 0 if ok, error code if not.
839 840 *
840 841 */
841   - LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk);
  842 + LIB3270_EXPORT int lib3270_register_io_controller(const LIB3270_IO_CONTROLLER *cbk);
842 843  
843 844 /**
844 845 * Register time handlers.
... ... @@ -1151,8 +1152,7 @@
1151 1152 * @param Delay in milliseconds.
1152 1153 *
1153 1154 */
1154   - LIB3270_EXPORT void lib3270_set_unlock_delay(H3270 *session, int delay);
1155   -
  1155 + LIB3270_EXPORT int lib3270_set_unlock_delay(H3270 *session, int delay);
1156 1156 LIB3270_EXPORT int lib3270_get_unlock_delay(H3270 *session);
1157 1157  
1158 1158 /**
... ... @@ -1239,6 +1239,19 @@
1239 1239 LIB3270_EXPORT LIB3270_POINTER lib3270_get_pointer(H3270 *hSession, int baddr);
1240 1240  
1241 1241 /**
  1242 + * @brief Run background task.
  1243 + *
  1244 + * Call task in a separate thread, keep gui main loop running until
  1245 + * the function returns.
  1246 + *
  1247 + * @param hSession TN3270 session.
  1248 + * @param callback Function to call.
  1249 + * @param parm Parameter to callback function.
  1250 + *
  1251 + */
  1252 + LIB3270_EXPORT int lib3270_run_task(H3270 *hSession, int(*callback)(H3270 *h, void *), void *parm);
  1253 +
  1254 + /**
1242 1255 * The host is TSO?
1243 1256 *
1244 1257 * @param hSession Session Handle
... ...
src/include/lib3270/session.h
... ... @@ -97,7 +97,7 @@
97 97 * @return 0 if ok, error code if not.
98 98 *
99 99 */
100   - LIB3270_EXPORT int lib3270_set_session_callbacks(const struct lib3270_callbacks *cbk);
  100 + LIB3270_EXPORT int lib3270_set_session_io_handler(const LIB3270_IO_CONTROLLER *cbk);
101 101  
102 102 LIB3270_EXPORT int lib3270_getpeername(H3270 *hSession, struct sockaddr *addr, socklen_t *addrlen);
103 103 LIB3270_EXPORT int lib3270_getsockname(H3270 *hSession, struct sockaddr *addr, socklen_t *addrlen);
... ...
src/lib3270/connect.c
... ... @@ -79,7 +79,7 @@
79 79  
80 80  
81 81 //static void net_connected(H3270 *hSession)
82   -static void net_connected(H3270 *hSession, int fd, LIB3270_IO_FLAG flag, void *dunno)
  82 +static void net_connected(H3270 *hSession, int fd unused, LIB3270_IO_FLAG flag unused, void *dunno unused)
83 83 {
84 84 int err;
85 85 socklen_t len = sizeof(err);
... ... @@ -550,7 +550,7 @@ static void net_connected(H3270 *hSession, int fd, LIB3270_IO_FLAG flag, void *d
550 550 }
551 551  
552 552 lib3270_disconnect(hSession);
553   - lib3270_write_log(hSession,"connect", "%s",__FUNCTION__,strerror(ETIMEDOUT));
  553 + lib3270_write_log(hSession,"connect", "%s: %s",__FUNCTION__,strerror(ETIMEDOUT));
554 554 return errno = ETIMEDOUT;
555 555 }
556 556  
... ...
src/lib3270/ft.c
... ... @@ -159,7 +159,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
159 159 return 0;
160 160 }
161 161  
162   - static void def_complete(H3270 *hSession, unsigned long length,double kbytes_sec,const char *msg, void *userdata)
  162 + static void def_complete(H3270 *hSession, unsigned long length unused, double kbytes_sec unused, const char *msg, void *userdata unused)
163 163 {
164 164 hSession->ft->cbk.message(hSession,msg,hSession->ft->user_data);
165 165 }
... ... @@ -169,27 +169,27 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state);
169 169 hSession->ft->cbk.complete(hSession,length,kbytes_sec,msg,userdata);
170 170 }
171 171  
172   - static void def_message(H3270 *hSession, const char *msg, void *userdata)
  172 + static void def_message(H3270 *hSession, const char *msg, void *userdata unused)
173 173 {
174 174 lib3270_write_log(hSession,"ft","%s",msg);
175 175 }
176 176  
177   - static void def_update(H3270 *hSession, unsigned long current, unsigned long length, double kbytes_sec, void *userdata)
  177 + static void def_update(H3270 *hSession unused, unsigned long current unused, unsigned long length unused, double kbytes_sec unused, void *userdata unused)
178 178 {
179 179  
180 180 }
181 181  
182   - static void def_running(H3270 *hSession, int is_cut, void *userdata)
  182 + static void def_running(H3270 *hSession unused, int is_cut unused, void *userdata unused)
183 183 {
184 184  
185 185 }
186 186  
187   - static void def_aborting(H3270 *hSession, void *userdata)
  187 + static void def_aborting(H3270 *hSession unused, void *userdata unused)
188 188 {
189 189  
190 190 }
191 191  
192   - static void def_state_changed(H3270 *hSession, LIB3270_FT_STATE state, const char *text, void *userdata)
  192 + static void def_state_changed(H3270 *hSession unused, LIB3270_FT_STATE state unused, const char *text unused, void *userdata unused)
193 193 {
194 194  
195 195 }
... ... @@ -662,14 +662,14 @@ void ft_aborting(H3270FT *h)
662 662 }
663 663  
664 664 /* Process a disconnect abort. */
665   -static void ft_connected(H3270 *hSession, int ignored, void *dunno)
  665 +static void ft_connected(H3270 *hSession, int ignored unused, void *dunno unused)
666 666 {
667 667 if (!CONNECTED && lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE)
668 668 ft_failed(get_ft_handle(hSession),_("Host disconnected, transfer cancelled"));
669 669 }
670 670  
671 671 /* Process an abort from no longer being in 3270 mode. */
672   -static void ft_in3270(H3270 *hSession, int ignored, void *dunno)
  672 +static void ft_in3270(H3270 *hSession, int ignored unused, void *dunno unused)
673 673 {
674 674 if (!IN_3270 && lib3270_get_ft_state(hSession) != LIB3270_FT_STATE_NONE)
675 675 ft_failed(get_ft_handle(hSession),_("Not in 3270 mode, transfer cancelled"));
... ...
src/lib3270/iocalls.c
... ... @@ -45,15 +45,17 @@
45 45 /*---[ Standard calls ]-------------------------------------------------------------------------------------*/
46 46  
47 47 // Timeout calls
48   -static void internal_remove_timeout(H3270 *session, void *timer);
49   -static void * internal_add_timeout(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session));
  48 + static void internal_remove_timeout(H3270 *session, void *timer);
  49 + static void * internal_add_timeout(H3270 *session, unsigned long interval_ms, void (*proc)(H3270 *session));
50 50  
51   -static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata );
52   -static void internal_remove_poll(H3270 *session, void *id);
  51 + static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, void(*proc)(H3270 *, int, LIB3270_IO_FLAG, void *), void *userdata );
  52 + static void internal_remove_poll(H3270 *session, void *id);
53 53  
54   -static int internal_wait(H3270 *session, int seconds);
  54 + static int internal_wait(H3270 *session, int seconds);
55 55  
56   -static void internal_ring_bell(H3270 *session);
  56 + static void internal_ring_bell(H3270 *session);
  57 +
  58 + static int internal_run_task(int(*callback)(H3270 *, void *), H3270 *session, void *parm);
57 59  
58 60 /*---[ Active callbacks ]-----------------------------------------------------------------------------------*/
59 61  
... ... @@ -78,6 +80,9 @@ static void internal_ring_bell(H3270 *session);
78 80 static void (*ring_bell)(H3270 *)
79 81 = internal_ring_bell;
80 82  
  83 + static int (*run_task)(H3270 *hSession, int(*callback)(H3270 *h, void *), void *parm)
  84 + = internal_run_task;
  85 +
81 86 /*---[ Typedefs ]-------------------------------------------------------------------------------------------*/
82 87  
83 88 #define TN (timeout_t *)NULL
... ... @@ -205,7 +210,7 @@ static void * internal_add_poll(H3270 *session, int fd, LIB3270_IO_FLAG flag, vo
205 210 ip->userdata = userdata;
206 211 ip->call = call;
207 212  
208   - ip->next = session->inputs;
  213 + ip->next = (input_t *) session->inputs;
209 214  
210 215 session->inputs = ip;
211 216 session->inputs_changed = 1;
... ... @@ -218,7 +223,7 @@ static void internal_remove_poll(H3270 *session, void *id)
218 223 input_t *ip;
219 224 input_t *prev = (input_t *)NULL;
220 225  
221   - for (ip = session->inputs; ip != (input_t *)NULL; ip = ip->next)
  226 + for (ip = session->inputs; ip != (input_t *) NULL; ip = (input_t *) ip->next)
222 227 {
223 228 if (ip == (input_t *)id)
224 229 break;
... ... @@ -235,7 +240,7 @@ static void internal_remove_poll(H3270 *session, void *id)
235 240 if (prev != (input_t *)NULL)
236 241 prev->next = ip->next;
237 242 else
238   - session->inputs = ip->next;
  243 + session->inputs = (input_t *) ip->next;
239 244  
240 245 lib3270_free(ip);
241 246 session->inputs_changed = 1;
... ... @@ -379,13 +384,10 @@ LIB3270_EXPORT void lib3270_register_fd_handlers(void * (*add)(H3270 *session, i
379 384 remove_poll = rm;
380 385 }
381 386  
382   -LIB3270_EXPORT int lib3270_register_handlers(const struct lib3270_callbacks *cbk)
  387 +LIB3270_EXPORT int lib3270_register_io_controller(const LIB3270_IO_CONTROLLER *cbk)
383 388 {
384   - if(!cbk)
385   - return EINVAL;
386   -
387   - if(cbk->sz != sizeof(struct lib3270_callbacks))
388   - return EINVAL;
  389 + if(!cbk || cbk->sz != sizeof(LIB3270_IO_CONTROLLER))
  390 + return errno = EINVAL;
389 391  
390 392 lib3270_register_time_handlers(cbk->AddTimeOut,cbk->RemoveTimeOut);
391 393 lib3270_register_fd_handlers(cbk->add_poll,cbk->remove_poll);
... ... @@ -447,5 +449,35 @@ LIB3270_EXPORT void lib3270_ring_bell(H3270 *session)
447 449 ring_bell(session);
448 450 }
449 451  
  452 +int internal_run_task(int(*callback)(H3270 *, void *), H3270 *session, void *parm) {
  453 + return callback(session,parm);
  454 +}
  455 +
  456 +/**
  457 + * @brief Run background task.
  458 + *
  459 + * Call task in a separate thread, keep gui main loop running until
  460 + * the function returns.
  461 + *
  462 + * @param hSession TN3270 session.
  463 + * @param callback Function to call.
  464 + * @param parm Parameter to callback function.
  465 + *
  466 + */
  467 +LIB3270_EXPORT int lib3270_run_task(H3270 *hSession, int(*callback)(H3270 *h, void *), void *parm)
  468 +{
  469 + int rc;
  470 +
  471 + CHECK_SESSION_HANDLE(hSession);
  472 +
  473 + hSession->cbk.set_timer(hSession,1);
  474 + rc = run_task(callback,hSession,parm);
  475 + hSession->cbk.set_timer(hSession,0);
  476 +
  477 + return rc;
  478 +
  479 +}
  480 +
  481 +
450 482  
451 483  
... ...
src/lib3270/kybd.c
... ... @@ -2922,13 +2922,15 @@ int kybd_prime(H3270 *hSession)
2922 2922 }
2923 2923 #endif /*]*/
2924 2924  
2925   -LIB3270_EXPORT void lib3270_set_unlock_delay(H3270 *session, int delay)
  2925 +LIB3270_EXPORT int lib3270_set_unlock_delay(H3270 *session, int delay)
2926 2926 {
2927 2927 CHECK_SESSION_HANDLE(session);
2928 2928  
2929 2929 trace("%s(%d)",__FUNCTION__,(int) delay);
2930 2930  
2931 2931 session->unlock_delay_ms = (unsigned short) delay;
  2932 +
  2933 + return 0;
2932 2934 }
2933 2935  
2934 2936 LIB3270_EXPORT int lib3270_get_unlock_delay(H3270 *session)
... ...
src/lib3270/log.c
... ... @@ -35,6 +35,7 @@
35 35 #include <windows.h>
36 36 #endif // WIN32
37 37  
  38 +#include "private.h"
38 39 #include <stdio.h>
39 40 #include <stdarg.h>
40 41 #include <config.h>
... ... @@ -80,7 +81,7 @@
80 81 loghandler(session,module,0,fmt,arg);
81 82 }
82 83  
83   - static void defaultlog(H3270 *session, const char *module, int rc, const char *fmt, va_list arg_ptr)
  84 + static void defaultlog(H3270 *session unused, const char *module, int rc unused, const char *fmt, va_list arg_ptr)
84 85 {
85 86 fprintf(stderr,"%s:\t",module);
86 87 vfprintf(stderr,fmt,arg_ptr);
... ...
src/lib3270/properties.c
... ... @@ -251,6 +251,16 @@
251 251 return properties;
252 252 }
253 253  
  254 + static const char * get_version(H3270 *hSession unused)
  255 + {
  256 + return lib3270_get_version();
  257 + }
  258 +
  259 + static const char * get_revision(H3270 *hSession unused)
  260 + {
  261 + return lib3270_get_revision();
  262 + }
  263 +
254 264 LIB3270_EXPORT const LIB3270_STRING_PROPERTY * lib3270_get_string_properties_list(void)
255 265 {
256 266 static const LIB3270_STRING_PROPERTY properties[] = {
... ... @@ -300,14 +310,14 @@
300 310 {
301 311 "version", // Property name.
302 312 N_( "lib3270 version" ), // Property description.
303   - lib3270_get_version, // Get value.
  313 + get_version, // Get value.
304 314 NULL // Set value.
305 315 },
306 316  
307 317 {
308 318 "revision", // Property name.
309 319 N_( "lib3270 revision" ), // Property description.
310   - lib3270_get_revision, // Get value.
  320 + get_revision, // Get value.
311 321 NULL // Set value.
312 322 },
313 323  
... ... @@ -360,7 +370,7 @@ int lib3270_get_int_property(H3270 *hSession, const char *name, int seconds)
360 370  
361 371 // Check for boolean properties
362 372 properties = lib3270_get_boolean_properties_list();
363   - for(ix = 0; ix < (sizeof(properties)/sizeof(properties[0])); ix++)
  373 + for(ix = 0; properties[ix].name; ix++)
364 374 {
365 375 if(!strcasecmp(name,properties[ix].name))
366 376 {
... ... @@ -380,7 +390,7 @@ int lib3270_get_int_property(H3270 *hSession, const char *name, int seconds)
380 390  
381 391 // Check for int properties
382 392 properties = lib3270_get_int_properties_list();
383   - for(ix = 0; ix < (sizeof(properties)/sizeof(properties[0])); ix++)
  393 + for(ix = 0; properties[ix].name; ix++)
384 394 {
385 395 if(!strcasecmp(name,properties[ix].name))
386 396 {
... ...
src/lib3270/session.c
... ... @@ -132,7 +132,7 @@ void lib3270_session_free(H3270 *h)
132 132 while(h->inputs)
133 133 {
134 134 input_t *ip = h->inputs;
135   - h->inputs = ip->next;
  135 + h->inputs = (input_t *) ip->next;
136 136 lib3270_free(ip);
137 137 }
138 138  
... ...