~ubuntu-branches/ubuntu/trusty/gnustep-base/trusty

« back to all changes in this revision

Viewing changes to Testing/call.m

Tags: upstream-1.20.0
ImportĀ upstreamĀ versionĀ 1.20.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/* call - Program to test NSFileHandle TCP/IP connection.
3
 
 
4
 
   Copyright (C) 2002 Free Software Foundation, Inc.
5
 
 
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.
9
 
 
10
 
   Written by:  Richard Frith-Macdonald <rfm@gnu.org>
11
 
   Date: Jun 2002
12
 
        
13
 
   This file is part of the GNUstep Base Library.
14
 
*/
15
 
 
16
 
#include <Foundation/Foundation.h>
17
 
 
18
 
GS_EXPORT NSString * const GSTelnetNotification;
19
 
GS_EXPORT NSString * const GSTelnetErrorKey;
20
 
GS_EXPORT NSString * const GSTelnetTextKey;
21
 
@class GSTelnetHandle;
22
 
 
23
 
@interface Call : NSObject
24
 
{
25
 
  NSFileHandle          *ichan;
26
 
  NSFileHandle          *ochan;
27
 
  id                    remote;
28
 
  NSMutableData         *buf;
29
 
}
30
 
- (void) didRead: (NSNotification*)notification;
31
 
- (void) didWrite: (NSNotification*)notification;
32
 
- (void) gotTelnet: (NSNotification*)notification;
33
 
@end
34
 
 
35
 
 
36
 
@implementation Call
37
 
 
38
 
- (void) dealloc
39
 
{
40
 
  RELEASE(ichan);
41
 
  RELEASE(ochan);
42
 
  RELEASE(remote);
43
 
  RELEASE(buf);
44
 
  [super dealloc];
45
 
}
46
 
 
47
 
- (void) didRead: (NSNotification*)notification
48
 
{
49
 
  NSDictionary  *userInfo = [notification userInfo];
50
 
  NSData        *d;
51
 
 
52
 
  d = [userInfo objectForKey: NSFileHandleNotificationDataItem];
53
 
  if (d == nil || [d length] == 0)
54
 
    {
55
 
      NSLog(@"Read EOF");
56
 
      exit(0);
57
 
    }
58
 
  else
59
 
    {
60
 
      char      *ptr;
61
 
      unsigned  len;
62
 
      int       i;
63
 
 
64
 
      [buf appendData: d];
65
 
      ptr = [buf mutableBytes];
66
 
      len = [buf length];
67
 
      for (i = 0; i < len; i++)
68
 
        {
69
 
          if (ptr[i] == '\n')
70
 
            {
71
 
              NSString  *s;
72
 
 
73
 
              if (i > 0 && ptr[i-1] == '\r')
74
 
                {
75
 
                  s = [[NSString alloc] initWithBytes: ptr
76
 
                                               length: i-1
77
 
                                             encoding: NSUTF8StringEncoding];
78
 
                }
79
 
              else
80
 
                {
81
 
                  s = [[NSString alloc] initWithBytes: ptr
82
 
                                               length: i
83
 
                                             encoding: NSUTF8StringEncoding];
84
 
                }
85
 
              len -= (i + 1);
86
 
              if (len > 0)
87
 
                {
88
 
                  memcpy(ptr, &ptr[i+1], len);
89
 
                }
90
 
              [buf setLength: len];
91
 
              ptr = [buf mutableBytes];
92
 
              i = -1;
93
 
              [remote putTelnetLine: s];
94
 
              [s release];
95
 
            }
96
 
        }
97
 
      [ichan readInBackgroundAndNotify];
98
 
    }
99
 
}
100
 
 
101
 
- (void) didWrite: (NSNotification*)notification
102
 
{
103
 
  NSDictionary  *userInfo = [notification userInfo];
104
 
  NSString      *e;
105
 
 
106
 
  e = [userInfo objectForKey: GSFileHandleNotificationError];
107
 
  if (e)
108
 
    {
109
 
      NSLog(@"%@", e);
110
 
      exit(0);
111
 
    }
112
 
}
113
 
 
114
 
- (void) gotTelnet: (NSNotification*)notification
115
 
{
116
 
  NSDictionary  *info = [notification userInfo];
117
 
  NSArray       *text;
118
 
 
119
 
  text = [info objectForKey: GSTelnetTextKey];
120
 
  if (text == nil)
121
 
    {
122
 
      NSLog(@"Lost telnet - %@", [info objectForKey: GSTelnetErrorKey]);
123
 
      exit(0);
124
 
    }
125
 
  else
126
 
    {
127
 
      unsigned  i;
128
 
 
129
 
      for (i = 0; i < [text count]; i++)
130
 
        {
131
 
          [ochan writeInBackgroundAndNotify:
132
 
            [[text objectAtIndex: i] dataUsingEncoding: NSUTF8StringEncoding]];
133
 
        }
134
 
    }
135
 
}
136
 
 
137
 
- (id) init
138
 
{
139
 
  NSArray       *args = [[NSProcessInfo processInfo] arguments];
140
 
  NSString      *host = @"localhost";
141
 
  NSString      *service = @"telnet";
142
 
  NSString      *protocol = @"tcp";
143
 
 
144
 
  if ([args count] > 1)
145
 
    {
146
 
      host = [args objectAtIndex: 1];
147
 
      if ([args count] > 2)
148
 
        {
149
 
          service = [args objectAtIndex: 2];
150
 
          if ([args count] > 3)
151
 
            {
152
 
              protocol = [args objectAtIndex: 3];
153
 
            }
154
 
        }
155
 
    }
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];
162
 
  if (remote == nil)
163
 
    {
164
 
      NSLog(@"Failed to create connection");
165
 
      DESTROY(self);
166
 
    }
167
 
  else
168
 
    {
169
 
      NSNotificationCenter      *nc = [NSNotificationCenter defaultCenter];
170
 
 
171
 
      [nc addObserver: self
172
 
             selector: @selector(didRead:)
173
 
                 name: NSFileHandleReadCompletionNotification
174
 
               object: ichan];
175
 
      [nc addObserver: self
176
 
             selector: @selector(didWrite:)
177
 
                 name: GSFileHandleWriteCompletionNotification
178
 
               object: ochan];
179
 
      [nc addObserver: self
180
 
             selector: @selector(gotTelnet:)
181
 
                 name: GSTelnetNotification
182
 
               object: remote];
183
 
      [ichan readInBackgroundAndNotify];
184
 
    }
185
 
  return self;
186
 
}
187
 
 
188
 
@end
189
 
 
190
 
 
191
 
 
192
 
int
193
 
main()
194
 
{
195
 
  Call  *console;
196
 
  CREATE_AUTORELEASE_POOL(arp);
197
 
 
198
 
  console = [Call new];
199
 
  RELEASE(arp);
200
 
  [[NSRunLoop currentRunLoop] run];
201
 
  RELEASE(console);
202
 
  return 0;
203
 
}
204