Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 15 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 16 | import grpc |
| 17 | from concurrent import futures |
| 18 | from concurrent.futures import Future |
| 19 | from twisted.internet import reactor |
| 20 | from twisted.internet.defer import Deferred, inlineCallbacks, returnValue |
| 21 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 22 | from common.utils.asleep import asleep |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 23 | from google.protobuf.empty_pb2 import Empty |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 24 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 25 | from common.utils.grpc_utils import twisted_async |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 26 | from streaming_pb2 import add_ExperimentalServiceServicer_to_server, \ |
| 27 | AsyncEvent, ExperimentalServiceServicer, Echo |
| 28 | |
| 29 | |
| 30 | class ShutDown(object): |
| 31 | stop = False # semaphore for all loops to stop when this flag is set |
| 32 | |
| 33 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 34 | class ShuttingDown(Exception): pass |
| 35 | |
| 36 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 37 | class Service(ExperimentalServiceServicer): |
| 38 | |
| 39 | def __init__(self): |
| 40 | self.event_seq = 0 |
| 41 | |
| 42 | @twisted_async |
| 43 | @inlineCallbacks |
| 44 | def GetEcho(self, request, context): |
| 45 | print 'got Echo({}) request'.format(request.msg) |
| 46 | yield asleep(request.delay) |
| 47 | msg = request.msg + ' <<' |
| 48 | print ' Echo({}) reply'.format(msg) |
| 49 | returnValue(Echo(msg=msg)) |
| 50 | |
| 51 | @twisted_async |
| 52 | @inlineCallbacks |
| 53 | def get_next_event(self): |
| 54 | """called on the twisted thread""" |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 55 | yield asleep(0.000001) |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 56 | event = AsyncEvent(seq=self.event_seq, details='foo') |
| 57 | self.event_seq += 1 |
| 58 | returnValue(event) |
| 59 | |
| 60 | def ReceiveStreamedEvents(self, request, context): |
| 61 | """called on a thread-pool thread""" |
| 62 | print 'got ReceiveStreamedEvents request' |
| 63 | while 1: |
| 64 | if ShutDown.stop: |
| 65 | break |
| 66 | yield self.get_next_event() |
| 67 | |
| 68 | def ReceivePackets(self, request, context): |
| 69 | pass |
| 70 | |
| 71 | def SendPackets(self, request, context): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 72 | count = 0 |
| 73 | for _ in request: |
| 74 | count += 1 |
| 75 | if count % 1000 == 0: |
| 76 | print '%s got %d packets' % (20 * ' ', count) |
| 77 | return Empty() |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 78 | |
| 79 | |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 80 | if __name__ == '__main__': |
| 81 | thread_pool = futures.ThreadPoolExecutor(max_workers=10) |
| 82 | server = grpc.server(thread_pool) |
| 83 | add_ExperimentalServiceServicer_to_server(Service(), server) |
Zsolt Haraszti | e39523b | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 84 | server.add_insecure_port('[::]:50050') |
| 85 | server.start() |
| 86 | def shutdown(): |
| 87 | ShutDown.stop = True |
| 88 | thread_pool.shutdown(wait=True) |
| 89 | server.stop(0) |
| 90 | reactor.addSystemEventTrigger('before', 'shutdown', shutdown) |
| 91 | reactor.run() |