cannam@133: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@133: // Licensed under the MIT License: cannam@133: // cannam@133: // Permission is hereby granted, free of charge, to any person obtaining a copy cannam@133: // of this software and associated documentation files (the "Software"), to deal cannam@133: // in the Software without restriction, including without limitation the rights cannam@133: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@133: // copies of the Software, and to permit persons to whom the Software is cannam@133: // furnished to do so, subject to the following conditions: cannam@133: // cannam@133: // The above copyright notice and this permission notice shall be included in cannam@133: // all copies or substantial portions of the Software. cannam@133: // cannam@133: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@133: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@133: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@133: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@133: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@133: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@133: // THE SOFTWARE. cannam@133: cannam@133: #include "calculator.capnp.h" cannam@133: #include cannam@133: #include cannam@133: #include cannam@133: #include cannam@133: cannam@133: class PowerFunction final: public Calculator::Function::Server { cannam@133: // An implementation of the Function interface wrapping pow(). Note that cannam@133: // we're implementing this on the client side and will pass a reference to cannam@133: // the server. The server will then be able to make calls back to the client. cannam@133: cannam@133: public: cannam@133: kj::Promise call(CallContext context) { cannam@133: auto params = context.getParams().getParams(); cannam@133: KJ_REQUIRE(params.size() == 2, "Wrong number of parameters."); cannam@133: context.getResults().setValue(pow(params[0], params[1])); cannam@133: return kj::READY_NOW; cannam@133: } cannam@133: }; cannam@133: cannam@133: int main(int argc, const char* argv[]) { cannam@133: if (argc != 2) { cannam@133: std::cerr << "usage: " << argv[0] << " HOST:PORT\n" cannam@133: "Connects to the Calculator server at the given address and " cannam@133: "does some RPCs." << std::endl; cannam@133: return 1; cannam@133: } cannam@133: cannam@133: capnp::EzRpcClient client(argv[1]); cannam@133: Calculator::Client calculator = client.getMain(); cannam@133: cannam@133: // Keep an eye on `waitScope`. Whenever you see it used is a place where we cannam@133: // stop and wait for the server to respond. If a line of code does not use cannam@133: // `waitScope`, then it does not block! cannam@133: auto& waitScope = client.getWaitScope(); cannam@133: cannam@133: { cannam@133: // Make a request that just evaluates the literal value 123. cannam@133: // cannam@133: // What's interesting here is that evaluate() returns a "Value", which is cannam@133: // another interface and therefore points back to an object living on the cannam@133: // server. We then have to call read() on that object to read it. cannam@133: // However, even though we are making two RPC's, this block executes in cannam@133: // *one* network round trip because of promise pipelining: we do not wait cannam@133: // for the first call to complete before we send the second call to the cannam@133: // server. cannam@133: cannam@133: std::cout << "Evaluating a literal... "; cannam@133: std::cout.flush(); cannam@133: cannam@133: // Set up the request. cannam@133: auto request = calculator.evaluateRequest(); cannam@133: request.getExpression().setLiteral(123); cannam@133: cannam@133: // Send it, which returns a promise for the result (without blocking). cannam@133: auto evalPromise = request.send(); cannam@133: cannam@133: // Using the promise, create a pipelined request to call read() on the cannam@133: // returned object, and then send that. cannam@133: auto readPromise = evalPromise.getValue().readRequest().send(); cannam@133: cannam@133: // Now that we've sent all the requests, wait for the response. Until this cannam@133: // point, we haven't waited at all! cannam@133: auto response = readPromise.wait(waitScope); cannam@133: KJ_ASSERT(response.getValue() == 123); cannam@133: cannam@133: std::cout << "PASS" << std::endl; cannam@133: } cannam@133: cannam@133: { cannam@133: // Make a request to evaluate 123 + 45 - 67. cannam@133: // cannam@133: // The Calculator interface requires that we first call getOperator() to cannam@133: // get the addition and subtraction functions, then call evaluate() to use cannam@133: // them. But, once again, we can get both functions, call evaluate(), and cannam@133: // then read() the result -- four RPCs -- in the time of *one* network cannam@133: // round trip, because of promise pipelining. cannam@133: cannam@133: std::cout << "Using add and subtract... "; cannam@133: std::cout.flush(); cannam@133: cannam@133: Calculator::Function::Client add = nullptr; cannam@133: Calculator::Function::Client subtract = nullptr; cannam@133: cannam@133: { cannam@133: // Get the "add" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::ADD); cannam@133: add = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: { cannam@133: // Get the "subtract" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::SUBTRACT); cannam@133: subtract = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: // Build the request to evaluate 123 + 45 - 67. cannam@133: auto request = calculator.evaluateRequest(); cannam@133: cannam@133: auto subtractCall = request.getExpression().initCall(); cannam@133: subtractCall.setFunction(subtract); cannam@133: auto subtractParams = subtractCall.initParams(2); cannam@133: subtractParams[1].setLiteral(67); cannam@133: cannam@133: auto addCall = subtractParams[0].initCall(); cannam@133: addCall.setFunction(add); cannam@133: auto addParams = addCall.initParams(2); cannam@133: addParams[0].setLiteral(123); cannam@133: addParams[1].setLiteral(45); cannam@133: cannam@133: // Send the evaluate() request, read() the result, and wait for read() to cannam@133: // finish. cannam@133: auto evalPromise = request.send(); cannam@133: auto readPromise = evalPromise.getValue().readRequest().send(); cannam@133: cannam@133: auto response = readPromise.wait(waitScope); cannam@133: KJ_ASSERT(response.getValue() == 101); cannam@133: cannam@133: std::cout << "PASS" << std::endl; cannam@133: } cannam@133: cannam@133: { cannam@133: // Make a request to evaluate 4 * 6, then use the result in two more cannam@133: // requests that add 3 and 5. cannam@133: // cannam@133: // Since evaluate() returns its result wrapped in a `Value`, we can pass cannam@133: // that `Value` back to the server in subsequent requests before the first cannam@133: // `evaluate()` has actually returned. Thus, this example again does only cannam@133: // one network round trip. cannam@133: cannam@133: std::cout << "Pipelining eval() calls... "; cannam@133: std::cout.flush(); cannam@133: cannam@133: Calculator::Function::Client add = nullptr; cannam@133: Calculator::Function::Client multiply = nullptr; cannam@133: cannam@133: { cannam@133: // Get the "add" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::ADD); cannam@133: add = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: { cannam@133: // Get the "multiply" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::MULTIPLY); cannam@133: multiply = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: // Build the request to evaluate 4 * 6 cannam@133: auto request = calculator.evaluateRequest(); cannam@133: cannam@133: auto multiplyCall = request.getExpression().initCall(); cannam@133: multiplyCall.setFunction(multiply); cannam@133: auto multiplyParams = multiplyCall.initParams(2); cannam@133: multiplyParams[0].setLiteral(4); cannam@133: multiplyParams[1].setLiteral(6); cannam@133: cannam@133: auto multiplyResult = request.send().getValue(); cannam@133: cannam@133: // Use the result in two calls that add 3 and add 5. cannam@133: cannam@133: auto add3Request = calculator.evaluateRequest(); cannam@133: auto add3Call = add3Request.getExpression().initCall(); cannam@133: add3Call.setFunction(add); cannam@133: auto add3Params = add3Call.initParams(2); cannam@133: add3Params[0].setPreviousResult(multiplyResult); cannam@133: add3Params[1].setLiteral(3); cannam@133: auto add3Promise = add3Request.send().getValue().readRequest().send(); cannam@133: cannam@133: auto add5Request = calculator.evaluateRequest(); cannam@133: auto add5Call = add5Request.getExpression().initCall(); cannam@133: add5Call.setFunction(add); cannam@133: auto add5Params = add5Call.initParams(2); cannam@133: add5Params[0].setPreviousResult(multiplyResult); cannam@133: add5Params[1].setLiteral(5); cannam@133: auto add5Promise = add5Request.send().getValue().readRequest().send(); cannam@133: cannam@133: // Now wait for the results. cannam@133: KJ_ASSERT(add3Promise.wait(waitScope).getValue() == 27); cannam@133: KJ_ASSERT(add5Promise.wait(waitScope).getValue() == 29); cannam@133: cannam@133: std::cout << "PASS" << std::endl; cannam@133: } cannam@133: cannam@133: { cannam@133: // Our calculator interface supports defining functions. Here we use it cannam@133: // to define two functions and then make calls to them as follows: cannam@133: // cannam@133: // f(x, y) = x * 100 + y cannam@133: // g(x) = f(x, x + 1) * 2; cannam@133: // f(12, 34) cannam@133: // g(21) cannam@133: // cannam@133: // Once again, the whole thing takes only one network round trip. cannam@133: cannam@133: std::cout << "Defining functions... "; cannam@133: std::cout.flush(); cannam@133: cannam@133: Calculator::Function::Client add = nullptr; cannam@133: Calculator::Function::Client multiply = nullptr; cannam@133: Calculator::Function::Client f = nullptr; cannam@133: Calculator::Function::Client g = nullptr; cannam@133: cannam@133: { cannam@133: // Get the "add" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::ADD); cannam@133: add = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: { cannam@133: // Get the "multiply" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::MULTIPLY); cannam@133: multiply = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: { cannam@133: // Define f. cannam@133: auto request = calculator.defFunctionRequest(); cannam@133: request.setParamCount(2); cannam@133: cannam@133: { cannam@133: // Build the function body. cannam@133: auto addCall = request.getBody().initCall(); cannam@133: addCall.setFunction(add); cannam@133: auto addParams = addCall.initParams(2); cannam@133: addParams[1].setParameter(1); // y cannam@133: cannam@133: auto multiplyCall = addParams[0].initCall(); cannam@133: multiplyCall.setFunction(multiply); cannam@133: auto multiplyParams = multiplyCall.initParams(2); cannam@133: multiplyParams[0].setParameter(0); // x cannam@133: multiplyParams[1].setLiteral(100); cannam@133: } cannam@133: cannam@133: f = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: { cannam@133: // Define g. cannam@133: auto request = calculator.defFunctionRequest(); cannam@133: request.setParamCount(1); cannam@133: cannam@133: { cannam@133: // Build the function body. cannam@133: auto multiplyCall = request.getBody().initCall(); cannam@133: multiplyCall.setFunction(multiply); cannam@133: auto multiplyParams = multiplyCall.initParams(2); cannam@133: multiplyParams[1].setLiteral(2); cannam@133: cannam@133: auto fCall = multiplyParams[0].initCall(); cannam@133: fCall.setFunction(f); cannam@133: auto fParams = fCall.initParams(2); cannam@133: fParams[0].setParameter(0); cannam@133: cannam@133: auto addCall = fParams[1].initCall(); cannam@133: addCall.setFunction(add); cannam@133: auto addParams = addCall.initParams(2); cannam@133: addParams[0].setParameter(0); cannam@133: addParams[1].setLiteral(1); cannam@133: } cannam@133: cannam@133: g = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: // OK, we've defined all our functions. Now create our eval requests. cannam@133: cannam@133: // f(12, 34) cannam@133: auto fEvalRequest = calculator.evaluateRequest(); cannam@133: auto fCall = fEvalRequest.initExpression().initCall(); cannam@133: fCall.setFunction(f); cannam@133: auto fParams = fCall.initParams(2); cannam@133: fParams[0].setLiteral(12); cannam@133: fParams[1].setLiteral(34); cannam@133: auto fEvalPromise = fEvalRequest.send().getValue().readRequest().send(); cannam@133: cannam@133: // g(21) cannam@133: auto gEvalRequest = calculator.evaluateRequest(); cannam@133: auto gCall = gEvalRequest.initExpression().initCall(); cannam@133: gCall.setFunction(g); cannam@133: gCall.initParams(1)[0].setLiteral(21); cannam@133: auto gEvalPromise = gEvalRequest.send().getValue().readRequest().send(); cannam@133: cannam@133: // Wait for the results. cannam@133: KJ_ASSERT(fEvalPromise.wait(waitScope).getValue() == 1234); cannam@133: KJ_ASSERT(gEvalPromise.wait(waitScope).getValue() == 4244); cannam@133: cannam@133: std::cout << "PASS" << std::endl; cannam@133: } cannam@133: cannam@133: { cannam@133: // Make a request that will call back to a function defined locally. cannam@133: // cannam@133: // Specifically, we will compute 2^(4 + 5). However, exponent is not cannam@133: // defined by the Calculator server. So, we'll implement the Function cannam@133: // interface locally and pass it to the server for it to use when cannam@133: // evaluating the expression. cannam@133: // cannam@133: // This example requires two network round trips to complete, because the cannam@133: // server calls back to the client once before finishing. In this cannam@133: // particular case, this could potentially be optimized by using a tail cannam@133: // call on the server side -- see CallContext::tailCall(). However, to cannam@133: // keep the example simpler, we haven't implemented this optimization in cannam@133: // the sample server. cannam@133: cannam@133: std::cout << "Using a callback... "; cannam@133: std::cout.flush(); cannam@133: cannam@133: Calculator::Function::Client add = nullptr; cannam@133: cannam@133: { cannam@133: // Get the "add" function from the server. cannam@133: auto request = calculator.getOperatorRequest(); cannam@133: request.setOp(Calculator::Operator::ADD); cannam@133: add = request.send().getFunc(); cannam@133: } cannam@133: cannam@133: // Build the eval request for 2^(4+5). cannam@133: auto request = calculator.evaluateRequest(); cannam@133: cannam@133: auto powCall = request.getExpression().initCall(); cannam@133: powCall.setFunction(kj::heap()); cannam@133: auto powParams = powCall.initParams(2); cannam@133: powParams[0].setLiteral(2); cannam@133: cannam@133: auto addCall = powParams[1].initCall(); cannam@133: addCall.setFunction(add); cannam@133: auto addParams = addCall.initParams(2); cannam@133: addParams[0].setLiteral(4); cannam@133: addParams[1].setLiteral(5); cannam@133: cannam@133: // Send the request and wait. cannam@133: auto response = request.send().getValue().readRequest() cannam@133: .send().wait(waitScope); cannam@133: KJ_ASSERT(response.getValue() == 512); cannam@133: cannam@133: std::cout << "PASS" << std::endl; cannam@133: } cannam@133: cannam@133: return 0; cannam@133: }