From ece6ad364652c3fb63b3060c442ecedc3ff7ce08 Mon Sep 17 00:00:00 2001 From: CaS Date: Wed, 12 Jun 2002 11:14:04 +0000 Subject: [PATCH] New trivial test program git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13845 72102866-910b-0410-8b05-ffd578937521 --- Testing/GNUmakefile | 2 + Testing/call.m | 145 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 Testing/call.m diff --git a/Testing/GNUmakefile b/Testing/GNUmakefile index 37e856eae..9674a4be6 100644 --- a/Testing/GNUmakefile +++ b/Testing/GNUmakefile @@ -38,6 +38,7 @@ TEST_TOOL_NAME = \ awake \ basic \ benchmark \ + call \ containers \ create-abbrevs \ create-regions \ @@ -78,6 +79,7 @@ TEST_TOOL_NAME = \ awake_OBJC_FILES = awake.m basic_OBJC_FILES = basic.m benchmark_OBJC_FILES = benchmark.m +call_OBJC_FILES = call.m containers_OBJC_FILES = containers.m create-abbrevs_OBJC_FILES = create-abbrevs.m create-regions_OBJC_FILES = create-regions.m diff --git a/Testing/call.m b/Testing/call.m new file mode 100644 index 000000000..358b88bf6 --- /dev/null +++ b/Testing/call.m @@ -0,0 +1,145 @@ + +/* call - Program to test NSFileHandle TCP/IP connection. + + Copyright (C) 2002 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: Jun 2002 + + This file is part of the GNUstep Base Library. +*/ + +#include + +@interface Call : NSObject +{ + NSFileHandle *ichan; + NSFileHandle *ochan; + NSFileHandle *remote; +} +- (void) didRead: (NSNotification*)notification; +- (void) didWrite: (NSNotification*)notification; +@end + + +@implementation Call + +- (void) dealloc +{ + RELEASE(ichan); + RELEASE(ochan); + RELEASE(remote); + [super dealloc]; +} + +- (void) didRead: (NSNotification*)notification +{ + NSDictionary *userInfo = [notification userInfo]; + NSFileHandle *object = [notification object]; + NSData *d; + + d = [userInfo objectForKey: NSFileHandleNotificationDataItem]; + if (d == nil || [d length] == 0) + { + NSLog(@"Read EOF"); + exit(0); + } + else + { + if (object == ichan) + { + [remote writeInBackgroundAndNotify: d]; + [ichan readInBackgroundAndNotify]; + } + else + { + [ochan writeInBackgroundAndNotify: d]; + [remote readInBackgroundAndNotify]; + } + } +} + +- (void) didWrite: (NSNotification*)notification +{ + NSDictionary *userInfo = [notification userInfo]; + NSString *e; + + e = [userInfo objectForKey: GSFileHandleNotificationError]; + if (e) + { + NSLog(@"%@", e); + exit(0); + } +} + +- (id) init +{ + NSArray *args = [[NSProcessInfo processInfo] arguments]; + NSString *host = @"localhost"; + NSString *service = @"telnet"; + NSString *protocol = @"tcp"; + + if ([args count] > 1) + { + host = [args objectAtIndex: 1]; + if ([args count] > 2) + { + service = [args objectAtIndex: 2]; + if ([args count] > 3) + { + protocol = [args objectAtIndex: 3]; + } + } + } + ichan = RETAIN([NSFileHandle fileHandleWithStandardInput]); + ochan = RETAIN([NSFileHandle fileHandleWithStandardOutput]); + remote = RETAIN([NSFileHandle fileHandleAsClientAtAddress: + host service: service protocol: protocol]); + if (remote == nil) + { + NSLog(@"Failed to create connection"); + DESTROY(self); + } + else + { + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + + [nc addObserver: self + selector: @selector(didRead:) + name: NSFileHandleReadCompletionNotification + object: (id)ichan]; + [nc addObserver: self + selector: @selector(didWrite:) + name: GSFileHandleWriteCompletionNotification + object: (id)ochan]; + [nc addObserver: self + selector: @selector(didRead:) + name: NSFileHandleReadCompletionNotification + object: (id)remote]; + [nc addObserver: self + selector: @selector(didWrite:) + name: GSFileHandleWriteCompletionNotification + object: (id)remote]; + [remote readInBackgroundAndNotify]; + [ichan readInBackgroundAndNotify]; + } + return self; +} + +@end + + + +int +main() +{ + Call *console; + CREATE_AUTORELEASE_POOL(arp); + + console = [Call new]; + RELEASE(arp); + [[NSRunLoop currentRunLoop] run]; + RELEASE(console); + return 0; +} +