home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / Foundation / TCPTransport / TCPTransport_main.m < prev   
Text File  |  1996-04-17  |  5KB  |  172 lines

  1. /*
  2.  * You may freely copy, distribute and reuse the code in this example.
  3.  * NeXT Software, Inc. disclaims any warranty of any kind, expressed or implied,
  4.  * as to its fitness for any particular use.  This disclaimer applies to all
  5.  * source files in this example.
  6.  */
  7.  
  8. // Test program for TCPPort
  9. //
  10. // On hosts moe, larry, and curly
  11. //  larry:
  12. //    TCPTransport
  13. // 
  14. //  curly:
  15. //    TCPTransport larry
  16. //
  17. //  moe:
  18. //    TCPTransport larry
  19. //
  20. //
  21. // If 3 hosts aren't available, on one host try:
  22. //   shell1>  TCPTransport
  23. //
  24. //   shell2>  TCPTransport -l 10001
  25. //
  26. //   shell3>  TCPTransport -l 10002
  27. //
  28. // Other options:
  29. // -l <port-number>      listen on this port instead of the default (10000)
  30. // -s <port-number>      send to this port instead of to the default (10000)
  31. // -v                    verbose - print log message from TCPPort
  32. // -V                    very verbose - print log messages from DO and TCPPort
  33.  
  34. #import <Foundation/Foundation.h>
  35. #import "TCPPort.h"
  36. #import "RemoteTest.h"
  37.  
  38. @interface NSConnection (NSConnectionLogging)
  39. + (void)_toggleLogging;
  40. @end
  41.  
  42. @interface TCPPort (TCPPortLogging)
  43. + (void)_toggleLogging;
  44. @end
  45.  
  46. #define DefaultTCPPortNumber 10000
  47.  
  48. void main() {
  49.     NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init];    // first thing
  50.     NSArray *args = [[NSProcessInfo processInfo] arguments];
  51.     unsigned int i, len = [args count];
  52.     RemoteTest *testObject;
  53.     RemoteTest *otherObject;
  54.     NSConnection *conn;
  55.     TCPPort *listener, *sender;
  56.     NSString *serverName;
  57.     NSString *hostName;
  58.     unsigned short listenPort, sendPort;
  59.     BOOL original, verbosePorts, verboseEverything;
  60.  
  61.  
  62.     original = YES;
  63.     verbosePorts = NO;
  64.     verboseEverything = NO;
  65.     listenPort = DefaultTCPPortNumber;
  66.     sendPort = DefaultTCPPortNumber;
  67.     hostName = nil;
  68.     serverName = nil;
  69.  
  70.     for (i = 1; i < len; i++) {
  71.         if ([[args objectAtIndex:i] isEqual:@"-l"])
  72.             listenPort = [[args objectAtIndex:++i] intValue];
  73.         else if ([[args objectAtIndex:i] isEqual:@"-s"])
  74.             sendPort = [[args objectAtIndex:++i] intValue];
  75.         else if ([[args objectAtIndex:i] isEqual:@"-v"])
  76.             verbosePorts = YES;
  77.         else if ([[args objectAtIndex:i] isEqual:@"-V"])
  78.             verboseEverything = YES;
  79.         else {
  80.             serverName = [args objectAtIndex:i];
  81.             original = NO;
  82.         }
  83.     }
  84.  
  85.     if (verbosePorts || verboseEverything) [TCPPort _toggleLogging];
  86.     if (verboseEverything) [NSConnection _toggleLogging];
  87.  
  88.     listener = [[TCPPort alloc] initWithNumber:listenPort];
  89.     if (listener == nil) {
  90.         NSLog(@"couldn't create a listener!");
  91.         [pool release];
  92.         exit(1);
  93.     }
  94.     NSLog(@"listening on TCP port: %@", [listener description]);
  95.  
  96.     conn = [NSConnection connectionWithReceivePort:listener sendPort:listener];
  97.     if (conn == nil) {
  98.         NSLog(@"couldn't create a connection!");
  99.         [pool release];
  100.         exit(1);
  101.     }
  102.     
  103.     if (original) {
  104.         testObject = [[RemoteTest alloc] init];
  105.         NSLog(@"Setting root object");
  106.         [conn setRootObject:testObject];
  107.         [testObject release];    // let conn own it
  108.     }
  109.     else {
  110.         if (serverName == nil) serverName = [[NSHost currentHost] name];
  111.         sender = [listener connectToNumber:sendPort host:serverName];
  112.         if (sender == nil) {
  113.             NSLog(@"couldn't create a sender (%@ %d)!", serverName, sendPort);
  114.             [pool release];
  115.             exit(1);
  116.         }
  117.  
  118.         NSLog(@"connecting to: %@", [sender description]);
  119.  
  120.         conn = [NSConnection connectionWithReceivePort:listener
  121.                                               sendPort:sender];
  122.         if (conn == nil) {
  123.             NSLog(@"couldn't create a connection!");
  124.             [pool release];
  125.             exit(1);
  126.         }
  127.  
  128.         NSLog(@"Fetching root object from host %@", serverName);
  129.         testObject = (RemoteTest *)[conn rootProxy];
  130.         if (!testObject) {
  131.             NSLog(@"couldn't get root proxy!");
  132.             [pool release];
  133.             exit(1);
  134.         }
  135.  
  136.         NSLog(@"Remote host: %@", [[testObject host] name]);
  137.         NSLog(@"Remote date: %@", [[testObject date] description]);
  138.  
  139.         NSLog(@"Sending hello message to host %@", serverName);
  140.         hostName = [[NSHost currentHost] name];
  141.         [testObject log:
  142.             [NSString stringWithFormat:@"Hello from %@!", hostName]];
  143.  
  144.         NSLog(@"Fetching other object from host %@", serverName);
  145.         otherObject = (RemoteTest *)[testObject otherObject];
  146.         if (otherObject == nil) NSLog(@"Other object is nil");
  147.         else {
  148.             NSLog(@"Other object host: %@", [[otherObject host] name]);
  149.             NSLog(@"Other object date: %@", [[otherObject date] description]);
  150.             NSLog(@"Sending hello message to host %@",
  151.                   [[otherObject host] name]);
  152.             [otherObject log:
  153.                 [NSString stringWithFormat:@"Hello from %@!", hostName]];
  154.         }
  155.  
  156.         NSLog(@"Setting other object on host %@ to my object", serverName);
  157.         [testObject setOtherObject:[[[RemoteTest alloc] init] autorelease]];
  158.  
  159.     }
  160.  
  161.     /* become a server */
  162.     
  163.  
  164.  
  165.     NSLog(@"Starting run loop");
  166.     [[NSRunLoop currentRunLoop] run];
  167.     NSLog(@"exiting server!");
  168.  
  169.     [pool release];
  170.     exit(0);
  171. }
  172.