Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 1 | #import <Cocoa/Cocoa.h> |
| 2 | #import "TSocketServer.h" |
| 3 | #import "TNSFileHandleTransport.h" |
| 4 | #import "TProtocol.h" |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 5 | #import "TTransportException.h" |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | @implementation TSocketServer |
| 9 | |
| 10 | - (id) initWithPort: (int) port |
| 11 | protocolFactory: (id <TProtocolFactory>) protocolFactory |
| 12 | processor: (id <TProcessor>) processor; |
| 13 | { |
| 14 | self = [super init]; |
| 15 | |
| 16 | mInputProtocolFactory = [protocolFactory retain]; |
| 17 | mOutputProtocolFactory = [protocolFactory retain]; |
| 18 | mProcessor = [processor retain]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 19 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 20 | // create a socket |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 21 | mServerSocket = [[NSSocketPort alloc] initWithTCPPort: port]; |
| 22 | // FIXME - move this separate start method and add method to close |
| 23 | // and cleanup any open ports |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 24 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 25 | if (mServerSocket == nil) { |
| 26 | NSLog(@"Unable to listen on TCP port %d", port); |
| 27 | } else { |
| 28 | NSLog(@"Listening on TCP port %d", port); |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 29 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 30 | // wrap it in a file handle so we can get messages from it |
| 31 | mSocketFileHandle = [[NSFileHandle alloc] initWithFileDescriptor: [mServerSocket socket] |
| 32 | closeOnDealloc: YES]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 33 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 34 | // register for notifications of accepted incoming connections |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 35 | [[NSNotificationCenter defaultCenter] addObserver: self |
| 36 | selector: @selector(connectionAccepted:) |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 37 | name: NSFileHandleConnectionAcceptedNotification |
| 38 | object: mSocketFileHandle]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 39 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 40 | // tell socket to listen |
| 41 | [mSocketFileHandle acceptConnectionInBackgroundAndNotify]; |
| 42 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 43 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 44 | return self; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | - (void) dealloc { |
| 49 | [mInputProtocolFactory release]; |
| 50 | [mOutputProtocolFactory release]; |
| 51 | [mProcessor release]; |
| 52 | [mSocketFileHandle release]; |
| 53 | [mServerSocket release]; |
| 54 | [super dealloc]; |
| 55 | } |
| 56 | |
| 57 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 58 | - (void) connectionAccepted: (NSNotification *) aNotification |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 59 | { |
| 60 | NSFileHandle * socket = [[aNotification userInfo] objectForKey: NSFileHandleNotificationFileHandleItem]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 61 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 62 | // now that we have a client connected, spin off a thread to handle activity |
| 63 | [NSThread detachNewThreadSelector: @selector(handleClientConnection:) |
| 64 | toTarget: self |
| 65 | withObject: socket]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 66 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 67 | [[aNotification object] acceptConnectionInBackgroundAndNotify]; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | - (void) handleClientConnection: (NSFileHandle *) clientSocket |
| 72 | { |
| 73 | NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; |
| 74 | |
| 75 | TNSFileHandleTransport * transport = [[TNSFileHandleTransport alloc] initWithFileHandle: clientSocket]; |
| 76 | |
| 77 | id <TProtocol> inProtocol = [mInputProtocolFactory newProtocolOnTransport: transport]; |
| 78 | id <TProtocol> outProtocol = [mOutputProtocolFactory newProtocolOnTransport: transport]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 79 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 80 | @try { |
| 81 | while ([mProcessor processOnInputProtocol: inProtocol outputProtocol: outProtocol]); |
| 82 | } |
| 83 | @catch (TTransportException * te) { |
| 84 | NSLog(@"%@", te); |
| 85 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame^] | 86 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 87 | [pool release]; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | @end |
| 93 | |
| 94 | |
| 95 | |