Adding a stream handler function that should be able to read from streams
[macgdbp.git] / Source / DebuggerConnection.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2002 - 2007, 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 "DebuggerConnection.h"
18
19
20 @implementation DebuggerConnection
21
22 /**
23 * Creates a new DebuggerConnection and initializes the socket from the given connection
24 * paramters.
25 */
26 - (id)initWithHost: (NSString *)host port: (int)port session: (NSString *)session
27 {
28 NSLog(@"initWithHost");
29 if (self = [super init])
30 {
31 _host = [host retain];
32 _port = port;
33 _session = [session retain];
34
35 _windowController = [[DebuggerWindowController alloc] initWithConnection: self];
36 [[_windowController window] makeKeyAndOrderFront: self];
37
38 // now that we have our host information, open the streams and put them in the run loop
39 [NSStream getStreamsToHost: [NSHost hostWithName: _host] port: _port inputStream: &_input outputStream: &_output];
40 [_input retain];
41 [_output retain];
42
43 [_input setDelegate: self];
44 [_output setDelegate: self];
45
46 [_input scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
47 [_output scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
48
49 [_input open];
50 [_output open];
51
52 // clean up after ourselves
53 [[NSNotificationCenter defaultCenter] addObserver: self
54 selector: @selector(applicationWillTerminate:)
55 name: NSApplicationWillTerminateNotification
56 object: NSApp];
57 }
58 return self;
59 }
60
61 /**
62 * Release ourselves when we're about to die
63 */
64 - (void)applicationWillTerminate: (NSNotification *)notif
65 {
66 [self release];
67 }
68
69 /**
70 * Releases all of the object's data members and closes the streams
71 */
72 - (void)dealloc
73 {
74 [_host release];
75 [_session release];
76 [_input close];
77 [_output close];
78 [_input release];
79 [_output release];
80
81 [super dealloc];
82 }
83
84 /**
85 * Gets the hostname
86 */
87 - (NSString *)host
88 {
89 return _host;
90 }
91
92 /**
93 * Gets the port number
94 */
95 - (int)port
96 {
97 return _port;
98 }
99
100 /**
101 * Gets the session name
102 */
103 - (NSString *)session
104 {
105 return _session;
106 }
107
108 /**
109 * Handles stream events. This is the delegate method implemented for NSStream and it
110 * merely calls other methods to do it's bidding
111 */
112 - (void)stream: (NSStream *)stream handleEvent: (NSStreamEvent)event
113 {
114 NSLog(@"hi");
115 if (event == NSStreamEventHasBytesAvailable)
116 {
117 if (!_data)
118 {
119 _data = [[NSMutableData data] retain];
120 }
121 uint8_t buf[1024];
122 unsigned int len = 0;
123 len = [(NSInputStream *)stream read: buf maxLength: 1024];
124 if (len)
125 {
126 [_data appendBytes: (const void *)buf length: len];
127 }
128 else
129 {
130 [self _readFromStream: _data];
131 [_data release];
132 _data = nil;
133 }
134 }
135 else if (event == NSStreamEventEndEncountered)
136 {
137 NSLog(@"we need to close and die right now");
138 }
139 else if (event == NSStreamEventErrorOccurred)
140 {
141 NSLog(@"error = %@", [stream streamError]);
142 }
143 NSLog(@"status = %d", [stream streamStatus]);
144 }
145
146 /**
147 * Called when the stream event handler has finished reading all of the data and
148 * passes it a data object
149 */
150 - (void)_readFromStream: (NSData *)data
151 {
152 [data retain];
153 NSLog(@"data = %@", data);
154 }
155
156 @end