Esempio n. 1
0
def CallRPCMethod(transport_cb, method, args, version=None):
  """Send a RPC request via a transport and return the response.

  """
  assert callable(transport_cb)
  t1 = time.time() * 1000
  request_msg = FormatRequest(method, args, version=version)
  t2 = time.time() * 1000
  # Send request and wait for response
  response_msg = transport_cb(request_msg)
  t3 = time.time() * 1000
  (success, result, resp_version) = ParseResponse(response_msg)
  t4 = time.time() * 1000
  logging.debug("CallRPCMethod %s: format: %dms, sock: %dms, parse: %dms",
                method, int(t2 - t1), int(t3 - t2), int(t4 - t3))
  # Verify version if there was one in the response
  if resp_version is not None and resp_version != version:
    raise LuxiError("RPC version mismatch, client %s, response %s" %
                    (version, resp_version))

  if success:
    return result

  errors.MaybeRaise(result)
  raise RequestError(result)
  def testMaybeRaise(self):
    testvals = [None, 1, 2, 3, "Hello World", (1, ), (1, 2, 3),
                ("NoErrorClassName", []), ("NoErrorClassName", None),
                ("GenericError", [1, 2, 3], None), ("GenericError", 1)]
    # These shouldn't raise
    for i in testvals:
      errors.MaybeRaise(i)

    self.assertRaises(errors.GenericError, errors.MaybeRaise,
                      ("GenericError", ["Hello"]))
    # Check error encoding
    for i in testvals:
      src = errors.GenericError(i)
      try:
        errors.MaybeRaise(errors.EncodeException(src))
      except errors.GenericError, dst:
        self.assertEqual(src.args, dst.args)
        self.assertEqual(src.__class__, dst.__class__)
      else:
        self.fail("Exception %s not raised" % repr(src))
Esempio n. 3
0
def CallRPCMethod(transport_cb, method, args, version=None):
    """Send a RPC request via a transport and return the response.

  """
    assert callable(transport_cb)

    request_msg = FormatRequest(method, args, version=version)

    # Send request and wait for response
    response_msg = transport_cb(request_msg)

    (success, result, resp_version) = ParseResponse(response_msg)

    # Verify version if there was one in the response
    if resp_version is not None and resp_version != version:
        raise LuxiError("RPC version mismatch, client %s, response %s" %
                        (version, resp_version))

    if success:
        return result

    errors.MaybeRaise(result)
    raise RequestError(result)