blob: 53f6846db57dcc89958cdf2aa66e8d2da3caa60b [file] [log] [blame]
Mark Sleec98d0502006-09-06 02:42:25 +00001import sys
2import traceback
3
Mark Sleec9676562006-09-05 17:34:52 +00004from thrift.Thrift import TProcessor
5from thrift.transport import TTransport
6
7class TServer:
8
9 """Base interface for a server, which must have a run method."""
10
Mark Sleed788b2e2006-09-07 01:26:35 +000011 def __init__(self, processor, serverTransport, transportFactory=None):
12 self.processor = processor
13 self.serverTransport = serverTransport
14 if transportFactory == None:
15 self.transportFactory = TTransport.TTransportFactoryBase()
16 else:
17 self.transportFactory = transportFactory
Mark Sleec9676562006-09-05 17:34:52 +000018
19 def run(self):
20 pass
21
22class TSimpleServer(TServer):
23
24 """Simple single-threaded server that just pumps around one transport."""
25
Mark Sleed788b2e2006-09-07 01:26:35 +000026 def __init__(self, processor, serverTransport, transportFactory=None):
27 TServer.__init__(self, processor, serverTransport, transportFactory)
Mark Sleec9676562006-09-05 17:34:52 +000028
29 def run(self):
Mark Sleed788b2e2006-09-07 01:26:35 +000030 self.serverTransport.listen()
Mark Sleec9676562006-09-05 17:34:52 +000031 while True:
Mark Sleed788b2e2006-09-07 01:26:35 +000032 client = self.serverTransport.accept()
33 (input, output) = self.transportFactory.getIOTransports(client)
Mark Sleec9676562006-09-05 17:34:52 +000034 try:
35 while True:
Mark Sleed788b2e2006-09-07 01:26:35 +000036 self.processor.process(input, output)
Mark Sleec9676562006-09-05 17:34:52 +000037 except Exception, x:
Mark Sleec98d0502006-09-06 02:42:25 +000038 print '%s, %s, %s' % (type(x), x, traceback.format_exc())
Mark Sleec9676562006-09-05 17:34:52 +000039 print 'Client died.'
Mark Sleed788b2e2006-09-07 01:26:35 +000040
41 input.close()
42 output.close()