Skip to content

Commit

Permalink
iOS TvCasting app: Adding async completion callback to startMatterSer…
Browse files Browse the repository at this point in the history
…ver API (#24285)
  • Loading branch information
sharadb-amazon authored Jan 7, 2023
1 parent 8c7c641 commit 2ba9901
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,15 @@
- (void)disconnect:(dispatch_queue_t _Nonnull)clientQueue requestSentHandler:(nullable void (^)())requestSentHandler;

/**
@brief Start the Matter server and reconnect to a previously connected Video Player (if any)
@brief Start the Matter server and reconnect to a previously connected Video Player (if any). This API is async
@param clientQueue Queue to invoke callbacks on
@param startMatterServerCompletionCallback Called after the Matter server has started and connected (or failed to connect) to a
previously connected video player (if any) are complete
*/
- (void)startMatterServer;
- (void)startMatterServer:(dispatch_queue_t _Nonnull)clientQueue
startMatterServerCompletionCallback:(nullable void (^)(MatterError * _Nonnull))startMatterServerCompletionCallback;

/**
@brief Stop the Matter server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,20 @@ - (void)shutdownAllSubscriptions:(dispatch_queue_t _Nonnull)clientQueue requestS
});
}

- (void)startMatterServer
- (void)startMatterServer:(dispatch_queue_t _Nonnull)clientQueue
startMatterServerCompletionCallback:(nullable void (^)(MatterError * _Nonnull))startMatterServerCompletionCallback
{
ChipLogProgress(AppServer, "CastingServerBridge().startMatterServer() called");

dispatch_sync(_chipWorkQueue, ^{
dispatch_async(_chipWorkQueue, ^{
// Initialize the Matter server
CHIP_ERROR err = chip::Server::GetInstance().Init(*self->_serverInitParams);
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "chip::Server init failed: %s", ErrorStr(err));
dispatch_async(clientQueue, ^{
startMatterServerCompletionCallback(
[[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]]);
});
return;
}

Expand All @@ -527,8 +532,28 @@ - (void)startMatterServer
ChipLogProgress(
AppServer, "CastingServerBridge().startMatterServer() reconnecting to previously connected VideoPlayer...");
err = CastingServer::GetInstance()->VerifyOrEstablishConnection(
*(self->_previouslyConnectedVideoPlayer), [](TargetVideoPlayerInfo * cppTargetVideoPlayerInfo) {},
[](CHIP_ERROR err) {}, [](TargetEndpointInfo * cppTargetEndpointInfo) {});
*(self->_previouslyConnectedVideoPlayer),
[clientQueue, startMatterServerCompletionCallback](TargetVideoPlayerInfo * cppTargetVideoPlayerInfo) {
dispatch_async(clientQueue, ^{
startMatterServerCompletionCallback(
[[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger()
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]);
});
},
[clientQueue, startMatterServerCompletionCallback](CHIP_ERROR err) {
dispatch_async(clientQueue, ^{
startMatterServerCompletionCallback(
[[MatterError alloc] initWithCode:err.AsInteger()
message:[NSString stringWithUTF8String:err.AsString()]]);
});
},
[](TargetEndpointInfo * cppTargetEndpointInfo) {});
} else {
dispatch_async(clientQueue, ^{
startMatterServerCompletionCallback(
[[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger()
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]]);
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ struct TvCastingApp: App {
{
if let castingServerBridge = CastingServerBridge.getSharedInstance()
{
castingServerBridge.startMatterServer()
castingServerBridge.startMatterServer(DispatchQueue.main, startMatterServerCompletionCallback: { (error: MatterError) -> () in
DispatchQueue.main.async {
self.Log.info("TvCastingApp.startMatterServerCompletionCallback called with \(error)")
}
})
}
}
firstAppActivation = false
Expand Down

0 comments on commit 2ba9901

Please sign in to comment.