2
/* call - Program to test NSFileHandle TCP/IP connection.
4
Copyright (C) 2002 Free Software Foundation, Inc.
6
Copying and distribution of this file, with or without modification,
7
are permitted in any medium without royalty provided the copyright
8
notice and this notice are preserved.
10
Written by: Richard Frith-Macdonald <rfm@gnu.org>
13
This file is part of the GNUstep Base Library.
16
#include <Foundation/Foundation.h>
18
GS_EXPORT NSString * const GSTelnetNotification;
19
GS_EXPORT NSString * const GSTelnetErrorKey;
20
GS_EXPORT NSString * const GSTelnetTextKey;
21
@class GSTelnetHandle;
23
@interface Call : NSObject
30
- (void) didRead: (NSNotification*)notification;
31
- (void) didWrite: (NSNotification*)notification;
32
- (void) gotTelnet: (NSNotification*)notification;
47
- (void) didRead: (NSNotification*)notification
49
NSDictionary *userInfo = [notification userInfo];
52
d = [userInfo objectForKey: NSFileHandleNotificationDataItem];
53
if (d == nil || [d length] == 0)
65
ptr = [buf mutableBytes];
67
for (i = 0; i < len; i++)
73
if (i > 0 && ptr[i-1] == '\r')
75
s = [[NSString alloc] initWithBytes: ptr
77
encoding: NSUTF8StringEncoding];
81
s = [[NSString alloc] initWithBytes: ptr
83
encoding: NSUTF8StringEncoding];
88
memcpy(ptr, &ptr[i+1], len);
91
ptr = [buf mutableBytes];
93
[remote putTelnetLine: s];
97
[ichan readInBackgroundAndNotify];
101
- (void) didWrite: (NSNotification*)notification
103
NSDictionary *userInfo = [notification userInfo];
106
e = [userInfo objectForKey: GSFileHandleNotificationError];
114
- (void) gotTelnet: (NSNotification*)notification
116
NSDictionary *info = [notification userInfo];
119
text = [info objectForKey: GSTelnetTextKey];
122
NSLog(@"Lost telnet - %@", [info objectForKey: GSTelnetErrorKey]);
129
for (i = 0; i < [text count]; i++)
131
[ochan writeInBackgroundAndNotify:
132
[[text objectAtIndex: i] dataUsingEncoding: NSUTF8StringEncoding]];
139
NSArray *args = [[NSProcessInfo processInfo] arguments];
140
NSString *host = @"localhost";
141
NSString *service = @"telnet";
142
NSString *protocol = @"tcp";
144
if ([args count] > 1)
146
host = [args objectAtIndex: 1];
147
if ([args count] > 2)
149
service = [args objectAtIndex: 2];
150
if ([args count] > 3)
152
protocol = [args objectAtIndex: 3];
156
buf = [NSMutableData new];
157
ichan = RETAIN([NSFileHandle fileHandleWithStandardInput]);
158
ochan = RETAIN([NSFileHandle fileHandleWithStandardOutput]);
159
remote = [[GSTelnetHandle alloc] initWithHandle:
160
[NSFileHandle fileHandleAsClientAtAddress:
161
host service: service protocol: protocol] isConnected: YES];
164
NSLog(@"Failed to create connection");
169
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
171
[nc addObserver: self
172
selector: @selector(didRead:)
173
name: NSFileHandleReadCompletionNotification
175
[nc addObserver: self
176
selector: @selector(didWrite:)
177
name: GSFileHandleWriteCompletionNotification
179
[nc addObserver: self
180
selector: @selector(gotTelnet:)
181
name: GSTelnetNotification
183
[ichan readInBackgroundAndNotify];
196
CREATE_AUTORELEASE_POOL(arp);
198
console = [Call new];
200
[[NSRunLoop currentRunLoop] run];