home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.1 (Developer) [x86] / NeXT Step 3.1 Intel dev.cdr.dmg / NextDeveloper / Examples / DistributedObjects / MultipleThreads / MultiThreadedServer.m
Encoding:
Text File  |  1993-02-18  |  2.0 KB  |  85 lines

  1. #import <objc/Object.h>
  2. #import <remote/NXConnection.h>
  3. #import <remote/NXProxy.h>
  4. #import <mach/cthreads.h>
  5. #import <machkit/NXPort.h>
  6.  
  7. /*
  8.  * A simple, multi-threaded example.
  9.  * Each new client gets a dedicated thread.
  10.  */
  11.  
  12. @protocol ServerProtocol
  13. - newThread;
  14. - (int) doWork;
  15. @end
  16.  
  17. @interface MTS : Object <ServerProtocol,NXSenderIsInvalid>
  18. @end
  19.  
  20. @implementation MTS
  21. // when a new client shows up, build a private connection to it
  22. - connection: (NXConnection *)oldConn didConnect:(NXConnection *)newConn {
  23.     id    outPort = [newConn outPort];
  24.     NXProxy *bogus = [NXConnection connectToPort:outPort];
  25.     NXConnection *newnewConn = [bogus connectionForProxy];
  26.  
  27.     [newConn free];    // don't need new connection on old inPort
  28.     [newnewConn runInNewThread];    // service new things in new thread
  29.     [newnewConn setRoot:self];    // give new conn a root to serve!
  30.     [newnewConn registerForInvalidationNotification:self];
  31.     return newnewConn;    // service this first request in main thread
  32. }
  33. - newThread {
  34.     return self;    // this will point to newnewConn (and new thread)
  35. }
  36. - (int) doWork {
  37.     return (int) cthread_self();
  38. }
  39.  
  40. - senderIsInvalid:sender {
  41.     [sender free];
  42.     return self;
  43. }
  44. @end
  45.  
  46. main() {
  47.     MTS *mts;
  48.     NXConnection *server;
  49.     id proxy1, proxy2;
  50.  
  51. //    [NXConnection debug:"MTS "];
  52.     mts = [MTS new];
  53.     server = [NXConnection registerRoot:mts withName:"MTS example"];
  54.     if (server) {    // we're it!
  55.         // start up a new thread that will deliver client death notifications
  56.         [NXPort worryAboutPortInvalidation];
  57.  
  58.         // tell initial connection to tell us about new incoming connections
  59.         [server setDelegate:mts];
  60.  
  61.         // serve requests
  62.         [server run];
  63.     }
  64.     else {
  65.         [mts free];
  66.         proxy1 = [NXConnection connectToName:"MTS example"];
  67.  
  68.         if (!proxy1) {
  69.             printf("couldn't get to server\n");
  70.             exit(1);
  71.         }
  72.         [proxy1 setProtocolForProxy:@protocol(ServerProtocol)];
  73.  
  74.         proxy2 = [proxy1 newThread];
  75.         if (!proxy2) {
  76.             printf("couldn't get new thread\n");
  77.             exit(2);
  78.         }
  79.         [proxy2 setProtocolForProxy:@protocol(ServerProtocol)];
  80.  
  81.         printf("new thread is %x\n", [proxy2 doWork]);
  82.     }
  83.     exit(0);
  84. }
  85.