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