@@ -66,7 +66,7 @@ bool waitForParticipant(Room* room, const std::string& identity, std::chrono::mi
6666 auto start = std::chrono::steady_clock::now ();
6767
6868 while (std::chrono::steady_clock::now () - start < timeout) {
69- if (room->remoteParticipant (identity) != nullptr ) {
69+ if (room->remoteParticipant (identity). lock () != nullptr ) {
7070 return true ;
7171 }
7272 std::this_thread::sleep_for (100ms);
@@ -207,8 +207,11 @@ std::string parseStringFromJson(const std::string& json) {
207207
208208// RPC handler registration
209209void registerReceiverMethods (Room* greeters_room, Room* math_genius_room) {
210- LocalParticipant* greeter_lp = greeters_room->localParticipant ();
211- LocalParticipant* math_genius_lp = math_genius_room->localParticipant ();
210+ auto greeter_lp = greeters_room->localParticipant ().lock ();
211+ auto math_genius_lp = math_genius_room->localParticipant ().lock ();
212+ if (!greeter_lp || !math_genius_lp) {
213+ throw std::runtime_error (" local participant unavailable" );
214+ }
212215
213216 // arrival
214217 greeter_lp->registerRpcMethod (" arrival" , [](const RpcInvocationData& data) -> std::optional<std::string> {
@@ -274,7 +277,9 @@ void performGreeting(Room* room) {
274277 std::cout << " [Caller] Letting the greeter know that I've arrived\n " ;
275278 double t0 = nowMs ();
276279 try {
277- std::string response = room->localParticipant ()->performRpc (" greeter" , " arrival" , " Hello" , std::nullopt );
280+ auto lp = room->localParticipant ().lock ();
281+ if (!lp) throw std::runtime_error (" local participant unavailable" );
282+ std::string response = lp->performRpc (" greeter" , " arrival" , " Hello" , std::nullopt );
278283 double t1 = nowMs ();
279284 std::cout << " [Caller] RTT: " << (t1 - t0) << " ms\n " ;
280285 std::cout << " [Caller] That's nice, the greeter said: \" " << response << " \"\n " ;
@@ -291,7 +296,9 @@ void performSquareRoot(Room* room) {
291296 double t0 = nowMs ();
292297 try {
293298 std::string payload = makeNumberJson (" number" , 16.0 );
294- std::string response = room->localParticipant ()->performRpc (" math-genius" , " square-root" , payload, std::nullopt );
299+ auto lp = room->localParticipant ().lock ();
300+ if (!lp) throw std::runtime_error (" local participant unavailable" );
301+ std::string response = lp->performRpc (" math-genius" , " square-root" , payload, std::nullopt );
295302 double t1 = nowMs ();
296303 std::cout << " [Caller] RTT: " << (t1 - t0) << " ms\n " ;
297304 double result = parseNumberFromJson (response);
@@ -311,8 +318,10 @@ void performQuantumHyperGeometricSeries(Room* room) {
311318 double t0 = nowMs ();
312319 try {
313320 std::string payload = makeNumberJson (" number" , 42.0 );
321+ auto lp = room->localParticipant ().lock ();
322+ if (!lp) throw std::runtime_error (" local participant unavailable" );
314323 std::string response =
315- room-> localParticipant () ->performRpc (" math-genius" , " quantum-hypergeometric-series" , payload, std::nullopt );
324+ lp ->performRpc (" math-genius" , " quantum-hypergeometric-series" , payload, std::nullopt );
316325 double t1 = nowMs ();
317326 std::cout << " [Caller] (Unexpected success) RTT=" << (t1 - t0) << " ms\n " ;
318327 std::cout << " [Caller] Result: " << response << " \n " ;
@@ -337,7 +346,9 @@ void performDivide(Room* room) {
337346 double t0 = nowMs ();
338347 try {
339348 std::string payload = " {\" dividend\" :10,\" divisor\" :0}" ;
340- std::string response = room->localParticipant ()->performRpc (" math-genius" , " divide" , payload, std::nullopt );
349+ auto lp = room->localParticipant ().lock ();
350+ if (!lp) throw std::runtime_error (" local participant unavailable" );
351+ std::string response = lp->performRpc (" math-genius" , " divide" , payload, std::nullopt );
341352 double t1 = nowMs ();
342353 std::cout << " [Caller] (Unexpected success) RTT=" << (t1 - t0) << " ms\n " ;
343354 std::cout << " [Caller] Result = " << response << " \n " ;
@@ -361,7 +372,9 @@ void performLongCalculation(Room* room) {
361372 std::cout << " [Caller] Giving only 10s to respond. EXPECTED RESULT: TIMEOUT.\n " ;
362373 double t0 = nowMs ();
363374 try {
364- std::string response = room->localParticipant ()->performRpc (" math-genius" , " long-calculation" , " {}" , 10.0 );
375+ auto lp = room->localParticipant ().lock ();
376+ if (!lp) throw std::runtime_error (" local participant unavailable" );
377+ std::string response = lp->performRpc (" math-genius" , " long-calculation" , " {}" , 10.0 );
365378 double t1 = nowMs ();
366379 std::cout << " [Caller] (Unexpected success) RTT=" << (t1 - t0) << " ms\n " ;
367380 std::cout << " [Caller] Result: " << response << " \n " ;
0 commit comments