From fe4a5aff05acab023646331ebfd0349678f42f9f Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Thu, 4 Apr 2013 19:01:57 +0000 Subject: [PATCH] Implementando interface DBUS no modulo rexx --- src/plugins/rx3270/Makefile.in | 2 +- src/plugins/rx3270/local.cc | 15 ++++++++++++--- src/plugins/rx3270/pluginmain.cc | 7 ++++--- src/plugins/rx3270/remote.cc | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------- src/plugins/rx3270/rexx_methods.cc | 30 ++++++++++++++++++++++++++++++ src/plugins/rx3270/rx3270.cls | 3 +++ src/plugins/rx3270/rx3270.h | 5 ++++- src/plugins/rx3270/rxapimain.cc | 2 ++ src/plugins/rx3270/sample/remote.rex | 11 +++++++++++ 9 files changed, 169 insertions(+), 51 deletions(-) create mode 100644 src/plugins/rx3270/sample/remote.rex diff --git a/src/plugins/rx3270/Makefile.in b/src/plugins/rx3270/Makefile.in index ca3d4fd..da1af8d 100644 --- a/src/plugins/rx3270/Makefile.in +++ b/src/plugins/rx3270/Makefile.in @@ -38,7 +38,7 @@ LN_S=@LN_S@ #---[ Include plugin rules ]--------------------------------------------------- -LIBS=@REXX_LIBS@ @LIBICONV@ +LIBS=@REXX_LIBS@ @LIBICONV@ @DBUS_LIBS@ CFLAGS=@REXX_CFLAGS@ @DBUS_CFLAGS@ REXX_HOME=@REXX_HOME@ REXXLIBDIR=$(libdir)/ooRexx diff --git a/src/plugins/rx3270/local.cc b/src/plugins/rx3270/local.cc index c640ebe..e5d5aa7 100644 --- a/src/plugins/rx3270/local.cc +++ b/src/plugins/rx3270/local.cc @@ -60,7 +60,7 @@ dynamic(); ~dynamic(); - const char * get_version(void); + char * get_version(void); LIB3270_CSTATE get_cstate(void); int disconnect(void); int connect(const char *uri, bool wait = true); @@ -154,6 +154,15 @@ rx3270 * rx3270::create(const char *name) return new dynamic(); } +char * rx3270::get_version(void) +{ + return strdup(PACKAGE_VERSION); +} + +char * rx3270::get_revision(void) +{ + return strdup(PACKAGE_REVISION); +} rx3270 * rx3270::get_default(void) { @@ -424,11 +433,11 @@ dynamic::~dynamic() } -const char * dynamic::get_version(void) +char * dynamic::get_version(void) { if(!hModule) return NULL; - return _get_version(); + return strdup(_get_version()); } LIB3270_CSTATE dynamic::get_cstate(void) diff --git a/src/plugins/rx3270/pluginmain.cc b/src/plugins/rx3270/pluginmain.cc index 9647763..d870e90 100644 --- a/src/plugins/rx3270/pluginmain.cc +++ b/src/plugins/rx3270/pluginmain.cc @@ -28,6 +28,7 @@ */ #include "rx3270.h" + #include #include #include @@ -38,7 +39,7 @@ public: plugin(H3270 *hSession); - const char * get_version(void); + char * get_version(void); LIB3270_CSTATE get_cstate(void); int disconnect(void); int connect(const char *uri, bool wait = true); @@ -93,9 +94,9 @@ this->hSession = hSession; } - const char * plugin::get_version(void) + char * plugin::get_version(void) { - return lib3270_get_version(); + return strdup(lib3270_get_version()); } LIB3270_CSTATE plugin::get_cstate(void) diff --git a/src/plugins/rx3270/remote.cc b/src/plugins/rx3270/remote.cc index c741205..75dee58 100644 --- a/src/plugins/rx3270/remote.cc +++ b/src/plugins/rx3270/remote.cc @@ -44,7 +44,7 @@ remote(const char *session); ~remote(); - const char * get_version(void); + char * get_revision(void); LIB3270_CSTATE get_cstate(void); int disconnect(void); int connect(const char *uri, bool wait = true); @@ -72,10 +72,12 @@ #elif defined(HAVE_DBUS) DBusConnection * conn; - char * service_name; - char * interface_name; + char * dest; + char * path; + char * intf; DBusMessage * create_message(const char *method); - + DBusMessage * call(DBusMessage *msg); + char * query_string(const char *method); #endif @@ -84,8 +86,8 @@ /*--[ Globals ]--------------------------------------------------------------------------------------*/ #if defined(HAVE_DBUS) - static const char * prefix = "br.com.bb."; - static const char * object = "br/com/bb/" PACKAGE_NAME; + static const char * prefix_dest = "br.com.bb."; + static const char * prefix_path = "/br/com/bb/"; #else #error AQUI #endif // HAVE_DBUS @@ -95,10 +97,10 @@ #if defined(HAVE_DBUS) DBusMessage * remote::create_message(const char *method) { - DBusMessage * msg = dbus_message_new_method_call( service_name, - object, - interface_name, - method); + DBusMessage * msg = dbus_message_new_method_call( this->dest, // Destination + this->path, // Path + this->intf, // Interface + method); // method if (!msg) fprintf(stderr, "Error creating message for method %s\n",method); @@ -130,40 +132,35 @@ remote::remote(const char *name) *(ptr++) = 0; - sz = strlen(ptr)+strlen(name)+strlen(prefix)+2; + // Build destination + sz = strlen(ptr)+strlen(str)+strlen(prefix_dest)+2; + dest = (char *) malloc(sz+1); + strncpy(dest,prefix_dest,sz); + strncat(dest,str,sz); + strncat(dest,".",sz); + strncat(dest,ptr,sz); + + // Build path + sz = strlen(str)+strlen(prefix_path); + path = (char *) malloc(sz+1); + strncpy(path,prefix_path,sz); + strncat(path,str,sz); + + // Build intf + sz = strlen(str)+strlen(prefix_dest)+1; + intf = (char *) malloc(sz+1); + strncpy(intf,prefix_dest,sz); + strncat(intf,str,sz); - service_name = (char *) malloc(sz+1); - strncpy(service_name,prefix,sz); - strncat(service_name,".",sz); - strncat(service_name,name,sz); - strncat(service_name,".",sz); - strncat(service_name,ptr,sz); - sz = strlen(prefix)+strlen(name)+1; - interface_name = (char *) malloc(sz+1); - strncpy(interface_name,prefix,sz); - strncat(interface_name,".",sz); - strncat(interface_name,name,sz); } else { - size_t sz = strlen(name)+strlen(prefix)+1; - - service_name = (char *) malloc(sz+1); - strncpy(service_name,prefix,sz); - strncat(service_name,".",sz); - strncat(service_name,name,sz); - - sz = strlen(prefix)+strlen(name)+1; - interface_name = (char *) malloc(sz+1); - strncpy(interface_name,prefix,sz); - strncat(interface_name,".",sz); - strncat(interface_name,name,sz); + exit(-1); } - trace("service_name: [%s]", service_name); - trace("interface_name: [%s]", interface_name); + trace("DBUS:\nDestination:\t[%s]\nPath:\t\t[%s]\nInterface:\t[%s]",dest,path,intf); free(str); @@ -213,28 +210,85 @@ remote::~remote() #elif defined(HAVE_DBUS) - if(conn) - dbus_connection_close(conn); - - free(service_name); - free(interface_name); + free(dest); + free(path); + free(intf); #else #endif } -const char * remote::get_version(void) +#if defined(HAVE_DBUS) +DBusMessage * remote::call(DBusMessage *msg) +{ + DBusMessage * reply; + DBusError error; + + dbus_error_init(&error); + reply = dbus_connection_send_with_reply_and_block(conn,msg,10000,&error); + dbus_message_unref(msg); + + if(reply) + return reply; + + fprintf(stderr,"%s\n",error.message); + dbus_error_free(&error); + + return NULL; + +} + +char * remote::query_string(const char *method) +{ + char *rc = NULL; + + if(conn) + { + DBusMessage * msg = call(create_message(method)); + if(msg) + { + DBusMessageIter iter; + + if(dbus_message_iter_init(msg, &iter)) + { + if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) + { + const char * str; + dbus_message_iter_get_basic(&iter, &str); + rc = strdup(str); + } + } + + dbus_message_unref(msg); + } + } + + return rc; +} + +#endif // HAVE_DBUS + +char * remote::get_revision(void) { #if defined(WIN32) + return NULL; + + #elif defined(HAVE_DBUS) -#endif + return query_string("getRevision"); + +#else return NULL; + +#endif + } + LIB3270_CSTATE remote::get_cstate(void) { #if defined(WIN32) @@ -297,6 +351,9 @@ int remote::iterate(bool wait) #elif defined(HAVE_DBUS) + if(wait) + this->wait(1); + #endif return -1; @@ -308,6 +365,8 @@ int remote::wait(int seconds) #elif defined(HAVE_DBUS) + sleep(seconds); + #endif return -1; diff --git a/src/plugins/rx3270/rexx_methods.cc b/src/plugins/rx3270/rexx_methods.cc index 143b18e..7396c59 100644 --- a/src/plugins/rx3270/rexx_methods.cc +++ b/src/plugins/rx3270/rexx_methods.cc @@ -54,6 +54,36 @@ RexxMethod1(int, rx3270_method_uninit, CSELF, sessionPtr) return 0; } +RexxMethod1(RexxStringObject, rx3270_method_version, CSELF, sessionPtr) +{ + rx3270 * session = (rx3270 *) sessionPtr; + + if(session) + { + char * version = session->get_version(); + RexxStringObject ret = context->String((CSTRING) (version ? version : "ERROR:")); + free(version); + return ret; + } + + return context->String((CSTRING) PACKAGE_VERSION); +} + +RexxMethod1(RexxStringObject, rx3270_method_revision, CSELF, sessionPtr) +{ + rx3270 * session = (rx3270 *) sessionPtr; + + if(session) + { + char * version = session->get_revision(); + RexxStringObject ret = context->String((CSTRING) (version ? version : PACKAGE_REVISION)); + free(version); + return ret; + } + + return context->String((CSTRING) PACKAGE_REVISION); +} + RexxMethod3(int, rx3270_method_connect, CSELF, sessionPtr, CSTRING, uri, OPTIONAL_int, wait) { rx3270 *hSession = (rx3270 *) sessionPtr; diff --git a/src/plugins/rx3270/rx3270.cls b/src/plugins/rx3270/rx3270.cls index 339ac26..6441aed 100644 --- a/src/plugins/rx3270/rx3270.cls +++ b/src/plugins/rx3270/rx3270.cls @@ -37,6 +37,9 @@ ::METHOD INIT EXTERNAL "LIBRARY rx3270 rx3270_method_init" ::METHOD UNINIT EXTERNAL "LIBRARY rx3270 rx3270_method_uninit" +::METHOD VERSION EXTERNAL "LIBRARY rx3270 rx3270_method_version" +::METHOD REVISION EXTERNAL "LIBRARY rx3270 rx3270_method_revision" + ::METHOD CONNECT EXTERNAL "LIBRARY rx3270 rx3270_method_connect" ::METHOD DISCONNECT EXTERNAL "LIBRARY rx3270 rx3270_method_disconnect" diff --git a/src/plugins/rx3270/rx3270.h b/src/plugins/rx3270/rx3270.h index 6e46e27..f31c0aa 100644 --- a/src/plugins/rx3270/rx3270.h +++ b/src/plugins/rx3270/rx3270.h @@ -74,6 +74,8 @@ REXX_TYPED_ROUTINE_PROTOTYPE(rx3270queryStringAt); REXX_TYPED_ROUTINE_PROTOTYPE(rx3270SetStringAt); + REXX_METHOD_PROTOTYPE(rx3270_method_version); + REXX_METHOD_PROTOTYPE(rx3270_method_revision); REXX_METHOD_PROTOTYPE(rx3270_method_init); REXX_METHOD_PROTOTYPE(rx3270_method_uninit); REXX_METHOD_PROTOTYPE(rx3270_method_connect); @@ -128,7 +130,8 @@ char * get_3270_string(const char *str); char * get_local_string(const char *str); - virtual const char * get_version(void) = 0; + virtual char * get_version(void); + virtual char * get_revision(void); virtual LIB3270_CSTATE get_cstate(void) = 0; virtual int connect(const char *uri, bool wait = true) = 0; diff --git a/src/plugins/rx3270/rxapimain.cc b/src/plugins/rx3270/rxapimain.cc index 9afcfc7..3190143 100644 --- a/src/plugins/rx3270/rxapimain.cc +++ b/src/plugins/rx3270/rxapimain.cc @@ -111,6 +111,8 @@ RexxRoutineEntry rx3270_functions[] = RexxMethodEntry rx3270_methods[] = { + REXX_METHOD(rx3270_method_version, rx3270_method_version ), + REXX_METHOD(rx3270_method_revision, rx3270_method_revision ), REXX_METHOD(rx3270_method_init, rx3270_method_init ), REXX_METHOD(rx3270_method_uninit, rx3270_method_uninit ), REXX_METHOD(rx3270_method_connect, rx3270_method_connect ), diff --git a/src/plugins/rx3270/sample/remote.rex b/src/plugins/rx3270/sample/remote.rex new file mode 100644 index 0000000..69f3a79 --- /dev/null +++ b/src/plugins/rx3270/sample/remote.rex @@ -0,0 +1,11 @@ + +use arg uri + +host = .rx3270~new("pw3270:a") + +say "PW3270 version is "||host~revision() + +return 0 + +::requires "rx3270.cls" + -- libgit2 0.21.2