summaryrefslogtreecommitdiff
path: root/docs/examples/pyhello.py
blob: e0db49893d07e1ae5b72bd05f642698fdf63ae6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
""" pyhello.py : very simple hello world python program using ivycpy
with a local main loop
"""
import traceback, time, string, os, sys, getopt
# importing ivycpy 
from ivycpy import *

IVYAPPNAME = "pyhello"

def lprint(fmt,*arg):
	print IVYAPPNAME + ": " + fmt % arg

def usage(scmd):
	lpathitem = string.split(scmd,'/')
	fmt = '''Usage: %s [-h] [-b IVYBUS | --ivybus=IVYBUS]
where
\t-h provides the usage message;
\t-b IVYBUS | --ivybus=IVYBUS allow to provide the IVYBUS string in the form
\t adresse:port eg. 127.255.255.255:2010
'''
	print fmt   %  lpathitem[-1]

def oncxproc(connected):
	if connected == IvyApplicationDisconnected :
		lprint( "An Ivy application was disconnected ")
	else:
		lprint( "An Ivy application was connected")
	lprint("currents Ivy application are [%s]", IvyGetApplicationList())
	
def ondieproc(id):
	lprint( "received the order to die with id = %d", id)

def onmsgproc(*larg):
	lprint( "%s received [%s] ", IVYAPPNAME , larg[0])

def onhello(*larg):
	sreply = "goodday %s to=%s from=%s " % (larg[0], larg[1],IVYAPPNAME) 
	lprint( "on hello , %s reply [%s]", IVYAPPNAME, sreply) 
	IvySendMsg(sreply)

def ontick():
	lprint( "%s send a tick", IVYAPPNAME) 
	IvySendMsg("%s_tick" % IVYAPPNAME)

if __name__ == '__main__':
	# initializing ivybus and isreadymsg
	sivybus = ""
	sisreadymsg = "%s is ready" % IVYAPPNAME
	# getting option
	try:
		optlist, left_args = getopt.getopt(sys.argv[1:],'hb:', ['ivybus='])
	except getopt.GetoptError:
		# print help information and exit:
		usage(sys.argv[0])
		sys.exit(2)
	for o, a in optlist:
		if o in ("-h", "--help"):
			usage(sys.argv[0])
			sys.exit()
		elif o in ("-b", "--ivybus"):
			sivybus= a
	if sivybus != "" :
		sechoivybus = sivybus
	elif os.environ.has_key("IVYBUS"):
		sechoivybus = os.environ["IVYBUS"]
	else:
		sechoivybus = "ivydefault"
	lprint( "Ivy will broadcast on %s ", sechoivybus)

	# initialising the bus 
	IvyInit(IVYAPPNAME,   # application name for Ivy
			sisreadymsg , # ready message
			0,            # main loop is local (ie. using IvyMainloop)
			oncxproc,     # handler called on connection/deconnection
			ondieproc     # handler called when a diemessage is received 
			)
	# starting the bus
	# Note: env variable IVYBUS will be used if no parameter or empty string
	# is given ; this is performed by IvyStart (C)
	IvyStart(sivybus)
	# binding to every message 
	IvyBindMsg(onmsgproc, "(.*)")
	# binding on dedicated message : starting with "hello ..."
	IvyBindMsg(onhello, "^hello=([^ ]*) from=([^ ]*)")
	# creating a infinite timer 
	timerid = IvyTimerRepeatAfter(0,     # number of time to be called
								  1000,  # delay in ms between calls
								  ontick # handler to call
								  )
	lprint( "IvyTimerRepeatAfter id is  %d", timerid)
	
	lprint( "%s doing IvyMainLoop", IVYAPPNAME)
	IvyMainLoop()