File: nsconnection_client.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (666 lines) | stat: -rw-r--r-- 16,204 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/* Test/example program for the base library

   Copyright (C) 2005 Free Software Foundation, Inc.
   
  Copying and distribution of this file, with or without modification,
  are permitted in any medium without royalty provided the copyright
  notice and this notice are preserved.

   This file is part of the GNUstep Base Library.
*/
#include <stdio.h>
#include <Foundation/NSObject.h>
#include <Foundation/NSConnection.h>
#include <Foundation/NSPort.h>
#include <Foundation/NSPortNameServer.h>
#include <Foundation/NSDistantObject.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSString.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSData.h>
#include <Foundation/NSDate.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSDebug.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSException.h>
#include <Foundation/NSUserDefaults.h>
#include <assert.h>
#include "server.h"

#include "wgetopt.h"

/*
 * Dummy declaration with different bycopy/byref info from the one
 * in the server ... we expect the info from the server to be used.
 */
@interface	Dummy : NSObject
- (id) quietBycopy: (byref id)a;
@end

@interface	CallbackClient : NSObject <ClientProtocol>
- (BOOL) callback;
@end

@implementation	CallbackClient
- (BOOL) callback
{
  return YES;
}
@end


@interface	Auth : NSObject
@end

@implementation	Auth
- (BOOL) authenticateComponents: (NSMutableArray*)components
		       withData: (NSData*)authData
{
  unsigned	count = [components count];

  while (count-- > 0)
    {
      id	obj = [components objectAtIndex: count];

      if ([obj isKindOfClass: [NSData class]] == YES)
	{
	  NSMutableData	*d = [obj mutableCopy];
	  unsigned	l = [d length];
	  char		*p = (char*)[d mutableBytes];

	  while (l-- > 0)
	    p[l] ^= 42;
	  [components replaceObjectAtIndex: count withObject: d];
	  RELEASE(d);
	}
    }
  return YES;
}
@end

int con_data (id prx)
{
  id	pool;
  BOOL b, br;
  unsigned char uc, ucr;
  char c, cr;
  short s, sr;
  int i, ir;
  long l, lr;
  float flt, fltr;
  double dbl, dblr;
  char *str;
  id obj;
  small_struct ss = {12};
  foo ffoo = {'Z', 1234.5678, 99, "cow", 9876543};
  foo bck;

  printf("Testing data sending\n");

  printf("Boolean:\n");
  b = YES;
  printf("  sending %d", b);
  br = [prx sendBoolean: b];
  printf(" got %d", br);
  if (b == !br)
    printf(" ...ok\n");
  else
    printf(" *** ERROR ***\n");
  br = b = YES;
  printf("  sending ptr to %d", br);
  [prx getBoolean: &br];
  printf(" got %d", br);
  if (b == !br)
    printf(" ...ok\n");
  else
    printf(" *** ERROR ***\n");
  printf("  error is ok (due to incorrect encoding by gcc)\n");

#define TEST_CALL(test, send, got, sendp, var, varr, val, msg1, msg2)	\
  pool = [NSAutoreleasePool new];					\
  printf(test);								\
  var = val;								\
  printf(send, var);							\
  varr = [prx msg1 var];						\
  printf(got, varr);							\
  if (varr != (var+ADD_CONST))					\
    printf(" *** ERROR ***\n");						\
  else									\
    printf(" ...ok\n");							\
  varr = var = val+1;							\
  printf(sendp, varr);							\
  [prx msg2 &varr];							\
  printf(got, varr);							\
  if (varr != (var+ADD_CONST))					\
    printf(" *** ERROR ***\n");						\
  else									\
    printf(" ...ok\n");							\
  [pool release];

#define TEST_FCALL(test, send, got, sendp, var, varr, val, msg1, msg2)	\
  pool = [NSAutoreleasePool new];					\
  printf(test);								\
  var = val;								\
  printf(send, var);							\
  varr = [prx msg1 var];						\
  printf(got, varr);							\
  if (varr - (var+ADD_CONST) > 1e-3)					\
    printf(" *** ERROR ***\n");						\
  else									\
    printf(" ...ok\n");							\
  varr = var = val+1;							\
  printf(sendp, varr);							\
  [prx msg2 &varr];							\
  printf(got, varr);							\
  if (varr - (var+ADD_CONST) > 1e-3)					\
    printf(" *** ERROR ***\n");						\
  else									\
    printf(" ...ok\n");							\
  [pool release];

  TEST_CALL("UChar:\n", "  sending %d", " got %d", "  sending ptr to %d",
	    uc, ucr, 23, sendUChar:, getUChar:)
  printf("  error is ok (due to incorrect encoding by gcc)\n");

  TEST_CALL("Char:\n", "  sending %d", " got %d", "  sending ptr to %d",
	    c, cr, 23, sendChar:, getChar:)
  printf("  error is ok (due to incorrect encoding by gcc)\n");

  TEST_CALL("Short:\n", "  sending %hd", " got %hd", "  sending ptr to %hd",
	    s, sr, 23, sendShort:, getShort:)

  TEST_CALL("Int:\n", "  sending %d", " got %d", "  sending ptr to %d",
	    i, ir, 23, sendInt:, getInt:)

  TEST_CALL("Long:\n", "  sending %ld", " got %ld", "  sending ptr to %ld",
	    l, lr, 23, sendLong:, getLong:)

  TEST_FCALL("Float:\n", "  sending %f", " got %f", "  sending ptr to %f",
	    flt, fltr, 23.2, sendFloat:, getFloat:)

  TEST_FCALL("Double:\n", "  sending %g", " got %g", "  sending ptr to %g",
	    dbl, dblr, 23.2, sendDouble:, getDouble:)

  flt = 2.718;
  dbl = 3.14159265358979323846264338327;
  printf("  sending double %f, float %f\n", dbl, flt);
  [prx sendDouble:dbl andFloat:flt];


  pool = [NSAutoreleasePool new];
  printf("String:\n");
  str = "My String 1";
  printf("  sending (%s)", str);
  str = [prx sendString: str];
  printf(" got (%s)\n", str);
  [pool release];

  pool = [NSAutoreleasePool new];
  str = "My String 3";
  printf("  sending ptr to (%s)", str);
  [prx getString: &str];
  printf(" got (%s)\n", str);
  [pool release];

  pool = [NSAutoreleasePool new];
  printf("Small Struct:\n");
  //printf("  sending %x", ss.z);
  //ss = [prx sendSmallStruct: ss];
  //printf(" got %x\n", ss.z);
  printf("  sending ptr to %x", ss.z);
  [prx getSmallStruct: &ss];
  printf(" got %x\n", ss.z);
  [pool release];

#if 1 || !defined(__MINGW__)
  pool = [NSAutoreleasePool new];
  printf("Struct:\n");
  memcpy(&bck, &ffoo, sizeof(bck));
  printf("  sending c='%c',d=%g,i=%d,s=%s,l=%ld",
    ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
  ffoo = [prx sendStruct: ffoo];
  printf(" got c='%c',d=%g,i=%d,s=%s,l=%ld\n",
    ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
  memcpy(&ffoo, &bck, sizeof(bck));
  printf("  sending ptr to  c='%c',d=%g,i=%d,s=%s,l=%ld",
    ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
  [prx getStruct: &ffoo];
  printf(" got c='%c',d=%g,i=%d,s=%s,l=%ld\n",
    ffoo.c, ffoo.d, ffoo.i, ffoo.s, ffoo.l);
  [pool release];
#endif

  pool = [NSAutoreleasePool new];
  printf("Object:\n");
  obj = [NSObject new];
  [prx addObject: obj];  // FIXME: Why is this needed?
  printf("  sending %s", [[obj description] cString]);
  obj = [prx sendObject: obj];
  printf(" got %s\n", [[obj description] cString]);
  printf("  sending ptr to %s", [[obj description] cString]);
  [prx getObject: &obj];
  printf(" got %s\n",  [[obj description] cString]);
  [pool release];

  printf("Many Arguments:\n");
  [prx manyArgs:1 :2 :3 :4 :5 :6 :7 :8 :9 :10 :11 :12];

  printf("Done\n");
  return 0;
}

int
con_messages (id prx)
{
  id obj;
  Protocol	*pc = @protocol(ClientProtocol);
  Protocol	*ps = @protocol(ServerProtocol);

  obj = [NSObject new];

  printf("Conforms to protocol (remote) should be 1: %d\n",
    [prx conformsToProtocol: ps]);
  printf("Conforms to protocol (remote) should be 0: %d\n",
    [prx conformsToProtocol: pc]);

  [prx setProtocolForProxy: ps];

  printf("Conforms to protocol (local) should be 1: %d\n",
    [prx conformsToProtocol: ps]);
  printf("Conforms to protocol (local) should be 0: %d\n",
    [prx conformsToProtocol: pc]);

  printf("Oneway Void message:\n");
  [prx shout];
  printf("  ok\n");

  printf("Testing exception in method with return value:\n");
  NS_DURING
    {
      [prx exceptionTest1];
      printf("  ERROR\n");
    }
  NS_HANDLER
    {
      printf("  ok ... %s\n", [[localException description] cString]);
    }
  NS_ENDHANDLER

  printf("Testing exception in method with void return:\n");
  NS_DURING
    {
      [prx exceptionTest2];
      printf("  ERROR\n");
    }
  NS_HANDLER
    {
      printf("  ok ... %s\n", [[localException description] cString]);
    }
  NS_ENDHANDLER

  printf("Testing exception in oneway void method:\n");
  NS_DURING
    {
      [prx exceptionTest3];
      printf("  ok\n");
    }
  NS_HANDLER
    {
      printf("  ERROR ... %s\n", [[localException description] cString]);
    }
  NS_ENDHANDLER

  /* this next line doesn't actually test callbacks, it tests
     sending the same object twice in the same message. */
  printf("Send same object twice in message\n");
  [prx sendObject: prx];
  printf("  ok\n");

  printf("performSelector:\n");
  if (prx != [prx performSelector: GSSelectorFromName("self")])
    printf("  ERROR\n");
  else
    printf("  ok\n");

  printf("Testing bycopy/byref:\n");
  [prx sendBycopy: obj];
  [prx quietBycopy: obj];

#ifdef	_F_BYREF
  [prx sendByref: obj];
  [prx sendByref: @"hello"];
  [prx sendByref: [NSDate date]];
  {
    NSMutableString	*str = [NSMutableString string];

    [prx modifyByref: str];
    printf("  Modified '%s'\n", [str lossyCString]);
  }
#endif
  printf("  ok\n");

  printf("Done\n");
  return 0;
}

int
con_benchmark (id prx)
{
  int i;
  NSDate	  *d = [NSDate date];
  NSMutableData *sen = [NSMutableData data];
  id localObj;
  id rep;

  printf("Benchmarking\n");
  [sen setLength: 100000];
  rep = [prx sendObject: sen];
  printf("  Sent: 0x%p, Reply: 0x%p, Length: %d\n", sen, rep, [rep length]);

  localObj = [[NSObject alloc] init];
  [prx addObject: localObj];  // FIXME: Why is this needed?
  for (i = 0; i < 10000; i++)
    {
#if 0
      k = [prx count];
      for (j = 0; j < k; j++)
	{
	  id remote_peer_obj = [prx objectAt: j];
	}
#endif
      [prx echoObject: localObj];
    }

  printf("  Delay is %f\n", [d timeIntervalSinceNow]);
  printf("Done\n");
  return 0;
}

int
con_statistics (id prx)
{
  int j;
  id localObj, cobj, a, o;

  printf("------------------------------------------------------------\n");
  printf("Printing Statistics\n");
  localObj = [[NSObject alloc] init];
  [prx outputStats: localObj];
  printf("  >>list proxy's hash is 0x%d\n", [prx hash]);
  printf("  >>list proxy's self is 0x%p = 0x%p\n", [prx self], prx);
  printf("  >>proxy's description is (%s)\n", [[prx description] lossyCString]);

  cobj = [prx connectionForProxy];
  o = [cobj statistics];
  a = [o allKeys];

  for (j = 0; j < [a count]; j++)
    {
      id k = [a objectAtIndex:j];
      id v = [o objectForKey:k];

      printf("  %s - %s\n", [k cString], [[v description] cString]);
    }
  printf("------------------------------------------------------------\n");

  return 0;
}

int
con_loop (id prx)
{
  NSAutoreleasePool *arp;
  id cobj;

  arp = [NSAutoreleasePool new];
  [prx addObject: [NSObject new]];  // So loss of this connection is logged.
  cobj = [prx connectionForProxy];
  printf("%d\n", [cobj retainCount]);
  printf("%s\n", [[[cobj statistics] description] cString]);
  //printf("%s\n", GSDebugAllocationList(YES));

  printf("loop left running idle for 30 seconds\n");
  [[NSRunLoop currentRunLoop] runUntilDate:
    [NSDate dateWithTimeIntervalSinceNow: 30]];
  [cobj invalidate];
  [arp release];
  return 0;
}

int
con_objects (id prx)
{
  int j, k;
  id localObj;

  localObj = [NSObject new];
  [prx addObject:localObj];
  k = [prx count];
  for (j = 0; j < k; j++)
    {
      id remote_peer_obj = [prx objectAt:j];
      printf("triangle %d object proxy's hash is 0x%x\n",
	     j, (unsigned)[remote_peer_obj hash]);

#if 0
      /* xxx look at this again after we use release/retain everywhere */
      if ([remote_peer_obj isProxy])
	[remote_peer_obj release];
#endif
      remote_peer_obj = [prx objectAt:j];
      printf("repeated triangle %d object proxy's hash is 0x%x\n",
	     j, (unsigned)[remote_peer_obj hash]);
    }
  return 0;
}

int
con_callback (id prx)
{
  int j, k;
  id localObj;

  localObj = [CallbackClient new];
  [prx registerClient: localObj];
  k = 1000;
  for (j = 0; j < k; j++)
    {
      ENTER_POOL
      [prx unregisterClient: localObj];
      [prx registerClient: localObj];
      [prx tryClientCallback];
      if (j < 10 || j %10 == 0)
	printf("repeated client registration and callback %d\n", j);
      LEAVE_POOL
    }
  printf("repeated client registration and callback %d\n", j);
  RELEASE(localObj);
  return 0;
}

void
usage(const char *program)
{
  printf("Usage: %s [-ds] [t|b|m|l|o] [host] [server]\n", program);
  printf("  -d     - Debug connection\n");
  printf("  -s     - Print Statistics\n");
  printf("  -t     - Data type test [default]\n");
  printf("  -b     - Benchmark test\n");
  printf("  -m     - Messaging test\n");
  printf("  -l     - Loop test\n");
  printf("  -o     - Objects test\n");
  printf("  -c     - Connect test\n");
  printf("  -r     - Registration and callback test\n");
}

typedef enum {
  NO_TEST, TYPE_TEST, BENCHMARK_TEST, MESSAGE_TEST,
  LOOP_TEST, OBJECT_TEST, CONNECT_TEST, CALLBACK_TEST
} test_t;

int main (int argc, char *argv[], char **env)
{
  int c, debug, stats;
  test_t type_test;
  id cobj, prx;
  unsigned	connect_attempts;
  NSAutoreleasePool	*arp;
  NSPortNameServer	*ns;
  Auth *auth;
#if	!defined(__MINGW__)
  extern int optind;
  extern char *optarg;
#endif

  [NSProcessInfo initializeWithArguments: argv count: argc environment: env];
  arp = [NSAutoreleasePool new];
  auth = [Auth new];
  GSDebugAllocationActive(YES);

  setvbuf(stdout, 0, _IONBF, 0);
  debug = 0;
  type_test = 0;
  stats = 0;
  while ((c = wgetopt(argc, argv, "hdtbmslocr")) != EOF)
    switch (c)
      {
      case 'd':
	debug++;
	break;
      case 't':
	type_test = TYPE_TEST;
	break;
      case 'b':
	type_test = BENCHMARK_TEST;
	break;
      case 'm':
	type_test = MESSAGE_TEST;
	break;
      case 's':
	stats = 1;
	break;
      case 'l':
	type_test = LOOP_TEST;
	break;
      case 'o':
	type_test = OBJECT_TEST;
	break;
      case 'c':
	type_test = CONNECT_TEST;
	break;
      case 'r':
	type_test = CALLBACK_TEST;
	break;
      case 'h':
	usage(argv[0]);
	exit(0);
	break;
      default:
#if 0
	usage(argv[0]);
	exit(1);
#endif
	break;
      }
  if (type_test == NO_TEST)
    type_test = TYPE_TEST;

  if (type_test == CONNECT_TEST)
    connect_attempts = 100000;
  else
    connect_attempts = 1;

  if (debug > 0)
    {
      [NSConnection setDebug: debug];
      [NSDistantObject setDebug: debug];
      [NSObject enableDoubleReleaseCheck: YES];
    }

  ns = [NSSocketPortNameServer sharedInstance];
  while (connect_attempts-- > 0)
    {
      if (optind < argc)
	{
	  if (optind+1 < argc)
	    prx = [NSConnection rootProxyForConnectionWithRegisteredName:
				  [NSString stringWithCString: argv[optind+1]]
			    host: [NSString stringWithCString:argv[optind]]
		 usingNameServer: ns];
	  else
	    prx = [NSConnection rootProxyForConnectionWithRegisteredName:
				 @"test2server"
			    host:[NSString stringWithCString:argv[optind]]
		 usingNameServer: ns];
	}
      else
	prx = [NSConnection rootProxyForConnectionWithRegisteredName:
		@"test2server" host: @""
		    usingNameServer: ns];
      if (prx == nil)
	{
	  printf("ERROR: Failed to connect to server\n");
	  return -1;
	}
      if (type_test == CONNECT_TEST)
	{
	  NSLog(@"Made connection\n");
	  if (connect_attempts > 0)
	    {
	      RELEASE(arp);
	      arp = [NSAutoreleasePool new];
	    }
	}
    }

#if 1
  /* Check that we can retain the connection, release the proxy,
   * and then regain the proxy from the connection.
   */
  cobj = RETAIN([prx connectionForProxy]);
  RELEASE(arp);
  arp = [NSAutoreleasePool new];
  prx = [cobj rootProxy];
  AUTORELEASE(cobj);
#else
  cobj = [prx connectionForProxy];
#endif

  [cobj setDelegate:auth];
  [cobj setRequestTimeout:180.0];
  [cobj setReplyTimeout:180.0];

  [prx print: "This is a message from the client. Starting Tests!"];

  switch (type_test)
    {
    case TYPE_TEST:
      con_data (prx);
      break;
    case BENCHMARK_TEST:
      con_benchmark (prx);
      break;
    case MESSAGE_TEST:
      con_messages (prx);
      break;
    case LOOP_TEST:
      con_loop (prx);
      break;
    case OBJECT_TEST:
      con_objects (prx);
      break;
    case CALLBACK_TEST:
      con_callback (prx);
      break;
    default:
      break;
    }

  if (stats)
    con_statistics (prx);

  [cobj invalidate];

  [arp release];
  return 0;
}