home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / Authenticator_main.m < prev    next >
Encoding:
Text File  |  1996-04-17  |  2.6 KB  |  86 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. /* Authenticator main
  9.  
  10.    Builds an executable with two personas
  11.        - run with no arguments to assume the server role
  12.        (and then)
  13.        - run with the name of your machine to assume the client role
  14.  */
  15.  
  16. #import <Foundation/Foundation.h>
  17. #import "Authenticator.h"
  18.  
  19. void client(NSString *hostname) {
  20.     NSConnection *conn;
  21.     NSDistantObject *proxy;
  22.  
  23.     // find the other process via the nmserver.
  24.     // Note: no actual messages are sent yet
  25.     conn = [NSConnection connectionWithRegisteredName:@"authentication test" host:hostname];
  26.     if (!conn) {
  27.         NSLog(@"didn't connect");
  28.         exit(1);
  29.     }
  30.          // set up authenticator
  31.          // all subsequent DO messages (including the one for rootProxy)
  32.          // will have some authetication data generated and sent along.
  33.          // Also (not required), all replies will be verified.
  34.          // In a real system the authentication data would include a user name etc.
  35.          // and a real password protected signature generator would get involved.
  36.     [conn setDelegate:[Authenticator class]];
  37.  
  38.     // This message will actually cause some message traffic
  39.     proxy = [conn rootProxy];
  40.     if (!proxy) {
  41.         NSLog(@"didn't get root proxy!");
  42.         exit(1);
  43.     }
  44.  
  45.     // since this is an example, we don't really care what the "served" object
  46.     // really does, just that we can message it.  So treat it like the NSObject
  47.     // that it is.
  48.  
  49.     (void)[proxy description];
  50.  
  51.     // if it didn't work, an exception is raised.
  52.     NSLog(@"sent message (with implicit authentication)");
  53. }
  54.  
  55. void server() {
  56.     NSConnection *conn = [NSConnection defaultConnection];
  57.  
  58.     [conn setRootObject:[[[NSObject alloc] init] autorelease]];
  59.     [conn setDelegate:[Authenticator class]];
  60.     if (![conn registerName:@"authentication test"]) {
  61.         NSLog(@"AuthServer: couldn't register!");
  62.         exit(1);
  63.     }
  64.     NSLog(@"serving");
  65.     // this will basically run forever
  66.     [[NSRunLoop currentRunLoop] run];
  67. }
  68.  
  69. int main (int argc, const char *argv[])
  70. {
  71.    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  72.    NSArray *arguments = [[NSProcessInfo processInfo] arguments];
  73.    unsigned limit = [arguments count];
  74.  
  75.    if (limit == 2)
  76.        client([arguments objectAtIndex:1]);
  77.    else
  78.        server();
  79.  
  80.  
  81.  
  82.    [pool release];
  83.    exit(0);       // insure the process exit status is 0
  84.    return 0;      // ...and make main fit the ANSI spec.
  85. }
  86.