Signed-off-by: Hoang Le <[email protected]>
---
 .gitignore                                      |  26 +
 Makefile.am                                     |  16 +-
 configure.ac                                    |  33 +-
 demos/Makefile.am                               |   8 +
 demos/pytipc/Makefile.am                        |   3 +
 demos/pytipc/connection/Makefile.am             |  26 +
 demos/pytipc/connection/py_api_client.py        | 219 +++++++++
 demos/pytipc/connection/py_api_server.py        | 157 ++++++
 demos/pytipc/hello_world/Makefile.am            |  26 +
 demos/pytipc/hello_world/py_api_hello_client.py |  37 ++
 demos/pytipc/hello_world/py_api_hello_server.py |  34 ++
 pytipc/Makefile.am                              |  16 +
 pytipc/pytipc.c                                 | 605 ++++++++++++++++++++++++
 pytipc/tipc.py                                  | 282 +++++++++++
 14 files changed, 1485 insertions(+), 3 deletions(-)
 create mode 100644 demos/pytipc/Makefile.am
 create mode 100644 demos/pytipc/connection/Makefile.am
 create mode 100644 demos/pytipc/connection/py_api_client.py
 create mode 100644 demos/pytipc/connection/py_api_server.py
 create mode 100644 demos/pytipc/hello_world/Makefile.am
 create mode 100644 demos/pytipc/hello_world/py_api_hello_client.py
 create mode 100644 demos/pytipc/hello_world/py_api_hello_server.py
 create mode 100644 pytipc/Makefile.am
 create mode 100644 pytipc/pytipc.c
 create mode 100644 pytipc/tipc.py

diff --git a/.gitignore b/.gitignore
index 39d06a7af575..cc108daa5821 100644
--- a/.gitignore
+++ b/.gitignore
@@ -58,3 +58,29 @@ demos/golang/hello_world/hello_go_api_server
 libtipc/libtipc.a
 test/overlapping_ranges/overlap_client
 test/overlapping_ranges/overlap_server
+
+# Standard shared library
+*.so
+
+# All java .class
+*.class
+*.jar
+
+# All java packages
+tipcj/.libs
+demos/tipcj/hello_world/Hello_Java_Api_Client.tar.gz
+demos/tipcj/hello_world/Hello_Java_Api_Server.tar.gz
+demos/tipcj/hello_world/.libs
+demos/tipcj/connection/Tipc_Java_Api_Client.tar.gz
+demos/tipcj/connection/Tipc_Java_Api_Server.tar.gz
+demos/tipcj/connection/.libs
+
+# Python objects
+*.pyc
+
+# Python demo
+demos/pytipc/connection/py_api_client.tar.gz
+demos/pytipc/connection/py_api_server.tar.gz
+demos/pytipc/hello_world/py_api_hello_client.tar.gz
+demos/pytipc/hello_world/py_api_hello_server.tar.gz
+
diff --git a/Makefile.am b/Makefile.am
index 37d0385b9171..7edfc3809e77 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1 +1,15 @@
-SUBDIRS=man scripts libtipc demos test utils
+SUBDIRS=man scripts libtipc
+
+if TIPC_GOAPI_EXAMPLE
+SUBDIRS+=golang
+endif
+
+if TIPC_JAVA
+SUBDIRS+=tipcj
+endif
+
+if TIPC_PYTHON
+SUBDIRS+=pytipc
+endif
+
+SUBDIRS+=demos test utils
diff --git a/configure.ac b/configure.ac
index c5fa53165526..7e7aee474464 100644
--- a/configure.ac
+++ b/configure.ac
@@ -24,7 +24,7 @@ AC_CHECK_TYPE(struct tipc_sioc_ln_req, [tipc_lss=yes],[], 
[[#include <linux/tipc
 AM_CONDITIONAL(TIPC_LINK_STATE_SUBSCRITION, test "x$tipc_lss" = "xyes")
 
 # Checks for TIPC GO API
-AC_ARG_WITH(goapi,[ --with-goapi  Compile TIPC GO APIs 
examples],[goapi=true],[])
+AC_ARG_WITH(goapi,AS_HELP_STRING([--with-goapi],[Compile TIPC GO APIs 
examples]),[goapi=true],[])
 if test "x$goapi" == "xtrue"; then
        AC_CHECK_PROG([gotool],[go],[true],[])
        if test "x$gotool" != "xtrue"; then
@@ -33,6 +33,27 @@ if test "x$goapi" == "xtrue"; then
 fi
 AM_CONDITIONAL(TIPC_GOAPI_EXAMPLE, test "x$goapi" == "xtrue" -a "x$gotool" == 
"xtrue")
 
+# Checks for TIPC JAVA API
+AC_ARG_WITH(tipcj,AS_HELP_STRING([--with-tipcj],[Compile TIPC JAVA API and a 
couple of examples]),[with_tipcj=true],[])
+if test "x$with_tipcj" == "xtrue"; then
+       AC_CHECK_PROG([javac],[javac],[true],[])
+       if test "x$javac" != "xtrue"; then
+               AC_MSG_NOTICE([javac are not available - ignore java compiling])
+       fi
+fi
+AM_CONDITIONAL(TIPC_JAVA, test "x$with_tipcj" == "xtrue" -a "x$javac" == 
"xtrue")
+
+# Checks for TIPC PYTHON API
+AC_ARG_WITH(py,AS_HELP_STRING([--with-py],[Compile TIPC PYTHON API and a 
couple of examples]),[pyapi=true],[])
+if test "x$pyapi" == "xtrue"; then
+       AM_PATH_PYTHON([2.6],[true],[])
+       AC_CHECK_PROG([pycompile],[pycompile],[true],[$PYTHON_PATH])
+       if test "x$pycompile" != "xtrue"; then
+               AC_MSG_NOTICE([pycompile tool not found - ignore compiling tipc 
python api example])
+       fi
+fi
+AM_CONDITIONAL(TIPC_PYTHON, [ test "x$pyapi" == "xtrue" -a "x$PYTHON" != 
"xtrue" -a "x$pycompile" == "xtrue"])
+
 AC_CONFIG_FILES([
        Makefile
        libtipc/Makefile
@@ -60,8 +81,16 @@ AC_CONFIG_FILES([
        demos/golang/Makefile
        demos/golang/hello_world/Makefile
        demos/golang/connection/Makefile
-        man/Makefile
+       man/Makefile
        scripts/Makefile
+       tipcj/Makefile
+       demos/tipcj/Makefile
+       demos/tipcj/hello_world/Makefile
+       demos/tipcj/connection/Makefile
+       pytipc/Makefile
+       demos/pytipc/Makefile
+       demos/pytipc/hello_world/Makefile
+       demos/pytipc/connection/Makefile
        ])
 AM_CONDITIONAL(WITH_SCRIPTS, false)
 AC_ARG_ENABLE(scripts,
diff --git a/demos/Makefile.am b/demos/Makefile.am
index 1d1a9fe17707..c3e32fd50c14 100644
--- a/demos/Makefile.am
+++ b/demos/Makefile.am
@@ -8,3 +8,11 @@ endif
 if TIPC_GOAPI_EXAMPLE
 SUBDIRS+= golang
 endif
+
+if TIPC_JAVA
+SUBDIRS+= tipcj
+endif
+
+if TIPC_PYTHON
+SUBDIRS+= pytipc
+endif
diff --git a/demos/pytipc/Makefile.am b/demos/pytipc/Makefile.am
new file mode 100644
index 000000000000..235e489f7cdf
--- /dev/null
+++ b/demos/pytipc/Makefile.am
@@ -0,0 +1,3 @@
+if TIPC_PYTHON
+SUBDIRS = hello_world connection
+endif
diff --git a/demos/pytipc/connection/Makefile.am 
b/demos/pytipc/connection/Makefile.am
new file mode 100644
index 000000000000..ffd339b16de8
--- /dev/null
+++ b/demos/pytipc/connection/Makefile.am
@@ -0,0 +1,26 @@
+bin_PROGRAMS=py_api_client py_api_server
+py_api_client_SOURCES=
+py_api_server_SOURCES=
+
+CLIENT_OBJECTS=py_api_client.pyc
+SERVER_OBJECTS=py_api_server.pyc
+
+CLEANFILES=$(CLIENT_OBJECTS) $(SERVER_OBJECTS) py_api_client$(EXEEXT).tar.gz 
py_api_server$(EXEEXT).tar.gz
+
+py_api_client$(EXEEXT): $(CLIENT_OBJECTS)
+       $(shell $(MKDIR_P) .libs)
+       cp -f ../../../pytipc/pytipc.so .libs
+       cp -f ../../../pytipc/tipc.pyc .libs
+       tar cvfz [email protected] .libs $(CLIENT_OBJECTS)
+
+py_api_server$(EXEEXT): $(SERVER_OBJECTS)
+       $(shell $(MKDIR_P) .libs)
+       cp -f ../../../pytipc/pytipc.so .libs
+       cp -f ../../../pytipc/tipc.pyc .libs
+       tar cvfz [email protected] .libs $(SERVER_OBJECTS)
+
+%.pyc: %.py
+       $(shell pycompile $<)
+
+clean-generic:
+       rm -rf $(CLEANFILES)
diff --git a/demos/pytipc/connection/py_api_client.py 
b/demos/pytipc/connection/py_api_client.py
new file mode 100644
index 000000000000..0cff2a68de25
--- /dev/null
+++ b/demos/pytipc/connection/py_api_client.py
@@ -0,0 +1,219 @@
+import sys
+sys.path.append("./libs")
+sys.path.append("./.libs")
+sys.path.append("../../../pytipc")
+
+from tipc import *
+import select
+import time
+import signal
+import pdb
+
+def pyapi_client_example():
+    RDM_SRV_TYPE     = 18888
+    STREAM_SRV_TYPE  = 17777
+    SEQPKT_SRV_TYPE  = 16666
+    SRV_INST         = 17
+    BUF_SZ           = 40
+
+    # polling timeout (seconds)
+    TIMEOUT = 3
+    READ_ONLY = select.POLLIN | select.POLLHUP
+
+    # global variable
+    quite = False
+
+    def die(*args):
+        global quite
+        """
+        TODO:
+        """
+        # Gracefully shutdown
+        if not quite:
+            print >>sys.stderr, args
+        sys.exit(0)
+
+    def handler(*args):
+        global quite
+        quite = True
+        print("****** TIPC PYTHON API Demo Client Finished ******\n")
+        sys.exit(0)
+
+    def rdm_service_demo(tipc_conn, up):
+        """
+        struct tipc_addr srv = {RDM_SRV_TYPE, SRV_INST, 0};
+        char sbuf[BUF_SZ], rmsg[BUF_SZ];
+        char msg[BUF_SZ] = {"Hello World"};
+        int rc, err;
+        """
+        msg = "Hello World"
+        srv = TipcAddr(RDM_SRV_TYPE, SRV_INST, 0)
+        if not up:
+            print("Service on SOCK_RDM went down\n")
+            return
+
+        print("\n-------------------------------------")
+        print("Service on SOCK_RDM came up")
+        print("Sending msg: %s on SOCK_RDM -->%s" %(msg, srv))
+        ret = tipc_conn.sock_rejectable()
+        if ret < 0:
+            die("Set rejectable failed\n")
+
+        ret = tipc_conn.sendto(msg, srv)
+        if ret != len(msg):
+            die("sendto() failed\n")
+
+        ret, rmsg, srv, _, err = tipc_conn.recvfrom(bufsize=BUF_SZ)
+        if ret < 0 or err:
+            die ("Unexpected response\n")
+
+        print("Received response: %s <-- %s" %(rmsg, srv))
+        print("-------------------------------------\n")
+        return srv.node
+
+    def rdm_reject_demo(tipc_conn, up, srv_node):
+        invalid_dest = TipcAddr(42, 1, srv_node)
+        msg = "Hello World"
+
+        if not up:
+            return
+
+        print("Sending msg: %s on SOCK_RDM --> %s (non-existing)" %(msg, 
invalid_dest))
+        ret = tipc_conn.sendto(msg, invalid_dest)
+        if ret != len(msg):
+            print("Client sendto() failed: No route to host")
+            return
+
+        rc, rmsg, srv, _, err = tipc_conn.recvfrom(bufsize=BUF_SZ)
+        if rc < 0 or err != 0:
+            die("Unexpected response\n")
+
+        print("Received rejected msg: %s <-- %s, err %d" %(rmsg, srv, err))
+        print("-------------------------------------\n")
+
+    def stream_service_demo(tipc_conn, up):
+        srv = TipcAddr(STREAM_SRV_TYPE, SRV_INST, 0)
+        msg = "Hello World"
+
+        if not up:
+            print("Service on SOCK_STREAM went down\n")
+            return
+
+        print("\n-------------------------------------")
+        print("Service on SOCK_STREAM came up")
+        print("Performing implicit two-way connect with message %s -->%s" 
%(msg, srv))
+        if  tipc_conn.sendto(msg, srv) != len(msg):
+            die("send() failed: %d\n" %ret)
+
+        ret, rmsg = tipc_conn.recv(bufsize=BUF_SZ, waitall=False)
+        if ret < 0:
+            die("Unexpected response\n")
+
+        print("Received response: %s on SOCK_STREAM connection" %rmsg)
+        print("SOCK_STREAM connection established --> %s" %srv)
+        print("-------------------------------------\n")
+
+    def seqpacket_service_demo(tipc_conn, up):
+        srv = TipcAddr(SEQPKT_SRV_TYPE, SRV_INST, 0)
+        msg = "Hello World"
+
+        if not up:
+            print("Service on SOCK_SEQPACKET went down\n")
+            return
+
+        print("\n-------------------------------------")
+        print("Service on SOCK_SEQPACKET came up")
+        print("Connecting to: -->%s" %srv)
+        if tipc_conn.connect(srv) < 0:
+            die("connect() failed\n")
+
+        print("Sending msg: %s on connection" %msg)
+        if tipc_conn.send(msg) != len(msg):
+            die("send() failed\n")
+
+        ret, rmsg = tipc_conn.recv(bufsize=BUF_SZ, waitall=False)
+        if ret < 0:
+            die("Unexpected response\n")
+
+        print("Received response: %s on SOCK_SEQPACKET connection" %rmsg)
+        print("-------------------------------------\n")
+
+    # Handling Ctrl+C
+    signal.signal(signal.SIGINT, handler)
+
+    srv = TipcAddr(RDM_SRV_TYPE, SRV_INST)
+    buf = "Hello World!!!"
+
+    print("****** TIPC PYTHON API Demo Client Started ******\n")
+
+    print("Waiting for Service %s" %srv)
+    srv_wait(srv, -1)
+
+    # Create Tipc socket objects
+    rdm_conn = TipcConn(SOCK_RDM)
+    stream_conn = TipcConn(SOCK_STREAM)
+    seq_conn = TipcConn(SOCK_SEQPACKET)
+
+    # Set up the poller
+    poller = select.poll()
+    poller.register(rdm_conn.get_fd(), READ_ONLY)
+    poller.register(stream_conn.get_fd(), READ_ONLY)
+    poller.register(seq_conn.get_fd(), READ_ONLY)
+
+    """
+    Subscribe for service events
+    """
+    topsrv = TopSrvConn(0)
+    poller.register(topsrv.get_fd(), READ_ONLY)
+
+    if topsrv.srv_subscr(RDM_SRV_TYPE, 0, ~0, False, -1):
+        die("subscribe for RDM server failed\n")
+    if topsrv.srv_subscr(STREAM_SRV_TYPE, 0, ~0, False, -1):
+        die("subscribe for STREAM server failed\n")
+    if topsrv.srv_subscr(SEQPKT_SRV_TYPE, 0, ~0, False, -1):
+        die("subscribe for SEQPACKET server failed\n")
+
+    while not quite:
+        try:
+            events = poller.poll(TIMEOUT * 1000000)
+            for fd, flag in events:
+                if flag & select.POLLHUP:
+                    if fd == stream_conn.get_fd():
+                        sock_str = "SOCK_STREAM"
+                        poller.unregister(fd)
+                        stream_conn.close()
+                        stream_conn = TipcConn(SOCK_STREAM)
+                        poller.register(stream_conn.get_fd(), READ_ONLY)
+                    else:
+                        if fd == seq_conn.get_fd():
+                            sock_str = "SOCK_SEQPACKET"
+                            poller.unregister(fd)
+                            seq_conn.close()
+                            seq_conn = TipcConn(SOCK_SEQPACKET)
+                            poller.register(seq_conn.get_fd(), READ_ONLY)
+
+                    print("%s connection hangup!" %sock_str)
+                else:
+                    if flag & select.POLLIN:
+                        err, srv, _, up, _ = topsrv.srv_evt()
+                        if err:
+                            die("reception of service event failed\n")
+
+                        if srv.srv_type == RDM_SRV_TYPE:
+                            srv_node = rdm_service_demo(rdm_conn, up)
+                            rdm_reject_demo(rdm_conn, up, srv_node)
+
+                        if srv.srv_type == STREAM_SRV_TYPE:
+                            stream_service_demo(stream_conn, up)
+
+                        if srv.srv_type == SEQPKT_SRV_TYPE:
+                            seqpacket_service_demo(seq_conn, up)
+        except IOError:
+            break
+        except Exception:
+            break
+if __name__== "__main__":
+    try:
+        pyapi_client_example()
+    except:
+        pass
diff --git a/demos/pytipc/connection/py_api_server.py 
b/demos/pytipc/connection/py_api_server.py
new file mode 100644
index 000000000000..93d3b72e42d9
--- /dev/null
+++ b/demos/pytipc/connection/py_api_server.py
@@ -0,0 +1,157 @@
+import sys
+sys.path.append("./libs")
+sys.path.append("./.libs")
+sys.path.append("../../../pytipc")
+
+from tipc import *
+import select
+import signal
+import time
+
+def pyapi_server_example():
+    RDM_SRV_TYPE     = 18888
+    STREAM_SRV_TYPE  = 17777
+    SEQPKT_SRV_TYPE  = 16666
+    BUF_SZ           = 40
+
+    # polling timeout (seconds)
+    TIMEOUT = 3
+    READ_ONLY = select.POLLIN | select.POLLHUP
+
+    # global variable
+    quite = False
+
+    def die(*args):
+        global quite
+        """
+        TODO:
+        """
+        # Gracefully shutdown
+        if not quite:
+            print >>sys.stderr, args
+        sys.exit(0)
+
+    def handler(*args):
+        global quite
+        quite = True
+        print("****** TIPC PYTHON API Demo Server Finished ******\n")
+
+    def bind_service(srv_type, scope, sktype, sk_str):
+        """
+        TODO
+        """
+        srv_conn = TipcConn(sktype)
+        if srv_conn.get_fd() <= 0:
+            die("failed to create %s socket" %sk_str)
+
+        if srv_conn.bind(srv_type, 0, ~0, scope) != 0:
+            die("failed to bind %s socket" %sk_str)
+
+        print("Bound %s socket %s to %s" %(sk_str, srv_conn.sockaddr(), 
TipcServiceRange(srv_type, 0, ~0, scope)))
+        return srv_conn
+
+    def recv_rdm_msg(tipc_conn):
+        print("\n-------------------------------------")
+
+        ret, msg, memberid, socketid, err = tipc_conn.recvfrom(bufsize=BUF_SZ) 
 
+        if ret <= 0:
+            die("unexpected message on RDM socket\n")
+
+        print("Received msg: %s on SOCK_RDM <-- %s" %(msg, memberid))
+        outbuf = "Huh?"
+        if tipc_conn.sendto(outbuf, memberid) <= 0:
+            die("Server: failed to send\n")
+
+        print("Responding with: %s --> %s" %(outbuf, memberid))
+        print("-------------------------------------\n")
+
+    def recv_stream_setup(tipc_conn):
+        print("\n-------------------------------------")
+        tipc_conn.listen(32)
+
+        new_conn, peer_addr = tipc_conn.accept()
+        if new_conn is None or new_conn.get_fd() <= 0:
+            die("accept on SOCK_STREAM failed\n")
+
+        print("SOCK_STREAM connection established --> %s" %peer_addr)
+        ret, msg = new_conn.recv(bufsize=BUF_SZ, waitall=False)
+        if ret <= 0:
+            die("unexpected message on STREAM socket")
+
+        print("Received msg: %s on STREAM connection" %msg)
+        msg = "Huh?"
+        print("Responding with: %s" %msg)
+        if new_conn.send(msg) <= 0:
+            die("failed to respond\n")
+        print("-------------------------------------\n")
+        return new_conn
+
+    def recv_seqpacket_setup(tipc_conn):
+        print("\n-------------------------------------")
+
+        tipc_conn.listen(32)
+        new_conn, peer_addr = tipc_conn.accept()
+
+        if new_conn.get_fd() <= 0:
+            die("accept on SOCK_SEQPACKET failed\n")
+
+        print("SOCK_SEQPACKET connection established <-- %s" %peer_addr)
+        ret, msg = new_conn.recv(bufsize=BUF_SZ, waitall=False)
+        if ret <= 0:
+            die("unexpected message on STREAM socket\n")
+
+        print("Received msg: %s on SOCK_SEQPACKET connection" %msg)
+        msg = "Huh?"
+        print("Responding with: %s" %msg)
+        if new_conn.send(msg) <= 0:
+            die("failed to respond\n")
+        print("-------------------------------------\n")
+        return new_conn
+
+    # Handling Ctrl+C
+    signal.signal(signal.SIGINT, handler)
+
+    print("****** TIPC PYTHON API Demo Server Started ******\n")
+    rdmsd = bind_service(RDM_SRV_TYPE, 0, SOCK_RDM, "RDM")
+    strsd = bind_service(STREAM_SRV_TYPE, 0, SOCK_STREAM, "STREAM")
+    pktsd = bind_service(SEQPKT_SRV_TYPE, 0, SOCK_SEQPACKET, "SEQPACKET")
+
+    while not quite:
+        try:
+            recv_rdm_msg(rdmsd)
+            client_conn_stream = recv_stream_setup(strsd)
+            client_conn_seq = recv_seqpacket_setup(pktsd)
+
+            # Set up the poller
+            poller = select.poll()
+            poller.register(client_conn_stream.get_fd(), READ_ONLY)
+            poller.register(client_conn_seq.get_fd(), READ_ONLY)
+
+            events = poller.poll(TIMEOUT * 1000000)
+            for fd, flag in events:
+                if not flag & select.POLLHUP:
+                    continue
+
+                print("-------------------------------------")
+                sock_str = ''
+                if fd == client_conn_stream.get_fd():
+                    sock_str = "SOCK_STREAM"
+                    poller.unregister(fd)
+                    client_conn_stream.close()
+                else:
+                    if fd == client_conn_seq.get_fd():
+                        sock_str = "SOCK_SEQPACKET"
+                        poller.unregister(fd)
+                        client_conn_seq.close()
+
+                print("%s connection hangup" %sock_str)
+        except IOError:
+            break
+        except Exception:
+            break
+
+if __name__== "__main__":
+    try:
+        pyapi_server_example()
+    except:
+        pass
diff --git a/demos/pytipc/hello_world/Makefile.am 
b/demos/pytipc/hello_world/Makefile.am
new file mode 100644
index 000000000000..d69bfb03c9f4
--- /dev/null
+++ b/demos/pytipc/hello_world/Makefile.am
@@ -0,0 +1,26 @@
+bin_PROGRAMS=py_api_hello_client py_api_hello_server
+py_api_hello_client_SOURCES=
+py_api_hello_server_SOURCES=
+
+CLIENT_OBJECTS=py_api_hello_client.pyc
+SERVER_OBJECTS=py_api_hello_server.pyc
+
+CLEANFILES=$(CLIENT_OBJECTS) $(SERVER_OBJECTS) 
py_api_hello_client$(EXEEXT).tar.gz py_api_hello_server$(EXEEXT).tar.gz
+
+py_api_hello_client$(EXEEXT): $(CLIENT_OBJECTS)
+       $(shell $(MKDIR_P) .libs)
+       cp -f ../../../pytipc/pytipc.so .libs
+       cp -f ../../../pytipc/tipc.pyc .libs
+       tar cvfz [email protected] .libs $(CLIENT_OBJECTS)
+
+py_api_hello_server$(EXEEXT): $(SERVER_OBJECTS)
+       $(shell $(MKDIR_P) .libs)
+       cp -f ../../../pytipc/pytipc.so .libs
+       cp -f ../../../pytipc/tipc.pyc .libs
+       tar cvfz [email protected] .libs $(SERVER_OBJECTS)
+
+%.pyc: %.py
+       $(shell pycompile $<)
+
+clean-generic:
+       rm -rf $(CLEANFILES)
diff --git a/demos/pytipc/hello_world/py_api_hello_client.py 
b/demos/pytipc/hello_world/py_api_hello_client.py
new file mode 100644
index 000000000000..2d2b1e7fe3d3
--- /dev/null
+++ b/demos/pytipc/hello_world/py_api_hello_client.py
@@ -0,0 +1,37 @@
+import sys
+sys.path.append("./libs")
+sys.path.append("./.libs")
+sys.path.append("../../../pytipc")
+
+from tipc import *
+
+SERVER_TYPE = 18888
+SERVER_INST = 17
+BUF_SIZE    = 40
+TIMEOUT     = 10
+
+def pyapi_hello_world_client():
+    print("****** TIPC hello world client started ******")
+    buf = "Hello World!!!"
+
+    c = TipcConn(SOCK_RDM)
+    srv = TipcAddr(SERVER_TYPE, SERVER_INST)
+    up = srv_wait(srv, TIMEOUT * 1000)
+    if up is True:
+        c.sendto(buf, srv)
+    else:
+        print >> sys.stderr, "%s did not come up within %ds!" %(srv, TIMEOUT)
+        sys.exit()
+
+    print("Client: sent message: %s" %buf)
+    ret, msg = c.recv()
+    if ret <= 0:
+        print("Client: unexpected response")
+        sys.exit(1);
+
+    print("Client: received response: %s" %msg)
+    print("****** TIPC hello client finished ******\n")
+
+
+if __name__== "__main__":
+    pyapi_hello_world_client()
diff --git a/demos/pytipc/hello_world/py_api_hello_server.py 
b/demos/pytipc/hello_world/py_api_hello_server.py
new file mode 100644
index 000000000000..a30b30db947f
--- /dev/null
+++ b/demos/pytipc/hello_world/py_api_hello_server.py
@@ -0,0 +1,34 @@
+import sys
+sys.path.append("./libs")
+sys.path.append("./.libs")
+sys.path.append("../../../pytipc")
+
+from tipc import *
+
+SERVER_TYPE = 18888
+SERVER_INST = 17
+BUF_SIZE = 40
+
+def pyapi_hello_world_server():
+    print("****** TIPC hello world server started ******")
+    s = TipcConn(SOCK_RDM)
+    if s.bind(SERVER_TYPE, SERVER_INST, SERVER_INST) != 0:
+        print("Server: failed to bind port name\n")
+        sys.exit(0)
+
+    ret, msg, memberid, socketid, err = s.recvfrom(BUF_SIZE)
+    if ret <= 0:
+        print >> sys.stderr, "Server: unexpected message"
+        sys.exit(0)
+
+    print("Server: Message received: %s" %msg)
+    outbuf = "Uh ?"
+    if s.sendto(outbuf, memberid) <= 0:
+        print("Server: failed to send")
+        sys.exit(0)
+
+    print("Server: Sent response : %s" %outbuf)
+    print("****** TIPC hello world server finished ******\n")
+
+if __name__== "__main__":
+    pyapi_hello_world_server()
diff --git a/pytipc/Makefile.am b/pytipc/Makefile.am
new file mode 100644
index 000000000000..c4eab897a760
--- /dev/null
+++ b/pytipc/Makefile.am
@@ -0,0 +1,16 @@
+PYTHON_CFLAGS=$(shell pkg-config --cflags --libs python)
+CPPFLAGS+=$(PYTHON_CFLAGS)
+
+bin_PROGRAMS=tipc
+
+tipc$(EXEEXT):pytipc.so tipc.pyc
+       $(shell pycompile tipc.py)
+
+pytipc.so:
+       $(AM_V_CC)$(COMPILE) -fPIC -shared -o $@ pytipc.c ../libtipc/libtipc.c
+
+%.pyc: %.py
+       $(shell pycompile $<)
+
+clean-generic:
+       rm -rf pytipc.so tipc.pyc
diff --git a/pytipc/pytipc.c b/pytipc/pytipc.c
new file mode 100644
index 000000000000..a0f4be2239e5
--- /dev/null
+++ b/pytipc/pytipc.c
@@ -0,0 +1,605 @@
+#include <Python.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/param.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <error.h>
+#include <poll.h>
+
+#include "tipcc.h"
+#define INVALID_VALUE "Argument value invalid"
+
+PyObject *createTipcAddrObj() {
+    PyObject *pyMod = NULL, *tipcAddrClass = NULL, *addObj = NULL;
+
+    pyMod = PyImport_ImportModule("tipc");
+    if (pyMod == NULL) {
+        PyErr_SetFromErrno(PyExc_IOError);
+        PyErr_Print();
+        return Py_None;
+    }
+
+    tipcAddrClass = PyObject_GetAttrString(pyMod, "TipcAddr");
+    if (tipcAddrClass == NULL) {
+        PyErr_SetFromErrno(PyExc_IOError);
+        PyErr_Print();
+        return Py_None;
+    }
+    Py_DECREF(pyMod);
+
+    addObj = PyObject_CallObject(tipcAddrClass, NULL);
+    if (addObj == NULL) {
+        PyErr_SetFromErrno(PyExc_IOError);
+        PyErr_Print();
+        return Py_None;
+    }
+    Py_DECREF(tipcAddrClass);
+    return addObj;
+}
+
+static PyObject *convertTipcAddr2py(struct tipc_addr *src) {
+    PyObject *dstObj = createTipcAddrObj();
+
+    if (src != NULL) {
+        PyObject_SetAttrString(dstObj, "srv_type", PyLong_FromLong(src->type));
+        PyObject_SetAttrString(dstObj, "srv_instance", 
PyLong_FromLong(src->instance));
+        PyObject_SetAttrString(dstObj, "node", PyLong_FromLong(src->node));
+    }
+
+    return dstObj;
+}
+
+static void *convertTipcAddr2c(PyObject *srcObj, struct tipc_addr *dst) {
+    PyObject *py_type = NULL, *py_instance = NULL, *py_node = NULL;
+
+    if (srcObj == NULL || dst == NULL)
+        return dst;
+
+    py_type = PyObject_GetAttrString(srcObj, "srv_type");
+    if (py_type == NULL) {
+        PyErr_SetString(PyExc_ValueError, INVALID_VALUE);
+        goto error;
+    }
+    dst->type = (uint32_t)PyLong_AsLong(py_type);
+
+    py_instance = PyObject_GetAttrString(srcObj, "srv_instance");
+    if (py_type == NULL) {
+        PyErr_SetString(PyExc_ValueError, INVALID_VALUE);
+        goto error;
+    }
+    dst->instance = (uint32_t)PyLong_AsLong(py_instance);
+
+    py_node = PyObject_GetAttrString(srcObj, "node");
+    if (py_type == NULL) {
+        PyErr_SetString(PyExc_ValueError, INVALID_VALUE);
+        goto error;
+    }
+    dst->node = (uint32_t)PyLong_AsLong(py_node);
+
+    return dst;
+
+error:
+    Py_XDECREF(py_type);
+    Py_XDECREF(py_instance);
+    Py_XDECREF(py_node);
+
+    return dst;
+}
+
+static PyObject *pytipc_socket(PyObject *self, PyObject *args)
+{
+    int fd, stype;
+
+    if (!PyArg_ParseTuple(args, "i", &stype)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    Py_BEGIN_ALLOW_THREADS
+    fd = tipc_socket(stype);
+    Py_END_ALLOW_THREADS
+
+    if (fd < 0)
+        return PyErr_SetFromErrno(PyExc_IOError);
+
+    return PyLong_FromLong(fd);
+}
+
+static PyObject *pytipc_sockaddr(PyObject *self, PyObject *args)
+{
+    struct tipc_addr addr;
+    int fd;
+    int err;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_sockaddr(fd, &addr);
+    if (err != 0) {
+        return NULL;
+    }
+
+    return Py_BuildValue("O&", convertTipcAddr2py, &addr);
+}
+
+static PyObject *pytipc_sock_non_block(PyObject *self, PyObject *args)
+{
+    int fd;
+    int err;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_sock_non_block(fd);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_sock_rejectable(PyObject *self, PyObject *args)
+{
+    int fd;
+    int err;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_sock_rejectable(fd);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_sock_importance(PyObject *self, PyObject *args)
+{
+    int fd;
+    int err;
+    int pri;
+
+    if (!PyArg_ParseTuple(args, "iI", &fd, &pri)) {
+        return NULL;
+    }
+
+    err = tipc_sock_importance(fd, pri);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_close(PyObject *self, PyObject *args)
+{
+    int fd;
+    int err;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_close(fd);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_bind(PyObject *self, PyObject *args)
+{
+    uint32_t type, lower, upper, scope;
+    int fd;
+
+    int err;
+
+    if (!PyArg_ParseTuple(args, "iIIII", &fd, &type, &lower, &upper, &scope)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_bind(fd, type, lower, upper, scope);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_unbind(PyObject *self, PyObject *args)
+{
+    uint32_t type, lower, upper;
+    int fd;
+
+    int err;
+
+    if (!PyArg_ParseTuple(args, "iIII", &fd, &type, &lower, &upper)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_unbind(fd, type, lower, upper);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_connect(PyObject *self, PyObject *args)
+{
+    struct tipc_addr addr = {0,0,0};
+    int fd;
+    int err = 0;
+
+    if (!PyArg_ParseTuple(args, "iO&", &fd, convertTipcAddr2c, &addr)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_connect(fd, &addr);
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_listen(PyObject *self, PyObject *args)
+{
+    int fd, backlog;
+    int err = 0;
+
+    if (!PyArg_ParseTuple(args, "ii", &fd, &backlog)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    err = tipc_listen(fd, backlog);
+
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_accept(PyObject *self, PyObject *args)
+{
+    struct tipc_addr addr = {0,0,0};
+    int fd;
+    int newsd = 0;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    newsd = tipc_accept(fd, &addr);
+    return Py_BuildValue("(iO&)", newsd, convertTipcAddr2py, &addr);
+}
+
+static PyObject *pytipc_join(PyObject *self, PyObject *args)
+{
+    struct tipc_addr addr = {0,0,0};
+    int fd;
+    int err = 0;
+    bool events, loopback;
+
+    if (!PyArg_ParseTuple(args, "iO&bb", &fd, convertTipcAddr2c, &addr, 
&events, &loopback)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_join(fd, &addr, events, loopback);
+
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_leave(PyObject *self, PyObject *args)
+{
+    int fd;
+    int err = 0;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    err = tipc_leave(fd);
+
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_topsrv_conn(PyObject *self, PyObject *args)
+{
+    uint32_t node;
+    int sd = 0;
+
+    if (!PyArg_ParseTuple(args, "I", &node)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    sd = tipc_topsrv_conn(node);
+
+    return PyLong_FromLong(sd);
+}
+
+static PyObject *pytipc_srv_subscr(PyObject *self, PyObject *args)
+{
+    int fd = 0;
+    uint32_t type, lower, upper;
+    bool all = false;
+    int expire, err;
+
+    if (!PyArg_ParseTuple(args, "iIIIbi", &fd, &type, &lower, &upper, &all, 
&expire)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    err = tipc_srv_subscr(fd, type, lower, upper, all, expire);
+
+    return PyLong_FromLong(err);
+}
+
+static PyObject *pytipc_srv_evt(PyObject *self, PyObject *args)
+{
+    struct tipc_addr srv = {0,0,0}, id = {0,0,0};
+    bool expire, available;
+    int fd = 0;
+    int err = 0;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_srv_evt(fd, &srv, &id, &available, &expire);
+    if (err)
+        return Py_None;
+
+    return Py_BuildValue("(iO&O&bb)", err, convertTipcAddr2py, &srv, 
convertTipcAddr2py, &id, available, expire);
+}
+
+static PyObject *pytipc_srv_wait(PyObject *self, PyObject *args)
+{
+    struct tipc_addr srv = {0,0,0};
+    int expire;
+    bool err = false;
+
+    if (!PyArg_ParseTuple(args, "O&i", convertTipcAddr2c, &srv, &expire)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    err = tipc_srv_wait(&srv, expire);
+
+    return Py_BuildValue("i", err);
+}
+
+static PyObject *pytipc_link_evt(PyObject *self, PyObject *args)
+{
+    uint32_t neigh_node = 0;
+    int fd = 0;
+    bool up;
+    int local_bearerid = 0, remote_bearerid = 0, err = 0;
+
+    if (!PyArg_ParseTuple(args, "i", &fd)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    err = tipc_link_evt(fd, &neigh_node, &up, &local_bearerid, 
&remote_bearerid);
+    if (err)
+        return Py_None;
+
+    return Py_BuildValue("(Ibii)", neigh_node, up, local_bearerid, 
remote_bearerid);
+}
+
+static PyObject *pytipc_linkname(PyObject *self, PyObject *args)
+{
+    uint32_t peer = 0;
+    int bearerid;
+    char buf[256] = {0};
+
+    if (!PyArg_ParseTuple(args, "Ii", &peer, &bearerid)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    return Py_BuildValue("s", tipc_linkname(buf, 255, peer, bearerid));
+}
+
+static PyObject *pytipc_own_node(PyObject *self, PyObject *args)
+{
+    return Py_BuildValue("I", tipc_own_node());
+}
+
+static PyObject *pytipc_recvfrom(PyObject *self, PyObject *args)
+{
+    int fd = 0;
+    struct tipc_addr socketid = {0,0,0};
+    struct tipc_addr memberid = {0,0,0};
+    size_t len = 0;
+    int ret = 0, err = 0;
+    char *buf = NULL;
+    PyObject *rbuf = NULL;
+
+    if (!PyArg_ParseTuple(args, "iK", &fd, &len)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    buf = (char *)malloc(len);
+    if (!buf) {
+        PyErr_NoMemory();
+        return Py_None;
+    }
+    memset(buf, 0, len);
+
+    Py_BEGIN_ALLOW_THREADS
+    ret = tipc_recvfrom(fd, buf, len, &socketid, &memberid, &err);
+    if (ret > 0) {
+        rbuf = PyByteArray_FromStringAndSize(buf, len);
+    } else {
+        rbuf = PyByteArray_FromStringAndSize("", 0);
+    }
+    free(buf);
+    Py_END_ALLOW_THREADS
+
+    return Py_BuildValue("iOO&O&i", ret, 
+                         rbuf,
+                         convertTipcAddr2py, &socketid,
+                         convertTipcAddr2py, &memberid,
+                         err);
+}
+
+static PyObject *pytipc_recv(PyObject *self, PyObject *args)
+{
+    int fd = 0;
+    size_t len = 0;
+    int ret = 0;
+    bool waitall;
+    char *buf = NULL;
+    PyObject *rbuf = NULL;
+
+    if (!PyArg_ParseTuple(args, "iKb", &fd, &len, &waitall)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    buf = (char *)malloc(len);
+    if (!buf) {
+        PyErr_NoMemory();
+        return Py_None;
+    }
+    memset(buf, 0, len);
+
+    Py_BEGIN_ALLOW_THREADS
+    ret = tipc_recv(fd, buf, len, waitall);
+    if (ret > 0) {
+        rbuf = PyByteArray_FromStringAndSize(buf, len);
+    } else {
+        rbuf = PyByteArray_FromStringAndSize("", 0);
+    }
+    free(buf);
+    Py_END_ALLOW_THREADS
+
+    return Py_BuildValue("iO", ret, rbuf);
+}
+
+static PyObject *pytipc_send(PyObject *self, PyObject *args)
+{
+    int fd = 0;
+    size_t len = 0;
+    int ret = 0;
+    char *buf = NULL;
+    PyObject *sbuf = NULL;
+
+    if (!PyArg_ParseTuple(args, "iO", &fd, &sbuf)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    if (!PyByteArray_Check(sbuf)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    Py_BEGIN_ALLOW_THREADS
+    buf = PyByteArray_AsString(sbuf);
+    len = PyByteArray_Size(sbuf);
+    ret = tipc_send(fd, buf, len);
+    Py_END_ALLOW_THREADS
+
+    return PyLong_FromLong(ret);
+}
+
+static PyObject *pytipc_sendto(PyObject *self, PyObject *args)
+{
+    int fd = 0;
+    size_t len = 0;
+    int ret = 0;
+    char *buf = NULL;
+    PyObject *sbuf = NULL;
+    struct tipc_addr dst = {0,0,0};
+
+    if (!PyArg_ParseTuple(args, "iOO&", &fd, &sbuf, convertTipcAddr2c, &dst)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    if (!PyByteArray_Check(sbuf)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    Py_BEGIN_ALLOW_THREADS
+    buf = PyByteArray_AsString(sbuf);
+    len = PyByteArray_Size(sbuf);
+    ret = tipc_sendto(fd, buf, len, &dst);
+    Py_END_ALLOW_THREADS
+
+    return PyLong_FromLong(ret);
+}
+
+static PyObject *pytipc_mcast(PyObject *self, PyObject *args)
+{
+    int fd = 0;
+    size_t len = 0;
+    int ret = 0;
+    char *buf = NULL;
+
+    PyObject *sbuf = NULL;
+    struct tipc_addr dst = {0,0,0};
+
+    if (!PyArg_ParseTuple(args, "iOO&", &fd, &sbuf, convertTipcAddr2c, &dst)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    if (!PyByteArray_Check(sbuf)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    Py_BEGIN_ALLOW_THREADS
+    buf = PyByteArray_AsString(sbuf);
+    len = PyByteArray_Size(sbuf);
+    ret = tipc_mcast(fd, buf, len, &dst);
+    Py_END_ALLOW_THREADS
+
+    return PyLong_FromLong(ret);
+}
+
+static PyMethodDef tipcc_functions[] = {
+    {"socket", pytipc_socket, METH_VARARGS, NULL},
+    {"sock_non_block", pytipc_sock_non_block, METH_VARARGS, NULL},
+    {"sock_rejectable", pytipc_sock_rejectable, METH_VARARGS, NULL},
+    {"sock_importance", pytipc_sock_importance, METH_VARARGS, NULL},
+    {"close", pytipc_close, METH_VARARGS, NULL},
+    {"sockaddr", pytipc_sockaddr, METH_VARARGS, NULL},
+    {"bind", pytipc_bind, METH_VARARGS, NULL},
+    {"unbind", pytipc_unbind, METH_VARARGS, NULL},
+    {"connect", pytipc_connect, METH_VARARGS, NULL},
+    {"listen", pytipc_listen, METH_VARARGS, NULL},
+    {"accept", pytipc_accept, METH_VARARGS, NULL},
+    {"join", pytipc_join, METH_VARARGS, NULL},
+    {"leave", pytipc_leave, METH_VARARGS, NULL},
+    {"recvfrom", pytipc_recvfrom, METH_VARARGS, NULL},
+    {"recv", pytipc_recv, METH_VARARGS, NULL},
+    {"sendto", pytipc_sendto, METH_VARARGS, NULL},
+    {"send", pytipc_send, METH_VARARGS, NULL},
+    {"mcast", pytipc_mcast, METH_VARARGS, NULL},
+    {"topsrv_conn", pytipc_topsrv_conn, METH_VARARGS, NULL},
+    {"srv_subscr", pytipc_srv_subscr, METH_VARARGS, NULL},
+    {"srv_evt", pytipc_srv_evt, METH_VARARGS, NULL},
+    {"srv_wait", pytipc_srv_wait, METH_VARARGS, NULL},
+    {"link_evt", pytipc_link_evt, METH_VARARGS, NULL},
+    {"linkname", pytipc_linkname, METH_VARARGS, NULL},
+    {"own_node", pytipc_own_node, METH_VARARGS, NULL},
+    {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC initpytipc(void) {
+    PyObject *mod = Py_InitModule("pytipc", tipcc_functions);
+
+    if (mod == NULL)
+        return;
+
+    // TODO: Copy & paste C definitions
+    PyModule_AddIntConstant(mod, "SOCK_STREAM", SOCK_STREAM);
+    PyModule_AddIntConstant(mod, "SOCK_DGRAM", SOCK_DGRAM);
+    PyModule_AddIntConstant(mod, "SOCK_RDM", SOCK_RDM);
+    PyModule_AddIntConstant(mod, "SOCK_SEQPACKET", SOCK_SEQPACKET);
+
+    PyModule_AddIntConstant(mod, "TIPC_LOW_IMPORTANCE", TIPC_LOW_IMPORTANCE);
+    PyModule_AddIntConstant(mod, "TIPC_MEDIUM_IMPORTANCE", 
TIPC_MEDIUM_IMPORTANCE);
+    PyModule_AddIntConstant(mod, "TIPC_HIGH_IMPORTANCE", TIPC_HIGH_IMPORTANCE);
+    PyModule_AddIntConstant(mod, "TIPC_CRITICAL_IMPORTANCE", 
TIPC_CRITICAL_IMPORTANCE);
+
+    /*
+     * Application-accessible service types
+     */
+    PyModule_AddIntConstant(mod, "TIPC_NODE_STATE", TIPC_NODE_STATE);
+    PyModule_AddIntConstant(mod, "TIPC_LINK_STATE", TIPC_LINK_STATE);
+
+    PyModule_AddIntConstant(mod, "TIPC_WAIT_FOREVER", TIPC_WAIT_FOREVER);
+};
diff --git a/pytipc/tipc.py b/pytipc/tipc.py
new file mode 100644
index 000000000000..fd7787dbddd9
--- /dev/null
+++ b/pytipc/tipc.py
@@ -0,0 +1,282 @@
+""" Wrapper tipc from libtipc """
+from pytipc import own_node as pylib_own_node
+from pytipc import srv_wait as pylib_srv_wait
+from pytipc import linkname as pylib_linkname
+
+from pytipc import socket as pylib_socket
+from pytipc import sock_non_block as pylib_sock_non_block
+from pytipc import sock_rejectable as pylib_sock_rejectable
+from pytipc import sock_importance as pylib_sock_importance
+from pytipc import bind as pylib_bind
+from pytipc import unbind as pylib_unbind
+from pytipc import close as pylib_close
+from pytipc import sockaddr as pylib_sockaddr
+from pytipc import connect as pylib_connect
+from pytipc import listen as pylib_listen
+from pytipc import accept as pylib_accept
+from pytipc import join as pylib_join
+from pytipc import leave as pylib_leave
+from pytipc import recvfrom as pylib_recvfrom
+from pytipc import recv as pylib_recv
+from pytipc import send as pylib_send
+from pytipc import sendto as pylib_sendto
+from pytipc import mcast as pylib_send_mcast
+
+from pytipc import topsrv_conn as pylib_topsrv_conn
+from pytipc import srv_subscr as pylib_srv_subscr
+from pytipc import srv_evt as pylib_srv_evt
+from pytipc import link_evt as pylib_link_evt
+
+from pytipc import TIPC_LOW_IMPORTANCE, \
+    TIPC_MEDIUM_IMPORTANCE, TIPC_HIGH_IMPORTANCE, \
+    TIPC_CRITICAL_IMPORTANCE, \
+    SOCK_STREAM, SOCK_DGRAM, SOCK_RDM, SOCK_SEQPACKET, \
+    TIPC_NODE_STATE, TIPC_LINK_STATE, TIPC_WAIT_FOREVER
+
+MSG_LEVELS = [TIPC_LOW_IMPORTANCE, \
+              TIPC_MEDIUM_IMPORTANCE, \
+              TIPC_HIGH_IMPORTANCE, \
+              TIPC_CRITICAL_IMPORTANCE]
+
+SOCK_TYPES = [SOCK_STREAM, SOCK_DGRAM, SOCK_RDM, SOCK_SEQPACKET]
+
+class TipcAddr(object):
+    """
+    TODO:
+        Attributes:
+    """
+
+    def __init__(self, srv_type=0, instance=0, node=0):
+        """ TODO: """
+        self.srv_type = srv_type
+        self.srv_instance = instance
+        self.node = node
+
+    def __repr__(self):
+        """ TODO: """
+        return "tipc.TipcAddr"
+
+    def __str__(self):
+        """ TODO: """
+        if self.srv_type != 0:
+            return "%u:%u@%x" %(self.srv_type, self.srv_instance, self.node)
+        return "0:%010u@%x" %(self.srv_instance, self.node)
+
+    def string(self):
+        """ TODO: """
+        return self.__str__()
+
+    def is_equal(self, item):
+        """ TODO: """
+        if not isinstance(item, TipcAddr):
+            return False
+
+        return ((self.srv_type == item.srv_type) and
+                (self.srv_instance == item.srv_instance) and
+                (self.node == item.node))
+
+class TipcServiceRange(object):
+    """
+    TODO:
+        Attributes:
+    """
+
+    def __init__(self, srv_type=0, lower=0, upper=0, node=0):
+        """ TODO: """
+        self.srv_type = srv_type
+        self.lower = lower
+        self.upper = upper
+        self.node = node
+
+    def __repr__(self):
+        """ TODO: """
+        return "tipc.TipcServiceRange"
+
+    def __str__(self):
+        """ TODO: """
+        return "%u:%u:%u@%x" %(self.srv_type, self.lower, self.upper, 
self.node)
+
+    def string(self):
+        """ TODO: """
+        return self.__str__()
+
+    def is_equal(self, item):
+        """ TODO: """
+        if not isinstance(item, TipcAddr):
+            return False
+
+        return ((self.srv_type == item.srv_type) and
+                (self.lower == item.lower) and
+                (self.upper == item.upper) and
+                (self.node == item.node))
+
+class TipcConn(object):
+    """
+    TODO:
+        Attributes:
+    """
+
+    def __init__(self, socktype, fd=0):
+        """ TODO: """
+
+        if socktype not in SOCK_TYPES:
+            raise ValueError("sock_type should be in range %s "%SOCK_TYPES)
+
+        self.file_fd = fd
+        self.sock_type = socktype
+
+        if fd <= 0:
+            self.socket(socktype)
+
+    def socket(self, socktype):
+        """ Create AF_TIPC socket API """
+        self.file_fd = pylib_socket(socktype)
+
+    def get_fd(self):
+        """ Return File Description TIPC socket """
+        return self.file_fd
+
+    def sock_non_block(self):
+        """ Set a TIPC socket to non-block """
+        return pylib_sock_non_block(self.file_fd)
+
+    def sock_rejectable(self):
+        """ Set messages sent from this socket as rejectable """
+        return pylib_sock_rejectable(self.file_fd)
+
+    def sock_priority(self, pri=TIPC_MEDIUM_IMPORTANCE):
+        """ Set priority from this socket """
+        if pri not in MSG_LEVELS:
+            raise ValueError("priority should be in range %s "%MSG_LEVELS)
+
+        return pylib_sock_importance(self.file_fd, pri)
+
+    def bind(self, srv_type, lower, upper, node=0):
+        """ Bind a TIPC socket """
+        return pylib_bind(self.file_fd, srv_type, lower, upper, node)
+
+    def unbind(self, srv_type, lower, upper):
+        """ Unbind a bound address """
+        return pylib_unbind(self.file_fd, srv_type, lower, upper)
+
+    def close(self):
+        """ Close a TIPC socket """
+        return pylib_close(self.file_fd)
+
+    def sockaddr(self):
+        """ Get a bound socket information """
+        return pylib_sockaddr(self.file_fd)
+
+    def connect(self, addr):
+        """ Establish connection to a TIPC socket address (service address) """
+        return pylib_connect(self.file_fd, addr)
+
+    def accept(self):
+        """ TODO: """
+        new_fd, addr = pylib_accept(self.file_fd)
+        if new_fd <= 0:
+            return None, addr
+        return TipcConn(self.sock_type, new_fd), addr
+
+    def listen(self, backlog=0):
+        """ TODO: """
+        return pylib_listen(self.file_fd, backlog)
+
+    def join(self, member, events=False, loopback=False):
+        """ TODO: """
+        return pylib_join(self.file_fd, member, events, loopback)
+
+    def leave(self):
+        """ TODO: """
+        return pylib_leave(self.file_fd)
+
+    def recvfrom(self, bufsize=1024):
+        """ TODO: """
+        return pylib_recvfrom(self.file_fd, bufsize)
+
+    def recv(self, bufsize=1024, waitall=False):
+        """ TODO: """
+        return pylib_recv(self.file_fd, bufsize, waitall)
+
+    def send(self, buf):
+        """ TODO: """
+        if not isinstance(buf, bytearray):
+            buf = bytearray(buf)
+        return pylib_send(self.file_fd, buf)
+
+    def sendto(self, buf, dst):
+        """ TODO: """
+        if not isinstance(buf, bytearray):
+            buf = bytearray(buf)
+        return pylib_sendto(self.file_fd, buf, dst)
+
+    def send_mcast(self, buf, dst):
+        """ TODO: """
+        return pylib_send_mcast(self.file_fd, buf, dst)
+
+
+class TopSrvConn(object):
+    """
+    TODO:
+        Attributes:
+    """
+
+    def __init__(self, topsrv_node):
+        """ TODO: """
+        self.file_fd = pylib_topsrv_conn(topsrv_node)
+
+    def get_fd(self):
+        """ Return File Description TIPC socket """
+        return self.file_fd
+
+    def close(self):
+        """ TODO: """
+        return pylib_close(self.file_fd)
+
+    def srv_subscr(self, srv_type, lower, upper, filter_all=False, 
expire=1000):
+        """ TODO: """
+        return pylib_srv_subscr(self.file_fd, srv_type, lower, upper, 
filter_all, expire)
+
+    def srv_evt(self):
+        """ TODO:
+
+            Args:
+
+            Returns:
+                err, TipcAddr, TipcAddr, available, expire
+
+            Raises:
+                None
+        """
+        return pylib_srv_evt(self.file_fd)
+
+    def neigh_subscr(self):
+        """ TODO: """
+        return pylib_srv_subscr(self.file_fd, TIPC_NODE_STATE, 0, ~0, True, 
TIPC_WAIT_FOREVER)
+
+    def neigh_evt(self):
+        """ TODO: """
+        (srv, _, available, _) = pylib_srv_evt(self.file_fd)
+        return srv.instance, available
+
+    def link_subscr(self):
+        """ TODO: """
+        return pylib_srv_subscr(self.file_fd, TIPC_LINK_STATE, 0, ~0, True, 
TIPC_WAIT_FOREVER)
+
+    def link_evt(self):
+        """ TODO: """
+        return pylib_link_evt(self.file_fd)
+
+def own_node():
+    """ TODO: """
+    return pylib_own_node()
+
+def srv_wait(srv, expire):
+    """ TODO: """
+    if pylib_srv_wait(srv, expire) == 1:
+        return True
+    return False
+
+def linkname(peer, bearerid):
+    """ TODO: """
+    return pylib_linkname(peer, bearerid)
-- 
2.7.4



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
tipc-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tipc-discussion

Reply via email to