ProxyManager.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2005
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "Manages diagnostic, telemetry and science proxies"
00012 __author__   = "<perazzo@slac.stanford.edu>"
00013 __updated__  = "$Date: 2006/03/16 23:55:34 $"
00014 __version__  = "$Revision: 1.3 $"
00015 __release__  = "$Name: HEAD $"
00016 __credits__  = "SLAC"
00017 
00018 import sys
00019 import os
00020 import signal
00021 
00022 from qt import QObject, QApplication, SIGNAL, SLOT
00023 
00024 from ProxyManagerGui import ProxyManagerGui
00025 from LICOS.util.gOptions import Options
00026 
00027 class ProxyParams(object):
00028   def __init__(self, label, button, script):
00029     self.label = label
00030     self.button = button
00031     self.script = script
00032     self.pid = None
00033 
00034 class ProxyManager(ProxyManagerGui):
00035   def __init__(self, app, vscip, config):
00036     ProxyManagerGui.__init__(self)
00037     self.setCaption("Proxy Manager")
00038     self.invscip.setText(vscip)
00039     self.inconfig.setText(config)
00040     self.__proxies = {
00041       'diagnostic': ProxyParams(self.status_dia, self.start_dia,
00042                                 'telemetry/DiagnosticProxy.py'),
00043       'telemetry':  ProxyParams(self.status_tlm, self.start_tlm,
00044                                 'telemetry/TelemetryProxy.py'),
00045       'science':    ProxyParams(self.status_sci, self.start_sci,
00046                                 'science/ScienceProxy.py')
00047       }
00048     self.startTimer(5000)
00049     self.__app = app
00050     
00051   def Exit(self):
00052     self.StopClicked()
00053     self.__app.quit()
00054 
00055   def StartClicked(self):
00056     for proxy in self.__proxies.values():
00057       if proxy.pid is None:
00058         self.__start(proxy)
00059 
00060   def StopClicked(self):
00061     for proxy in self.__proxies.values():
00062       if proxy.pid is not None:
00063         self.__stop(proxy)
00064 
00065   def DiaClicked(self):
00066     self.__clicked('diagnostic')
00067 
00068   def TlmClicked(self):
00069     self.__clicked('telemetry')
00070 
00071   def SciClicked(self):
00072     self.__clicked('science')
00073 
00074   def __clicked(self, which):
00075     proxy = self.__proxies[which]
00076     if proxy.pid is None:
00077       self.__start(proxy)
00078     else:
00079       self.__stop(proxy)
00080 
00081   def __start(self, proxy):
00082     command = os.path.expandvars('$ONLINE_PACKAGES/bin/python')
00083     args = (os.path.expandvars('$ONLINE_ROOT/LICOS/%s'%(proxy.script)),
00084             '--vscip', self.invscip.text().ascii(),
00085             '--config', os.path.expandvars(self.inconfig.text().ascii()),
00086             '--logtime')
00087     proxy.pid = os.spawnv(os.P_NOWAIT, command, (command,)+args)
00088     proxy.label.setText('<font color=blue>running</font>')
00089     proxy.button.setText('Stop')
00090     proxy.button.setDown(True)
00091 
00092   def __stop(self, proxy):
00093     os.kill(proxy.pid, signal.SIGKILL)
00094     try:
00095       os.waitpid(proxy.pid, 0)
00096     except:
00097       pass
00098     proxy.pid = None
00099     proxy.label.setText('<font color=black>stopped</font>')
00100     proxy.button.setText('Start')
00101     proxy.button.setDown(False)
00102 
00103   def timerEvent(self, event):
00104     for proxy in self.__proxies.values():
00105       if proxy.pid is not None:
00106         (pid, status) = os.waitpid(proxy.pid, os.WNOHANG)
00107         # signum = status & 0xff
00108         # exitstatus = status >> 8
00109         if pid != 0:
00110           proxy.pid = None
00111           proxy.label.setText('<font color=red>crashed</font>')
00112           proxy.button.setText('Start')
00113           proxy.button.setDown(False)
00114         
00115 
00116 if __name__ == '__main__':
00117   options = Options(['vscip', 'config'], [], [])
00118   try:
00119     options.parse()
00120   except Exception, msg:
00121     options.usage(str(msg))
00122     sys.exit()
00123 
00124   app = QApplication(sys.argv)
00125   app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"))
00126   win = ProxyManager(app, options.vscip, options.config)
00127   win.show()
00128   app.exec_loop()

Generated on Thu Apr 27 20:52:42 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO