Пример #1
0
 /**
  * Makes a synchronous RPC call.
  * @param string $package Package to call
  * @param string $call_name Specific RPC call to make
  * @param string $request Request proto, serialised to string
  * @param string $response Response proto string to populate
  * @param double $deadline Optional deadline for the RPC call
  */
 public function makeSyncCall($package, $call_name, $request, $response, $deadline = null)
 {
     if ($deadline === null) {
         $deadline = 5;
     }
     $remote_request = new Request();
     $remote_request->setServiceName($package);
     $remote_request->setMethod($call_name);
     $remote_request->setRequest($request->serializeToString());
     $remote_request->setRequestId($this->requestId);
     $serialized_remote_request = $remote_request->serializeToString();
     $opts = array('http' => array('method' => 'POST', 'header' => "Content-type: application/octet-stream\r\n" . 'Content-Length: ' . strlen($serialized_remote_request) . "\r\n", 'content' => $serialized_remote_request));
     $context = stream_context_create($opts);
     $serialized_remote_respone = file_get_contents('http://localhost:' . $this->apiPort, false, $context);
     $remote_response = new Response();
     $remote_response->parseFromString($serialized_remote_respone);
     if ($remote_response->hasApplicationError()) {
         throw new ApplicationError($remote_response->getApplicationError()->getCode(), $remote_response->getApplicationError()->getDetail());
     }
     if ($remote_response->hasException() || $remote_response->hasJavaException()) {
         // This indicates a bug in the remote implementation.
         throw new RPCFailedError(sprintf('Remote implementation for %s.%s failed', $package, $call_name));
     }
     $response->parseFromString($remote_response->getResponse());
 }
Пример #2
0
 /**
  * Makes a synchronous RPC call.
  * @param string $package Package to call
  * @param string $call_name Specific RPC call to make
  * @param string $request Request proto, serialised to string
  * @param string $response Response proto string to populate
  * @param double $deadline Optional deadline for the RPC call in seconds.
  */
 public function makeSyncCall($package, $call_name, $request, $response, $deadline = null)
 {
     if ($deadline === null) {
         $deadline = self::DEFAULT_TIMEOUT_SEC;
     }
     $ticket = getenv(self::TICKET_HEADER);
     if ($ticket === false) {
         $ticket = getenv(self::DEV_TICKET_HEADER);
         if ($ticket === false) {
             $ticket = $this->getDefaultTicket();
         }
     }
     $remote_request = new Request();
     $remote_request->setServiceName($package);
     $remote_request->setMethod($call_name);
     $remote_request->setRequest($request->serializeToString());
     $remote_request->setRequestId($ticket);
     $serialized_remote_request = $remote_request->serializeToString();
     $headers = [self::SERVICE_DEADLINE_HEADER => $deadline, self::SERVICE_ENDPOINT_HEADER => self::SERVICE_ENDPOINT_NAME, self::SERVICE_METHOD_HEADER => self::APIHOST_METHOD, 'Content-Type' => self::RPC_CONTENT_TYPE];
     $dapper_header_value = getenv(self::DAPPER_ENV_KEY);
     if ($dapper_header_value !== false) {
         $headers[self::DAPPER_HEADER] = $dapper_header_value;
     }
     // Headers are sorted so we can do a string comparison in the unit test.
     ksort($headers);
     $header_str = "";
     foreach ($headers as $k => $v) {
         $header_str .= sprintf("%s: %s\r\n", $k, $v);
     }
     $opts = ['http' => ['method' => 'POST', 'header' => $header_str, 'content' => $serialized_remote_request, 'timeout' => $deadline + self::DEADLINE_DELTA_SECONDS]];
     $context = stream_context_create($opts);
     $api_host = static::getEnvOrDefault('API_HOST', self::SERVICE_BRIDGE_HOST);
     $api_port = static::getEnvOrDefault('API_PORT', self::API_PORT);
     $endpoint_url = sprintf("http://%s:%s%s", $api_host, $api_port, self::PROXY_PATH);
     // We silence the error here to prevent spamming the users application.
     // @codingStandardsIgnoreStart
     $serialized_remote_respone = @file_get_contents($endpoint_url, false, $context);
     // @codingStandardsIgnoreEnd
     if ($serialized_remote_respone === false) {
         throw new RPCFailedError(sprintf('Remote implementation for %s.%s failed', $package, $call_name));
     }
     $remote_response = new Response();
     $remote_response->parseFromString($serialized_remote_respone);
     if ($remote_response->hasApplicationError()) {
         throw new ApplicationError($remote_response->getApplicationError()->getCode(), $remote_response->getApplicationError()->getDetail());
     }
     if ($remote_response->hasException() || $remote_response->hasJavaException()) {
         // This indicates a bug in the remote implementation.
         throw new RPCFailedError(sprintf('Remote implementation for %s.%s failed', $package, $call_name));
     }
     if ($remote_response->hasRpcError()) {
         $rpc_error = $remote_response->getRpcError();
         throw self::getRpcErrorFromException($rpc_error->getCode(), $package, $call_name);
     }
     $response->parseFromString($remote_response->getResponse());
 }
 protected function expectRpc($request, $response, $call_options = [])
 {
     $stream_call_data = [];
     $options = array_merge(self::$rpc_default_options, $call_options);
     // Open call will supply the address and the RPC request.
     $address = sprintf('http://%s:%s%s', $options['host'], $options['port'], $options['proxy_path']);
     $remote_request = new Request();
     $remote_request->setServiceName($options['package_name']);
     $remote_request->setMethod($options['call_name']);
     $remote_request->setRequestId($options['ticket']);
     $remote_request->setRequest($request->serializeToString());
     $options['context']['http']['content'] = $remote_request->serializeToString();
     $options['context']['http']["timeout"] = $options['timeout'] + VmApiProxy::DEADLINE_DELTA_SECONDS;
     $options['http_headers'][VmApiProxy::SERVICE_DEADLINE_HEADER] = $options['timeout'];
     // Form the header string - sort by key as we do a string compare to check
     // for a match.
     ksort($options['http_headers']);
     $header_str = "";
     foreach ($options['http_headers'] as $k => $v) {
         $header_str .= sprintf("%s: %s\r\n", $k, $v);
     }
     $options['context']['http']['header'] = $header_str;
     $stream_call_data['stream_open'] = ['address' => $address, 'mode' => 'rb', 'context' => $options['context']];
     if (isset($options['http_open_failure'])) {
         $stream_call_data['stream_open']['http_open_failure'] = true;
     }
     $remote_response = new Response();
     if (isset($options['rpc_exception'])) {
         $error = $remote_response->mutableRpcError();
         $error->setCode($options['rpc_exception']);
     } else {
         if (isset($options['application_error'])) {
             $error = $remote_response->mutableApplicationError();
             $error->setCode($options['application_error']['code']);
             $error->setDetail($options['application_error']['detail']);
         } else {
             if (isset($options['generic_exception'])) {
                 $remote_response->setException(true);
             } else {
                 $remote_response->setResponse($response->serializeToString());
             }
         }
     }
     $serialized_remote_response = $remote_response->serializeToString();
     $stream_call_data['stream_stat'] = ['size' => strlen($serialized_remote_response)];
     $stream_call_data['stream_read'] = ['bytes' => $serialized_remote_response];
     $GLOBALS['mock_http'][] = $stream_call_data;
 }