]> src.bluestatic.org Git - macgdbp.git/blob - Source/ProtocolClient.m
CFBundleVersion is no longer incremented by gitcrement.
[macgdbp.git] / Source / ProtocolClient.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2013, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "ProtocolClient.h"
18
19 #import "AppDelegate.h"
20 #import "LoggingController.h"
21
22 @implementation ProtocolClient {
23 // The object responsible for the actual communication with the debug server.
24 MessageQueue* _messageQueue;
25
26 // The delegate of this class, which receives high-level messages about the
27 // state of the debugger.
28 id<ProtocolClientDelegate> _delegate; // weak
29
30 // A map between transaction ID and handler block for that message.
31 NSMutableDictionary<NSNumber*, ProtocolClientMessageHandler>* _dispatchTable;
32
33 // The next transaction ID to assign.
34 int _nextID;
35
36 // Records the last read and written transaction IDs. These are only used in
37 // creating LogEntry objects.
38 NSInteger _lastReadID;
39 NSInteger _lastWrittenID;
40 }
41
42 - (id)initWithDelegate:(id<ProtocolClientDelegate>)delegate {
43 if ((self = [super init])) {
44 _delegate = delegate;
45 _dispatchTable = [[NSMutableDictionary alloc] init];
46 }
47 return self;
48 }
49
50 - (void)dealloc {
51 [_dispatchTable release];
52 [super dealloc];
53 }
54
55 - (BOOL)isConnected {
56 return [_messageQueue isConnected];
57 }
58
59 - (void)connectOnPort:(NSUInteger)port {
60 assert(!_messageQueue);
61 _messageQueue = [[MessageQueue alloc] initWithPort:port delegate:self];
62 [_messageQueue connect];
63 }
64
65 - (void)disconnect {
66 [_messageQueue disconnect];
67 }
68
69 - (void)sendCommandWithFormat:(NSString*)format, ... {
70 // Collect varargs and format command.
71 va_list args;
72 va_start(args, format);
73 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
74 va_end(args);
75
76 [self sendCommandWithFormat:command handler:^(NSXMLDocument* message){}];
77 }
78
79 - (void)sendCommandWithFormat:(NSString*)format
80 handler:(ProtocolClientMessageHandler)handler, ... {
81 // Collect varargs and format command.
82 va_list args;
83 va_start(args, handler);
84 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
85 va_end(args);
86
87 int transaction = _nextID++;
88 NSString* taggedCommand = [NSString stringWithFormat:@"%@ -i %d", [command autorelease], transaction];
89
90 assert(_messageQueue);
91 [_dispatchTable setObject:[[handler copy] autorelease] forKey:@(transaction)];
92 [_messageQueue sendMessage:taggedCommand];
93 }
94
95 - (void)sendCustomCommandWithFormat:(NSString*)format
96 handler:(ProtocolClientMessageHandler)handler, ... {
97 // Collect varargs and format command.
98 va_list args;
99 va_start(args, handler);
100 NSString* command = [[[NSString alloc] initWithFormat:format arguments:args] autorelease];
101 va_end(args);
102
103 int transaction = _nextID++;
104 NSString* taggedCommand =
105 [command stringByReplacingOccurrencesOfString:@"{txn}"
106 withString:[NSString stringWithFormat:@"%d", transaction]];
107
108 assert(_messageQueue);
109 [_dispatchTable setObject:[[handler copy] autorelease] forKey:@(transaction)];
110 [_messageQueue sendMessage:taggedCommand];
111 }
112
113
114 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response {
115 return [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
116 }
117
118 - (NSInteger)transactionIDFromCommand:(NSString*)command {
119 NSRange occurrence = [command rangeOfString:@"-i "];
120 if (occurrence.location == NSNotFound)
121 return NSNotFound;
122 NSString* transaction = [command substringFromIndex:occurrence.location + occurrence.length];
123 return [transaction intValue];
124 }
125
126 + (NSString*)escapedFilePathURI:(NSString*)path {
127 // Custon GDBp paths are fine.
128 if ([[path substringToIndex:4] isEqualToString:@"gdbp"])
129 return path;
130
131 // Create a temporary URL that will escape all the nasty characters.
132 NSURL* url = [NSURL fileURLWithPath:path];
133 NSString* urlString = [url absoluteString];
134
135 // Remove the host because this is a file:// URL;
136 NSString* host = [url host];
137 if (host)
138 urlString = [urlString stringByReplacingOccurrencesOfString:[url host] withString:@""];
139
140 // Escape % for use in printf-style NSString formatters.
141 urlString = [urlString stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
142 return urlString;
143 }
144
145 // MessageQueueDelegate ////////////////////////////////////////////////////////
146
147 - (void)messageQueue:(MessageQueue*)queue error:(NSError*)error {
148 NSLog(@"error = %@", error);
149 }
150
151 - (void)messageQueueDidConnect:(MessageQueue*)queue {
152 _nextID = 0;
153 _lastReadID = 0;
154 _lastWrittenID = 0;
155
156 [_delegate debuggerEngineConnected:self];
157 }
158
159 - (void)messageQueueDidDisconnect:(MessageQueue*)queue {
160 [_messageQueue release];
161 _messageQueue = nil;
162 [_dispatchTable removeAllObjects];
163 [_delegate debuggerEngineDisconnected:self];
164 }
165
166 // Callback for when a message has been sent.
167 - (void)messageQueue:(MessageQueue*)queue didSendMessage:(NSString*)message {
168 NSInteger tag = [self transactionIDFromCommand:message];
169 _lastWrittenID = tag;
170
171 LoggingController* logger = [[AppDelegate instance] loggingController];
172 LogEntry* entry = [LogEntry newSendEntry:message];
173 entry.lastReadTransactionID = _lastReadID;
174 entry.lastWrittenTransactionID = _lastWrittenID;
175 [logger recordEntry:[entry autorelease]];
176 }
177
178 // Callback with the message content when one has been receieved.
179 - (void)messageQueue:(MessageQueue*)queue didReceiveMessage:(NSString*)message {
180 // Record this message in the transaction log.
181 LoggingController* logger = [[AppDelegate instance] loggingController];
182 LogEntry* entry = [LogEntry newReceiveEntry:message];
183 entry.lastReadTransactionID = _lastReadID;
184 entry.lastWrittenTransactionID = _lastWrittenID;
185 [logger recordEntry:[entry autorelease]];
186
187 // Parse the XML and test for errors.
188 NSError* error = nil;
189 NSXMLDocument* xml = [[[NSXMLDocument alloc] initWithXMLString:message
190 options:NSXMLDocumentTidyXML
191 error:&error] autorelease];
192 if (error) {
193 [self messageQueue:queue error:error];
194 return;
195 }
196 NSInteger transactionID = [self transactionIDFromResponse:xml];
197
198 _lastReadID = transactionID;
199 entry.lastReadTransactionID = _lastReadID;
200
201 if ([[[xml rootElement] elementsForName:@"error"] count] > 0) {
202 // Handle back-end errors.
203 [_delegate protocolClient:self receivedErrorMessage:xml];
204 } else if ([[[xml rootElement] name] isEqualToString:@"init"]) {
205 // Handle the initial connection message.
206 [_delegate protocolClient:self receivedInitialMessage:xml];
207 } else {
208 // Dispatch the handler for the message.
209 ProtocolClientMessageHandler handler = [_dispatchTable objectForKey:@(transactionID)];
210 handler(xml);
211 [_dispatchTable removeObjectForKey:@(transactionID)];
212 }
213 }
214
215 @end