Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import logging |
| 5 | sys.path.append('../../../target/gen-py') |
| 6 | |
| 7 | from packetstreamer import PacketStreamer |
| 8 | from packetstreamer.ttypes import * |
| 9 | |
| 10 | from thrift.transport import TSocket |
| 11 | from thrift.transport import TTransport |
| 12 | from thrift.protocol import TBinaryProtocol |
| 13 | from thrift.server import TServer |
| 14 | |
| 15 | class PacketStreamerHandler: |
| 16 | def __init__(self): |
| 17 | logging.handlers.codecs = None |
| 18 | self.log = logging.getLogger("packetstreamer") |
| 19 | self.log.setLevel(logging.DEBUG) |
| 20 | handler = logging.handlers.SysLogHandler("/dev/log") |
| 21 | handler.setFormatter(logging.Formatter("%(name)s: %(levelname)s %(message)s")) |
| 22 | self.log.addHandler(handler) |
| 23 | |
| 24 | def ping(self): |
| 25 | self.log.debug('ping()') |
| 26 | return true |
| 27 | |
| 28 | def pushPacketSync(self, packet): |
| 29 | self.log.debug('receive a packet synchronously: %s' %(packet)) |
| 30 | return 0 |
| 31 | |
| 32 | def pushPacketAsync(self, packet): |
| 33 | self.log.debug('receive a packet Asynchronously: %s' %(packet)) |
| 34 | |
| 35 | handler = PacketStreamerHandler() |
| 36 | processor = PacketStreamer.Processor(handler) |
| 37 | transport = TSocket.TServerSocket(9090) |
| 38 | tfactory = TTransport.TBufferedTransportFactory() |
| 39 | pfactory = TBinaryProtocol.TBinaryProtocolFactory() |
| 40 | |
| 41 | server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) |
| 42 | |
| 43 | # You could do one of these for a multithreaded server |
| 44 | #server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) |
| 45 | #server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory) |
| 46 | |
| 47 | print 'Starting the server...' |
| 48 | server.serve() |
| 49 | print 'done.' |