Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "AppController.h"
- #import "AsyncUdpSocket.h"
- #define WELCOME_MSG 0
- #define ECHO_MSG 1
- #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
- - (id)init
- {
- if(self = [super init])
- {
- sendSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
- listenSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
- if ([listenSocket bindToPort:CHANNEL_PORT error:nil])
- {
- NSLog(@"bindToPort OK");
- }
- else
- {
- NSLog(@"bindToPort Failed");
- }
- //[listenSocket joinMulticastGroup:@CHANNEL_GROUP error:nil];
- if ([listenSocket joinMulticastGroup:@CHANNEL_GROUP error:nil])
- {
- NSLog(@"joinMulticastGroup OK");
- }
- else
- {
- NSLog(@"joinMulticastGroup Failed");
- }
- [listenSocket receiveWithTimeout:-1 tag: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
- //[sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
- [listenSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
- [self logInfo:FORMAT(@"Echo server started on port %hu", [listenSocket localPort])];
- }
- - (void)scrollToBottom
- {
- NSScrollView *scrollView = [logView enclosingScrollView];
- NSPoint newScrollOrigin;
- if ([[scrollView documentView] isFlipped])
- newScrollOrigin = NSMakePoint(0.0, NSMaxY([[scrollView documentView] frame]));
- else
- newScrollOrigin = NSMakePoint(0.0, 0.0);
- [[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];
- }
- - (IBAction)startStop:(id)sender
- {
- [self logInfo:@"Sending test packet..."];
- NSString *testMsg = @"Hello from Echo Server\r\n";
- NSData *testData = [testMsg dataUsingEncoding:NSUTF8StringEncoding];
- if ([sendSocket sendData:testData toHost:@CHANNEL_GROUP port:CHANNEL_PORT withTimeout:-1 tag:0])
- {
- [self logInfo:@"Sent test packet"];
- }
- else
- {
- [self logInfo:@"Did not send test packet"];
- }
- //[sendSocket sendData:testData toHost:@CHANNEL_GROUP port:CHANNEL_PORT withTimeout:-1 tag:0];
- }
- - (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag
- {
- [self logInfo:@"didSendDataWithTag"];
- }
- - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
- {
- [self logInfo:@"didNotSendDataWithTag"];
- }
- - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
- {
- [self logInfo:FORMAT(@"didReceiveData tag=%hu", tag)];
- NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
- NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
- if(msg)
- {
- [self logMessage:msg];
- }
- else
- {
- [self logError:@"Error converting received data into UTF-8 String"];
- }
- [listenSocket receiveWithTimeout:-1 tag:0];
- return YES;
- }
- - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error
- {
- [self logInfo:@"didNotReceiveDataWithTag"];
- }
- - (void)onUdpSocketDidClose:(AsyncUdpSocket *)sock
- {
- [self logInfo:@"onUdpSocketDidClose"];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement