From a2ea3a5742c06e513ff1b62758a4ec3813baa4da Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Tue, 10 Jul 2012 12:57:48 +0000 Subject: [PATCH] Implementando comunicação com o host através de sockets java --- android/Makefile | 9 ++++++++- android/jni/main.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++- android/lib3270NDK.cbp | 5 +++++ android/src/br/com/bb/pw3270/PW3270Activity.java | 13 +++++++------ android/src/br/com/bb/pw3270/lib3270.java | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ pw3270.cbp | 313 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------- 6 files changed, 347 insertions(+), 177 deletions(-) diff --git a/android/Makefile b/android/Makefile index c273c99..a6d0abf 100644 --- a/android/Makefile +++ b/android/Makefile @@ -32,12 +32,16 @@ CONVERT=rsvg-convert RESOLUTION=xhdpi mdpi ldpi hdpi -Debug: jni/lib3270jni.h $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png) +Debug: jni/lib3270jni.h icons @$(NDKBUILD) +icons: $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png) + clean: @rm -fr obj @rm -f jni/lib3270jni.h + @rm -f sigs.txt + @rm -f bin/classes/$(CLASSPATH)/lib3270.class @rm -f $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png) res/drawable-xhdpi/ic_launcher.png: ../src/pw3270/pixmaps/pw3270.svg @@ -60,6 +64,9 @@ res/drawable-hdpi/ic_launcher.png: ../src/pw3270/pixmaps/pw3270.svg @$(MKDIR) `dirname $@` @$(CONVERT) --format=png --width=72 --height=72 --output=$@ $< +sigs.txt: ./bin/classes/br/com/bb/pw3270/lib3270.class + @javap -classpath ./bin/classes -s -p br.com.bb.pw3270.lib3270 > $@ + jni/lib3270jni.h: bin/classes/$(CLASSPATH)/lib3270.class @echo " GEN `basename $@`" @$(JAVAH) -o $@ -classpath bin/classes $(subst /,.,$(CLASSPATH)).lib3270 diff --git a/android/jni/main.cpp b/android/jni/main.cpp index 19762c1..df26c5b 100644 --- a/android/jni/main.cpp +++ b/android/jni/main.cpp @@ -28,6 +28,7 @@ #include "globals.h" #include + #include /*--[ Globals ]--------------------------------------------------------------------------------------*/ @@ -69,7 +70,7 @@ static int popuphandler(H3270 *session, void *terminal, LIB3270_NOTIFY type, con JNIEnv * env = ((INFO *) session->widget)->env; jobject obj = ((INFO *) session->widget)->obj; jclass cls = env->GetObjectClass(obj); - jmethodID mid = env->GetMethodID(cls, "postPopup", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + jmethodID mid = env->GetMethodID(cls, "popupMessage", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); char * descr; descr = lib3270_vsprintf(fmt, args); @@ -91,6 +92,30 @@ static void ctlr_done(H3270 *session) post_message(session,4); } +static int write_buffer(H3270 *session, unsigned const char *buf, int len) +{ + int rc = -1; + + if(session->widget) + { + JNIEnv * env = ((INFO *) session->widget)->env; + jobject obj = ((INFO *) session->widget)->obj; + jclass cls = env->GetObjectClass(obj); + jmethodID mid = env->GetMethodID(cls, "send_data", "([BI)I"); + jbyteArray buffer = env->NewByteArray(len); + + env->SetByteArrayRegion(buffer, 0, len, (jbyte*) buf); + + rc = env->CallIntMethod(obj, mid, buffer, (jint) len ); + } + else + { + __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "Can't send %d bytes, no jni env for active session",len); + } + + return rc; +} + JNIEXPORT jint JNICALL Java_br_com_bb_pw3270_lib3270_init(JNIEnv *env, jclass obj) { H3270 * session = lib3270_session_new(""); @@ -99,6 +124,7 @@ JNIEXPORT jint JNICALL Java_br_com_bb_pw3270_lib3270_init(JNIEnv *env, jclass ob lib3270_set_popup_handler(popuphandler); + session->write = write_buffer; session->changed = changed; session->update_status = update_status; session->erase = erase; @@ -150,3 +176,18 @@ JNIEXPORT jint JNICALL Java_br_com_bb_pw3270_lib3270_do_1connect(JNIEnv *env, jo return (jint) rc; } +JNIEXPORT void JNICALL Java_br_com_bb_pw3270_lib3270_procRecvdata(JNIEnv *env, jobject obj, jbyteArray buffer, jint sz) +{ + unsigned char *netrbuf = (unsigned char *) env->GetByteArrayElements(buffer,NULL); + + session_request(env,obj); + + trace("Processando %d bytes",(size_t) sz); + + lib3270_data_recv(session, (size_t) sz, netrbuf); + + env->ReleaseByteArrayElements(buffer, (signed char *) netrbuf, 0); + + session_release(); + +} diff --git a/android/lib3270NDK.cbp b/android/lib3270NDK.cbp index 468702b..68c1c3d 100644 --- a/android/lib3270NDK.cbp +++ b/android/lib3270NDK.cbp @@ -34,13 +34,18 @@ + + + + diff --git a/android/src/br/com/bb/pw3270/PW3270Activity.java b/android/src/br/com/bb/pw3270/PW3270Activity.java index 89e9ec5..1d0f0eb 100644 --- a/android/src/br/com/bb/pw3270/PW3270Activity.java +++ b/android/src/br/com/bb/pw3270/PW3270Activity.java @@ -16,12 +16,11 @@ public class PW3270Activity extends Activity implements View.OnClickListener { private class terminal extends lib3270 { - private static final String TAG = "pw3270"; +// private static final String TAG = "pw3270"; TextView msgbox; Activity Main; - - + terminal(TextView msgbox, Activity Main) { this.msgbox = msgbox; @@ -36,7 +35,7 @@ public class PW3270Activity extends Activity implements View.OnClickListener } catch(Exception e) { this.msgbox.setText("Estado inesperado"); } } - public void popupMessage(int type, String title, String text, String info) + protected void popupMessage(int type, String title, String text, String info) { AlertDialog d = new AlertDialog.Builder(Main).create(); @@ -46,13 +45,15 @@ public class PW3270Activity extends Activity implements View.OnClickListener d.setCancelable(true); d.show(); } + + /* protected void redraw() { String text = getHTML(); Log.i(TAG,text); } - + */ }; @@ -88,7 +89,7 @@ public class PW3270Activity extends Activity implements View.OnClickListener public void onClick(View v) { // Perform action on click - host.setHost(uri.getText().toString()); + // host.setHost(uri.getText().toString()); host.connect(); } diff --git a/android/src/br/com/bb/pw3270/lib3270.java b/android/src/br/com/bb/pw3270/lib3270.java index 55dc411..a8dcac2 100644 --- a/android/src/br/com/bb/pw3270/lib3270.java +++ b/android/src/br/com/bb/pw3270/lib3270.java @@ -4,12 +4,26 @@ import java.lang.Thread; import android.os.Handler; import android.os.Message; import android.util.Log; +import javax.net.SocketFactory; +import java.net.Socket; +import javax.net.ssl.SSLSocketFactory; +import java.io.DataInputStream; +import java.io.DataOutputStream; public class lib3270 { private NetworkThread mainloop; private static final String TAG = "lib3270"; - private boolean changed; + + private boolean changed; + private boolean connected = false; + + DataOutputStream outData = null; + DataInputStream inData = null; + + private String hostname = "3270.df.bb"; + private int port = 8023; + private boolean ssl = false; static { @@ -37,27 +51,127 @@ public class lib3270 } } + protected int send_data(byte[] data, int len) + { + Log.i(TAG,"Bytes a enviar: " + len); + + try + { + outData.write(data,0,len); + outData.flush(); + } catch( Exception e ) + { + String msg = e.getLocalizedMessage(); + + if(msg == null) + msg = e.toString(); + + if(msg == null) + msg = "Erro indefinido"; + + Log.i(TAG,"Erro ao enviar dados: " + msg); + + postPopup(0,"Erro na comunicação","Não foi possível enviar dados",msg); + + } + return -1; + } + + // Main Thread private class NetworkThread extends Thread { - Handler mHandler; + Handler mHandler; + Socket sock = null; NetworkThread(Handler h) { mHandler = h; } + private boolean connect() + { + // Connecta no host + SocketFactory socketFactory; + if(ssl) + { + // Host é SSL + socketFactory = SSLSocketFactory.getDefault(); + } + else + { + socketFactory = SocketFactory.getDefault(); + } + + try + { + sock = socketFactory.createSocket(hostname,port); + outData = new DataOutputStream(sock.getOutputStream()); + inData = new DataInputStream(sock.getInputStream()); + + } catch( Exception e ) + { + String msg = e.getLocalizedMessage(); + + if(msg == null) + msg = e.toString(); + + if(msg == null) + msg = "Erro indefinido"; + + Log.i(TAG,"Erro ao conectar: " + msg); + + postPopup(0,"Erro na conexão","Não foi possível conectar",msg); + + postMessage(0,0,0); + + return false; + } + + Log.i(TAG,"Conectado ao host"); + return true; + + } + public void run() { - int rc; + info(TAG,"Network thread started"); postMessage(0,1,0); - rc = do_connect(); - info(TAG,"do_connect exits with rc="+rc); - while(isConnected()) - processEvents(); - postMessage(0,0,0); - info(TAG,"Network thread stopped"); + connected = connect(); + + while(connected) + { + byte[] in = new byte[4096]; + int sz = -1; + + try + { + sz = inData.read(in,0,4096); + } catch( Exception e ) { sz = -1; } + + if(sz < 0) + { + connected = false; + } + else if(sz > 0) + { + Log.d(TAG,sz + " bytes recebidos"); + procRecvdata(in,sz); + } + } + + try + { + sock.close(); + } catch( Exception e ) { } + + sock = null; + outData = null; + inData = null; + postMessage(0,0,0); + + info(TAG,"Network thread stopped"); } public void postMessage(int what, int arg1, int arg2) @@ -141,7 +255,7 @@ public class lib3270 { } - public void popupMessage(int type, String title, String text, String info) + protected void popupMessage(int type, String title, String text, String info) { } @@ -170,12 +284,12 @@ public class lib3270 { if(mainloop == null) { - info("jni","Starting comm thread"); + info(TAG,"Starting comm thread"); mainloop = new NetworkThread(handler); mainloop.start(); return 0; } - error("jni","Comm thread already active during connect"); + error(TAG,"Comm thread already active during connect"); return -1; } @@ -190,6 +304,9 @@ public class lib3270 public native String getVersion(); public native String getRevision(); + // Network I/O + public native void procRecvdata( byte[] data, int len); + // Connect/Disconnect status public native void setHost(String host); public native String getHost(); diff --git a/pw3270.cbp b/pw3270.cbp index 413c001..95a3403 100644 --- a/pw3270.cbp +++ b/pw3270.cbp @@ -9,8 +9,8 @@