From 36938523060ef41f47d79e8616709e5fbc1483e8 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 2 Aug 2007 23:33:38 -0700 Subject: [PATCH] Adding a selector argument to receive: so that the delegate method can forward the response to that method. * Source/DebuggerConnection.m: ([DebuggerConnection dataReceived:deliverTo:]): Add a handling for when we have a delivery to make to a specified selector ([DebuggerConnection socketDidAccept]): Tell the initial packet to go to handshake: ([DebuggerConnection handshake:]): New method * Source/SocketWrapper.h: Update declarations for receive: dataReceived:deliverTo: and added _postNotification:withObject:withDict: * Source/SocketWrapper.m: ([SocketWrapper _sendMessageToDelegate:]): Added support for NsockDataReceived for the deliverTo: paramater ([SocketWrapper receive:]): Now takes an optional selector to deliver data to when it's finally received ([SocketWrapper _postNotification:withObject:withDict:]): New method ([SocketWrapper _postNotification:withObject:]): Now calls _postNotification:withObject:withDict: --- Source/DebuggerConnection.m | 19 ++++++++++++++++--- Source/SocketWrapper.h | 5 +++-- Source/SocketWrapper.m | 25 +++++++++++++++++++------ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index 19aad6d..7098918 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -84,8 +84,13 @@ /** * SocketWrapper delegate method that is called whenever new data is received */ -- (void)dataReceived: (NSString *)response +- (void)dataReceived: (NSString *)response deliverTo: (SEL)selector { + // if the caller of [_socket receive:] specified a deliverTo, just forward the message to them + if (selector != nil) + { + [self performSelector: selector withObject: response]; + } NSLog(@"response = %@", response); } @@ -104,8 +109,7 @@ */ - (void)socketDidAccept { - NSLog(@"accepted connection"); - [_socket receive]; + [_socket receive: @selector(handshake:)]; } /** @@ -116,4 +120,13 @@ NSLog(@"error = %@", error); } +/** + * The initial packet handshake. This allows us to set things like the title of the window + * and glean information about hte server we are debugging + */ +- (void)handshake: (NSString *)packet +{ + NSLog(@"packet = %@", packet); +} + @end diff --git a/Source/SocketWrapper.h b/Source/SocketWrapper.h index 2643ddf..0288077 100644 --- a/Source/SocketWrapper.h +++ b/Source/SocketWrapper.h @@ -30,7 +30,7 @@ - (void)setDelegate: (id)delegate; - (void)connect; -- (void)receive; +- (void)receive: (SEL)selector; - (void)send: (NSString *)data; @end @@ -45,11 +45,12 @@ - (void)socketDidAccept; // data handlers -- (void)dataReceived: (NSString *)response; +- (void)dataReceived: (NSString *)response deliverTo: (SEL)selector; - (void)dataSent; // ============== internal functions for threading - (void)_connect: (id)obj; - (void)_postNotification: (NSString *)name withObject: (id)obj; +- (void)_postNotification: (NSString *)name withObject: (id)obj withDict: (NSMutableDictionary *)dict; @end diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index 8e080ac..3e2132e 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -22,6 +22,7 @@ #include NSString *sockNotificationDebuggerConnection = @"DebuggerConnection"; +NSString *sockNotificationReceiver = @"SEL-del-SocketWrapper_dataReceived"; NSString *NsockError = @"SocketWrapper_Error"; NSString *NsockDidAccept = @"SocketWrapper_DidAccept"; NSString *NsockDataReceived = @"SocketWrapper_DataReceived"; @@ -94,7 +95,7 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; } else if (name == NsockDataReceived) { - [_delegate dataReceived: [notif object]]; + [_delegate dataReceived: [notif object] deliverTo: NSSelectorFromString([[notif userInfo] objectForKey: sockNotificationReceiver])]; } else if (name == NsockDataSent) { @@ -176,9 +177,9 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; * is used either in a threaded environment so the interface does not hang, or when you *know* the server * will return something (which we almost always do). * - * Data string returned is autorelease'd + * The paramater is an optional selector which the delegate method dataReceived:deliverTo: should forward to */ -- (void)receive +- (void)receive: (SEL)selector { // create a buffer char buffer[1024]; @@ -227,7 +228,11 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; } // convert the NSData into a NSString - [self _postNotification: NsockDataReceived withObject: [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease]]; + NSString *string = [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease]; + + [self _postNotification: NsockDataReceived + withObject: string + withDict: [NSMutableDictionary dictionaryWithObject: NSStringFromSelector(selector) forKey: sockNotificationReceiver]]; } /** @@ -255,8 +260,16 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; */ - (void)_postNotification: (NSString *)name withObject: (id)obj { - NSDictionary *dict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: _delegate, nil] - forKeys: [NSArray arrayWithObjects: sockNotificationDebuggerConnection, nil]]; + [self _postNotification: name withObject: obj withDict: [NSMutableDictionary dictionary]]; +} + +/** + * Another helper method to aid in the posting of notifications. This one should be used if you have additional + * things for the userInfo. This automatically adds the sockNotificationDebuggerConnection key. + */ +- (void)_postNotification: (NSString *)name withObject: (id)obj withDict: (NSMutableDictionary *)dict +{ + [dict setValue: _delegate forKey: sockNotificationDebuggerConnection]; [[NSNotificationCenter defaultCenter] postNotificationName: name object: obj userInfo: dict]; } -- 2.22.5