#import #import #import "AppController.h" #import "AsyncSocket.h" #define WELCOME_MSG 0 #define ECHO_MSG 1 #define WARNING_MSG 2 #define READ_TIMEOUT 15.0 #define READ_TIMEOUT_EXTENSION 10.0 #define FORMAT(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__] @interface AppController (PrivateAPI) - (void)logError:(NSString *)msg; - (void)logInfo:(NSString *)msg; - (void)logMessage:(NSString *)msg; @end @implementation AppController NSString *strBuffer; long idis; long id50; long id144; long id222; long id432; long id1296; long idghz; - (id)init { if((self = [super init])) { listenSocket = [[AsyncSocket alloc] initWithDelegate:self]; connectedSockets = [[NSMutableArray alloc] initWithCapacity:1]; isRunning = NO; } return self; } - (void)awakeFromNib { [logView setString:@""]; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSLog(@"Ready"); // Advanced options - enable the socket to contine operations even during modal dialogs, and menu browsing [listenSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; int port = 2000; [portField setIntValue:port]; if(port < 0 || port > 65535) { port = 0; } NSError *error = nil; if(![listenSocket acceptOnPort:port error:&error]) { [self logError:FORMAT(@"Error starting server: %@", error)]; } [self logInfo:FORMAT(@"Echo server started on port %hu", [listenSocket localPort])]; isRunning = YES; [portField setEnabled:NO]; [startStopButton setTitle:@"Stop Server"]; NSInteger intFromFile; NSScanner *scanner; strBuffer = [NSString stringWithContentsOfFile:@"/flex50.txt" encoding:NSUTF8StringEncoding error:nil]; scanner = [NSScanner scannerWithString:strBuffer]; while ([scanner scanInteger:&intFromFile]) NSLog(@"id50 is %ld", intFromFile); id50 = intFromFile; strBuffer = [NSString stringWithContentsOfFile:@"/k3_144.txt" encoding:NSUTF8StringEncoding error:nil]; scanner = [NSScanner scannerWithString:strBuffer]; while ([scanner scanInteger:&intFromFile]) NSLog(@"id144 is %ld", intFromFile); id144 = intFromFile; strBuffer = [NSString stringWithContentsOfFile:@"/szc2duo1_222.txt" encoding:NSUTF8StringEncoding error:nil]; scanner = [NSScanner scannerWithString:strBuffer]; while ([scanner scanInteger:&intFromFile]) NSLog(@"id222 is %ld", intFromFile); id222 = intFromFile; strBuffer = [NSString stringWithContentsOfFile:@"/szc2duo2_432.txt" encoding:NSUTF8StringEncoding error:nil]; scanner = [NSScanner scannerWithString:strBuffer]; while ([scanner scanInteger:&intFromFile]) NSLog(@"id432 is %ld", intFromFile); id432 = intFromFile; strBuffer = [NSString stringWithContentsOfFile:@"/szc2duo4_1296.txt" encoding:NSUTF8StringEncoding error:nil]; scanner = [NSScanner scannerWithString:strBuffer]; while ([scanner scanInteger:&intFromFile]) NSLog(@"id1296 is %ld", intFromFile); id1296 = intFromFile; strBuffer = [NSString stringWithContentsOfFile:@"/szc2duo5_ghz.txt" encoding:NSUTF8StringEncoding error:nil]; scanner = [NSScanner scannerWithString:strBuffer]; while ([scanner scanInteger:&intFromFile]) NSLog(@"idghz is %ld", intFromFile); idghz = intFromFile; } - (IBAction)quitme:(id)sender; { exit; } - (IBAction)startStop:(id)sender { if(!isRunning) { int port = 2000; if(port < 0 || port > 65535) { port = 0; } NSError *error = nil; if(![listenSocket acceptOnPort:port error:&error]) { [self logError:FORMAT(@"Error starting server: %@", error)]; return; } [self logInfo:FORMAT(@"Echo server started on port %hu", [listenSocket localPort])]; isRunning = YES; [portField setEnabled:NO]; [startStopButton setTitle:@"Stop Server"]; } else { // Stop accepting connections [listenSocket disconnect]; // Stop any client connections NSUInteger i; for(i = 0; i < [connectedSockets count]; i++) { // Call disconnect on the socket, // which will invoke the onSocketDidDisconnect: method, // which will remove the socket from the list. [[connectedSockets objectAtIndex:i] disconnect]; } [self logInfo:@"Stopped Echo server"]; isRunning = false; [portField setEnabled:YES]; [startStopButton setTitle:@"Start"]; } } - (void)scrollToBottom { NSScrollView *scrollView = [logView enclosingScrollView]; NSPoint newScrollOrigin; if ([[scrollView documentView] isFlipped]) newScrollOrigin = NSMakePoint(0.0F, NSMaxY([[scrollView documentView] frame])); else newScrollOrigin = NSMakePoint(0.0F, 0.0F); [[scrollView documentView] scrollPoint:newScrollOrigin]; } - (void)logError:(NSString *)msg { NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg]; NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1]; [attributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName]; NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes]; [as autorelease]; [[logView textStorage] appendAttributedString:as]; [self scrollToBottom]; } - (void)logInfo:(NSString *)msg { NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg]; NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1]; [attributes setObject:[NSColor purpleColor] forKey:NSForegroundColorAttributeName]; NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes]; [as autorelease]; [[logView textStorage] appendAttributedString:as]; [self scrollToBottom]; } - (void)logMessage:(NSString *)msg { NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg]; NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1]; [attributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName]; NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes]; [as autorelease]; [[logView textStorage] appendAttributedString:as]; [self scrollToBottom]; } - (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket { [connectedSockets addObject:newSocket]; } - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port { [self logInfo:FORMAT(@"Accepted client %@:%hu", host, port)]; [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0]; } - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag { if(tag == ECHO_MSG) { [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0]; } } - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)]; NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease]; if(msg) { } else { [self logError:@"Error converting received data into UTF-8 String"]; } // Even if we were unable to write the incoming data to the log, // we're still going to echo it back to the client. [sock readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0]; if ([msg isEqualToString:@"0"]) { idis = id50; NSMutableString* teststring = [NSMutableString stringWithString:@"tell application \"System Events\" to set the frontmost of application processes whose id is "]; NSString *str; str=[NSString stringWithFormat:@"%d",id50]; [teststring appendString:str]; [teststring appendString:@" as integer to true"]; NSString * source = teststring; NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source]; [script executeAndReturnError:nil]; [script release]; [self logInfo:FORMAT(@"Focus 50 MHz")]; } else if ([msg isEqualToString:@"1"]) { idis = id144; NSMutableString* teststring = [NSMutableString stringWithString:@"tell application \"System Events\" to set the frontmost of application processes whose id is "]; NSString *str; str=[NSString stringWithFormat:@"%d",id144]; [teststring appendString:str]; [teststring appendString:@" as integer to true"]; NSString * source = teststring; NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source]; [script executeAndReturnError:nil]; [script release]; [self logInfo:FORMAT(@"Focus 144 MHz")]; } else if ([msg isEqualToString:@"2"]) { idis = id222; NSMutableString* teststring = [NSMutableString stringWithString:@"tell application \"System Events\" to set the frontmost of application processes whose id is "]; NSString *str; str=[NSString stringWithFormat:@"%d",id222]; [teststring appendString:str]; [teststring appendString:@" as integer to true"]; NSString * source = teststring; NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source]; [script executeAndReturnError:nil]; [script release]; [self logInfo:FORMAT(@"Focus 222 MHz")]; } else if ([msg isEqualToString:@"3"]) { idis = id432; NSMutableString* teststring = [NSMutableString stringWithString:@"tell application \"System Events\" to set the frontmost of application processes whose id is "]; NSString *str; str=[NSString stringWithFormat:@"%d",id432]; [teststring appendString:str]; [teststring appendString:@" as integer to true"]; NSString * source = teststring; NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source]; [script executeAndReturnError:nil]; [script release]; [self logInfo:FORMAT(@"Focus 432 MHz")]; } else if ([msg isEqualToString:@"4"]) { idis = id1296; NSMutableString* teststring = [NSMutableString stringWithString:@"tell application \"System Events\" to set the frontmost of application processes whose id is "]; NSString *str; str=[NSString stringWithFormat:@"%d",id1296]; [teststring appendString:str]; [teststring appendString:@" as integer to true"]; NSString * source = teststring; NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source]; [script executeAndReturnError:nil]; [script release]; [self logInfo:FORMAT(@"Focus 1296 MHz")]; } else if ([msg isEqualToString:@"5"]) { idis = idghz; NSMutableString* teststring = [NSMutableString stringWithString:@"tell application \"System Events\" to set the frontmost of application processes whose id is "]; NSString *str; str=[NSString stringWithFormat:@"%d",idghz]; [teststring appendString:str]; [teststring appendString:@" as integer to true"]; NSString * source = teststring; NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source]; [script executeAndReturnError:nil]; [script release]; [self logInfo:FORMAT(@"Focus Giga SDR")]; } else { [self logInfo:FORMAT(@"Bad Focus Message Received")]; idis = 0; } [self logMessage:msg]; NSLog(@"idis is %ld", idis); } // // * This method is called if a read has timed out. // * It allows us to optionally extend the timeout. // * We use this method to issue a warning to the user prior to disconnecting them. // - (NSTimeInterval)onSocket:(AsyncSocket *)sock shouldTimeoutReadWithTag:(long)tag elapsed:(NSTimeInterval)elapsed bytesDone:(NSUInteger)length { if(elapsed <= READ_TIMEOUT) { NSString *warningMsg = @"Are you still there?\r\n"; NSData *warningData = [warningMsg dataUsingEncoding:NSUTF8StringEncoding]; [sock writeData:warningData withTimeout:-1 tag:WARNING_MSG]; return READ_TIMEOUT_EXTENSION; } return 0.0; } - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err { [self logInfo:FORMAT(@"Client Disconnected: %@:%hu", [sock connectedHost], [sock connectedPort])]; } - (void)onSocketDidDisconnect:(AsyncSocket *)sock { [connectedSockets removeObject:sock]; } @end