-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[darwin-framework-tool][XPC] Add a XPC server registry.
- Loading branch information
1 parent
38ad07d
commit 2329976
Showing
5 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
examples/darwin-framework-tool/commands/common/xpc/XPCServerRegistry.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#import <Matter/Matter.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@protocol XPCServerBaseProtocol <NSObject> | ||
- (void)start; | ||
- (void)stop; | ||
@end | ||
|
||
@protocol XPCServerExternalCertificateParametersProtocol <XPCServerBaseProtocol, NSXPCListenerDelegate> | ||
- (MTRDeviceController *)createController:(MTRDeviceControllerExternalCertificateParameters *)params error:(NSError * __autoreleasing *)error; | ||
@end | ||
|
||
@protocol XPCServerStartupParametersProtocol <XPCServerBaseProtocol, NSXPCListenerDelegate> | ||
- (MTRDeviceController *)createController:(MTRDeviceControllerStartupParams *)params error:(NSError * __autoreleasing *)error; | ||
@end | ||
|
||
@protocol XPCServerExternalCertificateParametersWithServiceNameProtocol <XPCServerBaseProtocol> | ||
- (MTRDeviceController *)createController:(NSString *)controllerID serviceName:(NSString *)serviceName error:(NSError * __autoreleasing *)error; | ||
@end | ||
|
||
@protocol XPCServerStartupParametersWithServiceNameProtocol <XPCServerBaseProtocol> | ||
- (MTRDeviceController *)createController:(NSString *)controllerID serviceName:(NSString *)serviceName error:(NSError * __autoreleasing *)error; | ||
@end | ||
|
||
@interface XPCServerRegistry : NSObject | ||
- (instancetype)init NS_UNAVAILABLE; | ||
+ (instancetype)new NS_UNAVAILABLE; | ||
+ (XPCServerRegistry *)sharedInstance; | ||
|
||
- (void)register:(id<XPCServerBaseProtocol>)server; | ||
|
||
- (void)start; | ||
- (void)stop; | ||
- (MTRDeviceController *)createController:(NSString * _Nullable)controllerId serviceName:(NSString * _Nullable)serviceName params:(id)params error:(NSError * __autoreleasing *)error; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
98 changes: 98 additions & 0 deletions
98
examples/darwin-framework-tool/commands/common/xpc/XPCServerRegistry.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2024 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#import "XPCServerRegistry.h" | ||
|
||
@interface XPCServerRegistry () | ||
@property (strong, nonatomic) NSMutableArray<id<XPCServerBaseProtocol>> * servers; | ||
@end | ||
|
||
@implementation XPCServerRegistry | ||
- (instancetype)init | ||
{ | ||
if ((self = [super init])) { | ||
_servers = [NSMutableArray new]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
+ (XPCServerRegistry *)sharedInstance | ||
{ | ||
static XPCServerRegistry * registry = nil; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
registry = [[XPCServerRegistry alloc] init]; | ||
}); | ||
return registry; | ||
} | ||
|
||
- (void)start | ||
{ | ||
for (id<XPCServerBaseProtocol> server in _servers) { | ||
[server start]; | ||
} | ||
} | ||
|
||
- (void)stop | ||
{ | ||
for (id<XPCServerBaseProtocol> server in _servers) { | ||
[server start]; | ||
} | ||
} | ||
|
||
- (void)register:(id<XPCServerBaseProtocol>)server | ||
{ | ||
[_servers addObject:server]; | ||
} | ||
|
||
- (MTRDeviceController *)createController:(NSString * _Nullable)controllerId serviceName:(NSString * _Nullable)serviceName params:(id)params error:(NSError * __autoreleasing *)error | ||
{ | ||
return [self createControllerInternal:controllerId serviceName:serviceName params:params error:error]; | ||
} | ||
|
||
- (MTRDeviceController *)createControllerInternal:(NSString * _Nullable)controllerId serviceName:(NSString * _Nullable)serviceName params:(id)params error:(NSError * __autoreleasing *)error | ||
{ | ||
BOOL isExternalCertificateParameters = [params isKindOfClass:[MTRDeviceControllerExternalCertificateParameters class]]; | ||
BOOL isStartupParameters = [params isKindOfClass:[MTRDeviceControllerStartupParams class]]; | ||
for (id server in _servers) { | ||
if (controllerId && serviceName) { | ||
if ([server conformsToProtocol:@protocol(XPCServerExternalCertificateParametersWithServiceNameProtocol)] && isExternalCertificateParameters) { | ||
return [server createController:controllerId serviceName:serviceName error:error]; | ||
} | ||
|
||
if ([server conformsToProtocol:@protocol(XPCServerStartupParametersWithServiceNameProtocol)] && isStartupParameters) { | ||
return [server createController:controllerId serviceName:serviceName error:error]; | ||
} | ||
} else { | ||
if ([server conformsToProtocol:@protocol(XPCServerExternalCertificateParametersProtocol)] && isExternalCertificateParameters) { | ||
return [server createController:params error:error]; | ||
} | ||
|
||
if ([server conformsToProtocol:@protocol(XPCServerStartupParametersProtocol)] && isStartupParameters) { | ||
return [server createController:params error:error]; | ||
} | ||
} | ||
} | ||
|
||
__auto_type * userInfo = @{ NSLocalizedDescriptionKey : @"No XPC servers support this configuration." }; | ||
*error = [NSError errorWithDomain:@"Error" code:0 userInfo:userInfo]; | ||
return nil; | ||
} | ||
|
||
@end |