2017-01-18 15 views
1

Cap'n Proto C++ RPC와의 약속 된 파이프 라이닝을 시도하고 싶습니다만 어떻게해야할지 모르겠습니다.Cap'n Proto 및 약속 파이프 라이닝

auto request1 = test.getIntRequest(); 
auto promise = request1.send(); 

auto request2 = test.incrementRequest(); 
request2.setIntParam(promise.getIntResult()) // HERE IS THE PROBLEM 
auto promise2 = request2.send(); 
:

increment(getInt()); 

내가 그런 일을하려고 노력 :

여기
interface Test { 
    getInt @0() -> (intResult :Int32); 
    increment @1 (intParam :Int32) -> (intResult :Int32); 
} 

내가 (의사 코드)를 수행하고자하는 것입니다 : 여기

내 스키마입니다

하지만 약속을 사용하는 것은 좋지 않습니다. 내가하고 싶은 것을 이해하기를 바랍니다.

감사합니다.

EDIT : 다른 질문 : 서버에서 메소드를 구현하는 방법은 무엇입니까?

이 나는이 코드를 작성했습니다 :

#include <kj/debug.h> 
#include <capnp/ez-rpc.h> 
#include <capnp/message.h> 
#include <iostream> 
#include "test.capnp.h" 

using namespace std; 


class TestI : virtual public Test::Server 
{ 
public: 
     TestI() {} 
     ::kj::Promise<void> getInt(GetIntContext context) 
     { 
      // ???? 
     } 
     ::kj::Promise<void> increment(IncrementContext context) 
     { 
      // ???? 
     } 
}; 

class Int32BoxI : virtual public Int32Box::Server 
{ 
private: 
     int val = 12; 
public: 
     Int32BoxI(int value): val(value) {} 
     ::kj::Promise<void> get(GetContext context) 
     { 
      context.getResults().setValue(this->val); 
      return kj::READY_NOW; 
     } 
} 

하지만 난의 getInt() 및 증가()을 구현하는 방법을 모르겠어요.

답변

2

여기서 문제는 int에서 파이프 라인을 만들려고하지만 파이프 라이닝은 객체 참조에서만 작동한다는 것입니다. 다음과 같이 객체에 int를 래핑하여 해결할 수 있습니다.

interface Int32Box { 
    get @0() -> (value :Int32); 
} 

interface Test { 
    getInt @0() -> (intResult :Int32Box); 
    increment @1 (intParam :Int32Box) -> (intResult :Int32Box); 
} 

이제 코드는 작성된대로 작동합니다.

물론이 값을 읽으려면 Int32Box.get()을 추가로 호출해야합니다. 다행히도이 호출을 파이프 라인 할 수 있으므로 추가 네트워크 왕복이 필요하지 않습니다.

auto request1 = test.getIntRequest(); 
auto promise = request1.send(); 

auto request2 = test.incrementRequest(); 
request2.setIntParam(promise.getIntResult()); 
auto promise2 = request2.send(); 

auto request3 = promise2.getIntResult().getRequest(); 
auto promise3 = request3.send(); 

// This is the only wait! 
int finalResult = promise3.wait().getValue(); 

위의 시퀀스는 하나의 네트워크 왕복과 함께 실행됩니다.

+0

답변 해 주셔서 감사합니다. 지금은 분명합니다. 그러나 서버에서 메서드를 구현하는 데 어려움이 있습니다. 나는 나의 문제를 보여주기 위해 나의 첫 번째 글을 편집했다. 다시 감사합니다. –

+1

@ B.Clement Cap'n Proto 저장소의 "계산기"예제를 확인하는 것이 좋습니다. 이는 여러분이하고있는 것과 매우 유사합니다. 참조 : https://github.com/sandstorm-io/capnproto/tree/master/c++/samples –