Commit 90c109989615693c8aaf4876ebe02ec67a6e477c
1 parent
a046e5a6
Exists in
master
and in
5 other branches
Android - Debugando erros de comunicação
Showing
3 changed files
with
96 additions
and
46 deletions
Show diff stats
android/jni/main.cpp
| ... | ... | @@ -50,9 +50,11 @@ |
| 50 | 50 | const char * java_class_name = "br/com/bb/pw3270/lib3270"; |
| 51 | 51 | PW3270_JNI * pw3270_jni_active = NULL; |
| 52 | 52 | static pthread_mutex_t mutex; |
| 53 | + static char * startup_script = NULL; | |
| 53 | 54 | |
| 54 | 55 | /*--[ Implement ]------------------------------------------------------------------------------------*/ |
| 55 | 56 | |
| 57 | + | |
| 56 | 58 | jmethodID lib3270_getmethodID(const char *name, const char *sig) |
| 57 | 59 | { |
| 58 | 60 | if(!pw3270_jni_active) |
| ... | ... | @@ -268,25 +270,13 @@ static void ctlr_done(H3270 *session) |
| 268 | 270 | |
| 269 | 271 | static void autostart(H3270 *session) |
| 270 | 272 | { |
| 273 | + if(startup_script) | |
| 271 | 274 | { |
| 272 | - char *text = lib3270_get_text(PW3270_SESSION,0,-1); | |
| 273 | - if(text) | |
| 274 | - { | |
| 275 | - char *strtok_r(char *str, const char *delim, char **saveptr); | |
| 276 | - char *save; | |
| 277 | - | |
| 278 | -/* | |
| 279 | - __android_log_print(ANDROID_LOG_DEBUG, PACKAGE_NAME, "Contents:\n"); | |
| 280 | - for(char *ptr = strtok_r(text,"\n",&save);ptr;ptr = strtok_r(NULL,"\n",&save)) | |
| 281 | - __android_log_print(ANDROID_LOG_DEBUG, PACKAGE_NAME, "%s\n",ptr); | |
| 282 | -*/ | |
| 283 | - | |
| 284 | - lib3270_free(text); | |
| 285 | - } | |
| 275 | + // Input startup script contents | |
| 276 | + lib3270_emulate_input(PW3270_SESSION,startup_script,-1,0); | |
| 277 | + free(startup_script); | |
| 278 | + startup_script = NULL; | |
| 286 | 279 | } |
| 287 | - | |
| 288 | - pw3270_jni_post_message(10); | |
| 289 | - | |
| 290 | 280 | } |
| 291 | 281 | |
| 292 | 282 | jint JNI_OnLoad(JavaVM *vm, void *reserved) |
| ... | ... | @@ -352,6 +342,27 @@ JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_setHost(JNIEnv *env, jobjec |
| 352 | 342 | PW3270_JNI_END |
| 353 | 343 | } |
| 354 | 344 | |
| 345 | +JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_setStartupScript(JNIEnv *env, jobject obj, jstring str) | |
| 346 | +{ | |
| 347 | + const char *text; | |
| 348 | + | |
| 349 | + PW3270_JNI_BEGIN | |
| 350 | + | |
| 351 | + text = env->GetStringUTFChars(str, 0); | |
| 352 | + | |
| 353 | + if(startup_script) | |
| 354 | + free(startup_script); | |
| 355 | + | |
| 356 | + if(text || strlen(text) > 0) | |
| 357 | + startup_script = strdup(text); | |
| 358 | + else | |
| 359 | + startup_script = NULL; | |
| 360 | + | |
| 361 | + PW3270_JNI_END | |
| 362 | + | |
| 363 | +} | |
| 364 | + | |
| 365 | + | |
| 355 | 366 | JNIEXPORT jstring JNICALL Java_br_com_bb_pw3270_lib3270_getHost(JNIEnv *env, jobject obj) |
| 356 | 367 | { |
| 357 | 368 | return env->NewStringUTF(lib3270_get_host(lib3270_get_default_session_handle())); | ... | ... |
android/jni/text.cpp
| ... | ... | @@ -139,18 +139,3 @@ JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_setTextAt(JNIEnv *env, jobj |
| 139 | 139 | PW3270_JNI_END |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | -JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_runStartupString(JNIEnv *env, jobject obj, jstring str) | |
| 143 | -{ | |
| 144 | - int rc = -1; | |
| 145 | - | |
| 146 | - PW3270_JNI_BEGIN | |
| 147 | - | |
| 148 | - if(lib3270_connected(PW3270_SESSION)) | |
| 149 | - { | |
| 150 | - lib3270_set_cursor_address(PW3270_SESSION,lib3270_get_next_unprotected(PW3270_SESSION,0)); | |
| 151 | - lib3270_emulate_input(PW3270_SESSION, env->GetStringUTFChars(str, 0), -1, 0); | |
| 152 | - } | |
| 153 | - | |
| 154 | - PW3270_JNI_END | |
| 155 | - | |
| 156 | -} | ... | ... |
android/src/br/com/bb/pw3270/lib3270.java
| ... | ... | @@ -84,7 +84,8 @@ public abstract class lib3270 |
| 84 | 84 | { |
| 85 | 85 | switch (msg.what) |
| 86 | 86 | { |
| 87 | - case 0: // Reconnect | |
| 87 | + case 0: // Connection lost | |
| 88 | + reload(); | |
| 88 | 89 | if(!hSession.isConnected() && settings.getString("hostname","") != "" && settings.getBoolean("reconnect",false)) |
| 89 | 90 | { |
| 90 | 91 | Log.d(TAG,"Connection lost, reconnecting"); |
| ... | ... | @@ -139,14 +140,14 @@ public abstract class lib3270 |
| 139 | 140 | new timer(((Long) msg.obj).longValue(), msg.arg1); |
| 140 | 141 | break; |
| 141 | 142 | |
| 143 | +/* | |
| 142 | 144 | case 10: // Run autostart |
| 143 | - | |
| 144 | 145 | String str = settings.getString("logonstring",""); |
| 145 | 146 | Log.v(TAG, "Logon string is \"" + str + "\""); |
| 146 | 147 | if( str != "") |
| 147 | 148 | runStartupString(str); |
| 148 | 149 | break; |
| 149 | - | |
| 150 | +*/ | |
| 150 | 151 | } |
| 151 | 152 | } |
| 152 | 153 | }; |
| ... | ... | @@ -273,6 +274,10 @@ public abstract class lib3270 |
| 273 | 274 | { |
| 274 | 275 | Log.v(TAG,"Getting socket for " + hostname + ":" + port.toString()); |
| 275 | 276 | sock = socketFactory.createSocket(hostname, port); |
| 277 | + | |
| 278 | + sock.setKeepAlive(true); | |
| 279 | + // sock.setSoTimeout(1000); | |
| 280 | + | |
| 276 | 281 | outData = new DataOutputStream(sock.getOutputStream()); |
| 277 | 282 | inData = new DataInputStream(sock.getInputStream()); |
| 278 | 283 | } |
| ... | ... | @@ -418,6 +423,7 @@ public abstract class lib3270 |
| 418 | 423 | case 4: // LIB3270_MESSAGE_DISCONNECTED |
| 419 | 424 | Log.v(TAG, "Status changed to disconnected"); |
| 420 | 425 | connected = false; |
| 426 | + net_cleanup(); | |
| 421 | 427 | reload(); |
| 422 | 428 | break; |
| 423 | 429 | |
| ... | ... | @@ -447,10 +453,12 @@ public abstract class lib3270 |
| 447 | 453 | |
| 448 | 454 | protected void showPopupMessage(int type, String title, String text, String info) |
| 449 | 455 | { |
| 456 | + /* | |
| 450 | 457 | Log.v(TAG,"Popup Message:"); |
| 451 | 458 | Log.v(TAG,title); |
| 452 | 459 | Log.v(TAG,text); |
| 453 | 460 | Log.v(TAG,info); |
| 461 | + */ | |
| 454 | 462 | |
| 455 | 463 | AlertDialog d = new AlertDialog.Builder(mainact).create(); |
| 456 | 464 | |
| ... | ... | @@ -510,31 +518,76 @@ public abstract class lib3270 |
| 510 | 518 | |
| 511 | 519 | public int connect() |
| 512 | 520 | { |
| 521 | + if(connected) | |
| 522 | + { | |
| 523 | + Log.v(TAG, "Already connected"); | |
| 524 | + return -1; | |
| 525 | + } | |
| 526 | + | |
| 527 | + Log.v(TAG, "Connecting"); | |
| 513 | 528 | if (mainloop == null) |
| 514 | 529 | { |
| 515 | 530 | info(TAG, "Starting comm thread"); |
| 531 | + setStartupScript(settings.getString("logonstring","")); | |
| 516 | 532 | mainloop = new NetworkThread(); |
| 517 | 533 | mainloop.start(); |
| 518 | 534 | return 0; |
| 519 | 535 | } |
| 520 | - error(TAG, "Comm thread already active during connect"); | |
| 536 | + Log.v(TAG, "Comm thread already active during connect"); | |
| 537 | + disconnect(); | |
| 538 | + | |
| 521 | 539 | return -1; |
| 522 | 540 | } |
| 523 | 541 | |
| 524 | - public int disconnect() | |
| 542 | + private int net_cleanup() | |
| 525 | 543 | { |
| 526 | - Log.v(TAG, "Disconnecting"); | |
| 527 | - connected = reconnect = false; | |
| 528 | - | |
| 544 | + Log.v(TAG, "Stopping network activity"); | |
| 545 | + | |
| 529 | 546 | if(sock != null) |
| 530 | 547 | { |
| 531 | - try | |
| 548 | + if(sock.isConnected()) | |
| 549 | + { | |
| 550 | + try | |
| 551 | + { | |
| 552 | + sock.shutdownInput(); | |
| 553 | + return 0; | |
| 554 | + } | |
| 555 | + catch(Exception e) | |
| 556 | + { | |
| 557 | + String msg = e.getLocalizedMessage(); | |
| 558 | + if(msg == null) | |
| 559 | + msg = e.getMessage(); | |
| 560 | + | |
| 561 | + Log.v(TAG,": shutdownInput error" + (msg != null ? msg : e.toString())); | |
| 562 | + } | |
| 563 | + } | |
| 564 | + | |
| 565 | + if(!sock.isClosed()) | |
| 532 | 566 | { |
| 533 | - sock.shutdownInput(); | |
| 534 | - sock.shutdownOutput(); | |
| 535 | - } catch(Exception e) { } | |
| 567 | + try | |
| 568 | + { | |
| 569 | + sock.close(); | |
| 570 | + return 0; | |
| 571 | + } | |
| 572 | + catch(Exception e) | |
| 573 | + { | |
| 574 | + String msg = e.getLocalizedMessage(); | |
| 575 | + if(msg == null) | |
| 576 | + msg = e.getMessage(); | |
| 577 | + | |
| 578 | + Log.v(TAG,"sockclose error: " + (msg != null ? msg : e.toString())); | |
| 579 | + } | |
| 580 | + } | |
| 536 | 581 | } |
| 537 | - | |
| 582 | + | |
| 583 | + return 0; | |
| 584 | + } | |
| 585 | + | |
| 586 | + public int disconnect() | |
| 587 | + { | |
| 588 | + Log.v(TAG, "Disconnecting"); | |
| 589 | + connected = reconnect = false; | |
| 590 | + net_cleanup(); | |
| 538 | 591 | return 0; |
| 539 | 592 | } |
| 540 | 593 | |
| ... | ... | @@ -623,5 +676,6 @@ public abstract class lib3270 |
| 623 | 676 | |
| 624 | 677 | public native boolean in3270(); |
| 625 | 678 | |
| 626 | - public native void runStartupString(String text); | |
| 679 | + public native void setStartupScript(String str); | |
| 680 | + | |
| 627 | 681 | } | ... | ... |