#include #include #include "MyConnection.h" #include "server.h" @interface MyObjectRoot : NSObject { NSPort *_stealConnection; } - (void) changeConnectionRootObject; - (NSPort *) createNewConnectionWithPort: (NSPort *)sendPort; - (NSPort *) stealConnection; - (NSString *) message; - (NSString *) message2; @end @interface MyObject : MyObjectRoot @end @interface MyObject2 : MyObjectRoot @end @implementation MyObjectRoot /* * Change the root object serverd. This will change the root object * for all the connections with the same receivePort (cf NSConnection.m) */ - (void) changeConnectionRootObject { printf ("current connection root object: %p\n", [[MyConnection currentConnection] rootObject]); printf ("default connection root object: %p\n", [[MyConnection defaultConnection] rootObject]); [[MyConnection currentConnection] setRootObject: [[MyObject2 alloc] init]]; printf ("change current connection root object\n"); printf ("current connection root object: %p\n", [[MyConnection currentConnection] rootObject]); printf ("default connection root object: %p\n", [[MyConnection defaultConnection] rootObject]); } /* * Create a new (private) connection between the client and the server. */ - (NSPort *) createNewConnectionWithPort: (NSPort *)sendPort { NSPort *receivePort = [GSTcpPort port]; MyConnection *cnx = [[MyConnection alloc] initWithReceivePort: receivePort sendPort: sendPort]; [cnx setRootObject: [[MyObject2 alloc] init]]; puts ("*** Create connection ***"); printf ("new: "); [cnx debug]; [MyConnection debug]; _stealConnection = receivePort; return receivePort; } /* * Returns the last manually created port by createNewConnectionWithPort * to try to create a new connection with it, and steal the other client * access. */ - (NSPort *) stealConnection; { return _stealConnection; } - (NSString *) message { [self notImplemented: _cmd]; return nil; } - (NSString *) message2 { [self notImplemented: _cmd]; return nil; } @end /* * First object served */ @implementation MyObject /* * First succeful message */ - (NSString *) message { puts ("*** Message ***"); [MyConnection debug]; return @"meuh"; } /* * Second message. The client should not have seen this, as it will have done * a changeConnectionRootObject. */ - (NSString *) message2 { puts ("*** Error message2 ***"); [MyConnection debug]; return @"error2"; } @end /* * Second object served */ @implementation MyObject2 /* * First message served. Should not be called as MyObject is served * at the beginning of the communication. */ - (NSString *) message { puts ("*** Error message ***"); [MyConnection debug]; return @"error1"; } /* * Second message served. This one should replace the same one from * MyObject, as MyObject2 will be served by the connection at this * moment. */ - (NSString *) message2 { puts ("*** Message2 ***"); [MyConnection debug]; return @"meuh2"; } @end int main (int argc, char **argv) { CREATE_AUTORELEASE_POOL(pool); // [MyConnection setDebug: YES]; /* * Create object, and get connection */ puts ("*** Launch ***"); [MyConnection debug]; MyObject *myObject = AUTORELEASE([[MyObject alloc] init]); MyConnection *cnx = [MyConnection defaultConnection]; puts ("*** Init ***"); [MyConnection debug]; /* * Server MyObject, and register the server */ [cnx setRootObject: myObject]; if ([cnx registerName: @"MyObject"] == NO) { NSLog(@"Unable to register as 'DirectoryServer'"); NSLog(@"Perhaps another copy of this program is running?"); exit(1); } /* * Start the server main loop. */ puts ("*** Connection ***"); [MyConnection debug]; [[NSRunLoop currentRunLoop] run]; /* * Should never go there. */ puts ("*** End ***"); [MyConnection debug]; RELEASE(pool); return EXIT_SUCCESS; }