| 1 |
# Copyright 2017 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import grpc |
| 16 |
from grpc_testing import _common |
| 17 |
|
| 18 |
|
| 19 |
class ServicerContext(grpc.ServicerContext): |
| 20 |
|
| 21 |
def __init__(self, rpc, time, deadline): |
| 22 |
self._rpc = rpc |
| 23 |
self._time = time |
| 24 |
self._deadline = deadline |
| 25 |
|
| 26 |
def is_active(self): |
| 27 |
return self._rpc.is_active() |
| 28 |
|
| 29 |
def time_remaining(self): |
| 30 |
if self._rpc.is_active(): |
| 31 |
if self._deadline is None: |
| 32 |
return None |
| 33 |
else: |
| 34 |
return max(0.0, self._deadline - self._time.time()) |
| 35 |
else: |
| 36 |
return 0.0 |
| 37 |
|
| 38 |
def cancel(self): |
| 39 |
self._rpc.application_cancel() |
| 40 |
|
| 41 |
def add_callback(self, callback): |
| 42 |
return self._rpc.add_callback(callback) |
| 43 |
|
| 44 |
def invocation_metadata(self): |
| 45 |
return self._rpc.invocation_metadata() |
| 46 |
|
| 47 |
def peer(self): |
| 48 |
raise NotImplementedError() |
| 49 |
|
| 50 |
def peer_identities(self): |
| 51 |
raise NotImplementedError() |
| 52 |
|
| 53 |
def peer_identity_key(self): |
| 54 |
raise NotImplementedError() |
| 55 |
|
| 56 |
def auth_context(self): |
| 57 |
raise NotImplementedError() |
| 58 |
|
| 59 |
def set_compression(self): |
| 60 |
raise NotImplementedError() |
| 61 |
|
| 62 |
def send_initial_metadata(self, initial_metadata): |
| 63 |
initial_metadata_sent = self._rpc.send_initial_metadata( |
| 64 |
_common.fuss_with_metadata(initial_metadata)) |
| 65 |
if not initial_metadata_sent: |
| 66 |
raise ValueError( |
| 67 |
'ServicerContext.send_initial_metadata called too late!') |
| 68 |
|
| 69 |
def disable_next_message_compression(self): |
| 70 |
raise NotImplementedError() |
| 71 |
|
| 72 |
def set_trailing_metadata(self, trailing_metadata): |
| 73 |
self._rpc.set_trailing_metadata( |
| 74 |
_common.fuss_with_metadata(trailing_metadata)) |
| 75 |
|
| 76 |
def abort(self, code, details): |
| 77 |
with self._rpc._condition: |
| 78 |
self._rpc._abort(code, details) |
| 79 |
raise Exception() |
| 80 |
|
| 81 |
def abort_with_status(self, status): |
| 82 |
raise NotImplementedError() |
| 83 |
|
| 84 |
def set_code(self, code): |
| 85 |
self._rpc.set_code(code) |
| 86 |
|
| 87 |
def set_details(self, details): |
| 88 |
self._rpc.set_details(details) |