summaryrefslogtreecommitdiff
path: root/docs/examples/testtk.py
blob: 13bb1f9a3c053f6ba50fe990137f22ff838bd045 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
""" programme de test de ivycpy en mode boucle tk """

import traceback, time, os, sys, string, getopt
from Tkinter import *
from ivycpy import *

IVYAPPNAME = "pytesttk"

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]

global hellobindingid
hellobindingid = -1 

class TopLevel(Tk) :
	__single = 0
	def __init__(self,appname = "TopLevel"):
		if TopLevel.__single != 0 :
			lprint("TopLevel is singleton")
			sys.exit(1)
		__single = 1
		# the object is tk_root
		Tk.__init__(self) 
		self.appname = appname
		self.title(self.appname)
		# Frame contenant heure et bouton quit 
		self.tkFheadpanel = Frame(self)
		# Label hour
		self.tkLhour =  Label(self.tkFheadpanel,text="hh:mm:ss")
		self.tkLhour.pack(padx=4 , pady=4 , side = LEFT)
		# dummy button
## 		self.tkBdummy = Button(self.tkFheadpanel, 
## 							  text="dummy",  bg='pink')
## 		self.tkBdummy.pack(padx=4 , pady=4, side = RIGHT)
		
		# button quit
		self.tkBquit = Button(self.tkFheadpanel, 
							  text="QUIT",  bg='pink', command=self.quit)
		self.tkBquit.pack(padx=4 , pady=4, side = RIGHT)
		self.tkFheadpanel.pack(side=TOP)
	
		# Frame contenant un Text et un Scroller
		self.tkFreceive_msg = Frame(self)
		# Text
		self.tkTreceive_msg = Text(self.tkFreceive_msg, height=26, width=50)
		# Scrollbar
		self.tkSBreceive_scroller = Scrollbar(
			self.tkFreceive_msg,
			command = self.tkTreceive_msg.yview)
		self.tkTreceive_msg.configure(
			yscrollcommand = self.tkSBreceive_scroller.set)

		self.tkTreceive_msg.pack(padx=4 , pady=4,
								 side=LEFT, fill = BOTH, expand = YES)
		self.tkSBreceive_scroller.pack(side=RIGHT, fill = Y)
		self.tkFreceive_msg.pack(side=BOTTOM, fill = BOTH, expand = YES)

	def add_one_msg(self, smsg) :
		self.tkTreceive_msg.insert(END, "%s\n" % smsg )

	def quit(self):
		# cleanup action before detroying tk_root
		lprint("doing IvyStop")
		IvyStop()
		lprint("doing destroy" )
		self.destroy()
		lprint("after destroy")
		lprint("done.")

	def ontick(self):
		lprint("on tick method")
		IvySendMsg("testtk_tick")
		self.after(1000,self.ontick)
		
def oncxproc(connected):
	if connected == IvyApplicationDisconnected :
		lprint("an IvyApplication was Disconnected")
	else:
		lprint("an IvyApplication was Connected")

def ondieproc(id):
	lprint("On die proc id=%d", id )

def onhelloproc(*larg):
	sargs = string.join(larg,":")
	sresult = "On <hello %s>, i reply <goodday %s>" % (larg[0], larg[0])
	mytoplevel.add_one_msg(sresult)
	# lprint("[%s]" % sresult 
	IvySendMsg("goodday %s" % (larg[0]))

def onmsgproc(*larg):
	sresult = "On msg proc : arg number = %d , arglist = %s" % \
			  (len(larg), string.join(larg,":"))
	mytoplevel.add_one_msg(sresult)

	# abonnement selectif
	
def oncmd(*larg):
	global hellobindingid
	lprint("hb =%d", hellobindingid)
	lprint("cmd [%s] larg0=%s", string.join(larg,":"), larg[0])
	if larg[0] == 'bindhello' :
		if hellobindingid == -1  :
			hellobindingid = IvyBindMsg(onhelloproc,"^hello (.*)")
			lprint("binding hello id = %d ", hellobindingid)
	elif larg[0] == 'unbindhello' :
		if hellobindingid != -1 :
			lprint("unbinding hello id = %d ", hellobindingid)
			IvyUnBindMsg(hellobindingid)
			hellobindingid = -1
	elif larg[0] == 'getlist' :
		sapplist = IvyGetApplicationList()
		lprint("app list return %s" , sapplist)
		for sapp in string.split(sapplist):
			lprint("app name [%s] host [%s] ",
				   sapp, IvyGetApplicationHost(IvyGetApplication(sapp)))
			lprint(IvyGetApplication(sapp))
	elif larg[0] == 'senddie' :
		sapplist = IvyGetApplicationList()
		lprint("app list return %s", sapplist)
		for sapp in string.split(sapplist):
			lprint("sending die to %s ", sapp)
			IvySendDieMsg(IvyGetApplication(sapp))
	elif larg[0] == 'senderror' :
		sapplist = IvyGetApplicationList()
		lprint("app list return %s" , sapplist)
		for sapp in string.split(sapplist):
			lprint("sending error to %s " , sapp)
			IvySendError(IvyGetApplication(sapp),0,
						 "error msg to %s from %s" % (sapp,IVYAPPNAME))
	# print "[%s]" % sresult 
	# IvySendMsg("a recu et renvoi [%s]" % sargs)

def ontick():
	lprint("ontick")
	IvySendMsg("testtk_tick")
	
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)

	mytoplevel = TopLevel("%s top window" % IVYAPPNAME)
	# myinterface = SocketInt(mytoplevel, delay = 2.0, iter = 5)
	sisreadymsg = "%s is ready" % IVYAPPNAME
	lprint("IvyApplicationConnected is %s " , IvyApplicationConnected)
	lprint("IvyApplicationDisconnected is %s " , IvyApplicationDisconnected)
	IvyInit(IVYAPPNAME, sisreadymsg, 1 ,  oncxproc,  ondieproc )
	IvyStart(sivybus)
	bindingid = IvyBindMsg(onmsgproc, "([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*) (.*)") # "(.*)")
	# lprint("IvyBindMsg retourne %d" , bindingid )
	bindingid = IvyBindMsg(oncmd, "^\.(.*)") # "(.*)")
	# lprint("IvyBindMsg retourne %d", bindingid)

	# timerid = IvyTimerRepeatAfter(10,1000,ontick)
	# print("IvyTimerRepeatAfter retourne %d" % timerid
	mytoplevel.after(1000,mytoplevel.ontick)
    
	lprint("tk mainloop")
	mytoplevel.mainloop()