Permalink
Browse files

Added a working client/server test app

  • Loading branch information...
1 parent fdbe89f commit 624eb4bebb2ce914f3258e16474bd99e041453a1 @stevestreza committed Jul 26, 2011
@@ -18,15 +18,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
connection = [[XPCConnection alloc] initWithServiceName:@"com.mustacheware.TestService"];
- connection.eventHandler = ^(id object){
- NSLog(@"Received an object! %@",object);
+ connection.eventHandler = ^(NSDictionary *object, XPCConnection *inConnection){
+ NSLog(@"Received a response! %@",object);
};
- NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"justsome" ofType:@"json"];
- NSLog(@"JSON path: %@",jsonPath);
+ // Loading a JSON file with a canned dictionary, see TestApp/multiply.json
+ NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"multiply" ofType:@"json"];
id data = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:jsonPath]
options:0
error:nil];
+
[connection sendMessage:data];
}
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,4 @@
+{
+ "operation": "multiply",
+ "values": [7, 1.5, 12.313123, 1.3192, 0.481429841983]
+}
View
@@ -7,51 +7,22 @@
//
#import <Foundation/Foundation.h>
-#include <xpc/xpc.h>
-#include <assert.h>
+#import <xpc/xpc.h>
#import "XPCKit.h"
-static void TestService_peer_event_handler(xpc_connection_t peer, xpc_object_t event)
-{
- printf("TestService peer event handler\n");
- xpc_type_t type = xpc_get_type(event);
- if (type == XPC_TYPE_ERROR) {
- if (event == XPC_ERROR_CONNECTION_INVALID) {
- // The client process on the other end of the connection has either
- // crashed or cancelled the connection. After receiving this error,
- // the connection is in an invalid state, and you do not need to
- // call xpc_connection_cancel(). Just tear down any associated state
- // here.
- } else if (event == XPC_ERROR_TERMINATION_IMMINENT) {
- // Handle per-connection termination cleanup.
- }
- } else {
- assert(type == XPC_TYPE_DICTIONARY);
- // Handle the message.
-
- xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
- xpc_dictionary_set_string(message, "message", "hello thar!");
- xpc_connection_send_message(peer, message);
-}
-}
-
-static void TestService_event_handler(xpc_connection_t peer)
-{
- // By defaults, new connections will target the default dispatch
- // concurrent queue.
- xpc_connection_set_event_handler(peer, ^(xpc_object_t event) {
- TestService_peer_event_handler(peer, event);
- });
-
- // This will tell the connection to begin listening for events. If you
- // have some other initialization that must be done asynchronously, then
- // you can defer this call until after that initialization is done.
- xpc_connection_resume(peer);
-}
-
int main(int argc, const char *argv[])
{
- NSLog(@"Hello world! XPC Main");
- [XPCListener listenForEvents];
+ [XPCListener listenForEventsWithHandler:^(NSDictionary *message, XPCConnection *connection) {
+ if([[message objectForKey:@"operation"] isEqual:@"multiply"]){
+ __block double product = 1.0;
+ NSArray *values = [message objectForKey:@"values"];
+
+ [values enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ product = product * [(NSNumber *)obj doubleValue];
+ }];
+
+ [connection sendMessage:[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:product] forKey:@"result"]];
+ }
+ }];
return 0;
}
@@ -42,7 +42,7 @@
1EEDD0AF13DE680500D5AEC3 /* XPCExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EEDD0AE13DE680500D5AEC3 /* XPCExtensions.h */; };
1EEDD0B213DE684300D5AEC3 /* NSArray+XPCParse.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EEDD0B013DE684300D5AEC3 /* NSArray+XPCParse.h */; };
1EEDD0B313DE684300D5AEC3 /* NSArray+XPCParse.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EEDD0B113DE684300D5AEC3 /* NSArray+XPCParse.m */; };
- 1EEDD0B513DE6CA700D5AEC3 /* justsome.json in Resources */ = {isa = PBXBuildFile; fileRef = 1EEDD0B413DE6C5100D5AEC3 /* justsome.json */; };
+ 1EEDD0B513DE6CA700D5AEC3 /* multiply.json in Resources */ = {isa = PBXBuildFile; fileRef = 1EEDD0B413DE6C5100D5AEC3 /* multiply.json */; };
1EEDD0B713DE79D200D5AEC3 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD0B613DE79D200D5AEC3 /* Foundation.framework */; };
1EEDD0BA13DE7A1000D5AEC3 /* libXPCKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1EEDD02813DD485400D5AEC3 /* libXPCKit.a */; };
1EEDD0C213DE7CD900D5AEC3 /* XPCListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 1EEDD0C013DE7CD900D5AEC3 /* XPCListener.h */; };
@@ -137,7 +137,7 @@
1EEDD0AE13DE680500D5AEC3 /* XPCExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPCExtensions.h; sourceTree = "<group>"; };
1EEDD0B013DE684300D5AEC3 /* NSArray+XPCParse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+XPCParse.h"; sourceTree = "<group>"; };
1EEDD0B113DE684300D5AEC3 /* NSArray+XPCParse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+XPCParse.m"; sourceTree = "<group>"; };
- 1EEDD0B413DE6C5100D5AEC3 /* justsome.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = justsome.json; sourceTree = "<group>"; };
+ 1EEDD0B413DE6C5100D5AEC3 /* multiply.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = multiply.json; sourceTree = "<group>"; };
1EEDD0B613DE79D200D5AEC3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1EEDD0C013DE7CD900D5AEC3 /* XPCListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XPCListener.h; sourceTree = "<group>"; };
1EEDD0C113DE7CD900D5AEC3 /* XPCListener.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XPCListener.m; sourceTree = "<group>"; };
@@ -290,7 +290,7 @@
1EEDD06213DD48BB00D5AEC3 /* main.m */,
1EEDD06413DD48BB00D5AEC3 /* TestApp-Prefix.pch */,
1EEDD06513DD48BB00D5AEC3 /* Credits.rtf */,
- 1EEDD0B413DE6C5100D5AEC3 /* justsome.json */,
+ 1EEDD0B413DE6C5100D5AEC3 /* multiply.json */,
);
name = "Supporting Files";
sourceTree = "<group>";
@@ -475,7 +475,7 @@
1EEDD06113DD48BB00D5AEC3 /* InfoPlist.strings in Resources */,
1EEDD06713DD48BB00D5AEC3 /* Credits.rtf in Resources */,
1EEDD06D13DD48BC00D5AEC3 /* MainMenu.xib in Resources */,
- 1EEDD0B513DE6CA700D5AEC3 /* justsome.json in Resources */,
+ 1EEDD0B513DE6CA700D5AEC3 /* multiply.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -31,12 +31,12 @@ -(xpc_object_t)newXPCObject{
if(self == (NSNumber *)kCFBooleanTrue){
return xpc_bool_create(true);
}else if(self == (NSNumber *)kCFBooleanTrue){
- return xpc_bool_create(true);
+ return xpc_bool_create(false);
}else{
const char* objCType = [self objCType];
- if(strcmp(objCType, @encode(unsigned long))){
+ if(strcmp(objCType, @encode(unsigned long)) == 0){
return xpc_uint64_create([self unsignedLongValue]);
- }else if(strcmp(objCType, @encode(long))){
+ }else if(strcmp(objCType, @encode(long)) == 0){
return xpc_int64_create([self longValue]);
}else{
return xpc_double_create([self doubleValue]);
View
@@ -13,6 +13,8 @@
xpc_connection_t _connection;
}
+- (id)initWithServiceName:(NSString *)serviceName;
+
@property (nonatomic, readonly) NSString *serviceName;
@property (nonatomic, retain) XPCEventHandler eventHandler;
View
@@ -55,7 +55,7 @@ -(void)receiveConnection:(xpc_connection_t)connection{
}else{
id message = [NSObject objectWithXPCObject: object];
if(this.eventHandler){
- this.eventHandler(message);
+ this.eventHandler(message, this);
}
}
});
@@ -74,7 +74,7 @@ -(void)sendMessage:(NSDictionary *)dictMessage{
NSDate *date = [NSDate date];
message = [dictMessage newXPCObject];
- NSLog(@"Message encoding took %gs on average", [[NSDate date] timeIntervalSinceDate:date]);
+ NSLog(@"Message encoding took %gs on average - %@", [[NSDate date] timeIntervalSinceDate:date], dictMessage);
xpc_connection_send_message(_connection, message);
xpc_release(message);
View
@@ -10,6 +10,6 @@
@interface XPCListener : XPCConnection
-+(void)listenForEvents;
++(void)listenForEventsWithHandler:(XPCEventHandler)eventHandler;
@end
View
@@ -10,21 +10,27 @@
#import "XPCExtensions.h"
@interface XPCListener (Private)
-@property (nonatomic, copy) NSString *serviceName;
-(id)initWithConnection:(xpc_connection_t)connection;
@end
+static XPCEventHandler sStaticEventHandler = NULL;
+
static void XPCListener_EventHandler(xpc_connection_t handler);
static void XPCListener_EventHandler(xpc_connection_t handler){
XPCListener *listener = [[XPCListener alloc] initWithConnection:handler];
- [listener setEventHandler:[[^(NSDictionary *message){
- [listener sendMessage:(NSDictionary *)[NSString stringWithFormat:@"Hello there! You sent %i items.", [(NSArray *)[message objectForKey:@"contents"] count]]];
- } copy] autorelease]];
+ [listener setEventHandler:^(NSDictionary *message, XPCConnection *connection){
+ if(sStaticEventHandler){
+ sStaticEventHandler(message, connection);
+ }
+ }];
}
@implementation XPCListener
-+(void)listenForEvents{
++(void)listenForEventsWithHandler:(XPCEventHandler)eventHandler{
+ if(!eventHandler) return;
+
+ sStaticEventHandler = [eventHandler copy];
xpc_main(XPCListener_EventHandler);
}
View
@@ -8,4 +8,5 @@
#import <Foundation/Foundation.h>
-typedef void (^XPCEventHandler)(NSDictionary *);
+@class XPCConnection;
+typedef void (^XPCEventHandler)(NSDictionary *, XPCConnection *);

0 comments on commit 624eb4b

Please sign in to comment.