diff --git a/py3270.cbp b/py3270.cbp
index f6726cd..9112d78 100644
--- a/py3270.cbp
+++ b/py3270.cbp
@@ -49,10 +49,11 @@
+
-
+
diff --git a/src/include/py3270.h b/src/include/py3270.h
index 4065530..c86b7dc 100644
--- a/src/include/py3270.h
+++ b/src/include/py3270.h
@@ -59,6 +59,17 @@
#endif
+ #ifdef DEBUG
+ #include
+ #undef trace
+ #define trace( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n", __FILE__, __LINE__, __VA_ARGS__ ); fflush(stderr);
+ #define debug( fmt, ... ) fprintf(stderr, "%s(%d) " fmt "\n", __FILE__, __LINE__, __VA_ARGS__ ); fflush(stderr);
+ #else
+ #undef trace
+ #define trace(x, ...) // __VA_ARGS__
+ #define debug(x, ...) // __VA_ARGS__
+ #endif
+
#ifdef __cplusplus
#include
@@ -70,6 +81,9 @@
using std::runtime_error;
using TN3270::Host;
+ DLL_PRIVATE PyObject * py3270_session_call(PyObject *self, std::function worker) noexcept;
+ DLL_PRIVATE PyObject * py3270_session_call(PyObject *self, std::function worker) noexcept;
+
extern "C" {
#else
@@ -83,13 +97,20 @@
Host *host;
} pySession;
- DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args);
- DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args);
+ DLL_PRIVATE PyObject * py3270_get_module_version(PyObject *self, PyObject *args);
+ DLL_PRIVATE PyObject * py3270_get_module_revision(PyObject *self, PyObject *args);
+
+ DLL_PRIVATE PyObject * py3270_session_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds);
+ DLL_PRIVATE void py3270_session_dealloc(PyObject * self);
+
+ DLL_PRIVATE int py3270_session_init(PyObject *self, PyObject *args, PyObject *kwds);
+ DLL_PRIVATE void py3270_session_finalize(PyObject *self);
+
- DLL_PRIVATE PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
-// DLL_PRIVATE int py3270_session_init(pySession *self, PyObject *args, PyObject *kwds);
- DLL_PRIVATE void py3270_session_dealloc(pySession * self);
+// DLL_PRIVATE PyObject * py3270_session_getattr(PyObject *self, char *attr_name);
+ DLL_PRIVATE PyObject * py3270_session_connect(PyObject *self, PyObject *args);
+ DLL_PRIVATE PyObject * py3270_session_disconnect(PyObject *self, PyObject *args);
/*
diff --git a/src/module/init.c b/src/module/init.c
index 36ba405..348b4e7 100644
--- a/src/module/init.c
+++ b/src/module/init.c
@@ -67,15 +67,48 @@ static struct PyModuleDef definition = {
.m_methods = methods // Module methods
};
+//# Tornar essa tabela pública e testar em getattr, se for um método usar PyMethod_New para retornar um método.
+
+static PyMethodDef py3270_session_methods[] = {
+ {
+ "connect",
+ (PyCFunction) py3270_session_connect,
+ METH_VARARGS,
+ ""
+ },
+
+ {
+ "disconnect",
+ (PyCFunction) py3270_session_disconnect,
+ METH_NOARGS,
+ ""
+ },
+
+ {
+ NULL
+ }
+};
+
+// https://docs.python.org/3/c-api/typeobj.html
static PyTypeObject SessionType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "tn3270.Session",
.tp_doc = "TN3270 Session Object",
.tp_basicsize = sizeof(pySession),
.tp_itemsize = 0,
- .tp_flags = Py_TPFLAGS_DEFAULT,
+ .tp_flags = Py_TPFLAGS_HAVE_FINALIZE|Py_TPFLAGS_DEFAULT,
+
+ .tp_new = py3270_session_alloc,
.tp_dealloc = py3270_session_dealloc,
- .tp_new = py3270_session_new,
+
+ .tp_init = py3270_session_init,
+ .tp_finalize = py3270_session_finalize,
+
+ .tp_methods = py3270_session_methods,
+
+// .tp_alloc =
+// .tp_free =
+// .tp_getattr = py3270_session_getattr
};
/*---[ Implement ]----------------------------------------------------------------------------------*/
diff --git a/src/module/tools.cc b/src/module/tools.cc
new file mode 100644
index 0000000..29f6c50
--- /dev/null
+++ b/src/module/tools.cc
@@ -0,0 +1,82 @@
+/*
+ * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
+ * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
+ * aplicativos mainframe. Registro no INPI sob o nome G3270.
+ *
+ * Copyright (C) <2008>
+ *
+ * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
+ * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
+ * Free Software Foundation.
+ *
+ * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
+ * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
+ * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
+ * obter mais detalhes.
+ *
+ * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
+ * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
+ * St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Este programa está nomeado como misc.cc e possui - linhas de código.
+ *
+ * Contatos:
+ *
+ * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
+ * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
+ *
+ * Implementa métodos básicos inicio/final do objeto python
+ *
+ * Referências:
+ *
+ *
+ *
+ *
+ */
+
+ #include
+
+/*---[ Implement ]----------------------------------------------------------------------------------*/
+
+PyObject * py3270_session_call(PyObject *self, std::function worker) noexcept {
+
+ try {
+
+ TN3270::Host *host = ((pySession * ) self)->host;
+ return worker(*host);
+
+ } catch(const exception &e) {
+
+ PyErr_SetString(PyExc_RuntimeError, e.what());
+
+ } catch( ... ) {
+
+ PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module");
+
+ }
+
+ return NULL;
+
+}
+
+PyObject * py3270_session_call(PyObject *self, std::function worker) noexcept {
+
+ try {
+
+ TN3270::Host *host = ((pySession * ) self)->host;
+ return PyLong_FromLong(worker(*host));
+
+ } catch(const exception &e) {
+
+ PyErr_SetString(PyExc_RuntimeError, e.what());
+
+ } catch( ... ) {
+
+ PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module");
+
+ }
+
+ return NULL;
+
+}
+
diff --git a/src/terminal/get.cc b/src/terminal/get.cc
index 2d853e2..73f0660 100644
--- a/src/terminal/get.cc
+++ b/src/terminal/get.cc
@@ -36,6 +36,89 @@
/*---[ Implement ]----------------------------------------------------------------------------------*/
+ /*
+DLL_PRIVATE PyObject * py3270_session_getattr(PyObject *self, char *attr_name) {
+
+ PyObject * rc = NULL;
+
+ printf("\n\n*************%s(%s)\n\n",__FUNCTION__,attr_name);
+
+ try {
+
+ TN3270::Property * property = ((pySession * ) self)->host->getProperty(attr_name);
+
+ try {
+
+ switch(property->getType()) {
+ case TN3270::Property::String:
+ rc = PyUnicode_FromString(property->toString().c_str());
+ break;
+
+ case TN3270::Property::Boolean:
+ rc = PyBool_FromLong(property->toBool());
+ break;
+
+ case TN3270::Property::Uchar:
+ throw std::system_error(ENOTSUP, std::system_category());
+ break;
+
+ case TN3270::Property::Int16:
+ throw std::system_error(ENOTSUP, std::system_category());
+ break;
+
+ case TN3270::Property::Uint16:
+ throw std::system_error(ENOTSUP, std::system_category());
+ break;
+
+ case TN3270::Property::Int32:
+ rc = PyLong_FromLong(property->toInt32());
+ break;
+
+ case TN3270::Property::Int32x:
+ throw std::system_error(ENOTSUP, std::system_category());
+ break;
+
+ case TN3270::Property::Uint32:
+ rc = PyLong_FromLong(property->toUint32());
+ break;
+
+ case TN3270::Property::Int64:
+ throw std::system_error(ENOTSUP, std::system_category());
+ break;
+
+ case TN3270::Property::Uint64:
+ throw std::system_error(ENOTSUP, std::system_category());
+ break;
+
+ default:
+ throw runtime_error("Unexpected property type");
+ }
+
+ } catch(...) {
+
+ delete property;
+ throw;
+
+ }
+
+ delete property;
+
+ } catch(const exception &e) {
+
+ PyErr_SetString(PyExc_RuntimeError, e.what());
+
+ } catch( ... ) {
+
+ PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module");
+
+ }
+
+ return rc;
+
+}
+*/
+
+
/*
PyObject * terminal_get_version(PyObject *self, PyObject *args) {
diff --git a/src/terminal/init.cc b/src/terminal/init.cc
index 6a82fab..c75ba50 100644
--- a/src/terminal/init.cc
+++ b/src/terminal/init.cc
@@ -38,16 +38,77 @@
/*---[ Implement ]----------------------------------------------------------------------------------*/
-PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+static void cleanup(pySession * session) {
+
+ if(session->host) {
+ delete session->host;
+ session->host = nullptr;
+ }
+
+}
+
+int py3270_session_init(PyObject *self, PyObject *args, PyObject *kwds) {
+
+ pySession * session = (pySession *) self;
+ debug("%s session=%p host=%p",__FUNCTION__,session,session->host);
+
+ try {
+
+ cleanup(session);
+
+ const char *id = "";
+
+ if (!PyArg_ParseTuple(args, "s", &id))
+ id = "";
+
+ session->host = new TN3270::Host(id);
+
+ return 0;
+
+ } catch(const std::exception &e) {
+
+ PyErr_SetString(PyExc_RuntimeError, e.what());
+
+ } catch(...) {
+
+ PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module");
+
+ }
+
+ return -1;
+
+}
+
+void py3270_session_finalize(PyObject *self) {
+
+ debug("%s",__FUNCTION__);
+ cleanup((pySession *) self);
+
+}
+
+PyObject * py3270_session_alloc(PyTypeObject *type, PyObject *args, PyObject *kwds) {
+
+ debug("%s",__FUNCTION__);
+ return type->tp_alloc(type,0);
+
+}
+
+void py3270_session_dealloc(PyObject * self) {
+
+ debug("%s",__FUNCTION__);
+
+ cleanup((pySession *) self);
+ Py_TYPE(self)->tp_free(self);
+
+}
+
+ /*
const char *id = "";
if (!PyArg_ParseTuple(args, "s", &id))
id = "";
- pySession * session = (pySession *) type->tp_alloc(type,0);
-
- printf("---[%s]---\n",id);
if(session) {
@@ -55,17 +116,12 @@ PyObject * py3270_session_new(PyTypeObject *type, PyObject *args, PyObject *kwds
session->host = new TN3270::Host(id);
- printf("---[%s=%p]---\n",id,session->host);
- return (PyObject *) session;
-
} catch(const exception &e) {
- printf("---[%s]---\n",e.what());
PyErr_SetString(PyExc_RuntimeError, e.what());
} catch( ... ) {
- printf("---[%s]---\n","???");
PyErr_SetString(PyExc_RuntimeError, "Unexpected error in core module");
}
@@ -87,6 +143,7 @@ void py3270_session_dealloc(pySession * self) {
Py_TYPE(self)->tp_free((PyObject *) self);
}
+ */
/*
diff --git a/src/terminal/misc.cc b/src/terminal/misc.cc
deleted file mode 100644
index 3c3acc3..0000000
--- a/src/terminal/misc.cc
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
- * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
- * aplicativos mainframe. Registro no INPI sob o nome G3270.
- *
- * Copyright (C) <2008>
- *
- * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
- * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
- * Free Software Foundation.
- *
- * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
- * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
- * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
- * obter mais detalhes.
- *
- * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
- * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
- * St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * Este programa está nomeado como misc.cc e possui - linhas de código.
- *
- * Contatos:
- *
- * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
- * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
- *
- * Implementa métodos básicos inicio/final do objeto python
- *
- * Referências:
- *
- *
- *
- *
- */
-
- #include
-
-/*---[ Implement ]----------------------------------------------------------------------------------*/
-
-/*
- PyObject * terminal_connect(PyObject *self, PyObject *args) {
-
- int rc = -1;
- int wait = 60;
- const char * host = "";
-
- if (!PyArg_ParseTuple(args, "s|i", &host, &wait)) {
- PyErr_SetString(terminalError, "connect requires a host URL");
- return NULL;
- }
-
- try {
-
- rc = ((pw3270_TerminalObject *) self)->session->connect(host,wait);
-
- } catch(std::exception &e) {
-
- PyErr_SetString(terminalError, e.what());
- return NULL;
- }
-
- return PyLong_FromLong(rc);
-
- }
-
- PyObject * terminal_disconnect(PyObject *self, PyObject *args) {
-
- int rc = -1;
-
- try {
-
- rc = ((pw3270_TerminalObject *) self)->session->disconnect();
-
- } catch(std::exception &e) {
-
- PyErr_SetString(terminalError, e.what());
- return NULL;
- }
-
- return PyLong_FromLong(rc);
-
- }
-
- PyObject * terminal_wait_for_ready(PyObject *self, PyObject *args) {
-
- int rc;
- int timeout = 60;
-
- if (!PyArg_ParseTuple(args, "|i", &timeout)) {
- PyErr_SetString(terminalError, strerror(EINVAL));
- return NULL;
- }
-
- try {
-
- rc = ((pw3270_TerminalObject *) self)->session->wait_for_ready(timeout);
-
- } catch(std::exception &e) {
-
- PyErr_SetString(terminalError, e.what());
- return NULL;
- }
-
- return PyLong_FromLong(rc);
-
- }
-
-
- PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args) {
-
- int row, col, rc;
- int timeout = 10;
- const char *text;
-
- if (!PyArg_ParseTuple(args, "iis|i", &row, &col, &text, &timeout)) {
- PyErr_SetString(terminalError, strerror(EINVAL));
- return NULL;
- }
-
- try {
-
- rc = ((pw3270_TerminalObject *) self)->session->wait_for_string_at(row,col,text,timeout);
-
- } catch(std::exception &e) {
-
- PyErr_SetString(terminalError, e.what());
- return NULL;
- }
-
- return PyLong_FromLong(rc);
-
- }
-
-*/
diff --git a/src/terminal/network.cc b/src/terminal/network.cc
new file mode 100644
index 0000000..afbce2a
--- /dev/null
+++ b/src/terminal/network.cc
@@ -0,0 +1,168 @@
+/*
+ * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270
+ * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
+ * aplicativos mainframe. Registro no INPI sob o nome G3270.
+ *
+ * Copyright (C) <2008>
+ *
+ * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
+ * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela
+ * Free Software Foundation.
+ *
+ * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER
+ * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO
+ * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
+ * obter mais detalhes.
+ *
+ * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
+ * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
+ * St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Este programa está nomeado como misc.cc e possui - linhas de código.
+ *
+ * Contatos:
+ *
+ * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck)
+ * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça)
+ *
+ * Implementa métodos básicos inicio/final do objeto python
+ *
+ * Referências:
+ *
+ *
+ *
+ *
+ */
+
+ #include
+
+/*---[ Implement ]----------------------------------------------------------------------------------*/
+
+ PyObject * py3270_session_connect(PyObject *self, PyObject *args) {
+
+ printf("\n\n*************%s\n\n",__FUNCTION__);
+
+ return py3270_session_call(self, [args](TN3270::Host &host){
+
+ const char * url = "";
+
+ if(!PyArg_ParseTuple(args, "s", &url)) {
+ throw runtime_error("connect requires a host URL");
+ }
+
+ host.connect(url);
+
+ return 0;
+
+ });
+
+ }
+
+ PyObject * py3270_session_disconnect(PyObject *self, PyObject *args) {
+
+ return py3270_session_call(self, [args](TN3270::Host &host){
+
+ host.disconnect();
+
+ return 0;
+
+ });
+
+ }
+
+
+/*
+ PyObject * terminal_connect(PyObject *self, PyObject *args) {
+
+ int rc = -1;
+ int wait = 60;
+ const char * host = "";
+
+ if (!PyArg_ParseTuple(args, "s|i", &host, &wait)) {
+ PyErr_SetString(terminalError, "connect requires a host URL");
+ return NULL;
+ }
+
+ try {
+
+ rc = ((pw3270_TerminalObject *) self)->session->connect(host,wait);
+
+ } catch(std::exception &e) {
+
+ PyErr_SetString(terminalError, e.what());
+ return NULL;
+ }
+
+ return PyLong_FromLong(rc);
+
+ }
+
+ PyObject * terminal_disconnect(PyObject *self, PyObject *args) {
+
+ int rc = -1;
+
+ try {
+
+ rc = ((pw3270_TerminalObject *) self)->session->disconnect();
+
+ } catch(std::exception &e) {
+
+ PyErr_SetString(terminalError, e.what());
+ return NULL;
+ }
+
+ return PyLong_FromLong(rc);
+
+ }
+
+ PyObject * terminal_wait_for_ready(PyObject *self, PyObject *args) {
+
+ int rc;
+ int timeout = 60;
+
+ if (!PyArg_ParseTuple(args, "|i", &timeout)) {
+ PyErr_SetString(terminalError, strerror(EINVAL));
+ return NULL;
+ }
+
+ try {
+
+ rc = ((pw3270_TerminalObject *) self)->session->wait_for_ready(timeout);
+
+ } catch(std::exception &e) {
+
+ PyErr_SetString(terminalError, e.what());
+ return NULL;
+ }
+
+ return PyLong_FromLong(rc);
+
+ }
+
+
+ PyObject * terminal_wait_for_string_at(PyObject *self, PyObject *args) {
+
+ int row, col, rc;
+ int timeout = 10;
+ const char *text;
+
+ if (!PyArg_ParseTuple(args, "iis|i", &row, &col, &text, &timeout)) {
+ PyErr_SetString(terminalError, strerror(EINVAL));
+ return NULL;
+ }
+
+ try {
+
+ rc = ((pw3270_TerminalObject *) self)->session->wait_for_string_at(row,col,text,timeout);
+
+ } catch(std::exception &e) {
+
+ PyErr_SetString(terminalError, e.what());
+ return NULL;
+ }
+
+ return PyLong_FromLong(rc);
+
+ }
+
+*/
diff --git a/testprograms/sample.py b/testprograms/sample.py
index 2b43763..d8e7f3b 100644
--- a/testprograms/sample.py
+++ b/testprograms/sample.py
@@ -1,6 +1,7 @@
#!/usr/bin/python
#-*- coding: utf-8
+import weakref
import tn3270
print("Teste extensão pw3270")
@@ -8,11 +9,22 @@ print("Teste extensão pw3270")
print("Using TN3270 Version " + tn3270.version())
print(tn3270.revision())
-session = tn3270.Session(":a")
+session = tn3270.Session("")
-#print "Using pw3270 version " + term.Version() + " revision " + term.Revision()
+del session
-#term.Connect("tn3270://zos.efglobe.com:telnet",10);
+input("Press enter to exit")
+
+#print("Using tn3270 version " + session.version + " revision " + session.revision)
+
+#print(session.cstate)
+#print(session.width)
+#print(session.connected)
+#print(session.url)
+
+# print(session.connect)
+
+#print(session.connected)
#print term.IsConnected()
#print term.IsReady()
--
libgit2 0.21.2