Ejemplo n.º 1
0
    def _on_service_acceptance_event(self, event, server):
        server.service(None)

        service_acceptance = event.service_acceptance
        call = service_acceptance.call
        call.accept(self._completion_queue, call)
        try:
            group, method = service_acceptance.method.split('/')[1:3]
        except ValueError:
            logging.info('Illegal path "%s"!', service_acceptance.method)
            return
        request_deserializer = self._request_deserializers.get((group, method),
                                                               _IDENTITY)
        response_serializer = self._response_serializers.get((group, method),
                                                             _IDENTITY)

        call.read(call)
        context = _Context(call)
        self._rpc_states[call] = _RPCState(request_deserializer,
                                           response_serializer, 1,
                                           _Read.READING, None, 1,
                                           _HighWrite.OPEN, _LowWrite.OPEN,
                                           False, None, None, None,
                                           set((
                                               _READ,
                                               _FINISH,
                                           )), context)
        protocol = links.Protocol(links.Protocol.Kind.SERVICER_CONTEXT,
                                  context)
        ticket = links.Ticket(call, 0, group, method,
                              links.Ticket.Subscription.FULL,
                              service_acceptance.deadline - time.time(), None,
                              event.metadata, None, None, None, None, None,
                              protocol)
        self._relay.add_value(ticket)
Ejemplo n.º 2
0
 def kick_off(self, group, method, timeout, protocol_options,
              initial_metadata, payload, completion, allowance):
     """See _interfaces.TransmissionManager.kickoff for specification."""
     # TODO(nathaniel): Support other subscriptions.
     subscription = links.Ticket.Subscription.FULL
     terminal_metadata, code, message, termination = _explode_completion(
         completion)
     self._remote_allowance = 1 if payload is None else 0
     protocol = links.Protocol(links.Protocol.Kind.CALL_OPTION,
                               protocol_options)
     ticket = links.Ticket(self._operation_id, 0, group, method,
                           subscription, timeout, allowance,
                           initial_metadata, payload, terminal_metadata,
                           code, message, termination, protocol)
     self._lowest_unused_sequence_number = 1
     self._transmit(ticket)
Ejemplo n.º 3
0
    def _invoke(self, operation_id, group, method, initial_metadata, payload,
                termination, timeout, allowance, options):
        """Invoke an RPC.

    Args:
      operation_id: Any object to be used as an operation ID for the RPC.
      group: The group to which the RPC method belongs.
      method: The RPC method name.
      initial_metadata: The initial metadata object for the RPC.
      payload: A payload object for the RPC or None if no payload was given at
        invocation-time.
      termination: A links.Ticket.Termination value or None indicated whether or
        not more writes will follow from this side of the RPC.
      timeout: A duration of time in seconds to allow for the RPC.
      allowance: The number of payloads (beyond the free first one) that the
        local ticket exchange mate has granted permission to be read.
      options: A beta_interfaces.GRPCCallOptions value or None.
    """
        if termination is links.Ticket.Termination.COMPLETION:
            high_write = _HighWrite.CLOSED
        elif termination is None:
            high_write = _HighWrite.OPEN
        else:
            return

        transformed_initial_metadata = self._metadata_transformer(
            initial_metadata)
        request_serializer = self._request_serializers.get((group, method),
                                                           _IDENTITY)
        response_deserializer = self._response_deserializers.get(
            (group, method), _IDENTITY)

        call = _intermediary_low.Call(self._channel, self._completion_queue,
                                      '/%s/%s' % (group, method), self._host,
                                      time.time() + timeout)
        if options is not None and options.credentials is not None:
            call.set_credentials(options.credentials._low_credentials)
        if transformed_initial_metadata is not None:
            for metadata_key, metadata_value in transformed_initial_metadata:
                call.add_metadata(metadata_key, metadata_value)
        call.invoke(self._completion_queue, operation_id, operation_id)
        if payload is None:
            if high_write is _HighWrite.CLOSED:
                call.complete(operation_id)
                low_write = _LowWrite.CLOSED
                due = set((
                    _METADATA,
                    _COMPLETE,
                    _FINISH,
                ))
            else:
                low_write = _LowWrite.OPEN
                due = set((
                    _METADATA,
                    _FINISH,
                ))
        else:
            if options is not None and options.disable_compression:
                flags = _intermediary_low.WriteFlags.WRITE_NO_COMPRESS
            else:
                flags = 0
            call.write(request_serializer(payload), operation_id, flags)
            low_write = _LowWrite.ACTIVE
            due = set((
                _WRITE,
                _METADATA,
                _FINISH,
            ))
        context = _Context()
        self._rpc_states[operation_id] = _RPCState(
            call, request_serializer, response_deserializer, 1,
            _Read.AWAITING_METADATA, 1 if allowance is None else
            (1 + allowance), high_write, low_write, due, context)
        protocol = links.Protocol(links.Protocol.Kind.INVOCATION_CONTEXT,
                                  context)
        ticket = links.Ticket(operation_id, 0, None, None, None, None, None,
                              None, None, None, None, None, None, protocol)
        self._relay.add_value(ticket)