From a20a64aaf0c47c7d573fcefbd40627e5b0e63ab4 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Thu, 4 Jul 2013 17:52:43 +0000 Subject: [PATCH] Implementando tratamento de excessões em rexx --- src/plugins/rx3270/exception.cc | 40 +++++++++++++++++++++++++++------------- src/plugins/rx3270/remote.cc | 38 ++++++++++++++++++++------------------ src/plugins/rx3270/rexx_methods.cc | 17 +++++++++++++---- src/plugins/rx3270/rx3270.cc | 15 +++------------ src/plugins/rx3270/rx3270.h | 6 ++++-- src/plugins/rx3270/typed_routines.cc | 22 ++++++++++++++++++++-- 6 files changed, 87 insertions(+), 51 deletions(-) diff --git a/src/plugins/rx3270/exception.cc b/src/plugins/rx3270/exception.cc index e5b3512..71400ba 100644 --- a/src/plugins/rx3270/exception.cc +++ b/src/plugins/rx3270/exception.cc @@ -34,33 +34,34 @@ #endif // HAVE_SYSLOG #include + #include /*--[ Implement ]------------------------------------------------------------------------------------*/ rx3270::exception::exception(const char *fmt, ...) { - char buffer[4096]; va_list arg_ptr; va_start(arg_ptr, fmt); - vsnprintf(buffer,4095,fmt,arg_ptr); + vsnprintf(this->msg,4095,fmt,arg_ptr); va_end(arg_ptr); - msg = strdup(buffer); + this->code = Rexx_Error_Application_error; } -extern "C" { - static void memfree(void *ptr) - { - free(ptr); - } -} - -rx3270::exception::~exception() +rx3270::exception::exception(int code, const char *fmt, ...) { - memfree(msg); + va_list arg_ptr; + + va_start(arg_ptr, fmt); + vsnprintf(this->msg,4095,fmt,arg_ptr); + va_end(arg_ptr); + + this->code = code; + } + const char * rx3270::exception::getMessage(void) { return this->msg; @@ -79,8 +80,21 @@ void rx3270::exception::logMessage(void) void rx3270::exception::RaiseException(RexxMethodContext *context) { - // TODO: Raise rexx exception trace("%s: %s",__FUNCTION__,this->getMessage()); logMessage(); + + context->RaiseException1( this->code, + context->NewStringFromAsciiz(this->msg) + ); +} + +void rx3270::exception::RaiseException(RexxCallContext *context) +{ + trace("%s: %s",__FUNCTION__,this->getMessage()); + logMessage(); + + context->RaiseException1( this->code, + context->NewStringFromAsciiz(this->msg) + ); } diff --git a/src/plugins/rx3270/remote.cc b/src/plugins/rx3270/remote.cc index b0bfa5b..47d12d2 100644 --- a/src/plugins/rx3270/remote.cc +++ b/src/plugins/rx3270/remote.cc @@ -104,6 +104,9 @@ char * query_string(const char *method); int query_intval(const char *method); + char * get_string(DBusMessage * msg); + int get_intval(DBusMessage * msg); + #endif @@ -127,7 +130,7 @@ DBusMessage * remote::create_message(const char *method) method); // method if (!msg) - log("Error creating message for method %s",method); + throw exception("Error creating message for method %s",method); return msg; } @@ -167,7 +170,7 @@ remote::remote(const char *name) if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT)) { - log("%s","Invalid service instance"); + throw exception("%s","Invalid service instance"); return; } @@ -175,13 +178,13 @@ remote::remote(const char *name) if(hPipe == INVALID_HANDLE_VALUE) { - log("%s","Can´t create service pipe"); + throw exception("%s","Can´t create service pipe"); return; } if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL)) { - log("%s","Can´t set pipe state"); + throw exception("%s","Can´t set pipe state"); CloseHandle(hPipe); hPipe = INVALID_HANDLE_VALUE; return; @@ -267,13 +270,13 @@ remote::remote(const char *name) if (dbus_error_is_set(&err)) { - log("DBUS Connection Error (%s)", err.message); + throw exception("DBUS Connection Error (%s)", err.message); dbus_error_free(&err); } if(!conn) { - log("%s", "DBUS Connection failed"); + throw exception("%s", "DBUS Connection failed"); return; } @@ -293,7 +296,7 @@ remote::remote(const char *name) if (dbus_error_is_set(&err)) { - log("Name Error (%s)", err.message); + throw exception("Name Error (%s)", err.message); dbus_error_free(&err); conn = NULL; return; @@ -302,7 +305,7 @@ remote::remote(const char *name) if(rc != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { trace("%s: DBUS request for name %s failed",__FUNCTION__, busname); - log("DBUS for \"%s\" failed",name); + throw exception("DBUS request for \"%s\" failed",name); conn = NULL; return; } @@ -345,7 +348,7 @@ DBusMessage * remote::call(DBusMessage *msg) if(!reply) { - log("%s",error.message); + throw exception("%s",error.message); dbus_error_free(&error); } @@ -353,7 +356,7 @@ DBusMessage * remote::call(DBusMessage *msg) } -char * get_string(DBusMessage * msg) +char * remote::get_string(DBusMessage * msg) { char *rc = NULL; if(msg) @@ -369,12 +372,12 @@ char * get_string(DBusMessage * msg) trace("Response: [%s]",str); rc = strdup(str); } -#ifdef DEBUG else { - trace("Return type is %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_STRING); + dbus_message_unref(msg); + throw exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32); + return NULL; } -#endif } dbus_message_unref(msg); @@ -389,7 +392,7 @@ char * remote::query_string(const char *method) return NULL; } -int get_intval(DBusMessage * msg) +int remote::get_intval(DBusMessage * msg) { int rc = -1; @@ -405,12 +408,12 @@ int get_intval(DBusMessage * msg) dbus_message_iter_get_basic(&iter, &iSigned); rc = (int) iSigned; } -#ifdef DEBUG else { - trace("Return type is %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32); + dbus_message_unref(msg); + throw exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32); + return -1; } -#endif } dbus_message_unref(msg); @@ -447,7 +450,6 @@ char * remote::get_revision(void) } - LIB3270_CSTATE remote::get_cstate(void) { #if defined(WIN32) diff --git a/src/plugins/rx3270/rexx_methods.cc b/src/plugins/rx3270/rexx_methods.cc index ae1b032..9404ae9 100644 --- a/src/plugins/rx3270/rexx_methods.cc +++ b/src/plugins/rx3270/rexx_methods.cc @@ -125,10 +125,19 @@ RexxMethod2(int, rx3270_method_sleep, CSELF, sessionPtr, int, seconds) RexxMethod1(logical_t, rx3270_method_is_connected, CSELF, sessionPtr) { - rx3270 *hSession = (rx3270 *) sessionPtr; - if(!hSession) - return false; - return hSession->is_connected(); + try + { + rx3270 *hSession = (rx3270 *) sessionPtr; + if(!hSession) + return false; + return hSession->is_connected(); + } + catch(rx3270::exception e) + { + e.RaiseException(context); + } + + return 0; } RexxMethod1(logical_t, rx3270_method_is_ready, CSELF, sessionPtr) diff --git a/src/plugins/rx3270/rx3270.cc b/src/plugins/rx3270/rx3270.cc index 5931fae..16bc2b3 100644 --- a/src/plugins/rx3270/rx3270.cc +++ b/src/plugins/rx3270/rx3270.cc @@ -118,18 +118,9 @@ char * rx3270::get_revision(void) rx3270 * rx3270::get_default() { - try - { - if(defSession) - return defSession; - return create_local(); - } - catch(exception e) - { - e.logMessage(); - } - - return NULL; + if(defSession) + return defSession; + return create_local(); } void rx3270::log(const char *fmt, ...) diff --git a/src/plugins/rx3270/rx3270.h b/src/plugins/rx3270/rx3270.h index 95ca642..20061e4 100644 --- a/src/plugins/rx3270/rx3270.h +++ b/src/plugins/rx3270/rx3270.h @@ -147,16 +147,18 @@ class exception { public: + exception(int code, const char *fmt, ...); exception(const char *fmt, ...); - ~exception(); const char * getMessage(void); void logMessage(void); void RaiseException(RexxMethodContext *context); + void RaiseException(RexxCallContext *context); private: - char *msg; + int code; + char msg[4096]; }; diff --git a/src/plugins/rx3270/typed_routines.cc b/src/plugins/rx3270/typed_routines.cc index b345b3b..5a45abc 100644 --- a/src/plugins/rx3270/typed_routines.cc +++ b/src/plugins/rx3270/typed_routines.cc @@ -35,7 +35,16 @@ RexxRoutine0(CSTRING, rx3270version) { - return rx3270::get_default()->get_version(); + try + { + return rx3270::get_default()->get_version(); + } + catch(rx3270::exception e) + { + e.RaiseException(context); + } + + return NULL; } RexxRoutine0(CSTRING, rx3270QueryCState) @@ -184,7 +193,16 @@ RexxRoutine3(int, rx3270queryStringAt, int, row, int, col, CSTRING, key) RexxRoutine2(int, rx3270SetCursorPosition, int, row, int, col) { - return rx3270::get_default()->set_cursor_position(row,col); + try + { + return rx3270::get_default()->set_cursor_position(row,col); + } + catch(rx3270::exception e) + { + e.RaiseException(context); + } + + return -1; } RexxRoutine3(int, rx3270SetStringAt, int, row, int, col, CSTRING, text) -- libgit2 0.21.2