Annotation of Examples/EnterpriseObjects/DistributedEO/DEOClient.subproj/DEOServerMonitor.m, revision 1.1

1.1     ! root        1: /*
        !             2:    DEOServerMonitor.m created by enoyau on Fri 13-Jan-1995
        !             3: 
        !             4:    You may freely copy, distribute, and reuse the code in this example.
        !             5:    NeXT disclaims any warranty of any kind, expressed or implied, as to its
        !             6:    fitness for any particular use.
        !             7: */
        !             8: 
        !             9: #import "DEOServerMonitor.h"
        !            10: #import <foundation/NSNotification.h>
        !            11: #import <foundation/NSAutoreleasePool.h>
        !            12: #import <foundation/NSUtilities.h>
        !            13: #import <foundation/NSBundle.h>
        !            14: #import <foundation/NSException.h>
        !            15: #import <remote/NXConnection.h>
        !            16: #import <remote/NXProxy.h>
        !            17: 
        !            18: #import <libc.h>
        !            19: 
        !            20: static id instance = nil;
        !            21: NSString *DEOServerReinitialize = @"DEOServerReinitialize";
        !            22: 
        !            23: @implementation DEOServerMonitor
        !            24: 
        !            25: - (id <DEOServer>)server
        !            26: {
        !            27:     if(server) return server;
        !            28: 
        !            29:     if(wantLog) NSLog(@"DEOClient: try to connect to the server...\n");
        !            30:     // Work around for bug (release note reference 51800):
        !            31:     // This object is coming back retained instead of autoreleased,
        !            32:     // so we don't send it retain here.
        !            33:     server = (id <DEOServer>)[NXConnection connectToName:DEOServerName
        !            34:                                                   onHost:"*"];
        !            35: 
        !            36:     if(!server) {
        !            37:         NSString *path = [[NSBundle mainBundle] pathForResource:@"DEOServer"
        !            38:                                                           ofType:nil];
        !            39:         int pid;
        !            40: 
        !            41:         if(wantLog) NSLog(@"DEOClient: fork a new server\n");
        !            42: 
        !            43:         if((pid = fork()) == 0  ) { // fork the server
        !            44:             (void)execlp([path cString], [path cString], (char *)0);
        !            45:             exit(0); // if exec fail...
        !            46: 
        !            47:         } else if(pid != -1) { // try to reconnect
        !            48:             char here[1024];
        !            49:             int retry = 20;
        !            50: 
        !            51:             gethostname(here, 1024);
        !            52: 
        !            53:             while((retry--) && (!server)) {
        !            54:                 sleep(1);
        !            55:                 if(wantLog) NSLog(@"DEOClient: try to connect to the new server...\n");
        !            56:                 // Work around for bug (release note reference 51800):
        !            57:                 // see above.
        !            58:                 server = (id <DEOServer>)[NXConnection connectToName:DEOServerName onHost:here];
        !            59:             }
        !            60:         }
        !            61:     }
        !            62: 
        !            63:     if(server) {
        !            64:         NXConnection *myConnection;
        !            65: 
        !            66:         if(wantLog) NSLog(@"DEOClient: run the connection");
        !            67:         [(NXProxy *)server setProtocolForProxy:@protocol(DEOServer)];
        !            68: 
        !            69:         myConnection = [(NXProxy*)server connectionForProxy];
        !            70:         [myConnection registerForInvalidationNotification:self];
        !            71:         [myConnection runFromAppKit];
        !            72:     } else {
        !            73:         if(wantLog) NSLog(@"DEOClient: connection failed\n");
        !            74:     }
        !            75: 
        !            76:     return server;
        !            77: }
        !            78: 
        !            79: + serverMonitor
        !            80: {
        !            81:     return [(instance ? [instance retain] : (instance = [self new])) autorelease];
        !            82: }
        !            83: 
        !            84: - (void)dealloc
        !            85: {
        !            86:     NXConnection *myConnection = [(NXProxy*)server connectionForProxy];
        !            87: 
        !            88:     [myConnection unregisterForInvalidationNotification:self];
        !            89:     NS_DURING
        !            90:         [(NSObject *)server release];
        !            91:     NS_HANDLER
        !            92:         NSLog(@"Failed to send release to server.\n");
        !            93:     NS_ENDHANDLER
        !            94:     server = nil;
        !            95:     instance = nil;
        !            96:     [super dealloc];
        !            97: }
        !            98: 
        !            99: - senderIsInvalid:sender
        !           100: {
        !           101:     if(wantLog) NSLog(@"DEOClient: Server dead !\n");
        !           102:     server = nil;
        !           103:     [[NSNotificationCenter defaultCenter] postNotificationName:DEOServerReinitialize object:self];
        !           104:     return self;
        !           105: }
        !           106: 
        !           107: @end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.