Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 3 | import grpc |
| 4 | from concurrent import futures |
| 5 | from concurrent.futures import Future |
| 6 | from twisted.internet import reactor |
| 7 | from twisted.internet.defer import Deferred, inlineCallbacks, returnValue |
| 8 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 9 | from common.utils.asleep import asleep |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 10 | from google.protobuf.empty_pb2 import Empty |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 11 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 12 | from common.utils.grpc_utils import twisted_async |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 13 | from streaming_pb2 import add_ExperimentalServiceServicer_to_server, \ |
| 14 | AsyncEvent, ExperimentalServiceServicer, Echo |
| 15 | |
| 16 | |
| 17 | class ShutDown(object): |
| 18 | stop = False # semaphore for all loops to stop when this flag is set |
| 19 | |
| 20 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 21 | class ShuttingDown(Exception): pass |
| 22 | |
| 23 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 24 | class Service(ExperimentalServiceServicer): |
| 25 | |
| 26 | def __init__(self): |
| 27 | self.event_seq = 0 |
| 28 | |
| 29 | @twisted_async |
| 30 | @inlineCallbacks |
| 31 | def GetEcho(self, request, context): |
| 32 | print 'got Echo({}) request'.format(request.msg) |
| 33 | yield asleep(request.delay) |
| 34 | msg = request.msg + ' <<' |
| 35 | print ' Echo({}) reply'.format(msg) |
| 36 | returnValue(Echo(msg=msg)) |
| 37 | |
| 38 | @twisted_async |
| 39 | @inlineCallbacks |
| 40 | def get_next_event(self): |
| 41 | """called on the twisted thread""" |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 42 | yield asleep(0.000001) |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 43 | event = AsyncEvent(seq=self.event_seq, details='foo') |
| 44 | self.event_seq += 1 |
| 45 | returnValue(event) |
| 46 | |
| 47 | def ReceiveStreamedEvents(self, request, context): |
| 48 | """called on a thread-pool thread""" |
| 49 | print 'got ReceiveStreamedEvents request' |
| 50 | while 1: |
| 51 | if ShutDown.stop: |
| 52 | break |
| 53 | yield self.get_next_event() |
| 54 | |
| 55 | def ReceivePackets(self, request, context): |
| 56 | pass |
| 57 | |
| 58 | def SendPackets(self, request, context): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 59 | count = 0 |
| 60 | for _ in request: |
| 61 | count += 1 |
| 62 | if count % 1000 == 0: |
| 63 | print '%s got %d packets' % (20 * ' ', count) |
| 64 | return Empty() |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 65 | |
| 66 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 67 | if __name__ == '__main__': |
| 68 | thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 69 | server = grpc.server(thread_pool) |
| 70 | add_ExperimentalServiceServicer_to_server(Service(), server) |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 71 | server.add_insecure_port('[::]:50050') |
| 72 | server.start() |
| 73 | def shutdown(): |
| 74 | ShutDown.stop = True |
| 75 | thread_pool.shutdown(wait=True) |
| 76 | server.stop(0) |
| 77 | reactor.addSystemEventTrigger('before', 'shutdown', shutdown) |
| 78 | reactor.run() |