This is a quick prototype to play with swig, try:
shell1 $ ./examples/ipcserver -u
shell2 $ ./examples/ipcclient.py
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
.gitignore | 7 ++---
Makefile.am | 2 +-
bindings/.gitignore | 2 +
bindings/Makefile.am | 32 +++++++++++++++++++++++
bindings/__init__.py | 2 +
bindings/helper.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++
bindings/helper.h | 26 +++++++++++++++++++
bindings/qbipc.py | 41 ++++++++++++++++++++++++++++++
bindings/qbipcmod.i | 34 +++++++++++++++++++++++++
configure.ac | 8 +++++-
examples/ipcclient.py | 41 ++++++++++++++++++++++++++++++
include/.gitignore | 3 ++
12 files changed, 259 insertions(+), 6 deletions(-)
create mode 100644 bindings/.gitignore
create mode 100644 bindings/Makefile.am
create mode 100644 bindings/__init__.py
create mode 100644 bindings/helper.c
create mode 100644 bindings/helper.h
create mode 100644 bindings/qbipc.py
create mode 100644 bindings/qbipcmod.i
create mode 100755 examples/ipcclient.py
create mode 100644 include/.gitignore
diff --git a/.gitignore b/.gitignore
index 8bed236..547124e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,11 +8,9 @@
*.rpm
*.pc
*.log
+*.pyc
Makefile
Makefile.in
-include/config.*
-include/qb/qbconfig.h
-include/stamp-h1
aclocal.m4
autom4te.cache/
config.guess
@@ -23,6 +21,7 @@ configure
depcomp
install-sh
missing
+py-compile
.libs
m4
libtool
@@ -36,4 +35,4 @@ libqb.spec
include/qb/stamp-h2
cov
compat_reports
-abi_dumps
\ No newline at end of file
+abi_dumps
diff --git a/Makefile.am b/Makefile.am
index 3420bea..def7ddd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,7 +41,7 @@ ACLOCAL_AMFLAGS = -I m4
dist_doc_DATA = COPYING INSTALL README.markdown
-SUBDIRS = include lib docs tests examples
+SUBDIRS = include lib docs tests examples bindings
doxygen:
$(MAKE) -C docs doxygen
diff --git a/bindings/.gitignore b/bindings/.gitignore
new file mode 100644
index 0000000..f1ae8b4
--- /dev/null
+++ b/bindings/.gitignore
@@ -0,0 +1,2 @@
+qbipcmod.py
+qbipcmod_wrap.c
diff --git a/bindings/Makefile.am b/bindings/Makefile.am
new file mode 100644
index 0000000..cabc405
--- /dev/null
+++ b/bindings/Makefile.am
@@ -0,0 +1,32 @@
+# Copyright (c) 2012 Red Hat, Inc.
+#
+# Authors: Angus Salkeld <asalkeld(a)redhat.com>
+#
+# This file is part of libqb.
+#
+# libqb is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# libqb 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 Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with libqb. If not, see <
http://www.gnu.org/licenses/>.
+#
+BUILT_SOURCES = $(srcdir)/qbipcmod_wrap.c
+SWIG_SOURCES = qbipcmod.i
+
+pkgpython_PYTHON = qbipc.py qbipcmod.py __init__.py
+pkgpyexec_LTLIBRARIES = _qbipcmod.la
+_qbipcmod_la_SOURCES = $(BUILT_SOURCES) $(SWIG_SOURCES) helper.c
+_qbipcmod_la_CFLAGS = $(shell $(PKGCONFIG) python --cflags) $(CFLAGS)
$(SWIG_PYTHON_CPPFLAGS)
+_qbipcmod_la_LDFLAGS = -avoid-version -module
+_qbipcmod_la_LIBADD = $(top_builddir)/lib/libqb.la
+_qbipcmod_la_DEPENDENCIES = $(top_builddir)/lib/libqb.la
+
+$(srcdir)/%_wrap.c : %.i
+ $(SWIG) -python -nothreads $(SWIG_PYTHON_OPT) -o $@ $<
diff --git a/bindings/__init__.py b/bindings/__init__.py
new file mode 100644
index 0000000..feff2bb
--- /dev/null
+++ b/bindings/__init__.py
@@ -0,0 +1,2 @@
+# This file is required for python packages.
+# It is intentionally empty.
diff --git a/bindings/helper.c b/bindings/helper.c
new file mode 100644
index 0000000..b642407
--- /dev/null
+++ b/bindings/helper.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <
http://www.gnu.org/licenses/>.
+ */
+
+#include "os_base.h"
+
+#include <qb/qbdefs.h>
+#include <qb/qbutil.h>
+#include <qb/qbipcc.h>
+
+struct my_req {
+ struct qb_ipc_request_header hdr;
+ char message[1];
+};
+
+struct my_res {
+ struct qb_ipc_response_header hdr;
+ char message[1];
+};
+
+#define BUF_SIZE 4096
+static char buffer[BUF_SIZE];
+
+char*
+qb_ipcc_send_recv_string(qb_ipcc_connection_t *c, int id, const char *request)
+{
+ struct my_req *req;
+ struct my_res *res = (struct my_res*)buffer;
+ int rc;
+ int s = sizeof(struct qb_ipc_request_header) + (sizeof(char) * (strlen(request) + 1));
+
+ req = malloc(s);
+ req->hdr.id = QB_IPC_MSG_USER_START + id;
+ req->hdr.size = s;
+ strcpy(req->message, request);
+
+ rc = qb_ipcc_send(c, req, s);
+ free(req);
+ if (rc < 0) {
+ perror("qb_ipcc_send");
+ return NULL;
+ }
+ rc = qb_ipcc_recv(c, res, BUF_SIZE - sizeof(struct my_res), -1);
+ if (rc < 0) {
+ perror("qb_ipcc_recv");
+ return NULL;
+ }
+ return res->message;
+}
+
diff --git a/bindings/helper.h b/bindings/helper.h
new file mode 100644
index 0000000..2336277
--- /dev/null
+++ b/bindings/helper.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <
http://www.gnu.org/licenses/>.
+ */
+#ifndef _QB_HELPER_H_
+#define _QB_HELPER_H_
+
+char* qb_ipcc_send_recv_string(qb_ipcc_connection_t *c, int mid, const char *request);
+
+#endif /* _QB_HELPER_H_ */
\ No newline at end of file
diff --git a/bindings/qbipc.py b/bindings/qbipc.py
new file mode 100644
index 0000000..a8f0517
--- /dev/null
+++ b/bindings/qbipc.py
@@ -0,0 +1,41 @@
+# Copyright (C) 2012 Red Hat, Inc.
+#
+# Author: Angus Salkeld <asalkeld(a)redhat.com>
+#
+# 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, see <
http://www.gnu.org/licenses/>.
+#
+import sys
+
+sys.path.append('/usr/lib64/python2.7/site-packages/libqb/')
+sys.path.append('/usr/lib/python2.7/site-packages/libqb/')
+
+from libqb import qbipcmod
+
+class qbipcconnection(object):
+
+ def __init__(self, service_name, max_message_size):
+
+ self.c = qbipcmod.qb_ipcc_connect(service_name, max_message_size)
+ if self.c is None:
+ raise Exception('qb_ipcc_connect')
+
+ def send_recv(self, mid, message):
+ return qbipcmod.qb_ipcc_send_recv_string(self.c, mid, message)
+
+ def disconnect(self):
+ if self.c:
+ qbipcmod.qb_ipcc_disconnect(self.c)
+
+
+
diff --git a/bindings/qbipcmod.i b/bindings/qbipcmod.i
new file mode 100644
index 0000000..1e6e532
--- /dev/null
+++ b/bindings/qbipcmod.i
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Angus Salkeld <asalkeld(a)redhat.com>
+ *
+ * This file is part of libqb.
+ *
+ * libqb is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * libqb 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libqb. If not, see <
http://www.gnu.org/licenses/>.
+ */
+
+%module qbipcmod
+%{
+#include <qb/qbipcc.h>
+#include <qb/qbipcs.h>
+#include "helper.h"
+%}
+
+%include "../include/qb/qbipcc.h"
+
+char* qb_ipcc_send_recv_string(qb_ipcc_connection_t *c, int mid, const char *request);
+
+%include "../include/qb/qbipcs.h"
+
diff --git a/configure.ac b/configure.ac
index fd158aa..f635f7d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,10 @@ AC_CONFIG_MACRO_DIR([m4])
AC_CANONICAL_HOST
AC_PROG_LIBTOOL
AC_LANG([C])
+AC_PROG_CXX
+AM_PATH_PYTHON
+SWIG_ENABLE_CXX
+SWIG_PYTHON
dnl Fix default variables - "prefix" variable if not specified
if test "$prefix" = "NONE"; then
@@ -62,6 +66,7 @@ AM_PROG_CC_C_O
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
+AC_PATH_PROGS(SWIG, swig)
AC_CHECK_PROGS([PKGCONFIG], [pkg-config])
AC_CHECK_PROGS([DOXYGEN], [doxygen])
AM_CONDITIONAL(HAVE_DOXYGEN, test -n "${DOXYGEN}")
@@ -510,7 +515,8 @@ AC_CONFIG_FILES([Makefile
examples/Makefile
docs/Makefile
docs/man.dox
- docs/html.dox])
+ docs/html.dox
+ bindings/Makefile])
AC_OUTPUT
diff --git a/examples/ipcclient.py b/examples/ipcclient.py
new file mode 100755
index 0000000..6c5bc32
--- /dev/null
+++ b/examples/ipcclient.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2012 Red Hat, Inc.
+#
+# Author: Angus Salkeld <asalkeld(a)redhat.com>
+#
+# 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, see <
http://www.gnu.org/licenses/>.
+#
+import sys
+
+from libqb import qbipc
+
+if __name__ == '__main__':
+ c = qbipc.qbipcconnection('ipcserver', 512)
+
+ while True:
+ print "SEND (q to quit) : "
+ str_data = raw_input("> ")
+ if str_data is None:
+ continue
+ if 'q' in str_data:
+ break
+
+ msg = c.send_recv(123, str_data)
+
+ if msg:
+ print("Response: %s " % msg)
+
+ c.disconnect()
+
diff --git a/include/.gitignore b/include/.gitignore
new file mode 100644
index 0000000..63f0fc5
--- /dev/null
+++ b/include/.gitignore
@@ -0,0 +1,3 @@
+config.*
+qb/qbconfig.h
+stamp-h1
--
1.7.7.6