mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
{{occlass|library=AppSupport.framework}} | {{occlass|library=AppSupport.framework}} | ||
[[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 | [[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. | ||
== Server == | == Server == | ||
<source lang="objc"> | <source lang="objc"> | ||
-(id)init... { | |||
... | |||
// Register Messages | CPDistributedMessagingCenter *messagingCenter; | ||
[messagingCenter registerForMessageName:@"messageThatHasInfo" target:self selector:@selector(handleMessageNamed:withUserInfo:)]; | // Center name must be unique, recommend using application identifier. | ||
[messagingCenter registerForMessageName:@"message" target:self selector:@selector(handleSimpleMessageNamed:)]; | 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:)]; | |||
... | |||
} | |||
- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo { | - (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo { |
Revision as of 18:40, 22 September 2009
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.
Server
-(id)init... {
...
CPDistributedMessagingCenter *messagingCenter;
// Center name must be unique, recommend using application identifier.
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:)];
...
}
- (NSDictionary *)handleMessageNamed:(NSString *)name withUserInfo:(NSDictionary *)userinfo {
// Process userinfo (simple dictionary) and return a dictionary (or nil)
}
- (void)handleSimpleMessageNamed:(NSString *)name {
// ...
}
Client
CPDistributedMessagingCenter *messagingCenter;
messagingCenter = [CPDistributedMessagingCenter centerNamed:@"unique.name.for.messaging.center"];
// One-way (message only)
[messagingCenter sendMessageName:@"message" userInfo:/* optional dictionary. in this example it will be ignored. */];
// Two-way (wait for reply)
NSDictionary *reply;
reply = [messagingCenter sendMessageAndReceiveReplyName:@"messageThatHasInfo" userInfo:/* optional dictionary */];