Thotz/Thotz.html
 

Distributed Objects Demo

This XCode project was created for a DTS bug report to demonstrate a thread-related port leak in Cocoa’s Distributed Objects implementation (Tiger and earlier), but in the process it also demonstrates:

  1. 1.Single threaded DO

  2. 2.Multi-threaded DO

  3. 3.Use of NSPortNameServer (a Bonjour wrapper, basically).

The port leak seems to have been fixed in Leopard.

The code was never intended as a tutorial, but lucid DO tutorial code is pretty rare, and code that demonstrates the obscure NSPortNameServer technique is rarer still.

This project produces an application which controls both the server and the client in the same window. If you want to explore DO between Macs instead of on the same Mac, simply enable the Server section on one Mac and the Client on the other, and use the NSSocketPort mode on both.


Some example code from the project:


- (NSConnection *) createServerConnection

{

     NSConnection * connection = nil;

     id port = nil;

     if([self portType] == NSSocketPortType)

          {

          port = [[[NSSocketPort alloc] initWithTCPPort: 0] autorelease];// random port

          }

     else

          {

          port = [NSPort port];

          }

     NSLog(@"Port type: %@", [port className]);

     if(port)

          {

         

          connection = [[[NSConnection alloc] initWithReceivePort: port

                                                         sendPort:nil] autorelease];

          if(connection)

               {

               id portNameServer = [self portNameServer];

                             

               if(![portNameServer registerPort: port name: PORTNAME])

                    {

                    connection = nil;

                    NSLog(@"registerPort:name failed");

                    }

               }

         }

return connection;

}

DODemo.zipDistributed_Objects_Demo_files/DODemo.zipDistributed_Objects_Demo_files/DODemo_1.zipshapeimage_3_link_0