git » pytipc » commit 2e442da

Add two simple examples.

author Alberto Bertogli
2007-12-17 03:44:45 UTC
committer Alberto Bertogli
2007-12-17 03:45:43 UTC
parent a148d734342085eb4213d4d47da02930a0bec77e

Add two simple examples.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

examples/cs.py +38 -0
examples/rdm.py +42 -0
examples/stream.py +45 -0

diff --git a/examples/cs.py b/examples/cs.py
new file mode 100644
index 0000000..ea96e09
--- /dev/null
+++ b/examples/cs.py
@@ -0,0 +1,38 @@
+
+import os
+import sys
+import time
+import threading
+
+
+def withthreads(srv, cli):
+	t1 = threading.Thread(target = srv)
+	t1.start()
+	cli()
+
+
+def withfork(srv, cli):
+	ppid = os.getpid()
+	cpid = os.fork()
+	if cpid < 0:
+		raise
+	elif cpid == 0:
+		# child
+		try:
+			srv()
+		finally:
+			os.kill(ppid, 15)
+	else:
+		# parent
+		try:
+			cli()
+		finally:
+			os.kill(cpid, 15)
+
+
+def run(srv, cli):
+	if int(os.environ.get('USE_THREADS', '0')):
+		withthreads(srv, cli)
+	else:
+		withfork(srv, cli)
+
diff --git a/examples/rdm.py b/examples/rdm.py
new file mode 100755
index 0000000..435bff2
--- /dev/null
+++ b/examples/rdm.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+import sys
+import socket
+import tipc
+import cs
+
+#srvaddr = (tipc.TIPC_ADDR_NAMESEQ, 2000, 10, 100, tipc.TIPC_NODE_SCOPE)
+#srvaddr = (tipc.TIPC_ADDR_NAMESEQ, 2000, 10, 100)
+srvaddr = (tipc.TIPC_ADDR_NAME, 2000, 10, 0)
+#srvaddr = (tipc.TIPC_ADDR_NAME, 2000, 10)  # broken
+
+def srv():
+	fd = tipc.socket(socket.SOCK_RDM)
+	fd.bind(srvaddr)
+	print 'server started'
+
+	buf, addr = fd.recvfrom(1024)
+	while buf != "EXIT":
+		print 'srv:', addr, buf
+		buf, addr = fd.recvfrom(1024)
+	print 'srv exit'
+
+def cli():
+	print 'waiting'
+	evt = tipc.wait_for(srvaddr, timeout = 500)
+	if not evt or evt[0] == tipc.TIPC_SUBSCR_TIMEOUT:
+		print 'timeout'
+		return
+	print 'server up', evt
+
+	fd = tipc.socket(socket.SOCK_RDM)
+	l = sys.stdin.readline()
+	while l:
+		fd.sendto(l.strip(), srvaddr)
+		l = sys.stdin.readline()
+	fd.sendto('EXIT', srvaddr)
+	print 'cli exit'
+
+
+cs.run(srv, cli)
+
diff --git a/examples/stream.py b/examples/stream.py
new file mode 100755
index 0000000..046095a
--- /dev/null
+++ b/examples/stream.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+
+import sys
+import socket
+import tipc
+import cs
+
+srvaddr = (tipc.TIPC_ADDR_NAME, 2000, 10, 0)
+
+def srv():
+	fd = tipc.socket(socket.SOCK_STREAM)
+	fd.bind(srvaddr)
+	print 'server started'
+
+	fd.listen(5)
+	conn, addr = fd.accept()
+	print 'connected', addr
+
+	buf = conn.recv(1024)
+	while buf != "EXIT":
+		print 'srv:', buf
+		buf = conn.recv(1024)
+	print 'srv exit'
+
+def cli():
+	print 'waiting'
+	evt = tipc.wait_for(srvaddr, timeout = 500)
+	if not evt or evt[0] == tipc.TIPC_SUBSCR_TIMEOUT:
+		print 'timeout'
+		return
+	print 'server up', evt
+
+	fd = tipc.socket(socket.SOCK_STREAM)
+	fd.connect(srvaddr)
+
+	l = sys.stdin.readline()
+	while l:
+		fd.send(l.strip())
+		l = sys.stdin.readline()
+	fd.send('EXIT')
+	print 'cli exit'
+
+
+cs.run(srv, cli)
+