E.3.1 Hauptprogramm des Monitor-/Steuerungswerkzeug
from Tkinter import *
import socket
import string
class moni:
def __init__(self,master):
self.master=master
self.br_anz=0
self.sp_anz=0
self.if_anz=0
self.upint=5
self.widgets={}
self.host="localhost"
self.port=6444
self.connected=0
self.conn=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.confstr=""
self.create_everything()
def create_everything(self):
self.mbar=Frame(self.master,relief=RAISED, bd=2)
self.mbar.pack(side=TOP, fill=X)
self.m_verbindung =Menubutton(self.mbar,text="Verbindung",underline=0)
self.m_broker =Menubutton(self.mbar,text="Broker",underline=0)
self.m_options =Menubutton(self.mbar,text='Options',underline=0)
self.m_info =Menubutton(self.mbar,text="Info",underline=0)
self.m_verbindung.pack(side=LEFT)
self.m_broker.pack(side=LEFT)
self.m_options.pack(side=LEFT)
self.m_info.pack(side=RIGHT)
self.m1=Menu(self.m_verbindung,tearoff=NO)
self.m1.add_command(label="Aufbauen",underline=0,command=self.br_connect)
self.m1.add_command(label="Abbauen",underline=0,command=self.br_disconnect)
self.m1.add_command(label="Beenden",underline=0,command=self.ende)
self.m2=Menu(self.m_broker,tearoff=NO)
self.m2.add_command(label="Anhalten",underline=0,command=self.br_stop)
self.m2.add_command(label="Fortsetzen",underline=0,command=self.br_cont)
self.m2.add_command(label="Manuelle Verteilung",underline=0,command=self.br_man)
self.m2.add_command(label="Automatische Verteilung",underline=0,command=self.br_auto)
self.m3=Menu(self.m_info,tearoff=NO)
self.m3.add_command(label="Info",underline=0,command=self.info1)
self.m4=Menu(self.m_options,tearoff=NO)
self.m4.add_command(label="Update",underline=0,command=self.brupdate)
self.m4.add_command(label="Parameter",underline=0,command=self.br_parms)
self.m_verbindung.config(menu=self.m1)
self.m_broker.config(menu=self.m2)
self.m_options.config(menu=self.m4)
self.m_info.config(menu=self.m3)
self.mbar.tk_menuBar(self.m_verbindung, self.m_broker, self.m_options,self.m_info)
self.f2=Frame(self.master)
self.f2.pack()
self.draw=Canvas(self.f2, bg='Black', height=400, width=500,scrollregion=(0,0,1000,400))
self.draw.pack()
self.dsbx=Scrollbar(self.f2,orient=HORIZONTAL)
self.dsbx.pack(fill=X,side=BOTTOM,expand=YES)
self.draw.config(xscrollcommand=self.dsbx.set)
self.dsbx.config(command=self.draw.xview)
self.draw.bind("<Double-Button-1>",self.action)
self.f4=Frame(self.master)
self.f4.pack(fill=X)
self.f3=Frame(self.f4,relief=SUNKEN,bd=3)
self.f3.pack(side=LEFT,fill=X)
self.sc1=Scale(self.f3,{'from':0,'to':300}, label="Updateintervall (sek.)",showvalue=YES,orient=HORIZONTAL,length=300,tickinterval=50,command=self.setupint)
self.sc1.pack(side=LEFT)
self.sc1.set(self.upint)
from ampel import ampel
self.f5=Frame(self.f4,relief=SUNKEN,bd=3)
self.f5.pack(side=RIGHT)
self.amp=ampel(self.f5,20)
self.amp.pack("side=RIGHT")
self.amp.switch(3)
self.amp.bind("<1>",self.connchange)
self.lamp=Label(self.f5,text="Nicht verbunden")
self.lamp.pack(side=LEFT)
self.f1=Frame(self.master)
self.f1.pack(side=RIGHT,fill=X)
self.b4= Button(self.f1, text='Quit', command=self.ende)
self.b4.pack(side=RIGHT)
def ende(self):
if self.connected==1:
self.br_disconnect()
raise SystemExit
def connchange(self,event):
typ=self.amp.which(event.x,event.y)
if typ==3:
if self.connected==0:
self.br_connect()
elif typ==2:
if self.connected==0:
self.br_connect()
self.brupdate()
else:
if self.connected==1:
self.br_disconnect()
def setupint(self,x):
self.upint=self.sc1.get()
def action(self,event):
x=event.x
y=event.y
for i in self.widgets.keys():
a=self.widgets[i]
xo=a[8][0]
yo=a[8][1]
if (x >=xo) and (x<=xo+100) and (y>=yo) and (y<=yo+100):
obj=i
self.olist=[]
clist=string.split(self.confstr,";")
if string.split(clist[0][2:-2],",")[0]==obj:
list=string.split(clist[0][2:-2],",")
self.olist=["Broker",list[0],self.host,str(self.port),list[1],list[2],list[3]]
else:
spl=eval(clist[1])
for j in spl.keys():
if spl[j][1]==obj:
self.olist=["Speicher",spl[j][1],spl[j][2],spl[j][3],spl[j][4],"0","0"]
if self.olist==[]:
afl=eval(clist[2])
for j in afl.keys():
if afl[j][1]==obj:
self.olist=["Auftrag",afl[j][1],afl[j][2],afl[j][3],afl[j][4],"0","0"]
self.iw=Tk()
if1=Frame(self.iw)
if1.pack(fill=X)
if2=Frame(self.iw)
if2.pack(side=BOTTOM)
ml1=Label(if1,text="Objekttyp : "+self.olist[0])
ml2=Label(if1,text="Objektname : "+self.olist[1])
ml3=Label(if1,text="Hostname : "+self.olist[2])
ml4=Label(if1,text="Port : "+self.olist[3])
self.objstate=self.olist[4]
ml5=Frame(if1)
self.vcb1=StringVar()
rb1=Radiobutton(ml5,text="Laufend",value="r",variable=self.vcb1,command=self.sv_1)
rb2=Radiobutton(ml5,text="Angehalten",value="s",variable=self.vcb1,command=self.sv_2)
rb3=Radiobutton(ml5,text="Terminiert",value="t",variable=self.vcb1,command=self.sv_3)
rb1.pack(side=LEFT)
rb2.pack(side=LEFT)
rb3.pack(side=LEFT)
if self.objstate=="run":
rb1.select()
self.vcbt="run"
elif self.objstate=="paused":
rb2.select()
self.vcbt="paused"
else:
rb3.select()
ml6=Label(if1,text="Aufträge : "+self.olist[5])
if self.olist[0]=="Broker":
ml7=Label(if1,text="offene Auftr. : "+self.olist[6])
elif self.olist[0]=="Speicher":
ml7=Label(if1,text="Auslastung : "+self.olist[6])
else:
ml7=Label(if1,text="")
bl1=Button(if2,text="OK",width=10,command=self.setobj_parms)
bl2=Button(if2,text="Cancel",width=10,command=self.iw.destroy)
ml1.pack(anchor="w")
ml2.pack(anchor="w")
ml3.pack(anchor="w")
ml4.pack(anchor="w")
ml5.pack(anchor="w")
ml6.pack(anchor="w")
ml7.pack(anchor="w")
bl1.pack(side=LEFT)
bl2.pack(side=LEFT)
self.iw.wait_window(self.iw)
del self.iw
def sv_1(self):
self.vcbt="run"
def sv_2(self):
self.vcbt="paused"
def sv_3(self):
self.vcbt="term"
def setobj_parms(self):
self.vcb1=self.vcbt
self.objstate= self.vcb1
commandstr="CMD_CHGST "+self.olist[1]+" "+self.objstate
if self.connected==1:
self.conn.send(commandstr)
self.iw.destroy()
del self.olist, self.objstate, self.vcb1, self.vcbt
def refresh(self,confstr):
for i in self.widgets.keys():
for j in range(0,8):
w=self.widgets[i][j]
self.draw.delete(w)
self.widgets={}
self.sp_anz=0
self.br_anz=0
self.if_anz=0
self.master.update_idletasks()
clist=string.split(confstr,";")
brlist=string.split(clist[0][2:-2],",")
brstr="X,"+brlist[0]+","+self.host+","+str(self.port)+","+brlist[1]+","+brlist[2]+","+brlist[3]
self.broker(brstr)
spdict=eval(clist[1])
for i in spdict.keys():
spl=spdict[i]
spstr=str(spl[0])+","+spl[1]+","+spl[2]+","+spl[3]+","+spl[4]+",0,0"
self.speicher(spstr)
ifdict=eval(clist[2])
for i in ifdict.keys():
ifl=ifdict[i]
def auftrag(self,confstr=""):
y=10
x=self.if_anz*105+5
if confstr!="":
clist=string.split(confstr,",")
else:
clist=[" "," "," "," "," "," "," "]
n=str(clist[1])
h=str(clist[2])
p=str(clist[3])
s=str(clist[4])
c=str(clist[5])
s0=self.draw.create_rectangle(x,y,x+100,y+100,fill='lightGreen')
s1=self.draw.create_text(x+1,y+10,text="Name:"+n,anchor='w',width=95)
s2=self.draw.create_text(x+1,y+25,text="Host:"+h,anchor='w',width=95)
s3=self.draw.create_text(x+1,y+40,text="Port:"+p,anchor='w',width=95)
s4=self.draw.create_text(x+1,y+55,text="State:"+s,anchor='w',width=95)
s5=self.draw.create_text(x+1,y+70,text="Count:"+c,anchor='w',width=95)
self.if_anz=self.if_anz+1
s6=""
s7=self.draw.create_line(x+50,y+100,self.br_anz*50+5,150,fill="yellow",width="5",arrow=BOTH)
s8=(x,y)
self.widgets[n]=(s0,s1,s2,s3,s4,s5,s6,s7,s8)
def speicher(self,confstr=""):
y=290
x=self.sp_anz*105+5
if confstr!="":
clist=string.split(confstr,",")
else:
clist=[" "," "," "," "," "," "," "]
n=str(clist[1])
h=str(clist[2])
p=str(clist[3])
s=str(clist[4])
c=str(clist[5])
l=str(clist[6])
s0=self.draw.create_rectangle(x,y,x+100,y+100,fill='moccasin')
s1=self.draw.create_text(x+1,y+10,text="Name :"+n,anchor='w',width=90)
s2=self.draw.create_text(x+1,y+25,text="Host :"+h,anchor='w',width=90)
s3=self.draw.create_text(x+1,y+40,text="Port :"+p,anchor='w',width=90)
s4=self.draw.create_text(x+1,y+55,text="State:"+s,anchor='w',width=90)
s5=self.draw.create_text(x+1,y+70,text="Count:"+c,anchor='w',width=90)
s6=self.draw.create_text(x+1,y+85,text="Load :"+l,anchor='w',width=90)
self.sp_anz=self.sp_anz+1
s7=self.draw.create_line(x+50,y,self.br_anz*50+5,250,fill="white",width="5",arrow=BOTH)
s8=(x,y)
self.widgets[n]=(s0,s1,s2,s3,s4,s5,s6,s7,s8)
def broker(self,confstr=""):
y=150
x=self.br_anz*105+5
if confstr!="":
clist=string.split(confstr,",")
else:
clist=[" "," "," "," "," "," "," "]
n=str(clist[1])
h=str(clist[2])
p=str(clist[3])
s=str(clist[4])
c=str(clist[5])
q=str(clist[6])
s0=self.draw.create_rectangle(x,y,x+100,y+100,fill='Red')
s1=self.draw.create_text(x+1,y+10,text="Name : "+n,anchor='w')
s2=self.draw.create_text(x+1,y+25,text="Host : "+h,anchor='w')
s3=self.draw.create_text(x+1,y+40,text="Port : "+p,anchor='w')
s4=self.draw.create_text(x+1,y+55,text="State : "+s,anchor='w')
s5=self.draw.create_text(x+1,y+70,text="Count : "+c,anchor='w')
s6=self.draw.create_text(x+1,y+85,text="Queued: "+q,anchor='w')
self.br_anz=self.br_anz+1
s7=""
s8=(x,y)
self.widgets[n]=(s0,s1,s2,s3,s4,s5,s6,s7,s8)
def info1(self):
iw=Tk()
if1=Frame(iw)
if1.pack()
m1=Message(if1,text="Monitor V0.99\n\n (c) by Hans Matzen, 1997,\n
Germany, Frankfurt/M\n email: hans@tm.informatik.uni-frankfurt.de",
width="10c")
b1=Button(if1,text="OK",command=iw.destroy)
m1.pack()
b1.pack()
iw.wait_window(iw)
del iw
def br_parms(self):
self.iw=Tk()
self.if1=Frame(self.iw)
self.if1.pack()
self.if2=Frame(self.iw)
self.if2.pack(side=BOTTOM)
self.ml1=Label(self.if1,text="Hostname:")
self.ml2=Entry(self.if1,width=30)
self.ml3=Label(self.if1,text="Port:")
self.ml4=Entry(self.if1,width=30)
self.bl1=Button(self.if2,text="OK",width=10,command=self.setbr_parms)
self.bl2=Button(self.if2,text="Cancel",width=10,command=self.iw.destroy)
self.ml1.pack()
self.ml2.pack()
self.ml3.pack()
self.ml4.pack()
self.bl1.pack(side=LEFT)
self.bl2.pack(side=LEFT)
self.ml2.insert(END,self.host)
self.ml4.insert(END,str(self.port))
self.iw.wait_window(self.iw)
def setbr_parms(self):
self.host=self.ml2.get()
self.port=eval(self.ml4.get())
self.iw.destroy()
del self.iw
def br_connect(self):
if self.connected==0:
try:
self.conn.connect(self.host,self.port)
self.lamp.config(text="Verbunden")
self.amp.switch(1)
self.connected=1
self.master.after(self.upint*1000,self.brupdate)
except:
import time
self.lamp.config(text="Verbindungsfehler")
self.amp.switch(2)
self.master.update_idletasks()
time.sleep(3)
self.lamp.config(text="Nicht verbunden")
self.amp.switch(3)
def brupdate(self):
if self.connected==1:
self.conn.send("CMD_CONFGET")
self.confstr=self.conn.recv(8192)
self.refresh(self.confstr)
self.amp.switch(1)
self.master.update_idletasks()
self.master.after(self.upint*1000,self.brupdate)
def br_disconnect(self):
if self.connected==1:
self.conn.send("QUIT")
self.conn.close()
del self.conn
self.conn=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.connected=0
self.lamp.config(text="Nicht verbunden")
self.amp.switch(3)
def br_stop(self):
if self.connectd==1:
self.conn.send("CMD_PAUSE")
def br_cont(self):
if self.connectd==1:
self.conn.send("CMD_CONT")
def br_mapcommand(self,job,spname):
if self.connectd==1:
self.conn.send("CMD_MAPJOB "+str(job)+" "+str(spname))
def br_man(self):
if self.connectd==1:
self.conn.send("CMD_MAN")
def br_auto(self):
if self.connectd==1:
self.conn.send("CMD_AUTO")
root=Tk()
to=moni(root)
root.mainloop()