Skip to content
Browse files

2006-10-01 Al Riddoch <alriddoch@zepler.org>

	* PythonCallback.cpp, PythonCallback.h: New class to call python
	  functions in response to sigc++ signals.

	* eris_account.cpp, eris_connection.cpp: Implement the various
	  callbacks required to track logging in.

	* test.py: New script to test the bindings.
  • Loading branch information...
1 parent 64b88dd commit 398450b23d6b142482104a79cf7865a0be57c9be alriddoch committed Oct 1, 2006
Showing with 212 additions and 6 deletions.
  1. +2 −0 .cvsignore
  2. +10 −0 ChangeLog
  3. +5 −0 Makefile
  4. +36 −0 PythonCallback.cpp
  5. +36 −0 PythonCallback.h
  6. +28 −3 eris_account.cpp
  7. +36 −3 eris_connection.cpp
  8. +1 −0 setup.py
  9. +58 −0 test.py
View
2 .cvsignore
@@ -1 +1,3 @@
+*.pyc
build
+dist
View
10 ChangeLog
@@ -1,5 +1,15 @@
2006-10-01 Al Riddoch <alriddoch@zepler.org>
+ * PythonCallback.cpp, PythonCallback.h: New class to call python
+ functions in response to sigc++ signals.
+
+ * eris_account.cpp, eris_connection.cpp: Implement the various
+ callbacks required to track logging in.
+
+ * test.py: New script to test the bindings.
+
+2006-10-01 Al Riddoch <alriddoch@zepler.org>
+
* eris_connection.cpp: Implement argument parsing for the constructor
and start work on using setattr to install callbacks.
View
5 Makefile
@@ -0,0 +1,5 @@
+all:
+ python setup.py build
+
+install:
+ python setup.py install
View
36 PythonCallback.cpp
@@ -0,0 +1,36 @@
+// Kallisti Online RPG Library Modules for Python
+// Copyright (C) 2006 Alistair Riddoch
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "PythonCallback.h"
+
+#include <iostream>
+
+PythonCallback::PythonCallback(PyObject * callable) : m_callback(callable)
+{
+ Py_INCREF(m_callback);
+}
+
+PythonCallback::~PythonCallback()
+{
+}
+
+// FIXME Destructor must DECREF.
+
+void PythonCallback::call()
+{
+ PyObject * ret = PyEval_CallFunction(m_callback, "()");
+}
View
36 PythonCallback.h
@@ -0,0 +1,36 @@
+// Kallisti Online RPG Library Modules for Python
+// Copyright (C) 2006 Alistair Riddoch
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#ifndef KALLISTI_PYTHONCALLBACK_H
+#define KALLISTI_PYTHONCALLBACK_H
+
+#include <Python.h>
+
+class PythonCallback {
+ protected:
+ PyObject * m_callback;
+ public:
+ explicit PythonCallback(PyObject * callable);
+ ~PythonCallback();
+
+ void call();
+};
+
+// FIXME Is this legal any more?
+#define PyErisAccount_Check(_o) ((_o)->ob_type == &PyErisAccount_Type)
+
+#endif // KALLISTI_PYTHONCALLBACK_H
View
31 eris_account.cpp
@@ -18,8 +18,14 @@
#include "eris_account.h"
#include "eris_connection.h"
+#include "PythonCallback.h"
#include <Eris/Account.h>
+#include <Eris/Connection.h>
+
+#include <sigc++/hide.h>
+
+#include <iostream>
static PyObject * ErisAccount_login(PyErisAccount * self, PyObject * args)
{
@@ -35,6 +41,7 @@ static PyObject * ErisAccount_login(PyErisAccount * self, PyObject * args)
}
Eris::Result res = self->account->login(u, p);
+ std::cout << self->account->getConnection()->getStatus() << " " << res << " so there" << std::endl << std::flush;
if (res == Eris::NOT_CONNECTED) {
PyErr_SetString(PyExc_IOError, "Not connected");
return NULL;
@@ -116,11 +123,29 @@ static PyObject * ErisAccount_getattr(PyErisAccount * self, char * name)
static int ErisAccount_setattr(PyErisAccount * self, char * name, PyObject * v)
{
-#if 0
- if (strcmp(name, "StatusChanged") == 0) {
+ if (strcmp(name, "LoginSuccess") == 0) {
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
+ return -1;
+ }
+ // Will need to be a template, or something. Possibly a number of
+ // templates, one for each type we will need to wrap.
+ PythonCallback * callback = new PythonCallback(v);
+ self->account->LoginSuccess.connect(sigc::mem_fun(*callback, &PythonCallback::call));
return 0;
}
-#endif
+ if (strcmp(name, "LoginFailure") == 0) {
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
+ return -1;
+ }
+ // Will need to be a template, or something. Possibly a number of
+ // templates, one for each type we will need to wrap.
+ PythonCallback * callback = new PythonCallback(v);
+ self->account->LoginFailure.connect(sigc::hide(sigc::mem_fun(*callback, &PythonCallback::call)));
+ return 0;
+ }
+
PyErr_SetString(PyExc_AttributeError, "unknown attribute");
return -1;
}
View
39 eris_connection.cpp
@@ -17,8 +17,13 @@
#include "eris_connection.h"
+#include "PythonCallback.h"
+
#include <Eris/Connection.h>
+#include <sigc++/bind_return.h>
+#include <sigc++/hide.h>
+
static PyObject * ErisConnection_connect(PyErisConnection * self)
{
if (self->connection == 0) {
@@ -92,26 +97,54 @@ static PyObject * ErisConnection_getattr(PyErisConnection * self, char * name)
static int ErisConnection_setattr(PyErisConnection * self, char * name,
PyObject * v)
{
- if (strcmp(name, "GotServerInfo") == 0) {
+ if (strcmp(name, "Connected") == 0) {
if (!PyCallable_Check(v)) {
PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
return -1;
}
// Will need to be a template, or something. Possibly a number of
// templates, one for each type we will need to wrap.
- // PythonCallback * callback = new PythonCallback(v);
- // self->connection->GotServerInfo.connect(sigc::mem_fun(*callback, &PythonCallback::call));
+ PythonCallback * callback = new PythonCallback(v);
+ self->connection->Connected.connect(sigc::mem_fun(*callback, &PythonCallback::call));
+ return 0;
+ }
+ if (strcmp(name, "Disconnected") == 0) {
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
+ return -1;
+ }
+ PythonCallback * callback = new PythonCallback(v);
+ self->connection->Disconnected.connect(sigc::mem_fun(*callback, &PythonCallback::call));
return 0;
}
if (strcmp(name, "Disconnecting") == 0) {
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
+ return -1;
+ }
+ PythonCallback * callback = new PythonCallback(v);
+ self->connection->Disconnecting.connect(sigc::bind_return(sigc::mem_fun(*callback, &PythonCallback::call), true));
return 0;
}
if (strcmp(name, "Failure") == 0) {
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
+ return -1;
+ }
+ PythonCallback * callback = new PythonCallback(v);
+ self->connection->Failure.connect(sigc::hide(sigc::mem_fun(*callback, &PythonCallback::call)));
return 0;
}
if (strcmp(name, "StatusChanged") == 0) {
+ if (!PyCallable_Check(v)) {
+ PyErr_SetString(PyExc_TypeError, "Callback requires a callable");
+ return -1;
+ }
+ PythonCallback * callback = new PythonCallback(v);
+ self->connection->StatusChanged.connect(sigc::hide(sigc::mem_fun(*callback, &PythonCallback::call)));
return 0;
}
+ std::cout << "NNO!" << std::endl << std::flush;
PyErr_SetString(PyExc_AttributeError, "unknown attribute");
return -1;
}
View
1 setup.py
@@ -19,6 +19,7 @@ def flags_to_names(flags, arg):
eris = Extension('eris', sources=['eris_connection.cpp',
'eris_account.cpp',
'eris_polldefault.cpp',
+ 'PythonCallback.cpp',
'eris.cpp'],
include_dirs=eris_includes,
libraries=eris_libs,
View
58 test.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+import eris
+
+is_connected = 0
+is_logged = 0
+
+def connected():
+ print "connected called"
+ global is_connected
+ is_connected = 1
+
+def disconnected():
+ print "disconnected called"
+
+def disconnecting():
+ print "disconnecting called"
+
+def failure():
+ print "failure called"
+
+def status_changed():
+ print "status_changed called"
+
+def login_success():
+ print "login_success called"
+ global is_logged
+ is_logged = 1
+
+def login_failure():
+ print "login_failure called"
+
+if __name__ == "__main__":
+
+ foo = eris.Connection("foo", "localhost", 6767, 1)
+
+ foo.Connected = connected
+ foo.Disconnected = disconnected
+ foo.Disconnecting = disconnecting
+ foo.Failure = failure
+ foo.StatusChanged = status_changed
+
+ foo.connect()
+
+ while is_connected == 0:
+ eris.polldefault.poll()
+
+ print "Okay"
+
+ acc = eris.Account(foo)
+
+ acc.LoginSuccess = login_success
+ acc.LoginFailure = login_success
+
+ acc.login("ajr", "hel")
+
+ while is_logged == 0:
+ eris.polldefault.poll()
+

0 comments on commit 398450b

Please sign in to comment.
Something went wrong with that request. Please try again.