Commit ef0594fab7ea7b4dd8fe51d50500b7ce5d7ab38a
1 parent
95897d64
Exists in
master
and in
5 other branches
Iniciando implementação de uma classe básica capaz de funcionar via DBUS/HLLAPI …
…ou carregando dinamicamente a lib3270 para evitar a duplicação de código a manter nos módulos rx3270 e jni
Showing
9 changed files
with
1881 additions
and
0 deletions
Show diff stats
| @@ -0,0 +1,89 @@ | @@ -0,0 +1,89 @@ | ||
| 1 | +# | ||
| 2 | +# "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | +# (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | +# aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | +# | ||
| 6 | +# Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | +# | ||
| 8 | +# Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | +# os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | +# Free Software Foundation. | ||
| 11 | +# | ||
| 12 | +# Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | +# GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | +# A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | +# obter mais detalhes. | ||
| 16 | +# | ||
| 17 | +# Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | +# programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | +# Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | +# | ||
| 21 | +# Contatos: | ||
| 22 | +# | ||
| 23 | +# perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 24 | +# erico.mendonca@gmail.com (Erico Mascarenhas de Mendonça) | ||
| 25 | +# | ||
| 26 | + | ||
| 27 | +LIBNAME=pw3270cxx.a | ||
| 28 | + | ||
| 29 | +#---[ Sources ]---------------------------------------------------------------- | ||
| 30 | + | ||
| 31 | +SOURCES=main.cc session.cc exception.cc local.cc remote.cc | ||
| 32 | + | ||
| 33 | +#---[ Paths ]------------------------------------------------------------------ | ||
| 34 | + | ||
| 35 | +ROOTDIR ?= . | ||
| 36 | +OBJDIR ?= $(ROOTDIR)/.obj | ||
| 37 | +BINDIR ?= $(ROOTDIR)/.bin | ||
| 38 | +BINDBG ?= $(BINDIR)/Debug | ||
| 39 | +BINRLS ?= $(BINDIR)/Release | ||
| 40 | + | ||
| 41 | +OBJDBG = $(OBJDIR)/Debug | ||
| 42 | +OBJRLS = $(OBJDIR)/Release | ||
| 43 | + | ||
| 44 | +#---[ Tools ]------------------------------------------------------------------ | ||
| 45 | + | ||
| 46 | +MKDIR = mkdir -p | ||
| 47 | + | ||
| 48 | +#---[ Flags ]------------------------------------------------------------------ | ||
| 49 | + | ||
| 50 | +LIBS=-ldl -ldbus-glib-1 -ldbus-1 | ||
| 51 | +CFLAGS=-fPIC -I../include -I/usr/include/dbus-1.0 -I/usr/lib64/dbus-1.0/include | ||
| 52 | +DEBUG_CFLAGS=-DDEBUG=1 -g -Wall | ||
| 53 | + | ||
| 54 | + | ||
| 55 | +#---[ Rules ]------------------------------------------------------------------ | ||
| 56 | + | ||
| 57 | +CXX=g++ | ||
| 58 | +LD=g++ | ||
| 59 | + | ||
| 60 | +DEPENDS=*.h ../include/*.h ../include/lib3270/*.h Makefile | ||
| 61 | + | ||
| 62 | +$(OBJDBG)/%.o: %.cc $(DEPENDS) | ||
| 63 | + @echo " CC `basename $@`" | ||
| 64 | + @$(MKDIR) `dirname $@` | ||
| 65 | + @$(CXX) $(CFLAGS) $(DEBUG_CFLAGS) -o $@ -c $< | ||
| 66 | + | ||
| 67 | + | ||
| 68 | +#---[ Debug targets ]---------------------------------------------------------- | ||
| 69 | + | ||
| 70 | +Debug: $(BINDBG)/testprogram$(EXEEXT) | ||
| 71 | + | ||
| 72 | +run: $(BINDBG)/testprogram$(EXEEXT) | ||
| 73 | + $(BINDBG)/testprogram$(EXEEXT) | ||
| 74 | + | ||
| 75 | +$(BINDBG)/testprogram$(EXEEXT): $(OBJDBG)/testprogram.o $(BINDBG)/$(LIBNAME) | ||
| 76 | + @echo " CCLD `basename $@`" | ||
| 77 | + @$(MKDIR) `dirname $@` | ||
| 78 | + @$(LD) -o $@ $^ $(LIBS) | ||
| 79 | + | ||
| 80 | +$(BINDBG)/$(LIBNAME): $(foreach SRC, $(basename $(SOURCES)), $(OBJDBG)/$(SRC).o) | ||
| 81 | + @echo " CCLD `basename $@`" | ||
| 82 | + @$(MKDIR) `dirname $@` | ||
| 83 | + @ar rs $@ $^ | ||
| 84 | + | ||
| 85 | +clean: | ||
| 86 | + @rm -fr $(OBJDIR) | ||
| 87 | + @rm -fr $(BINDIR) | ||
| 88 | + @rm -f testprogram | ||
| 89 | + @find . -name "*~" -exec rm -f {} \; |
| @@ -0,0 +1,50 @@ | @@ -0,0 +1,50 @@ | ||
| 1 | +<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> | ||
| 2 | +<CodeBlocks_project_file> | ||
| 3 | + <FileVersion major="1" minor="6" /> | ||
| 4 | + <Project> | ||
| 5 | + <Option title="pw3270 Class Library" /> | ||
| 6 | + <Option makefile_is_custom="1" /> | ||
| 7 | + <Option pch_mode="2" /> | ||
| 8 | + <Option compiler="gcc" /> | ||
| 9 | + <Build> | ||
| 10 | + <Target title="Debug"> | ||
| 11 | + <Option output=".bin/Debug/testprogram" prefix_auto="1" extension_auto="1" /> | ||
| 12 | + <Option object_output=".obj/Debug/" /> | ||
| 13 | + <Option type="1" /> | ||
| 14 | + <Option compiler="gcc" /> | ||
| 15 | + <Compiler> | ||
| 16 | + <Add option="-g" /> | ||
| 17 | + </Compiler> | ||
| 18 | + </Target> | ||
| 19 | + <Target title="Release"> | ||
| 20 | + <Option output=".bin/Release/pw3270 Class Library" prefix_auto="1" extension_auto="1" /> | ||
| 21 | + <Option working_dir="" /> | ||
| 22 | + <Option object_output=".obj/Release/" /> | ||
| 23 | + <Option type="2" /> | ||
| 24 | + <Option compiler="gcc" /> | ||
| 25 | + <Compiler> | ||
| 26 | + <Add option="-O2" /> | ||
| 27 | + </Compiler> | ||
| 28 | + <Linker> | ||
| 29 | + <Add option="-s" /> | ||
| 30 | + </Linker> | ||
| 31 | + </Target> | ||
| 32 | + </Build> | ||
| 33 | + <Compiler> | ||
| 34 | + <Add option="-Wall" /> | ||
| 35 | + </Compiler> | ||
| 36 | + <Unit filename="Makefile" /> | ||
| 37 | + <Unit filename="exception.cc" /> | ||
| 38 | + <Unit filename="local.cc" /> | ||
| 39 | + <Unit filename="main.cc" /> | ||
| 40 | + <Unit filename="pw3270class.h" /> | ||
| 41 | + <Unit filename="remote.cc" /> | ||
| 42 | + <Unit filename="session.cc" /> | ||
| 43 | + <Unit filename="testprogram.cc" /> | ||
| 44 | + <Extensions> | ||
| 45 | + <envvars /> | ||
| 46 | + <code_completion /> | ||
| 47 | + <debugger /> | ||
| 48 | + </Extensions> | ||
| 49 | + </Project> | ||
| 50 | +</CodeBlocks_project_file> |
| @@ -0,0 +1,65 @@ | @@ -0,0 +1,65 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como exception.cc e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | + #include <stdarg.h> | ||
| 31 | + #include <stdio.h> | ||
| 32 | + | ||
| 33 | + #include "pw3270class.h" | ||
| 34 | + | ||
| 35 | +/*--[ Implement ]--------------------------------------------------------------------------------------------------*/ | ||
| 36 | + | ||
| 37 | + namespace pw3270 | ||
| 38 | + { | ||
| 39 | + | ||
| 40 | + exception::exception(int code, const char *fmt, ...) | ||
| 41 | + { | ||
| 42 | + va_list arg_ptr; | ||
| 43 | + va_start(arg_ptr, fmt); | ||
| 44 | + vsnprintf(this->msg,4095,fmt,arg_ptr); | ||
| 45 | + va_end(arg_ptr); | ||
| 46 | + | ||
| 47 | + this->code = code; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + exception::exception(const char *fmt, ...) | ||
| 51 | + { | ||
| 52 | + va_list arg_ptr; | ||
| 53 | + va_start(arg_ptr, fmt); | ||
| 54 | + vsnprintf(this->msg,4095,fmt,arg_ptr); | ||
| 55 | + va_end(arg_ptr); | ||
| 56 | + | ||
| 57 | + this->code = -1; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + const char * exception::what() const throw() | ||
| 61 | + { | ||
| 62 | + return this->msg; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + } |
| @@ -0,0 +1,478 @@ | @@ -0,0 +1,478 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como local.cc e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | +#if defined WIN32 | ||
| 31 | + | ||
| 32 | + // http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx | ||
| 33 | + #ifndef LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | ||
| 34 | + #define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000 | ||
| 35 | + #endif // LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | ||
| 36 | + | ||
| 37 | + #ifndef LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | ||
| 38 | + #define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 0x00000100 | ||
| 39 | + #endif // LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | ||
| 40 | + | ||
| 41 | + #include <windows.h> | ||
| 42 | + | ||
| 43 | +#else | ||
| 44 | + | ||
| 45 | + #include <dlfcn.h> | ||
| 46 | + | ||
| 47 | +#endif | ||
| 48 | + | ||
| 49 | +#include "pw3270class.h" | ||
| 50 | +#include <lib3270/log.h> | ||
| 51 | +#include <lib3270/popup.h> | ||
| 52 | +#include <string.h> | ||
| 53 | +#include <stdio.h> | ||
| 54 | + | ||
| 55 | +#ifdef HAVE_SYSLOG | ||
| 56 | + #include <syslog.h> | ||
| 57 | + #include <stdarg.h> | ||
| 58 | +#endif // HAVE_SYSLOG | ||
| 59 | + | ||
| 60 | +/*--[ Implement ]--------------------------------------------------------------------------------------------------*/ | ||
| 61 | + | ||
| 62 | + extern "C" | ||
| 63 | + { | ||
| 64 | + static void loghandler(H3270 *session, const char *module, int rc, const char *fmt, va_list args) | ||
| 65 | + { | ||
| 66 | + #ifdef HAVE_SYSLOG | ||
| 67 | + openlog(PACKAGE_NAME, LOG_NDELAY, LOG_USER); | ||
| 68 | + vsyslog(LOG_INFO,fmt,args); | ||
| 69 | + closelog(); | ||
| 70 | + #endif // HAVE_SYSLOG | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + static void tracehandler(H3270 *session, const char *fmt, va_list args) | ||
| 74 | + { | ||
| 75 | + #ifdef HAVE_SYSLOG | ||
| 76 | + | ||
| 77 | + #define MAX_LOG_LENGTH 200 | ||
| 78 | + | ||
| 79 | + static char line[MAX_LOG_LENGTH+1]; | ||
| 80 | + char temp[MAX_LOG_LENGTH]; | ||
| 81 | + char * ptr; | ||
| 82 | + size_t len = strlen(line); | ||
| 83 | + | ||
| 84 | + vsnprintf(temp,MAX_LOG_LENGTH-len,fmt,args); | ||
| 85 | + | ||
| 86 | + ptr = strchr(temp,'\n'); | ||
| 87 | + if(!ptr) | ||
| 88 | + { | ||
| 89 | + strncat(line,temp,MAX_LOG_LENGTH); | ||
| 90 | + if(strlen(line) >= MAX_LOG_LENGTH) | ||
| 91 | + { | ||
| 92 | + openlog(PACKAGE_NAME, LOG_NDELAY, LOG_USER); | ||
| 93 | + syslog(LOG_INFO,line); | ||
| 94 | + closelog(); | ||
| 95 | + *line = 0; | ||
| 96 | + } | ||
| 97 | + return; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + *ptr = 0; | ||
| 101 | + strncat(line,temp,MAX_LOG_LENGTH); | ||
| 102 | + | ||
| 103 | + openlog(PACKAGE_NAME, LOG_NDELAY, LOG_USER); | ||
| 104 | + syslog(LOG_DEBUG,line); | ||
| 105 | + closelog(); | ||
| 106 | + | ||
| 107 | + strncpy(line,ptr+1,MAX_LOG_LENGTH); | ||
| 108 | + | ||
| 109 | + #endif // HAVE_SYSLOG | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + namespace pw3270 | ||
| 115 | + { | ||
| 116 | + | ||
| 117 | + class local : public session | ||
| 118 | + { | ||
| 119 | + private: | ||
| 120 | + | ||
| 121 | + H3270 * hSession; | ||
| 122 | + | ||
| 123 | + #ifdef WIN32 | ||
| 124 | + | ||
| 125 | + HMODULE hModule; | ||
| 126 | + | ||
| 127 | + int get_datadir(LPSTR datadir) | ||
| 128 | + { | ||
| 129 | + HKEY hKey = 0; | ||
| 130 | + unsigned long datalen = strlen(datadir); | ||
| 131 | + | ||
| 132 | + *datadir = 0; | ||
| 133 | + | ||
| 134 | + if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS) | ||
| 135 | + { | ||
| 136 | + unsigned long datatype; // #defined in winnt.h (predefined types 0-11) | ||
| 137 | + if(RegQueryValueExA(hKey,"datadir",NULL,&datatype,(LPBYTE) datadir,&datalen) != ERROR_SUCCESS) | ||
| 138 | + *datadir = 0; | ||
| 139 | + RegCloseKey(hKey); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + return *datadir; | ||
| 143 | + } | ||
| 144 | + #else | ||
| 145 | + | ||
| 146 | + void * hModule; | ||
| 147 | + | ||
| 148 | + #endif // WIN32 | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * Dynamically load lib3270 | ||
| 152 | + * | ||
| 153 | + * @return 0 if the library was loaded, -1 on error. | ||
| 154 | + * | ||
| 155 | + */ | ||
| 156 | + int load3270(void) | ||
| 157 | + { | ||
| 158 | + #ifdef WIN32 | ||
| 159 | + static const char *dllname = "lib3270.dll." PACKAGE_VERSION; | ||
| 160 | + | ||
| 161 | + HMODULE kernel; | ||
| 162 | + HANDLE cookie = NULL; | ||
| 163 | + DWORD rc; | ||
| 164 | + HANDLE WINAPI (*AddDllDirectory)(PCWSTR NewDirectory); | ||
| 165 | + BOOL WINAPI (*RemoveDllDirectory)(HANDLE Cookie); | ||
| 166 | + UINT errorMode; | ||
| 167 | + char datadir[4096]; | ||
| 168 | + char buffer[4096]; | ||
| 169 | + | ||
| 170 | + kernel = LoadLibrary("kernel32.dll"); | ||
| 171 | + AddDllDirectory = (HANDLE WINAPI (*)(PCWSTR)) GetProcAddress(kernel,"AddDllDirectory"); | ||
| 172 | + RemoveDllDirectory = (BOOL WINAPI (*)(HANDLE)) GetProcAddress(kernel,"RemoveDllDirectory"); | ||
| 173 | + | ||
| 174 | + // Notify user in case of error loading protocol DLL | ||
| 175 | + // http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v=vs.85).aspx | ||
| 176 | + errorMode = SetErrorMode(1); | ||
| 177 | + | ||
| 178 | + memset(datadir,' ',4095); | ||
| 179 | + datadir[4095] = 0; | ||
| 180 | + | ||
| 181 | + if(get_datadir(datadir)) | ||
| 182 | + { | ||
| 183 | + trace("Datadir=[%s] AddDllDirectory=%p RemoveDllDirectory=%p\n",datadir,AddDllDirectory,RemoveDllDirectory); | ||
| 184 | + | ||
| 185 | + if(AddDllDirectory) | ||
| 186 | + { | ||
| 187 | + wchar_t *path = (wchar_t *) malloc(4096*sizeof(wchar_t)); | ||
| 188 | + mbstowcs(path, datadir, 4095); | ||
| 189 | + cookie = AddDllDirectory(path); | ||
| 190 | + free(path); | ||
| 191 | + } | ||
| 192 | + | ||
| 193 | + #ifdef DEBUG | ||
| 194 | + snprintf(buffer,4096,"%s\\.bin\\Debug\\%s",datadir,dllname); | ||
| 195 | + #else | ||
| 196 | + snprintf(buffer,4096,"%s\\%s",datadir,dllname); | ||
| 197 | + #endif // DEBUG | ||
| 198 | + | ||
| 199 | + trace("Loading [%s] [%s]",buffer,datadir); | ||
| 200 | + hModule = LoadLibrary(buffer); | ||
| 201 | + | ||
| 202 | + trace("Module=%p rc=%d",hModule,(int) GetLastError()); | ||
| 203 | + | ||
| 204 | + if(hModule == NULL) | ||
| 205 | + { | ||
| 206 | + // Enable DLL error popup and try again with full path | ||
| 207 | + SetErrorMode(0); | ||
| 208 | + hModule = LoadLibraryEx(buffer,NULL,LOAD_LIBRARY_SEARCH_DEFAULT_DIRS|LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR); | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + rc = GetLastError(); | ||
| 212 | + | ||
| 213 | + trace("%s hModule=%p rc=%d",buffer,hModule,(int) rc); | ||
| 214 | + } | ||
| 215 | + else | ||
| 216 | + { | ||
| 217 | + hModule = LoadLibrary(dllname); | ||
| 218 | + rc = GetLastError(); | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + SetErrorMode(errorMode); | ||
| 222 | + | ||
| 223 | + trace("%s hModule=%p rc=%d",dllname,hModule,(int) rc); | ||
| 224 | + | ||
| 225 | + if(cookie && RemoveDllDirectory) | ||
| 226 | + RemoveDllDirectory(cookie); | ||
| 227 | + | ||
| 228 | + if(kernel) | ||
| 229 | + FreeLibrary(kernel); | ||
| 230 | + | ||
| 231 | + if(hModule) | ||
| 232 | + return 0; | ||
| 233 | + | ||
| 234 | + throw exception("Can't load %s",dllname); | ||
| 235 | + | ||
| 236 | + #else | ||
| 237 | + dlerror(); | ||
| 238 | + | ||
| 239 | + hModule = dlopen("lib3270.so." PACKAGE_VERSION, RTLD_NOW); | ||
| 240 | + if(hModule) | ||
| 241 | + return 0; | ||
| 242 | + | ||
| 243 | + throw exception("Can't load lib3270: %s",dlerror()); | ||
| 244 | + | ||
| 245 | + #endif // WIN32 | ||
| 246 | + | ||
| 247 | + return -1; | ||
| 248 | + | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + void * get_symbol(const char *name) | ||
| 252 | + { | ||
| 253 | +#ifdef WIN32 | ||
| 254 | + void *symbol = (void *) GetProcAddress(hModule,name); | ||
| 255 | + | ||
| 256 | + if(symbol) | ||
| 257 | + return symbol; | ||
| 258 | + | ||
| 259 | +#else | ||
| 260 | + void *symbol; | ||
| 261 | + | ||
| 262 | + symbol = dlsym(hModule,name); | ||
| 263 | + | ||
| 264 | + if(symbol) | ||
| 265 | + return symbol; | ||
| 266 | + | ||
| 267 | + throw exception("Can't load symbol lib3270::%s dlerror was \"%s\"",name,dlerror()); | ||
| 268 | + | ||
| 269 | +#endif // WIN32 | ||
| 270 | + | ||
| 271 | + return NULL; | ||
| 272 | + | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + | ||
| 276 | + // Lib3270 entry points | ||
| 277 | + const char * (*_get_version)(void); | ||
| 278 | + LIB3270_CSTATE (*_get_connection_state)(H3270 *h); | ||
| 279 | + int (*_disconnect)(H3270 *h); | ||
| 280 | + int (*_connect)(H3270 *h,const char *n, int wait); | ||
| 281 | + int (*_is_connected)(H3270 *h); | ||
| 282 | + void (*_main_iterate)(H3270 *h, int wait); | ||
| 283 | + int (*_wait)(H3270 *hSession, int seconds); | ||
| 284 | + int (*_enter)(H3270 *hSession); | ||
| 285 | + int (*_pfkey)(H3270 *hSession, int key); | ||
| 286 | + int (*_pakey)(H3270 *hSession, int key); | ||
| 287 | + int (*_wait_for_ready)(H3270 *hSession, int seconds); | ||
| 288 | + char * (*_get_text)(H3270 *h, int offset, int len); | ||
| 289 | + char * (*_get_text_at)(H3270 *h, int row, int col, int len); | ||
| 290 | + int (*_cmp_text_at)(H3270 *h, int row, int col, const char *text); | ||
| 291 | + int (*_set_text_at)(H3270 *h, int row, int col, const unsigned char *str); | ||
| 292 | + int (*_is_ready)(H3270 *h); | ||
| 293 | + int (*_set_cursor_position)(H3270 *h, int row, int col); | ||
| 294 | + int (*_set_toggle)(H3270 *h, LIB3270_TOGGLE ix, int value); | ||
| 295 | + int (*_get_field_start)(H3270 *h, int baddr); | ||
| 296 | + int (*_get_field_len)(H3270 *h, int baddr); | ||
| 297 | + int (*_set_cursor_addr)(H3270 *h, int addr); | ||
| 298 | + int (*_get_cursor_addr)(H3270 *h); | ||
| 299 | + int (*_emulate_input)(H3270 *session, const char *s, int len, int pasting); | ||
| 300 | + int (*_get_next_unprotected)(H3270 *hSession, int baddr0); | ||
| 301 | + void (*_popup_va)(H3270 *session, LIB3270_NOTIFY id , const char *title, const char *message, const char *fmt, va_list); | ||
| 302 | + void * (*_free)(void *); | ||
| 303 | + | ||
| 304 | + public: | ||
| 305 | + | ||
| 306 | + local() | ||
| 307 | + { | ||
| 308 | + H3270 * (*lib3270_new)(const char *); | ||
| 309 | + void (*set_log_handler)(void (*loghandler)(H3270 *, const char *, int, const char *, va_list)); | ||
| 310 | + void (*set_trace_handler)( void (*handler)(H3270 *session, const char *fmt, va_list args) ); | ||
| 311 | + | ||
| 312 | + struct _call | ||
| 313 | + { | ||
| 314 | + void **entry; | ||
| 315 | + const char * name; | ||
| 316 | + } call[] = | ||
| 317 | + { | ||
| 318 | + { (void **) & lib3270_new, "lib3270_session_new" }, | ||
| 319 | + { (void **) & set_log_handler, "lib3270_set_log_handler" }, | ||
| 320 | + { (void **) & set_trace_handler, "lib3270_set_trace_handler" }, | ||
| 321 | + | ||
| 322 | + { (void **) & _is_connected, "lib3270_is_connected" }, | ||
| 323 | + { (void **) & _get_connection_state, "lib3270_get_connection_state" }, | ||
| 324 | + | ||
| 325 | + { (void **) & _get_version, "lib3270_get_version" }, | ||
| 326 | + { (void **) & _disconnect, "lib3270_disconnect" }, | ||
| 327 | + { (void **) & _connect, "lib3270_connect" }, | ||
| 328 | + { (void **) & _main_iterate, "lib3270_main_iterate" }, | ||
| 329 | + { (void **) & _wait, "lib3270_wait" }, | ||
| 330 | + { (void **) & _enter, "lib3270_enter" }, | ||
| 331 | + { (void **) & _pfkey, "lib3270_pfkey" }, | ||
| 332 | + { (void **) & _pakey, "lib3270_pakey" }, | ||
| 333 | + { (void **) & _wait_for_ready, "lib3270_wait_for_ready" }, | ||
| 334 | + { (void **) & _get_text, "lib3270_get_text" }, | ||
| 335 | + { (void **) & _get_text_at, "lib3270_get_text_at" }, | ||
| 336 | + { (void **) & _cmp_text_at, "lib3270_cmp_text_at" }, | ||
| 337 | + { (void **) & _set_text_at, "lib3270_set_string_at" }, | ||
| 338 | + { (void **) & _is_ready, "lib3270_is_ready" }, | ||
| 339 | + { (void **) & _set_cursor_position, "lib3270_set_cursor_position" }, | ||
| 340 | + { (void **) & _set_toggle, "lib3270_set_toggle" }, | ||
| 341 | + { (void **) & _get_field_start, "lib3270_get_field_start" }, | ||
| 342 | + { (void **) & _get_field_len, "lib3270_get_field_len" }, | ||
| 343 | + { (void **) & _set_cursor_addr, "lib3270_set_cursor_address" }, | ||
| 344 | + { (void **) & _get_cursor_addr, "lib3270_get_cursor_address" }, | ||
| 345 | + { (void **) & _emulate_input, "lib3270_emulate_input" }, | ||
| 346 | + { (void **) & _get_next_unprotected, "lib3270_get_next_unprotected" }, | ||
| 347 | + { (void **) & _popup_va, "lib3270_popup_va" }, | ||
| 348 | + { (void **) & _free, "lib3270_free" }, | ||
| 349 | + }; | ||
| 350 | + | ||
| 351 | + | ||
| 352 | + if(load3270()) | ||
| 353 | + return; | ||
| 354 | + | ||
| 355 | + trace("hModule=%p",hModule); | ||
| 356 | + | ||
| 357 | + for(unsigned int f = 0; f < (sizeof (call) / sizeof ((call)[0]));f++) | ||
| 358 | + { | ||
| 359 | + *call[f].entry = (void *) get_symbol(call[f].name); | ||
| 360 | + if(!*call[f].entry) | ||
| 361 | + return; | ||
| 362 | + } | ||
| 363 | + | ||
| 364 | + // Get Session handle, setup base callbacks | ||
| 365 | + set_log_handler(loghandler); | ||
| 366 | + set_trace_handler(tracehandler); | ||
| 367 | + this->hSession = lib3270_new(""); | ||
| 368 | + | ||
| 369 | + } | ||
| 370 | + | ||
| 371 | + virtual ~local() | ||
| 372 | + { | ||
| 373 | + try | ||
| 374 | + { | ||
| 375 | + static void (*session_free)(H3270 *h) = (void (*)(H3270 *)) get_symbol("lib3270_session_free"); | ||
| 376 | + | ||
| 377 | + if(session_free && this->hSession) | ||
| 378 | + session_free(this->hSession); | ||
| 379 | + } | ||
| 380 | + catch(exception e) { } | ||
| 381 | + | ||
| 382 | + #ifdef WIN32 | ||
| 383 | + FreeLibrary(hModule); | ||
| 384 | + #else | ||
| 385 | + dlclose(hModule); | ||
| 386 | + #endif // WIN32 | ||
| 387 | + | ||
| 388 | + } | ||
| 389 | + | ||
| 390 | + bool is_connected(void) | ||
| 391 | + { | ||
| 392 | + return _is_connected(hSession); | ||
| 393 | + } | ||
| 394 | + | ||
| 395 | + LIB3270_CSTATE get_cstate(void) | ||
| 396 | + { | ||
| 397 | + return _get_connection_state(hSession); | ||
| 398 | + } | ||
| 399 | + | ||
| 400 | + int connect(const char *uri, bool wait) | ||
| 401 | + { | ||
| 402 | + return _connect(hSession,uri,(int) wait); | ||
| 403 | + } | ||
| 404 | + | ||
| 405 | + int disconnect(void) | ||
| 406 | + { | ||
| 407 | + return _disconnect(hSession); | ||
| 408 | + } | ||
| 409 | + | ||
| 410 | + bool is_ready(void) | ||
| 411 | + { | ||
| 412 | + return _is_ready(hSession) != 0; | ||
| 413 | + } | ||
| 414 | + | ||
| 415 | + int wait_for_ready(int seconds) | ||
| 416 | + { | ||
| 417 | + return _wait_for_ready(hSession,seconds); | ||
| 418 | + } | ||
| 419 | + | ||
| 420 | + int wait(int seconds) | ||
| 421 | + { | ||
| 422 | + return _wait(hSession,seconds); | ||
| 423 | + } | ||
| 424 | + | ||
| 425 | + int iterate(bool wait) | ||
| 426 | + { | ||
| 427 | + _main_iterate(hSession,wait); | ||
| 428 | + return 0; | ||
| 429 | + } | ||
| 430 | + | ||
| 431 | + string * get_text_at(int row, int col, size_t sz) | ||
| 432 | + { | ||
| 433 | + char * ptr = _get_text_at(hSession,row,col,sz); | ||
| 434 | + | ||
| 435 | + if(ptr) | ||
| 436 | + { | ||
| 437 | + string *s = new string(ptr); | ||
| 438 | + _free(ptr); | ||
| 439 | + return s; | ||
| 440 | + } | ||
| 441 | + | ||
| 442 | + return new string(""); | ||
| 443 | + } | ||
| 444 | + | ||
| 445 | + int set_text_at(int row, int col, const char *str) | ||
| 446 | + { | ||
| 447 | + return _set_text_at(hSession,row,col,(const unsigned char *) str); | ||
| 448 | + } | ||
| 449 | + | ||
| 450 | + int cmp_text_at(int row, int col, const char *text) | ||
| 451 | + { | ||
| 452 | + return _cmp_text_at(hSession,row,col,text); | ||
| 453 | + } | ||
| 454 | + | ||
| 455 | + string * get_text(int offset, size_t len) | ||
| 456 | + { | ||
| 457 | + char *ptr = _get_text(hSession,offset,len); | ||
| 458 | + | ||
| 459 | + if(ptr) | ||
| 460 | + { | ||
| 461 | + string *s = new string(ptr); | ||
| 462 | + _free(ptr); | ||
| 463 | + return s; | ||
| 464 | + } | ||
| 465 | + | ||
| 466 | + return new string(""); | ||
| 467 | + } | ||
| 468 | + | ||
| 469 | + | ||
| 470 | + }; | ||
| 471 | + | ||
| 472 | + session * session::create_local(void) | ||
| 473 | + { | ||
| 474 | + return new local(); | ||
| 475 | + } | ||
| 476 | + | ||
| 477 | + } | ||
| 478 | + |
| @@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como main.cc e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + |
| @@ -0,0 +1,164 @@ | @@ -0,0 +1,164 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como pw3270class.c e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | +#ifndef PW3270_CLASS_H_INCLUDED | ||
| 31 | + | ||
| 32 | + #define PW3270_CLASS_H_INCLUDED 1 | ||
| 33 | + | ||
| 34 | + #ifdef WIN32 | ||
| 35 | + #define SYSTEM_CHARSET "CP1252" | ||
| 36 | + #else | ||
| 37 | + #define SYSTEM_CHARSET "UTF-8" | ||
| 38 | + #endif // WIN32 | ||
| 39 | + | ||
| 40 | + #include <exception> | ||
| 41 | + #include <lib3270/config.h> | ||
| 42 | + #include <lib3270.h> | ||
| 43 | + | ||
| 44 | + #ifdef HAVE_ICONV | ||
| 45 | + #include <iconv.h> | ||
| 46 | + #endif // HAVE_ICONV | ||
| 47 | + | ||
| 48 | + #include <string> | ||
| 49 | + #include <stdarg.h> | ||
| 50 | + #include <lib3270.h> | ||
| 51 | + | ||
| 52 | + namespace pw3270 | ||
| 53 | + { | ||
| 54 | + using namespace std; | ||
| 55 | + | ||
| 56 | + class exception : public std::exception | ||
| 57 | + { | ||
| 58 | + public: | ||
| 59 | + exception(int code, const char *fmt, ...); | ||
| 60 | + exception(const char *fmt, ...); | ||
| 61 | + | ||
| 62 | + virtual const char * what() const throw(); | ||
| 63 | + | ||
| 64 | + private: | ||
| 65 | + int code; | ||
| 66 | + char msg[4096]; | ||
| 67 | + | ||
| 68 | + }; | ||
| 69 | + | ||
| 70 | + class session | ||
| 71 | + { | ||
| 72 | + public: | ||
| 73 | + | ||
| 74 | + session(); | ||
| 75 | + virtual ~session(); | ||
| 76 | + | ||
| 77 | + // Factory methods and settings | ||
| 78 | + static session * create(const char *name = 0); | ||
| 79 | + static session * start(const char *name = 0); | ||
| 80 | + static session * get_default(void); | ||
| 81 | + static void set_plugin(session * (*factory)(const char *name)); | ||
| 82 | + | ||
| 83 | + // Object settings | ||
| 84 | + void set_charset(const char *charset); | ||
| 85 | + | ||
| 86 | + // Log management | ||
| 87 | + void log(const char *fmt, ...); | ||
| 88 | + void logva(const char *fmt, va_list args); | ||
| 89 | + | ||
| 90 | + // 3270 methods | ||
| 91 | + virtual string get_version(void); | ||
| 92 | + virtual string get_revision(void); | ||
| 93 | + | ||
| 94 | + virtual bool is_connected(void) = 0; | ||
| 95 | + virtual bool is_ready(void) = 0; | ||
| 96 | + | ||
| 97 | + virtual LIB3270_CSTATE get_cstate(void) = 0; | ||
| 98 | + | ||
| 99 | + virtual int connect(const char *uri, bool wait = true) = 0; | ||
| 100 | + virtual int disconnect(void) = 0; | ||
| 101 | + | ||
| 102 | + virtual int wait_for_ready(int seconds) = 0; | ||
| 103 | + virtual int wait(int seconds) = 0; | ||
| 104 | + virtual int iterate(bool wait = true) = 0; | ||
| 105 | + | ||
| 106 | + virtual string * get_text_at(int row, int col, size_t sz) = 0; | ||
| 107 | + virtual int set_text_at(int row, int col, const char *str) = 0; | ||
| 108 | + virtual string * get_text(int baddr, size_t len) = 0; | ||
| 109 | + virtual int cmp_text_at(int row, int col, const char *text) = 0; | ||
| 110 | + virtual int wait_for_text_at(int row, int col, const char *key, int timeout); | ||
| 111 | + | ||
| 112 | +// virtual int set_cursor_position(int row, int col) = 0; | ||
| 113 | +// virtual int set_cursor_addr(int addr) = 0; | ||
| 114 | +// virtual int get_cursor_addr(void) = 0; | ||
| 115 | + | ||
| 116 | +// virtual int set_toggle(LIB3270_TOGGLE ix, bool value) = 0; | ||
| 117 | + | ||
| 118 | +// virtual int enter(void) = 0; | ||
| 119 | +// virtual int pfkey(int key) = 0; | ||
| 120 | +// virtual int pakey(int key) = 0; | ||
| 121 | + | ||
| 122 | +// virtual int emulate_input(const char *str) = 0; | ||
| 123 | + | ||
| 124 | +// virtual int get_field_start(int baddr = -1) = 0; | ||
| 125 | +// virtual int get_field_len(int baddr = -1) = 0; | ||
| 126 | +// virtual int get_next_unprotected(int baddr = -1) = 0; | ||
| 127 | + | ||
| 128 | +// virtual int set_copy(const char *text); | ||
| 129 | +// virtual char * get_copy(void); | ||
| 130 | + | ||
| 131 | +// virtual char * get_clipboard(void); | ||
| 132 | +// virtual int set_clipboard(const char *text); | ||
| 133 | + | ||
| 134 | +// virtual int quit(void) = 0; | ||
| 135 | + | ||
| 136 | + // Dialogs | ||
| 137 | +// virtual int popup_dialog(LIB3270_NOTIFY id , const char *title, const char *message, const char *fmt, ...); | ||
| 138 | +// virtual char * file_chooser_dialog(GtkFileChooserAction action, const char *title, const char *extension, const char *filename); | ||
| 139 | + | ||
| 140 | + private: | ||
| 141 | + | ||
| 142 | + session * prev; | ||
| 143 | + session * next; | ||
| 144 | + | ||
| 145 | + static session * first; | ||
| 146 | + static session * last; | ||
| 147 | + | ||
| 148 | + static session * (*factory)(const char *name); | ||
| 149 | + | ||
| 150 | + static session * create_remote(const char *name); | ||
| 151 | + static session * create_local(void); | ||
| 152 | + | ||
| 153 | +#ifdef HAVE_ICONV | ||
| 154 | + iconv_t conv2Local; | ||
| 155 | + iconv_t conv2Host; | ||
| 156 | +#endif | ||
| 157 | + | ||
| 158 | + }; | ||
| 159 | + | ||
| 160 | + | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + | ||
| 164 | +#endif // PW3270_CLASS_H_INCLUDED |
| @@ -0,0 +1,787 @@ | @@ -0,0 +1,787 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como remote.cc e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | + #include <lib3270/config.h> | ||
| 31 | + | ||
| 32 | + #if defined(HAVE_DBUS) | ||
| 33 | + #include <stdio.h> | ||
| 34 | + #include <dbus/dbus.h> | ||
| 35 | + #include <string.h> | ||
| 36 | + #include <malloc.h> | ||
| 37 | + #include <sys/types.h> | ||
| 38 | + #include <unistd.h> | ||
| 39 | + #endif // HAVE_DBUS | ||
| 40 | + | ||
| 41 | + #if defined(WIN32) | ||
| 42 | + #include <windows.h> | ||
| 43 | + #include <pw3270/ipcpackets.h> | ||
| 44 | + #endif // WIN32 | ||
| 45 | + | ||
| 46 | + #include "pw3270class.h" | ||
| 47 | + #include <lib3270/log.h> | ||
| 48 | + | ||
| 49 | +#if defined(HAVE_DBUS) | ||
| 50 | + static const char * prefix_dest = "br.com.bb."; | ||
| 51 | + static const char * prefix_path = "/br/com/bb/"; | ||
| 52 | +#endif // HAVE_DBUS | ||
| 53 | + | ||
| 54 | +/*--[ Implement ]--------------------------------------------------------------------------------------------------*/ | ||
| 55 | + | ||
| 56 | + namespace pw3270 | ||
| 57 | + { | ||
| 58 | + | ||
| 59 | + class remote : public session | ||
| 60 | + { | ||
| 61 | + private: | ||
| 62 | +#if defined(WIN32) | ||
| 63 | + | ||
| 64 | + HANDLE hPipe; | ||
| 65 | + | ||
| 66 | +#elif defined(HAVE_DBUS) | ||
| 67 | + | ||
| 68 | + DBusConnection * conn; | ||
| 69 | + char * dest; | ||
| 70 | + char * path; | ||
| 71 | + char * intf; | ||
| 72 | + | ||
| 73 | + DBusMessage * create_message(const char *method) | ||
| 74 | + { | ||
| 75 | + DBusMessage * msg = dbus_message_new_method_call( this->dest, // Destination | ||
| 76 | + this->path, // Path | ||
| 77 | + this->intf, // Interface | ||
| 78 | + method); // method | ||
| 79 | + | ||
| 80 | + if (!msg) | ||
| 81 | + throw exception("Error creating message for method %s",method); | ||
| 82 | + | ||
| 83 | + return msg; | ||
| 84 | + | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + DBusMessage * call(DBusMessage *msg) | ||
| 88 | + { | ||
| 89 | + DBusMessage * reply; | ||
| 90 | + DBusError error; | ||
| 91 | + | ||
| 92 | + dbus_error_init(&error); | ||
| 93 | + reply = dbus_connection_send_with_reply_and_block(conn,msg,10000,&error); | ||
| 94 | + dbus_message_unref(msg); | ||
| 95 | + | ||
| 96 | + if(!reply) | ||
| 97 | + { | ||
| 98 | + exception e = exception("%s",error.message); | ||
| 99 | + dbus_error_free(&error); | ||
| 100 | + throw e; | ||
| 101 | + } | ||
| 102 | + return reply; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + string * get_string(DBusMessage * msg) | ||
| 106 | + { | ||
| 107 | + if(msg) | ||
| 108 | + { | ||
| 109 | + DBusMessageIter iter; | ||
| 110 | + | ||
| 111 | + if(dbus_message_iter_init(msg, &iter)) | ||
| 112 | + { | ||
| 113 | + if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) | ||
| 114 | + { | ||
| 115 | + string * rc; | ||
| 116 | + const char * str; | ||
| 117 | + dbus_message_iter_get_basic(&iter, &str); | ||
| 118 | + trace("Response: [%s]",str); | ||
| 119 | + rc = new string(str); | ||
| 120 | + dbus_message_unref(msg); | ||
| 121 | + return rc; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + exception e = exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32); | ||
| 125 | + dbus_message_unref(msg); | ||
| 126 | + | ||
| 127 | + throw e; | ||
| 128 | + return NULL; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + return NULL; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + string * query_string(const char *method) | ||
| 137 | + { | ||
| 138 | + return get_string(call(create_message(method))); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + int get_intval(DBusMessage * msg) | ||
| 142 | + { | ||
| 143 | + if(msg) | ||
| 144 | + { | ||
| 145 | + DBusMessageIter iter; | ||
| 146 | + | ||
| 147 | + if(dbus_message_iter_init(msg, &iter)) | ||
| 148 | + { | ||
| 149 | + if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32) | ||
| 150 | + { | ||
| 151 | + dbus_int32_t iSigned; | ||
| 152 | + dbus_message_iter_get_basic(&iter, &iSigned); | ||
| 153 | + dbus_message_unref(msg); | ||
| 154 | + return (int) iSigned; | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + exception e = exception("DBUS Return type was %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32); | ||
| 158 | + | ||
| 159 | + dbus_message_unref(msg); | ||
| 160 | + throw e; | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + return -1; | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + int query_intval(const char *method) | ||
| 167 | + { | ||
| 168 | + if(conn) | ||
| 169 | + return get_intval(call(create_message(method))); | ||
| 170 | + return -1; | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | +#endif | ||
| 174 | + | ||
| 175 | + public: | ||
| 176 | + | ||
| 177 | + remote(const char *session) | ||
| 178 | + { | ||
| 179 | +#if defined(WIN32) | ||
| 180 | + static DWORD dwMode = PIPE_READMODE_MESSAGE; | ||
| 181 | + char buffer[4096]; | ||
| 182 | + char * str = strdup(session); | ||
| 183 | + char * ptr; | ||
| 184 | + | ||
| 185 | + hPipe = INVALID_HANDLE_VALUE; | ||
| 186 | + | ||
| 187 | + for(ptr=str;*ptr;ptr++) | ||
| 188 | + { | ||
| 189 | + if(*ptr == ':') | ||
| 190 | + *ptr = '_'; | ||
| 191 | + else | ||
| 192 | + *ptr = tolower(*ptr); | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + snprintf(buffer,4095,"\\\\.\\pipe\\%s",str); | ||
| 196 | + | ||
| 197 | + free(str); | ||
| 198 | + | ||
| 199 | + if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT)) | ||
| 200 | + { | ||
| 201 | + throw exception("Invalid service instance: %s",name); | ||
| 202 | + return; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + hPipe = CreateFile(buffer,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); | ||
| 206 | + | ||
| 207 | + if(hPipe == INVALID_HANDLE_VALUE) | ||
| 208 | + { | ||
| 209 | + throw exception("Can´t create service pipe %s",buffer); | ||
| 210 | + } | ||
| 211 | + else if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL)) | ||
| 212 | + { | ||
| 213 | + exception e = exception("%s","Can´t set pipe state"); | ||
| 214 | + CloseHandle(hPipe); | ||
| 215 | + hPipe = INVALID_HANDLE_VALUE; | ||
| 216 | + throw e; | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | +#elif defined(HAVE_DBUS) | ||
| 220 | + | ||
| 221 | + DBusError err; | ||
| 222 | + int rc; | ||
| 223 | + char * str = strdup(session); | ||
| 224 | + char * ptr; | ||
| 225 | + char busname[4096]; | ||
| 226 | + char pidname[10]; | ||
| 227 | + int pid = (int) getpid(); | ||
| 228 | + | ||
| 229 | + trace("%s str=%p",__FUNCTION__,str); | ||
| 230 | + | ||
| 231 | + for(ptr=str;*ptr;ptr++) | ||
| 232 | + *ptr = tolower(*ptr); | ||
| 233 | + | ||
| 234 | + ptr = strchr(str,':'); | ||
| 235 | + | ||
| 236 | + if(ptr) | ||
| 237 | + { | ||
| 238 | + size_t sz; | ||
| 239 | + | ||
| 240 | + *(ptr++) = 0; | ||
| 241 | + | ||
| 242 | + // Build destination | ||
| 243 | + sz = strlen(ptr)+strlen(str)+strlen(prefix_dest)+2; | ||
| 244 | + dest = (char *) malloc(sz+1); | ||
| 245 | + strncpy(dest,prefix_dest,sz); | ||
| 246 | + strncat(dest,str,sz); | ||
| 247 | + strncat(dest,".",sz); | ||
| 248 | + strncat(dest,ptr,sz); | ||
| 249 | + | ||
| 250 | + // Build path | ||
| 251 | + sz = strlen(str)+strlen(prefix_path); | ||
| 252 | + path = (char *) malloc(sz+1); | ||
| 253 | + strncpy(path,prefix_path,sz); | ||
| 254 | + strncat(path,str,sz); | ||
| 255 | + | ||
| 256 | + // Build intf | ||
| 257 | + sz = strlen(str)+strlen(prefix_dest)+1; | ||
| 258 | + intf = (char *) malloc(sz+1); | ||
| 259 | + strncpy(intf,prefix_dest,sz); | ||
| 260 | + strncat(intf,str,sz); | ||
| 261 | + | ||
| 262 | + } | ||
| 263 | + else | ||
| 264 | + { | ||
| 265 | + size_t sz; | ||
| 266 | + | ||
| 267 | + // Build destination | ||
| 268 | + sz = strlen(str)+strlen(prefix_dest)+2; | ||
| 269 | + dest = (char *) malloc(sz+1); | ||
| 270 | + strncpy(dest,prefix_dest,sz); | ||
| 271 | + strncat(dest,str,sz); | ||
| 272 | + | ||
| 273 | + // Build path | ||
| 274 | + sz = strlen(str)+strlen(prefix_path); | ||
| 275 | + path = (char *) malloc(sz+1); | ||
| 276 | + strncpy(path,prefix_path,sz); | ||
| 277 | + strncat(path,str,sz); | ||
| 278 | + | ||
| 279 | + // Build intf | ||
| 280 | + sz = strlen(str)+strlen(prefix_dest)+1; | ||
| 281 | + intf = (char *) malloc(sz+1); | ||
| 282 | + strncpy(intf,prefix_dest,sz); | ||
| 283 | + strncat(intf,str,sz); | ||
| 284 | + | ||
| 285 | + } | ||
| 286 | + | ||
| 287 | + trace("DBUS:\nDestination:\t[%s]\nPath:\t\t[%s]\nInterface:\t[%s]",dest,path,intf); | ||
| 288 | + | ||
| 289 | + free(str); | ||
| 290 | + | ||
| 291 | + dbus_error_init(&err); | ||
| 292 | + | ||
| 293 | + conn = dbus_bus_get(DBUS_BUS_SESSION, &err); | ||
| 294 | + trace("dbus_bus_get conn=%p",conn); | ||
| 295 | + | ||
| 296 | + if (dbus_error_is_set(&err)) | ||
| 297 | + { | ||
| 298 | + exception e = exception("DBUS Connection Error (%s)", err.message); | ||
| 299 | + dbus_error_free(&err); | ||
| 300 | + throw e; | ||
| 301 | + } | ||
| 302 | + | ||
| 303 | + if(!conn) | ||
| 304 | + { | ||
| 305 | + throw exception("%s", "DBUS Connection failed"); | ||
| 306 | + return; | ||
| 307 | + } | ||
| 308 | + | ||
| 309 | + memset(pidname,0,10); | ||
| 310 | + for(int f = 0; f < 9 && pid > 0;f++) | ||
| 311 | + { | ||
| 312 | + pidname[f] = 'a'+(pid % 25); | ||
| 313 | + pid /= 25; | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + snprintf(busname, 4095, "%s.rx3270.br.com.bb",pidname); | ||
| 317 | + | ||
| 318 | + trace("Busname: [%s]",busname); | ||
| 319 | + | ||
| 320 | + rc = dbus_bus_request_name(conn, busname, DBUS_NAME_FLAG_REPLACE_EXISTING , &err); | ||
| 321 | + trace("dbus_bus_request_name rc=%d",rc); | ||
| 322 | + | ||
| 323 | + if (dbus_error_is_set(&err)) | ||
| 324 | + { | ||
| 325 | + exception e = exception("Name Error (%s)", err.message); | ||
| 326 | + dbus_error_free(&err); | ||
| 327 | + throw e; | ||
| 328 | + return; | ||
| 329 | + } | ||
| 330 | + | ||
| 331 | + if(rc != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) | ||
| 332 | + { | ||
| 333 | + trace("%s: DBUS request for name %s failed",__FUNCTION__, busname); | ||
| 334 | + throw exception("DBUS request for \"%s\" failed",session); | ||
| 335 | + return; | ||
| 336 | + } | ||
| 337 | + | ||
| 338 | + trace("%s: Using DBUS name %s",__FUNCTION__,busname); | ||
| 339 | + | ||
| 340 | + DBusMessage * msg = create_message("setScript"); | ||
| 341 | + | ||
| 342 | + if(msg) | ||
| 343 | + { | ||
| 344 | + const char * id = "r"; | ||
| 345 | + static const dbus_int32_t flag = 1; | ||
| 346 | + dbus_message_append_args(msg, DBUS_TYPE_STRING, &id, DBUS_TYPE_INT32, &flag, DBUS_TYPE_INVALID); | ||
| 347 | + get_intval(call(msg)); | ||
| 348 | + } | ||
| 349 | + | ||
| 350 | + #else | ||
| 351 | + | ||
| 352 | +#endif | ||
| 353 | + } | ||
| 354 | + | ||
| 355 | + virtual ~remote() | ||
| 356 | + { | ||
| 357 | +#if defined(WIN32) | ||
| 358 | + | ||
| 359 | + if(hPipe != INVALID_HANDLE_VALUE) | ||
| 360 | + CloseHandle(hPipe); | ||
| 361 | + | ||
| 362 | +#elif defined(HAVE_DBUS) | ||
| 363 | + | ||
| 364 | + try | ||
| 365 | + { | ||
| 366 | + DBusMessage * msg = create_message("setScript"); | ||
| 367 | + if(msg) | ||
| 368 | + { | ||
| 369 | + const char * id = "r"; | ||
| 370 | + static const dbus_int32_t flag = 0; | ||
| 371 | + dbus_message_append_args(msg, DBUS_TYPE_STRING, &id, DBUS_TYPE_INT32, &flag, DBUS_TYPE_INVALID); | ||
| 372 | + get_intval(call(msg)); | ||
| 373 | + } | ||
| 374 | + } | ||
| 375 | + catch(exception e) | ||
| 376 | + { | ||
| 377 | + | ||
| 378 | + } | ||
| 379 | + | ||
| 380 | + free(dest); | ||
| 381 | + free(path); | ||
| 382 | + free(intf); | ||
| 383 | + | ||
| 384 | +#else | ||
| 385 | + | ||
| 386 | +#endif | ||
| 387 | + } | ||
| 388 | + | ||
| 389 | + bool is_connected(void) | ||
| 390 | + { | ||
| 391 | +#if defined(WIN32) | ||
| 392 | + | ||
| 393 | + static const struct hllapi_packet_query query = { HLLAPI_PACKET_IS_CONNECTED }; | ||
| 394 | + struct hllapi_packet_result response; | ||
| 395 | + DWORD cbSize = sizeof(query); | ||
| 396 | + TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL); | ||
| 397 | + return response.rc != 0; | ||
| 398 | + | ||
| 399 | +#elif defined(HAVE_DBUS) | ||
| 400 | + | ||
| 401 | + return query_intval("isConnected") != 0; | ||
| 402 | + | ||
| 403 | +#endif | ||
| 404 | + | ||
| 405 | + return false; | ||
| 406 | + } | ||
| 407 | + | ||
| 408 | + LIB3270_CSTATE get_cstate(void) | ||
| 409 | + { | ||
| 410 | +#if defined(WIN32) | ||
| 411 | + | ||
| 412 | + static const struct hllapi_packet_query query = { HLLAPI_PACKET_GET_CSTATE }; | ||
| 413 | + struct hllapi_packet_result response; | ||
| 414 | + DWORD cbSize = sizeof(query); | ||
| 415 | + TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL); | ||
| 416 | + return (LIB3270_CSTATE) response.rc; | ||
| 417 | + | ||
| 418 | +#elif defined(HAVE_DBUS) | ||
| 419 | + | ||
| 420 | + return (LIB3270_CSTATE) query_intval("getConnectionState"); | ||
| 421 | + | ||
| 422 | +#else | ||
| 423 | + | ||
| 424 | + return (LIB3270_CSTATE) -1; | ||
| 425 | + | ||
| 426 | +#endif | ||
| 427 | + | ||
| 428 | + } | ||
| 429 | + | ||
| 430 | + int connect(const char *uri, bool wait) | ||
| 431 | + { | ||
| 432 | +#if defined(WIN32) | ||
| 433 | + struct hllapi_packet_connect * pkt; | ||
| 434 | + struct hllapi_packet_result response; | ||
| 435 | + DWORD cbSize; | ||
| 436 | + | ||
| 437 | + cbSize = sizeof(struct hllapi_packet_connect)+strlen(uri); | ||
| 438 | + pkt = (struct hllapi_packet_connect *) malloc(cbSize); | ||
| 439 | + | ||
| 440 | + pkt->packet_id = HLLAPI_PACKET_CONNECT; | ||
| 441 | + pkt->wait = (unsigned char) wait; | ||
| 442 | + strcpy(pkt->hostname,uri); | ||
| 443 | + | ||
| 444 | + trace("Sending %s",pkt->hostname); | ||
| 445 | + | ||
| 446 | + if(TransactNamedPipe(hPipe,(LPVOID) pkt, cbSize, &response, sizeof(response), &cbSize,NULL)) | ||
| 447 | + { | ||
| 448 | + free(pkt); | ||
| 449 | + return response.rc | ||
| 450 | + } | ||
| 451 | + | ||
| 452 | + free(pkt); | ||
| 453 | + | ||
| 454 | + throw exception("Transaction error %d",GetLastError()); | ||
| 455 | + | ||
| 456 | +#elif defined(HAVE_DBUS) | ||
| 457 | + | ||
| 458 | + int rc; | ||
| 459 | + DBusMessage * msg = create_message("connect"); | ||
| 460 | + if(!msg) | ||
| 461 | + return -1; | ||
| 462 | + | ||
| 463 | + dbus_message_append_args(msg, DBUS_TYPE_STRING, &uri, DBUS_TYPE_INVALID); | ||
| 464 | + | ||
| 465 | + rc = get_intval(call(msg)); | ||
| 466 | + | ||
| 467 | + if(!rc && wait) | ||
| 468 | + return wait_for_ready(120); | ||
| 469 | + | ||
| 470 | + return rc; | ||
| 471 | + | ||
| 472 | +#else | ||
| 473 | + return -1; | ||
| 474 | + | ||
| 475 | +#endif | ||
| 476 | + } | ||
| 477 | + | ||
| 478 | + int wait_for_ready(int seconds) | ||
| 479 | + { | ||
| 480 | +#if defined(WIN32) | ||
| 481 | + | ||
| 482 | + time_t end = time(0)+seconds; | ||
| 483 | + | ||
| 484 | + while(time(0) < end) | ||
| 485 | + { | ||
| 486 | + if(!is_connected()) | ||
| 487 | + return ENOTCONN; | ||
| 488 | + | ||
| 489 | + if(is_ready()) | ||
| 490 | + return 0; | ||
| 491 | + | ||
| 492 | + Sleep(250); | ||
| 493 | + } | ||
| 494 | + | ||
| 495 | + return ETIMEDOUT; | ||
| 496 | + | ||
| 497 | +#elif defined(HAVE_DBUS) | ||
| 498 | + | ||
| 499 | + time_t end = time(0)+seconds; | ||
| 500 | + | ||
| 501 | + while(time(0) < end) | ||
| 502 | + { | ||
| 503 | + static const dbus_int32_t delay = 2; | ||
| 504 | + | ||
| 505 | + DBusMessage * msg = create_message("waitForReady"); | ||
| 506 | + int rc; | ||
| 507 | + | ||
| 508 | + if(!msg) | ||
| 509 | + return -1; | ||
| 510 | + | ||
| 511 | + dbus_message_append_args(msg, DBUS_TYPE_INT32, &delay, DBUS_TYPE_INVALID); | ||
| 512 | + | ||
| 513 | + rc = get_intval(call(msg)); | ||
| 514 | + trace("waitForReady exits with rc=%d",rc); | ||
| 515 | + | ||
| 516 | + if(rc != ETIMEDOUT) | ||
| 517 | + return rc; | ||
| 518 | + } | ||
| 519 | + | ||
| 520 | + return ETIMEDOUT; | ||
| 521 | + | ||
| 522 | +#else | ||
| 523 | + | ||
| 524 | + return -1; | ||
| 525 | + | ||
| 526 | +#endif | ||
| 527 | + | ||
| 528 | + } | ||
| 529 | + | ||
| 530 | + bool is_ready(void) | ||
| 531 | + { | ||
| 532 | +#if defined(WIN32) | ||
| 533 | + | ||
| 534 | + static const struct hllapi_packet_query query = { HLLAPI_PACKET_IS_READY }; | ||
| 535 | + struct hllapi_packet_result response; | ||
| 536 | + DWORD cbSize = sizeof(query); | ||
| 537 | + TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL); | ||
| 538 | + return response.rc != 0; | ||
| 539 | + | ||
| 540 | +#elif defined(HAVE_DBUS) | ||
| 541 | + | ||
| 542 | + return query_intval("isReady") != 0; | ||
| 543 | + | ||
| 544 | +#endif | ||
| 545 | + | ||
| 546 | + return false; | ||
| 547 | + } | ||
| 548 | + | ||
| 549 | + | ||
| 550 | + int disconnect(void) | ||
| 551 | + { | ||
| 552 | +#if defined(WIN32) | ||
| 553 | + | ||
| 554 | + static const struct hllapi_packet_query query = { HLLAPI_PACKET_DISCONNECT }; | ||
| 555 | + struct hllapi_packet_result response; | ||
| 556 | + DWORD cbSize = sizeof(query); | ||
| 557 | + TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL); | ||
| 558 | + return 0; | ||
| 559 | + | ||
| 560 | +#elif defined(HAVE_DBUS) | ||
| 561 | + | ||
| 562 | + return query_intval("disconnect"); | ||
| 563 | + | ||
| 564 | +#else | ||
| 565 | + | ||
| 566 | + return -1; | ||
| 567 | + | ||
| 568 | +#endif | ||
| 569 | + | ||
| 570 | + } | ||
| 571 | + | ||
| 572 | + | ||
| 573 | + int wait(int seconds) | ||
| 574 | + { | ||
| 575 | +#if defined(WIN32) | ||
| 576 | + | ||
| 577 | + time_t end = time(0)+seconds; | ||
| 578 | + | ||
| 579 | + while(time(0) < end) | ||
| 580 | + { | ||
| 581 | + if(!is_connected()) | ||
| 582 | + return ENOTCONN; | ||
| 583 | + Sleep(500); | ||
| 584 | + } | ||
| 585 | + | ||
| 586 | + return 0; | ||
| 587 | + | ||
| 588 | +#elif defined(HAVE_DBUS) | ||
| 589 | + | ||
| 590 | + time_t end = time(0)+seconds; | ||
| 591 | + | ||
| 592 | + while(time(0) < end) | ||
| 593 | + { | ||
| 594 | + if(!is_connected()) | ||
| 595 | + return ENOTCONN; | ||
| 596 | + usleep(500); | ||
| 597 | + } | ||
| 598 | + | ||
| 599 | + return 0; | ||
| 600 | + | ||
| 601 | +#else | ||
| 602 | + | ||
| 603 | + return -1; | ||
| 604 | + | ||
| 605 | +#endif | ||
| 606 | + | ||
| 607 | + } | ||
| 608 | + | ||
| 609 | + int iterate(bool wait) | ||
| 610 | + { | ||
| 611 | +#if defined(WIN32) | ||
| 612 | + return 0; | ||
| 613 | +#elif defined(HAVE_DBUS) | ||
| 614 | + return 0; | ||
| 615 | +#else | ||
| 616 | + return -1; | ||
| 617 | +#endif | ||
| 618 | + } | ||
| 619 | + | ||
| 620 | + string * get_text_at(int row, int col, size_t sz) | ||
| 621 | + { | ||
| 622 | +#if defined(WIN32) | ||
| 623 | + | ||
| 624 | + struct hllapi_packet_query_at query = { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz }; | ||
| 625 | + struct hllapi_packet_text * response; | ||
| 626 | + DWORD cbSize = sizeof(struct hllapi_packet_text)+sz; | ||
| 627 | + string * s; | ||
| 628 | + | ||
| 629 | + response = (struct hllapi_packet_text *) malloc(cbSize+2); | ||
| 630 | + memset(response,0,cbSize+2); | ||
| 631 | + | ||
| 632 | + if(TransactNamedPipe(hPipe,(LPVOID) &query, sizeof(struct hllapi_packet_query_at), &response, cbSize, &cbSize,NULL)) | ||
| 633 | + { | ||
| 634 | + if(response->packet_id) | ||
| 635 | + s = new string(""); | ||
| 636 | + else | ||
| 637 | + s = new string(response->text); | ||
| 638 | + } | ||
| 639 | + else | ||
| 640 | + { | ||
| 641 | + s = new string(""); | ||
| 642 | + } | ||
| 643 | + | ||
| 644 | + free(response); | ||
| 645 | + | ||
| 646 | + return s; | ||
| 647 | + | ||
| 648 | +#elif defined(HAVE_DBUS) | ||
| 649 | + | ||
| 650 | + dbus_int32_t r = (dbus_int32_t) row; | ||
| 651 | + dbus_int32_t c = (dbus_int32_t) col; | ||
| 652 | + dbus_int32_t l = (dbus_int32_t) sz; | ||
| 653 | + | ||
| 654 | + DBusMessage * msg = create_message("getTextAt"); | ||
| 655 | + if(!msg) | ||
| 656 | + return NULL; | ||
| 657 | + | ||
| 658 | + trace("%s(%d,%d,%d)",__FUNCTION__,r,c,l); | ||
| 659 | + dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_INT32, &l, DBUS_TYPE_INVALID); | ||
| 660 | + | ||
| 661 | + return get_string(call(msg)); | ||
| 662 | + | ||
| 663 | +#else | ||
| 664 | + | ||
| 665 | + return NULL; | ||
| 666 | + | ||
| 667 | +#endif | ||
| 668 | + | ||
| 669 | + } | ||
| 670 | + | ||
| 671 | + int set_text_at(int row, int col, const char *str) | ||
| 672 | + { | ||
| 673 | +#if defined(WIN32) | ||
| 674 | + | ||
| 675 | + struct hllapi_packet_text_at * query; | ||
| 676 | + struct hllapi_packet_result response; | ||
| 677 | + DWORD cbSize = sizeof(struct hllapi_packet_text_at)+strlen((const char *) str); | ||
| 678 | + | ||
| 679 | + query = (struct hllapi_packet_text_at *) malloc(cbSize); | ||
| 680 | + query->packet_id = HLLAPI_PACKET_SET_TEXT_AT; | ||
| 681 | + query->row = row; | ||
| 682 | + query->col = col; | ||
| 683 | + strcpy(query->text,(const char *) str); | ||
| 684 | + | ||
| 685 | + TransactNamedPipe(hPipe,(LPVOID) query, cbSize, &response, sizeof(response), &cbSize,NULL); | ||
| 686 | + | ||
| 687 | + free(query); | ||
| 688 | + | ||
| 689 | + return response.rc; | ||
| 690 | + | ||
| 691 | +#elif defined(HAVE_DBUS) | ||
| 692 | + | ||
| 693 | + dbus_int32_t r = (dbus_int32_t) row; | ||
| 694 | + dbus_int32_t c = (dbus_int32_t) col; | ||
| 695 | + | ||
| 696 | + DBusMessage * msg = create_message("setTextAt"); | ||
| 697 | + if(msg) | ||
| 698 | + { | ||
| 699 | + dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID); | ||
| 700 | + return get_intval(call(msg)); | ||
| 701 | + } | ||
| 702 | + | ||
| 703 | + return -1; | ||
| 704 | + | ||
| 705 | +#else | ||
| 706 | + | ||
| 707 | + return -1; | ||
| 708 | + | ||
| 709 | +#endif | ||
| 710 | + | ||
| 711 | + } | ||
| 712 | + | ||
| 713 | + int cmp_text_at(int row, int col, const char *text) | ||
| 714 | + { | ||
| 715 | +#if defined(WIN32) | ||
| 716 | + | ||
| 717 | + struct hllapi_packet_text_at * query; | ||
| 718 | + struct hllapi_packet_result response; | ||
| 719 | + DWORD cbSize = sizeof(struct hllapi_packet_text_at)+strlen(text); | ||
| 720 | + | ||
| 721 | + query = (struct hllapi_packet_text_at *) malloc(cbSize); | ||
| 722 | + query->packet_id = HLLAPI_PACKET_CMP_TEXT_AT; | ||
| 723 | + query->row = row; | ||
| 724 | + query->col = col; | ||
| 725 | + strcpy(query->text,text); | ||
| 726 | + | ||
| 727 | + TransactNamedPipe(hPipe,(LPVOID) query, cbSize, &response, sizeof(response), &cbSize,NULL); | ||
| 728 | + | ||
| 729 | + free(query); | ||
| 730 | + | ||
| 731 | + return response.rc; | ||
| 732 | + | ||
| 733 | +#elif defined(HAVE_DBUS) | ||
| 734 | + | ||
| 735 | + dbus_int32_t r = (dbus_int32_t) row; | ||
| 736 | + dbus_int32_t c = (dbus_int32_t) col; | ||
| 737 | + | ||
| 738 | + DBusMessage * msg = create_message("cmpTextAt"); | ||
| 739 | + if(msg) | ||
| 740 | + { | ||
| 741 | + dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID); | ||
| 742 | + return get_intval(call(msg)); | ||
| 743 | + } | ||
| 744 | + | ||
| 745 | +#endif | ||
| 746 | + | ||
| 747 | + return 0; | ||
| 748 | + } | ||
| 749 | + | ||
| 750 | + int wait_for_text_at(int row, int col, const char *key, int timeout) | ||
| 751 | + { | ||
| 752 | + time_t end = time(0)+timeout; | ||
| 753 | + | ||
| 754 | + while(time(0) < end) | ||
| 755 | + { | ||
| 756 | + if(!is_connected()) | ||
| 757 | + return ENOTCONN; | ||
| 758 | + | ||
| 759 | + if(!cmp_text_at(row,col,key)) | ||
| 760 | + return 0; | ||
| 761 | + | ||
| 762 | +#ifdef WIN32 | ||
| 763 | + Sleep(500); | ||
| 764 | +#else | ||
| 765 | + usleep(500); | ||
| 766 | +#endif | ||
| 767 | + } | ||
| 768 | + | ||
| 769 | + return ETIMEDOUT; | ||
| 770 | + } | ||
| 771 | + | ||
| 772 | + string * get_text(int baddr, size_t len) | ||
| 773 | + { | ||
| 774 | + #warning IMPLEMENTAR | ||
| 775 | + return NULL; | ||
| 776 | + } | ||
| 777 | + | ||
| 778 | + | ||
| 779 | + }; | ||
| 780 | + | ||
| 781 | + session * session::create_remote(const char *session) | ||
| 782 | + { | ||
| 783 | + return new remote(session); | ||
| 784 | + } | ||
| 785 | + | ||
| 786 | + } | ||
| 787 | + |
| @@ -0,0 +1,163 @@ | @@ -0,0 +1,163 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como exception.cc e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | + #include <stdarg.h> | ||
| 31 | + #include <stdio.h> | ||
| 32 | + | ||
| 33 | + #include "pw3270class.h" | ||
| 34 | + | ||
| 35 | + #ifdef HAVE_SYSLOG | ||
| 36 | + #include <syslog.h> | ||
| 37 | + #endif // HAVE_SYSLOG | ||
| 38 | + | ||
| 39 | +/*--[ Implement ]--------------------------------------------------------------------------------------------------*/ | ||
| 40 | + | ||
| 41 | + namespace pw3270 | ||
| 42 | + { | ||
| 43 | + session * session::first = 0; | ||
| 44 | + session * session::last = 0; | ||
| 45 | + session * (*session::factory)(const char *name) = 0; | ||
| 46 | + | ||
| 47 | + session::session() | ||
| 48 | + { | ||
| 49 | + if(first) | ||
| 50 | + { | ||
| 51 | + prev = last; | ||
| 52 | + next = 0; | ||
| 53 | + last->next = this; | ||
| 54 | + last = this; | ||
| 55 | + } | ||
| 56 | + else | ||
| 57 | + { | ||
| 58 | + prev = next = 0; | ||
| 59 | + first = last = this; | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + session::~session() | ||
| 64 | + { | ||
| 65 | + if(prev) | ||
| 66 | + prev->next = next; | ||
| 67 | + else | ||
| 68 | + first = next; | ||
| 69 | + | ||
| 70 | + if(next) | ||
| 71 | + next->prev = prev; | ||
| 72 | + else | ||
| 73 | + last = prev; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + // Factory methods and settings | ||
| 77 | + session * session::create(const char *name) | ||
| 78 | + { | ||
| 79 | + if(factory) | ||
| 80 | + return factory(name); | ||
| 81 | + | ||
| 82 | + if(name && *name) | ||
| 83 | + return create_remote(name); | ||
| 84 | + | ||
| 85 | + return create_local(); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + session * session::start(const char *name) | ||
| 89 | + { | ||
| 90 | + return create(name); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + session * session::get_default(void) | ||
| 94 | + { | ||
| 95 | + if(first) | ||
| 96 | + return first; | ||
| 97 | + return create(NULL); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + void session::set_plugin(session * (*factory)(const char *name)) | ||
| 101 | + { | ||
| 102 | + session::factory = factory; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + // Object settings | ||
| 106 | + void session::set_charset(const char *charset) | ||
| 107 | + { | ||
| 108 | + | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + // 3270 methods | ||
| 112 | + string session::get_version(void) | ||
| 113 | + { | ||
| 114 | + return string(PACKAGE_VERSION); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + string session::get_revision(void) | ||
| 118 | + { | ||
| 119 | + return string(PACKAGE_REVISION); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + void session::log(const char *fmt, ...) | ||
| 123 | + { | ||
| 124 | + va_list arg_ptr; | ||
| 125 | + va_start(arg_ptr, fmt); | ||
| 126 | + this->logva(fmt,arg_ptr); | ||
| 127 | + va_end(arg_ptr); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + void session::logva(const char *fmt, va_list args) | ||
| 131 | + { | ||
| 132 | + #ifdef HAVE_SYSLOG | ||
| 133 | + openlog(PACKAGE_NAME, LOG_NDELAY, LOG_USER); | ||
| 134 | + vsyslog(LOG_INFO,fmt,args); | ||
| 135 | + closelog(); | ||
| 136 | + #else | ||
| 137 | + vfprintf(stderr,fmt,args); | ||
| 138 | + #endif | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + int session::wait_for_text_at(int row, int col, const char *key, int timeout) | ||
| 142 | + { | ||
| 143 | + time_t end = time(0)+timeout; | ||
| 144 | + | ||
| 145 | + while(time(0) < end) | ||
| 146 | + { | ||
| 147 | + if(!is_connected()) | ||
| 148 | + return ENOTCONN; | ||
| 149 | + | ||
| 150 | + if(!cmp_text_at(row,col,key)) | ||
| 151 | + return 0; | ||
| 152 | + | ||
| 153 | + iterate(); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + return ETIMEDOUT; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + | ||
| 160 | + | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + |
| @@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
| 1 | +/* | ||
| 2 | + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 | ||
| 3 | + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a | ||
| 4 | + * aplicativos mainframe. Registro no INPI sob o nome G3270. | ||
| 5 | + * | ||
| 6 | + * Copyright (C) <2008> <Banco do Brasil S.A.> | ||
| 7 | + * | ||
| 8 | + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob | ||
| 9 | + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela | ||
| 10 | + * Free Software Foundation. | ||
| 11 | + * | ||
| 12 | + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER | ||
| 13 | + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO | ||
| 14 | + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para | ||
| 15 | + * obter mais detalhes. | ||
| 16 | + * | ||
| 17 | + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este | ||
| 18 | + * programa; se não, escreva para a Free Software Foundation, Inc., 59 Temple | ||
| 19 | + * Place, Suite 330, Boston, MA, 02111-1307, USA | ||
| 20 | + * | ||
| 21 | + * Este programa está nomeado como testprogram.cc e possui - linhas de código. | ||
| 22 | + * | ||
| 23 | + * Contatos: | ||
| 24 | + * | ||
| 25 | + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) | ||
| 26 | + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) | ||
| 27 | + * | ||
| 28 | + */ | ||
| 29 | + | ||
| 30 | + #include "pw3270class.h" | ||
| 31 | + #include <iostream> | ||
| 32 | + | ||
| 33 | + using namespace std; | ||
| 34 | + using namespace pw3270; | ||
| 35 | + | ||
| 36 | +/*--[ Implement ]------------------------------------------------------------------------------------*/ | ||
| 37 | + | ||
| 38 | + int main(int numpar, char *param[]) | ||
| 39 | + { | ||
| 40 | + session *session = session::start(); | ||
| 41 | + | ||
| 42 | + cout << "pw3270 version: " << session->get_version() << endl; | ||
| 43 | + cout << "pw3270 revision: " << session->get_revision() << endl << endl; | ||
| 44 | + | ||
| 45 | + if(session->is_connected()) | ||
| 46 | + cout << "\tConnected to host" << endl; | ||
| 47 | + else | ||
| 48 | + cout << "\tDisconnected" << endl; | ||
| 49 | + | ||
| 50 | + cout << "\tSession state: " << session->get_cstate() << endl; | ||
| 51 | + | ||
| 52 | + delete session; | ||
| 53 | + return 0; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + |