#!/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()