From a5803c3a3e49d5d10e017a70c9e94d0545d59a09 Mon Sep 17 00:00:00 2001 From: pavet Date: Thu, 9 Sep 2004 15:33:37 +0000 Subject: Initial revision --- docs/examples/pyhello.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 docs/examples/pyhello.py (limited to 'docs/examples/pyhello.py') diff --git a/docs/examples/pyhello.py b/docs/examples/pyhello.py new file mode 100755 index 0000000..e0db498 --- /dev/null +++ b/docs/examples/pyhello.py @@ -0,0 +1,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() -- cgit v1.1