3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
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.
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.
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
17 #import "NetworkConnection.h"
19 // This is the private interface for the NetworkConnection class. This is shared
20 // by the C++ NetworkCallbackController to communicate.
21 @interface NetworkConnection (Private
)
23 - (void)handleResponse
:(NSXMLDocument
*)response
;
25 // Threadsafe wrappers for the delegate's methods.
26 - (void)errorEncountered
:(NSString
*)error
;
31 ////////////////////////////////////////////////////////////////////////////////
33 @implementation NetworkConnection
35 @synthesize port
= port_
;
36 @synthesize connected
= connected_
;
37 @synthesize delegate
= delegate_
;
39 - (id)initWithPort
:(NSUInteger
)aPort
41 if (self = [super initWithDelegate
:self]) {
49 * Kicks off the socket on another thread.
53 [_ideClient connectOnPort
:port_
];
58 [_ideClient disconnect
];
61 - (void)debuggerEngineConnected
:(ProtocolClient
*)client
63 if ([delegate_ respondsToSelector
:@selector(connectionDidAccept
:)])
64 [delegate_ connectionDidAccept
:self];
67 - (void)debuggerEngineDisconnected
:(ProtocolClient
*)client
69 if ([delegate_ respondsToSelector
:@selector(connectionDidClose
:)])
70 [delegate_ connectionDidClose
:self];
73 - (void)debuggerEngine
:(ProtocolClient
*)client receivedMessage
:(NSXMLDocument
*)message
75 [self handleResponse
:message
];
85 * Given a file path, this returns a file:// URI and escapes any spaces for the
88 - (NSString
*)escapedURIPath
:(NSString
*)path
90 // Custon GDBp paths are fine.
91 if ([[path substringToIndex
:4] isEqualToString
:@
"gdbp"])
94 // Create a temporary URL that will escape all the nasty characters.
95 NSURL
* url
= [NSURL fileURLWithPath
:path
];
96 NSString
* urlString
= [url absoluteString
];
98 // Remove the host because this is a file:// URL;
99 urlString
= [urlString stringByReplacingOccurrencesOfString
:[url host
] withString
:@
""];
101 // Escape % for use in printf-style NSString formatters.
102 urlString
= [urlString stringByReplacingOccurrencesOfString
:@
"%" withString
:@
"%%"];
106 // Private /////////////////////////////////////////////////////////////////////
109 // Delegate Thread-Safe Wrappers ///////////////////////////////////////////////
112 * Receives errors from the SocketWrapper and updates the display
114 - (void)errorEncountered
:(NSString
*)error
116 if (![delegate_ respondsToSelector
:@selector(errorEncountered
:)])
118 [delegate_ performSelectorOnMainThread
:@selector(errorEncountered
:)
123 - (void)handleResponse
:(NSXMLDocument
*)response
125 // Check and see if there's an error.
126 NSArray
* error
= [[response rootElement
] elementsForName
:@
"error"];
127 if ([error count
] > 0) {
128 NSLog(@
"Xdebug error: %@", error
);
129 NSString
* errorMessage
= [[[[error objectAtIndex
:0] children
] objectAtIndex
:0] stringValue
];
130 [self errorEncountered
:errorMessage
];
133 if ([[[response rootElement
] name
] isEqualToString
:@
"init"]) {
135 [delegate_ handleInitialResponse
:response
];
139 if ([delegate_ respondsToSelector
:@selector(handleResponse
:)])
140 [delegate_ handleResponse
:response
];