CPDistributedMessagingCenter: Difference between revisions

From iPhone Development Wiki
mNo edit summary
m (→‎Server: Example as class implementation.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{occlass|library=AppSupport.framework}}
[[CPDistributedMessagingCenter]] is a wrapper over the existing messaging facilities in the operating system. It provides server-client communication between different processes using simple messages and dictionaries. All dictionaries transferred must be serializable as a property list.


[[CPDistributedMessagingCenter]] is a simple wrapper over the existing messaging facilities in the operating system. It provides two-way communication between different processes using simple messages and dictionaries. All dictionaries transferred must be serializable as a property list.
== Usage ==
=== Server ===
 
Here's an example implementation using a shared instance server class


== Server ==
<source lang="objc">
<source lang="objc">
-(id)init... {
@interface MyServerClass : NSObject
...
@end


  CPDistributedMessagingCenter *messagingCenter;
@implementation MyServerClass
  // Center name must be unique, recommend using application identifier.
  messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];
  [messagingCenter runServerOnCurrentThread];


  // Register Messages
+ (void)load {
  [messagingCenter registerForMessageName:@"messageThatHasInfo" target:self selector:@selector(handleMessageNamed:withUserInfo:)];
[self sharedInstance];
  [messagingCenter registerForMessageName:@"message" target:self selector:@selector(handleSimpleMessageNamed:)];
}


...
+ (id)sharedInstance {
static dispatch_once_t once = 0;
__strong static id sharedInstance = nil;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
 
- (id)init {
if ((self = [super init])) {
// ...
// Center name must be unique, recommend using application identifier.
CPDistributedMessagingCenter * messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];
[messagingCenter runServerOnCurrentThread];
 
// Register Messages
[messagingCenter registerForMessageName:@"messageThatHasInfo" target:self selector:@selector(handleMessageNamed:withUserInfo:)];
[messagingCenter registerForMessageName:@"message" target:self selector:@selector(handleSimpleMessageNamed:)];
}
 
return self;
}
}


- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo {
- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo {
    // Process userinfo (simple dictionary) and return a dictionary (or nil)
// Process userinfo (simple dictionary) and return a dictionary (or nil)
return nil;
}
}


- (void)handleSimpleMessageNamed:(NSString *)name {
- (void)handleSimpleMessageNamed:(NSString *)name {
    // ...
// ...
}
}
@end
</source>
</source>


== Client ==
=== Client ===
<source lang="objc">
<source lang="objc">
CPDistributedMessagingCenter *messagingCenter;
CPDistributedMessagingCenter *messagingCenter;
Line 35: Line 57:


// One-way (message only)
// One-way (message only)
[messagingCenter sendMessageName:@"message" userInfo:/* optional dictionary. in this example it will be ignored. */];
[messagingCenter sendMessageName:@"message" userInfo:nil/* optional dictionary. in this example it will be ignored. */];


// Two-way (wait for reply)
// Two-way (wait for reply)
NSDictionary *reply;
NSDictionary *reply;
reply = [messagingCenter sendMessageAndReceiveReplyName:@"messageThatHasInfo" userInfo:/* optional dictionary */];
reply = [messagingCenter sendMessageAndReceiveReplyName:@"messageThatHasInfo" userInfo:nil/* optional dictionary */];
</source>
</source>


== Header ==
== CPDistributedMessagingCenter as a MIG subsystem ==
* http://github.com/kennytm/iphone-private-frameworks/blob/master/AppSupport/CPDistributedMessagingCenter.h
The CPDistributedMessagingCenter is a complex wrapper on top of the MIG-generated RPC subsystem (<tt>_CPDMCPDistributedMessaging_subsystem</tt>). The center name will in fact be registered as ''the'' service name in the bootstrap name. Therefore, existing services like <tt>com.apple.springboard.services</tt> cannot be used as the center name.
 
This subsystem has only 2 routines: 79000 (CPDMMessage) and 79001 (CPDMTwoWayMessage).
 
== External links ==
 
{{IPFHeader|AppSupport}}

Latest revision as of 23:47, 12 May 2017

CPDistributedMessagingCenter is a wrapper over the existing messaging facilities in the operating system. It provides server-client communication between different processes using simple messages and dictionaries. All dictionaries transferred must be serializable as a property list.

Usage

Server

Here's an example implementation using a shared instance server class

@interface MyServerClass : NSObject
@end

@implementation MyServerClass

+ (void)load {
	[self sharedInstance];
}

+ (id)sharedInstance {
	static dispatch_once_t once = 0;
	__strong static id sharedInstance = nil;
	dispatch_once(&once, ^{
		sharedInstance = [[self alloc] init];
	});
	return sharedInstance;
}

- (id)init {
	if ((self = [super init])) {
		// ...
		// Center name must be unique, recommend using application identifier.
		CPDistributedMessagingCenter * messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];
		[messagingCenter runServerOnCurrentThread];

		// Register Messages
		[messagingCenter registerForMessageName:@"messageThatHasInfo" target:self selector:@selector(handleMessageNamed:withUserInfo:)];
		[messagingCenter registerForMessageName:@"message" target:self selector:@selector(handleSimpleMessageNamed:)];
	}

	return self;
}

- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo {
	// Process userinfo (simple dictionary) and return a dictionary (or nil)
	return nil;
}

- (void)handleSimpleMessageNamed:(NSString *)name {
	// ...
}
@end

Client

CPDistributedMessagingCenter *messagingCenter;
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];

// One-way (message only)
[messagingCenter sendMessageName:@"message" userInfo:nil/* optional dictionary. in this example it will be ignored. */];

// Two-way (wait for reply)
NSDictionary *reply;
reply = [messagingCenter sendMessageAndReceiveReplyName:@"messageThatHasInfo" userInfo:nil/* optional dictionary */];

CPDistributedMessagingCenter as a MIG subsystem

The CPDistributedMessagingCenter is a complex wrapper on top of the MIG-generated RPC subsystem (_CPDMCPDistributedMessaging_subsystem). The center name will in fact be registered as the service name in the bootstrap name. Therefore, existing services like com.apple.springboard.services cannot be used as the center name.

This subsystem has only 2 routines: 79000 (CPDMMessage) and 79001 (CPDMTwoWayMessage).

External links