1
2 import win32serviceutil, win32service
3 from optparse import OptionParser
4 from blaster import Blaster
5 from filesender import BlasterCallback_i
6 import logging, sys
7
8 class XMLBlasterFileSenderService(win32serviceutil.ServiceFramework):
9 _svc_name_ = "XMLBlasterFileSenderService"
10 _svc_display_name_ = "XMLBlaster File Sender Service"
11
12 def __init__(self, args):
13 win32serviceutil.ServiceFramework.__init__(self, args)
14 parser = OptionParser("usage: \%prog [-d <directory>] [-s <server>] [-l <login>] [-p <password>]")
15 parser.add_option("-d", "--directory", dest="directory", help="upload directory", default=".\\")
16 parser.add_option("-s", "--server", dest="server", help="auth server host", default="localhost")
17 parser.add_option("-l", "--login", dest="login", help="login", default="")
18 parser.add_option("-p", "--password", dest="password", help="password", default="")
19 parser.add_option("-c", "--config", dest="config", help="config file", default="")
20 parser.add_option("-f", "--filelog", dest="filelog", help="log file name", default="service.log")
21 argv = eval(win32serviceutil.GetServiceCustomOption(XMLBlasterFileSenderService, "argv", "[]"))
22 (self.options, args) = parser.parse_args(argv)
23 self.setupLog()
24 logging.info("argv=%s", argv)
25
26 def setupLog(self):
27 from warnings import filterwarnings
28 filterwarnings("ignore", category = DeprecationWarning, module = "logging")
29 if self.options.config:
30 logging.fileConfig(self.options.config)
31 else:
32 hdlr = logging.FileHandler(self.options.filelog)
33 fmt = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
34 hdlr.setFormatter(fmt)
35 logging.getLogger().addHandler(hdlr)
36 logging.getLogger().setLevel(logging.INFO)
37
38 def SvcDoRun(self):
39 import servicemanager
40 self.sender = Blaster(self.options.server, 3412, [])
41 try:
42 logging.info("login as %s", self.options.login)
43 callback = BlasterCallback_i(self.options)
44 self.sender.login(self.options.login, self.options.password, callback)
45 logging.info("sessionId: %s", self.sender.sessionId)
46 logging.info("serverIOR: %s", self.sender.serverIOR)
47 logging.info("callbackIOR: %s", self.sender.callbackior)
48 logging.info("start uploading")
49 self.sender.orb.run()
50 logging.info("done")
51 self.ReportServiceStatus(win32service.SERVICE_STOPPED)
52 except:
53 logging.exception("fatal error")
54
55 def SvcStop(self):
56 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
57 logging.info("logout")
58 self.sender.logout()
59 logging.info("stop uploading")
60 self.sender.orb.shutdown(True)
61
62 def SvcPause(self):
63 logging.info("pause")
64 self.sender.poa._get_the_POAManager().hold_requests(True)
65 self.ReportServiceStatus(win32service.SERVICE_PAUSED)
66
67 def SvcContinue(self):
68 logging.info("continue")
69 self.sender.poa._get_the_POAManager().activate()
70 self.ReportServiceStatus(win32service.SERVICE_RUNNING)
71
72 if __name__=='__main__':
73 win32serviceutil.HandleCommandLine(XMLBlasterFileSenderService)
syntax highlighted by Code2HTML, v. 0.9.1