From 4e73407ac39a9b84854446e97a0ad9d6d98a4258 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 31 May 2010 22:20:39 -0400 Subject: [PATCH] Move the callTable back into DebuggerProcessor. --- Source/DebuggerConnection.h | 7 +--- Source/DebuggerConnection.m | 19 +++-------- Source/DebuggerProcessor.h | 7 +++- Source/DebuggerProcessor.m | 64 +++++++++++++++++++++++++++---------- 4 files changed, 60 insertions(+), 37 deletions(-) diff --git a/Source/DebuggerConnection.h b/Source/DebuggerConnection.h index 7c5d526..4c1f0dd 100644 --- a/Source/DebuggerConnection.h +++ b/Source/DebuggerConnection.h @@ -46,11 +46,6 @@ // The last transactionID written to the stream. NSUInteger lastWrittenTransaction_; - // Callback table. This maps transaction IDs to selectors. When the engine - // returns a response to the debugger, we will dispatch the response XML to - // the selector, based on transaction_id. - NSMutableDictionary* callTable_; - // To prevent blocked writing, we enqueue all writes and then wait for the // write stream to tell us it's ready. We store the pending commands in this // array. We use this as a stack (FIFO), with index 0 being first. @@ -89,7 +84,7 @@ - (void)handleResponse:(NSXMLDocument*)response; - (void)handlePacket:(NSString*)packet; -- (NSNumber*)sendCommandWithCallback:(SEL)callback format:(NSString*)format, ...; +- (NSNumber*)sendCommandWithFormat:(NSString*)format, ...; - (void)sendQueuedWrites; diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index 296c42b..c4e1005 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -209,7 +209,6 @@ void SocketAcceptCallback(CFSocketRef socket, transactionID = 1; self.queuedWrites = [NSMutableArray array]; writeQueueLock_ = [NSRecursiveLock new]; - callTable_ = [NSMutableDictionary new]; } /** @@ -279,7 +278,6 @@ void SocketAcceptCallback(CFSocketRef socket, CFRelease(socket_); self.queuedWrites = nil; [writeQueueLock_ release]; - [callTable_ release]; } /** @@ -519,14 +517,10 @@ void SocketAcceptCallback(CFSocketRef socket, [delegate_ handleInitialResponse:response]; return; } - - NSString* callbackStr = [callTable_ objectForKey:[NSNumber numberWithInt:lastReadTransaction_]]; - if (callbackStr) - { - SEL callback = NSSelectorFromString(callbackStr); - [delegate_ performSelector:callback withObject:response]; - } - + + if ([delegate_ respondsToSelector:@selector(handleResponse:)]) + [(NSObject*)delegate_ performSelectorOnMainThread:@selector(handleResponse:) withObject:response waitUntilDone:NO]; + [self sendQueuedWrites]; } @@ -538,7 +532,7 @@ void SocketAcceptCallback(CFSocketRef socket, * a variable number of arguments to substitute into the command, a la * +[NSString stringWithFormat:]. Returns the transaction ID as a NSNumber. */ -- (NSNumber*)sendCommandWithCallback:(SEL)callback format:(NSString*)format, ... +- (NSNumber*)sendCommandWithFormat:(NSString*)format, ... { // Collect varargs and format command. va_list args; @@ -547,9 +541,6 @@ void SocketAcceptCallback(CFSocketRef socket, va_end(args); NSNumber* callbackKey = [NSNumber numberWithInt:transactionID++]; - if (callback) - [callTable_ setObject:NSStringFromSelector(callback) forKey:callbackKey]; - [self send:[NSString stringWithFormat:@"%@ -i %@", [command autorelease], callbackKey]]; return callbackKey; diff --git a/Source/DebuggerProcessor.h b/Source/DebuggerProcessor.h index e5a4ca6..f567af3 100644 --- a/Source/DebuggerProcessor.h +++ b/Source/DebuggerProcessor.h @@ -46,7 +46,12 @@ NSInteger stackDepth_; // The earliest transaction ID for the current build of |stackFrames_|. NSInteger stackFirstTransactionID_; - + + // Callback table. This maps transaction IDs to selectors. When the engine + // returns a response to the debugger, we will dispatch the response XML to + // the selector, based on transaction_id. + NSMutableDictionary* callTable_; + // This stores additional context information for the callback selector. // This dictionary is keyed by the same transaction IDs in |callTable_|, but // also stores some other object that can be accessed in the callback. diff --git a/Source/DebuggerProcessor.m b/Source/DebuggerProcessor.m index 5da5352..530dec1 100644 --- a/Source/DebuggerProcessor.m +++ b/Source/DebuggerProcessor.m @@ -23,6 +23,8 @@ @interface DebuggerProcessor () @property (readwrite, copy) NSString* status; +- (void)recordCallback:(SEL)callback forTransaction:(NSNumber*)txn; + - (void)updateStatus:(NSXMLDocument*)response; - (void)debuggerStep:(NSXMLDocument*)response; - (void)rebuildStack:(NSXMLDocument*)response; @@ -51,6 +53,7 @@ { stackFrames_ = [[NSMutableDictionary alloc] init]; callbackContext_ = [NSMutableDictionary new]; + callTable_ = [NSMutableDictionary new]; [[BreakpointManager sharedManager] setConnection:self]; connection_ = [[DebuggerConnection alloc] initWithPort:aPort]; @@ -67,6 +70,7 @@ { [connection_ close]; [stackFrames_ release]; + [callTable_ release]; [callbackContext_ release]; [super dealloc]; } @@ -122,7 +126,8 @@ */ - (void)run { - [connection_ sendCommandWithCallback:@selector(debuggerStep:) format:@"run"]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"run"]; + [self recordCallback:@selector(debuggerStep:) forTransaction:tx]; } /** @@ -130,7 +135,8 @@ */ - (void)stepIn { - [connection_ sendCommandWithCallback:@selector(debuggerStep:) format:@"step_into"]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"step_into"]; + [self recordCallback:@selector(debuggerStep:) forTransaction:tx]; } /** @@ -138,7 +144,8 @@ */ - (void)stepOut { - [connection_ sendCommandWithCallback:@selector(debuggerStep:) format:@"step_out"]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"step_out"]; + [self recordCallback:@selector(debuggerStep:) forTransaction:tx]; } /** @@ -146,7 +153,8 @@ */ - (void)stepOver { - [connection_ sendCommandWithCallback:@selector(debuggerStep:) format:@"step_over"]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"step_over"]; + [self recordCallback:@selector(debuggerStep:) forTransaction:tx]; } /** @@ -155,7 +163,8 @@ */ - (NSInteger)getProperty:(NSString*)property { - [connection_ sendCommandWithCallback:@selector(propertiesReceived:) format:@"property_get -n \"%@\"", property]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"property_get -n \"%@\"", property]; + [self recordCallback:@selector(propertiesReceived:) forTransaction:tx]; } // Breakpoint Management /////////////////////////////////////////////////////// @@ -170,9 +179,9 @@ return; NSString* file = [connection_ escapedURIPath:[bp transformedPath]]; - NSNumber* transaction = [connection_ sendCommandWithCallback:@selector(breakpointReceived:) - format:@"breakpoint_set -t line -f %@ -n %i", file, [bp line]]; - [callbackContext_ setObject:bp forKey:transaction]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"breakpoint_set -t line -f %@ -n %i", file, [bp line]]; + [self recordCallback:@selector(breakpointReceived:) forTransaction:tx]; + [callbackContext_ setObject:bp forKey:tx]; } /** @@ -183,7 +192,7 @@ if (![connection_ connected]) return; - [connection_ sendCommandWithCallback:nil format:@"breakpoint_remove -d %i", [bp debuggerId]]; + [connection_ sendCommandWithFormat:@"breakpoint_remove -d %i", [bp debuggerId]]; } // Specific Response Handlers ////////////////////////////////////////////////// @@ -204,6 +213,17 @@ // TODO: update the status. } +- (void)handleResponse:(NSXMLDocument*)response +{ + NSInteger transactionID = [connection_ transactionIDFromResponse:response]; + NSString* callbackStr = [callTable_ objectForKey:[NSNumber numberWithInt:transactionID]]; + if (callbackStr) + { + SEL callback = NSSelectorFromString(callbackStr); + [self performSelector:callback withObject:response]; + } +} + /** * Receiver for status updates. This just freshens up the UI. */ @@ -238,7 +258,9 @@ if ([delegate respondsToSelector:@selector(clobberStack)]) [delegate clobberStack]; [stackFrames_ removeAllObjects]; - stackFirstTransactionID_ = [[connection_ sendCommandWithCallback:@selector(rebuildStack:) format:@"stack_depth"] intValue]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"stack_depth"]; + [self recordCallback:@selector(rebuildStack:) forTransaction:tx]; + stackFirstTransactionID_ = [tx intValue]; } } @@ -258,7 +280,8 @@ for (NSInteger i = 0; i < depth; i++) { // Use the transaction ID to create a routing path. - NSNumber* routingID = [connection_ sendCommandWithCallback:@selector(getStackFrame:) format:@"stack_get -d %d", i]; + NSNumber* routingID = [connection_ sendCommandWithFormat:@"stack_get -d %d", i]; + [self recordCallback:@selector(getStackFrame:) forTransaction:routingID]; [stackFrames_ setObject:[StackFrame alloc] forKey:routingID]; } } @@ -292,11 +315,13 @@ // Get the source code of the file. Escape % in URL chars. NSString* escapedFilename = [frame.filename stringByReplacingOccurrencesOfString:@"%" withString:@"%%"]; - NSNumber* transaction = [connection_ sendCommandWithCallback:@selector(setSource:) format:@"source -f %@", escapedFilename]; + NSNumber* transaction = [connection_ sendCommandWithFormat:@"source -f %@", escapedFilename]; + [self recordCallback:@selector(setSource:) forTransaction:transaction]; [callbackContext_ setObject:routingNumber forKey:transaction]; // Get the names of all the contexts. - transaction = [connection_ sendCommandWithCallback:@selector(contextsReceived:) format:@"context_names -d %d", frame.index]; + transaction = [connection_ sendCommandWithFormat:@"context_names -d %d", frame.index]; + [self recordCallback:@selector(contextsReceived:) forTransaction:transaction]; [callbackContext_ setObject:routingNumber forKey:transaction]; if ([delegate respondsToSelector:@selector(newStackFrame:)]) @@ -350,9 +375,9 @@ NSInteger cid = [[[context attributeForName:@"id"] stringValue] intValue]; // Fetch each context's variables. - NSNumber* transaction = [connection_ sendCommandWithCallback:@selector(variablesReceived:) - format:@"context_get -d %d -c %d", frame.index, cid]; - [callbackContext_ setObject:routingID forKey:transaction]; + NSNumber* tx = [connection_ sendCommandWithFormat:@"context_get -d %d -c %d", frame.index, cid]; + [self recordCallback:@selector(variablesReceived:) forTransaction:tx]; + [callbackContext_ setObject:routingID forKey:tx]; } } @@ -424,4 +449,11 @@ [bp setDebuggerId:[[[[response rootElement] attributeForName:@"id"] stringValue] intValue]]; } +// Private ///////////////////////////////////////////////////////////////////// + +- (void)recordCallback:(SEL)callback forTransaction:(NSNumber*)txn +{ + [callTable_ setObject:NSStringFromSelector(callback) forKey:txn]; +} + @end -- 2.43.5