1
2 import sys, os, glob, shutil, logging, blaster
3 from xml.dom import minidom
4 from clientIdl__POA import BlasterCallback
5
6 class BlasterCallback_i(BlasterCallback):
7
8 def __init__(self, options):
9 self.options = options
10
11 def update(self, sessionId, msgUnitArr):
12 logging.info("update. sessionId=%s", sessionId)
13 result = []
14 for msgUnit in msgUnitArr:
15 try:
16 logging.info("%s%s", msgUnit.xmlKey, msgUnit.qos)
17 dom = minidom.parseString(msgUnit.xmlKey)
18 oid = dom.getElementsByTagName("key")[0].getAttribute("oid").encode()
19 addresses = dom.getElementsByTagName("address")
20 if addresses.length > 0:
21 address = addresses[0].firstChild.nodeValue
22 else:
23 address = self.options.directory
24 file = os.path.join(address, oid)
25 logging.info("save content to %s", file)
26 open(file, "wb").write(msgUnit.content)
27 result.append("<qos><state id='OK'/></qos>")
28 except:
29 logging.exception("failed to upload message")
30 result.append("<qos><state id='ERROR'/></qos>")
31 return result
32
33 def updateOneway(self, sessionId, msgUnitArr):
34 logging.info("updateOneway. sessionId=%s", sessionId)
35 for msgUnit in msgUnitArr:
36 logging.info("%s%s", msgUnit.xmlKey, msgUnit.qos)
37
38 def ping(self, qos):
39 logging.info("ping. qos=%s", qos)
40 return ""
41
42 def setupLog(options):
43 from warnings import filterwarnings
44 filterwarnings("ignore", category = DeprecationWarning, module = "logging")
45 hdlr = logging.StreamHandler()
46 fmt = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
47 hdlr.setFormatter(fmt)
48 logging.getLogger().addHandler(hdlr)
49 logging.getLogger().setLevel(logging.INFO)
50
51 def main(argv):
52 from optparse import OptionParser
53 parser = OptionParser("usage: \%prog [-d <directory>] [-s <server>] [-l <login>] [-p <password>]")
54 parser.add_option("-d", "--directory", dest="directory", help="upload directory", default=".\\")
55 parser.add_option("-s", "--server", dest="server", help="auth server host", default="localhost")
56 parser.add_option("-l", "--login", dest="login", help="login", default="")
57 parser.add_option("-p", "--password", dest="password", help="password", default="")
58 (options, args) = parser.parse_args()
59 setupLog(options)
60 uploader = blaster.Blaster(options.server, 3412, [])
61 try:
62 logging.info("login as %s", options.login)
63 callback = BlasterCallback_i(options)
64 uploader.login(options.login, options.password, callback)
65 try:
66 logging.info("sessionId: %s", uploader.sessionId)
67 logging.info("serverIOR: %s", uploader.serverIOR)
68 logging.info("callbackIOR: %s", uploader.callbackior)
69 logging.info("start uploading")
70 uploader.orb.run()
71 finally:
72 logging.info("logout")
73 uploader.logout()
74 except KeyboardInterrupt, ex:
75 logging.info("stop uploading")
76 uploader.orb.shutdown(True)
77 logging.info("done")
78
79 if __name__ == '__main__':
80 main(sys.argv)
syntax highlighted by Code2HTML, v. 0.9.1