Re: [capnproto] Usage of TwoPartyClient() for client callback mechanism

2023-10-31 Thread Bhavya R
Hello Kenton,

Thanks a lot for the input. kj::newPromiseAndCrossThreadFulfiller() 
approach works for me.

On Tuesday, October 24, 2023 at 9:17:08 PM UTC+1 ken...@cloudflare.com 
wrote:

> This is expected. The KJ event loop runs when you call `.wait()`. The 
> event loop checks for network events and delivers callbacks. If you haven't 
> called .wait(), then the event loop has no way to run. KJ does not create 
> any background threads for this purpose; the thread that created the event 
> loop must call .wait() on a promise in order to run it. Usually, large 
> programs will actually have a single top-level `.wait()` call that runs the 
> entire program -- this might literally be `kj::NEVER_DONE.wait(waitScope)` 
> to wait forever. In such programs, all actual work is done in `.then()` 
> callbacks.
>
> If you need to be able to receive KJ events at the same time as executing 
> other code (especially bulk CPU-heavy work), then you will have to create a 
> separate thread yourself, and figure out how to communicate between them. 
> The KJ event loop is tied to a specific thread and cannot be directly 
> accessed from other threads; however, you can use the `kj::Executor` 
> interface to schedule work on another thread's event loop, or 
> kj::newPromiseAndCrossThreadFulfiller() to create a promise/fulfiller pair 
> that can be fulfilled from a different thread.
>
> -Kenton
>
> On Tue, Oct 17, 2023 at 6:37 AM Bhavya R  wrote:
>
>> Hello All,
>>
>> I am trying to pass client capability to sever, client should receive  
>> notify back  on any updates.
>> In my application, Service is sending statusHearbeat continuously, but 
>> client receives callback only when  it is waiting on any promise. Could 
>> anyone please guide on how to receives callback from server? My client 
>> application is running on windows.
>>
>> For e.g  only duration when i am blocked on wait() call, i see callback 
>> gets received and dispatchCall is triggered
>>
>> This call is done from client -> server
>> auto request = connect->directorRef->versionRequest();
>> auto responsePromise = request.send();
>>auto response = *responsePromise.wait(*
>> connect->clientRef->getWaitScope());
>>
>> e.g interface FaTfServerToClient {
>>  statusHeartbeat   @0 () -> ();
>> }
>>
>> interface FaTfClientToServer {
>>  version @0 () -> (version :Base.Version);
>> }
>>
>> Client connection---> I am passing client bootstrap TransformEventHandler 
>> object
>>
>>
>>  auto address = connect->ioContext.provider->getNetwork()
>> .parseAddress(ipAddress, 
>> port).wait(connect->ioContext.waitScope);
>> connect->connection = 
>> address->connect().wait(connect->ioContext.waitScope);
>> connect->clientRef = 
>> std::make_shared(*(connect->connection), kj::heap<
>> *TransformEventHandler*>(observer),
>> capnp::schemas::Side_9fd69ebc87b9719c::CLIENT);
>> connect->directorRef = 
>> std::make_shared(connect->clientRef->bootstrap().castAs<
>>  
>> FaTfClientToServer >());
>>
>>
>> *class TransformEventHandler : public FaTfServerToClient::Server, public 
>> EventHandler {*
>> ::kj::Promise statusHeartbeat(StatusHeartbeatContext context) 
>> override {
>> std::cout << "status heartbeat" << std::endl;
>> return kj::READY_NOW;
>> }
>>  }
>>
>> I tried adding another iocontext, client in different thread and ran 
>> wait() on that. It doesnt receive nothing.
>> Looks like i haven't understood basic mechanism!! Any pointers would be 
>> very helpful. Thanks
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Cap'n Proto" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to capnproto+...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/capnproto/97c4321a-fcf7-4904-a9fc-11ece2bc6aecn%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to capnproto+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/3543-8ccf-427c-ad92-2037b35d8e26n%40googlegroups.com.


Re: [capnproto] Usage of TwoPartyClient() for client callback mechanism

2023-10-24 Thread 'Kenton Varda' via Cap'n Proto
This is expected. The KJ event loop runs when you call `.wait()`. The event
loop checks for network events and delivers callbacks. If you haven't
called .wait(), then the event loop has no way to run. KJ does not create
any background threads for this purpose; the thread that created the event
loop must call .wait() on a promise in order to run it. Usually, large
programs will actually have a single top-level `.wait()` call that runs the
entire program -- this might literally be `kj::NEVER_DONE.wait(waitScope)`
to wait forever. In such programs, all actual work is done in `.then()`
callbacks.

If you need to be able to receive KJ events at the same time as executing
other code (especially bulk CPU-heavy work), then you will have to create a
separate thread yourself, and figure out how to communicate between them.
The KJ event loop is tied to a specific thread and cannot be directly
accessed from other threads; however, you can use the `kj::Executor`
interface to schedule work on another thread's event loop, or
kj::newPromiseAndCrossThreadFulfiller() to create a promise/fulfiller pair
that can be fulfilled from a different thread.

-Kenton

On Tue, Oct 17, 2023 at 6:37 AM Bhavya R  wrote:

> Hello All,
>
> I am trying to pass client capability to sever, client should receive
> notify back  on any updates.
> In my application, Service is sending statusHearbeat continuously, but
> client receives callback only when  it is waiting on any promise. Could
> anyone please guide on how to receives callback from server? My client
> application is running on windows.
>
> For e.g  only duration when i am blocked on wait() call, i see callback
> gets received and dispatchCall is triggered
>
> This call is done from client -> server
> auto request = connect->directorRef->versionRequest();
> auto responsePromise = request.send();
>auto response = *responsePromise.wait(*
> connect->clientRef->getWaitScope());
>
> e.g interface FaTfServerToClient {
>  statusHeartbeat   @0 () -> ();
> }
>
> interface FaTfClientToServer {
>  version @0 () -> (version :Base.Version);
> }
>
> Client connection---> I am passing client bootstrap TransformEventHandler
> object
>
>
>  auto address = connect->ioContext.provider->getNetwork()
> .parseAddress(ipAddress,
> port).wait(connect->ioContext.waitScope);
> connect->connection =
> address->connect().wait(connect->ioContext.waitScope);
> connect->clientRef =
> std::make_shared(*(connect->connection), kj::heap<
> *TransformEventHandler*>(observer),
> capnp::schemas::Side_9fd69ebc87b9719c::CLIENT);
> connect->directorRef =
> std::make_shared(connect->clientRef->bootstrap().castAs<
> FaTfClientToServer >());
>
>
> *class TransformEventHandler : public FaTfServerToClient::Server, public
> EventHandler {*
> ::kj::Promise statusHeartbeat(StatusHeartbeatContext context)
> override {
> std::cout << "status heartbeat" << std::endl;
> return kj::READY_NOW;
> }
>  }
>
> I tried adding another iocontext, client in different thread and ran
> wait() on that. It doesnt receive nothing.
> Looks like i haven't understood basic mechanism!! Any pointers would be
> very helpful. Thanks
>
> --
> You received this message because you are subscribed to the Google Groups
> "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to capnproto+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/capnproto/97c4321a-fcf7-4904-a9fc-11ece2bc6aecn%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to capnproto+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/CAJouXQnzCdEN7QxEVu9XDr6mYCLoBoSDooa0FdYy0vRaBShCGw%40mail.gmail.com.


[capnproto] Usage of TwoPartyClient() for client callback mechanism

2023-10-17 Thread Bhavya R
Hello All,

I am trying to pass client capability to sever, client should receive  
notify back  on any updates.
In my application, Service is sending statusHearbeat continuously, but 
client receives callback only when  it is waiting on any promise. Could 
anyone please guide on how to receives callback from server? My client 
application is running on windows.

For e.g  only duration when i am blocked on wait() call, i see callback 
gets received and dispatchCall is triggered

This call is done from client -> server
auto request = connect->directorRef->versionRequest();
auto responsePromise = request.send();
   auto response = *responsePromise.wait(*
connect->clientRef->getWaitScope());

e.g interface FaTfServerToClient {
 statusHeartbeat   @0 () -> ();
}

interface FaTfClientToServer {
 version @0 () -> (version :Base.Version);
}

Client connection---> I am passing client bootstrap TransformEventHandler 
object


 auto address = connect->ioContext.provider->getNetwork()
.parseAddress(ipAddress, 
port).wait(connect->ioContext.waitScope);
connect->connection = 
address->connect().wait(connect->ioContext.waitScope);
connect->clientRef = 
std::make_shared(*(connect->connection), kj::heap<
*TransformEventHandler*>(observer),
capnp::schemas::Side_9fd69ebc87b9719c::CLIENT);
connect->directorRef = 
std::make_shared(connect->clientRef->bootstrap().castAs<
 
FaTfClientToServer >());


*class TransformEventHandler : public FaTfServerToClient::Server, public 
EventHandler {*
::kj::Promise statusHeartbeat(StatusHeartbeatContext context) 
override {
std::cout << "status heartbeat" << std::endl;
return kj::READY_NOW;
}
 }

I tried adding another iocontext, client in different thread and ran wait() 
on that. It doesnt receive nothing.
Looks like i haven't understood basic mechanism!! Any pointers would be 
very helpful. Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to capnproto+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/capnproto/97c4321a-fcf7-4904-a9fc-11ece2bc6aecn%40googlegroups.com.