Exemple #1
0
    def _on_service_acceptance_event(self, event, server):
        """Handle a service invocation event."""
        service_acceptance = event.service_acceptance
        if service_acceptance is None:
            return

        call = service_acceptance.call
        call.accept(self._completion_queue, call)
        # TODO(nathaniel): Metadata support.
        call.premetadata()
        call.read(call)
        method = service_acceptance.method.decode()

        self._rpc_states[call] = _common.CommonRPCState(
            _common.WriteState(_LowWrite.OPEN, _common.HighWrite.OPEN,
                               []), 1, self._request_deserializers[method],
            self._response_serializers[method])

        ticket = tickets.FrontToBackPacket(
            call, 0, tickets.Kind.COMMENCEMENT, method,
            interfaces.ServicedSubscription.Kind.FULL, None, None,
            service_acceptance.deadline - time.time())
        self._rear_link.accept_front_to_back_ticket(ticket)

        server.service(None)
Exemple #2
0
    def _invoke(self, operation_id, name, high_state, payload, timeout):
        """Invoke an RPC.

    Args:
      operation_id: Any object to be used as an operation ID for the RPC.
      name: The RPC method name.
      high_state: A _common.HighWrite value representing the "high write state"
        of the RPC.
      payload: A payload object for the RPC or None if no payload was given at
        invocation-time.
      timeout: A duration of time in seconds to allow for the RPC.
    """
        request_serializer = self._request_serializers[name]
        call = _low.Call(self._channel, self._completion_queue, name,
                         self._host,
                         time.time() + timeout)
        if self._metadata_transformer is not None:
            metadata = self._metadata_transformer([])
            for metadata_key, metadata_value in metadata:
                call.add_metadata(metadata_key, metadata_value)
        call.invoke(self._completion_queue, operation_id, operation_id)
        outstanding = set(_INVOCATION_EVENT_KINDS)

        if payload is None:
            if high_state is _common.HighWrite.CLOSED:
                call.complete(operation_id)
                low_state = _LowWrite.CLOSED
                outstanding.add(_low.Event.Kind.COMPLETE_ACCEPTED)
            else:
                low_state = _LowWrite.OPEN
        else:
            serialized_payload = request_serializer(payload)
            call.write(serialized_payload, operation_id)
            outstanding.add(_low.Event.Kind.WRITE_ACCEPTED)
            low_state = _LowWrite.ACTIVE

        write_state = _common.WriteState(low_state, high_state, [])
        common_state = _common.CommonRPCState(
            write_state, 0, self._response_deserializers[name],
            request_serializer)
        self._rpc_states[operation_id] = _RPCState(call, outstanding, True,
                                                   common_state)

        if not self._spinning:
            self._pool.submit(self._spin, self._completion_queue)
            self._spinning = True