From 5842014aeaf81e1e029a8c00740663802500eab8 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 3 Aug 2007 16:11:52 -0700 Subject: [PATCH] Adding functionality to get the IP address of the remote host and then using it in the title * Source/DebuggerConnection.m|h: Added a _connected ivar to check and see whether or not the connection has been established ([DebuggerConnection remoteHost]): New function that returns the name of the remote host * Source/DebuggerConnection.m: ([DebuggerConnection initWithPort:]): Do not set the window title anymore ([DebuggerConnection setStatus:]): Set the window title upon status updates * Source/SocketWrapper.m|h: ([SocketWrapper remoteHost]): New method that gets the remote host's IP address --- Source/DebuggerConnection.h | 2 ++ Source/DebuggerConnection.m | 14 ++++++++++++++ Source/DebuggerWindowController.m | 3 +-- Source/SocketWrapper.h | 2 ++ Source/SocketWrapper.m | 18 ++++++++++++++++++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Source/DebuggerConnection.h b/Source/DebuggerConnection.h index f81f3d0..3e746cb 100644 --- a/Source/DebuggerConnection.h +++ b/Source/DebuggerConnection.h @@ -22,6 +22,7 @@ { int _port; NSString *_session; + BOOL _connected; DebuggerWindowController *_windowController; @@ -34,6 +35,7 @@ // getter - (int)port; - (NSString *)session; +- (NSString *)remoteHost; // communication - (void)run; diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index 69ab596..f393c3a 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -28,6 +28,7 @@ { _port = port; _session = [session retain]; + _connected = NO; _windowController = [[DebuggerWindowController alloc] initWithConnection: self]; [[_windowController window] makeKeyAndOrderFront: self]; @@ -82,6 +83,18 @@ return _session; } +/** + * Returns the name of the remote host + */ +- (NSString *)remoteHost +{ + if (!_connected) + { + return @"(DISCONNECTED)"; + } + return [_socket remoteHost]; +} + /** * SocketWrapper delegate method that is called whenever new data is received */ @@ -110,6 +123,7 @@ */ - (void)socketDidAccept { + _connected = YES; [_socket receive: @selector(_handshake:)]; } diff --git a/Source/DebuggerWindowController.m b/Source/DebuggerWindowController.m index f165c5c..4e8c1b2 100644 --- a/Source/DebuggerWindowController.m +++ b/Source/DebuggerWindowController.m @@ -27,8 +27,6 @@ if (self = [super initWithWindowNibName: @"Debugger"]) { _connection = [cnx retain]; - // TODO - get the host from SocketWrapper and use that in the title - [[self window] setTitle: [NSString stringWithFormat: @"GDBp @ localhost:%d/%@", [_connection port], [_connection session]]]; } return self; } @@ -50,6 +48,7 @@ { [_error setHidden: YES]; [_status setStringValue: status]; + [[self window] setTitle: [NSString stringWithFormat: @"GDBp @ %@:%d/%@", [_connection remoteHost], [_connection port], [_connection session]]]; } /** diff --git a/Source/SocketWrapper.h b/Source/SocketWrapper.h index 2906025..5dda2fb 100644 --- a/Source/SocketWrapper.h +++ b/Source/SocketWrapper.h @@ -33,6 +33,8 @@ - (void)receive: (SEL)selector; - (void)send: (NSString *)data; +- (NSString *)remoteHost; + // ============== internal functions for threading - (void)_connect: (id)obj; - (void)_postNotification: (NSString *)name withObject: (id)obj; diff --git a/Source/SocketWrapper.m b/Source/SocketWrapper.m index 78200ec..1f5417a 100644 --- a/Source/SocketWrapper.m +++ b/Source/SocketWrapper.m @@ -73,6 +73,24 @@ NSString *NsockDataSent = @"SocketWrapper_DataSent"; _delegate = delegate; } +/** + * Returns the name of the host to whom we are currently connected. + */ +- (NSString *)remoteHost +{ + struct sockaddr_in addr; + socklen_t addrLength; + + if (getpeername(_socket, (struct sockaddr *)&addr, &addrLength) < 0) + { + [self _postNotification: NsockError withObject: [NSError errorWithDomain: @"Could not get remote hostname." code: -1 userInfo: nil]]; + } + + char *name = inet_ntoa(addr.sin_addr); + + return [NSString stringWithUTF8String: name]; +} + /** * This is the notification listener for all types of notifications. If the notifications are from a SocketWrapper * class, it checks that the value of _delegate in the NSNotification's userInfo matches that of this object. If it does, -- 2.22.5