Advertisement
Guest User

Ed

a guest
Apr 13th, 2009
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. #import "AppController.h"
  2. #import "AsyncUdpSocket.h"
  3.  
  4. #define WELCOME_MSG 0
  5. #define ECHO_MSG 1
  6.  
  7. #define FORMAT(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__]
  8.  
  9. @interface AppController (PrivateAPI)
  10. - (void)logError:(NSString *)msg;
  11. - (void)logInfo:(NSString *)msg;
  12. - (void)logMessage:(NSString *)msg;
  13. @end
  14.  
  15.  
  16. @implementation AppController
  17.  
  18. - (id)init
  19. {
  20. if(self = [super init])
  21. {
  22. sendSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
  23.  
  24. listenSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
  25. if ([listenSocket bindToPort:CHANNEL_PORT error:nil])
  26. {
  27. NSLog(@"bindToPort OK");
  28. }
  29. else
  30. {
  31. NSLog(@"bindToPort Failed");
  32. }
  33. //[listenSocket joinMulticastGroup:@CHANNEL_GROUP error:nil];
  34. if ([listenSocket joinMulticastGroup:@CHANNEL_GROUP error:nil])
  35. {
  36. NSLog(@"joinMulticastGroup OK");
  37. }
  38. else
  39. {
  40. NSLog(@"joinMulticastGroup Failed");
  41. }
  42. [listenSocket receiveWithTimeout:-1 tag:1];
  43.  
  44. isRunning = NO;
  45. }
  46. return self;
  47. }
  48.  
  49. - (void)awakeFromNib
  50. {
  51. [logView setString:@""];
  52. }
  53.  
  54. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  55. {
  56. NSLog(@"Ready");
  57.  
  58. // Advanced options - enable the socket to contine operations even during modal dialogs, and menu browsing
  59. //[sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
  60. [listenSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
  61.  
  62. [self logInfo:FORMAT(@"Echo server started on port %hu", [listenSocket localPort])];
  63. }
  64.  
  65. - (void)scrollToBottom
  66. {
  67. NSScrollView *scrollView = [logView enclosingScrollView];
  68. NSPoint newScrollOrigin;
  69.  
  70. if ([[scrollView documentView] isFlipped])
  71. newScrollOrigin = NSMakePoint(0.0, NSMaxY([[scrollView documentView] frame]));
  72. else
  73. newScrollOrigin = NSMakePoint(0.0, 0.0);
  74.  
  75. [[scrollView documentView] scrollPoint:newScrollOrigin];
  76. }
  77.  
  78. - (void)logError:(NSString *)msg
  79. {
  80. NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg];
  81.  
  82. NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
  83. [attributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
  84.  
  85. NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
  86. [as autorelease];
  87.  
  88. [[logView textStorage] appendAttributedString:as];
  89. [self scrollToBottom];
  90. }
  91.  
  92. - (void)logInfo:(NSString *)msg
  93. {
  94. NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg];
  95.  
  96. NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
  97. [attributes setObject:[NSColor purpleColor] forKey:NSForegroundColorAttributeName];
  98.  
  99. NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
  100. [as autorelease];
  101.  
  102. [[logView textStorage] appendAttributedString:as];
  103. [self scrollToBottom];
  104. }
  105.  
  106. - (void)logMessage:(NSString *)msg
  107. {
  108. NSString *paragraph = [NSString stringWithFormat:@"%@\n", msg];
  109.  
  110. NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
  111. [attributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
  112.  
  113. NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
  114. [as autorelease];
  115.  
  116. [[logView textStorage] appendAttributedString:as];
  117. [self scrollToBottom];
  118. }
  119.  
  120. - (IBAction)startStop:(id)sender
  121. {
  122.  
  123. [self logInfo:@"Sending test packet..."];
  124.  
  125. NSString *testMsg = @"Hello from Echo Server\r\n";
  126. NSData *testData = [testMsg dataUsingEncoding:NSUTF8StringEncoding];
  127.  
  128. if ([sendSocket sendData:testData toHost:@CHANNEL_GROUP port:CHANNEL_PORT withTimeout:-1 tag:0])
  129. {
  130. [self logInfo:@"Sent test packet"];
  131. }
  132. else
  133. {
  134. [self logInfo:@"Did not send test packet"];
  135. }
  136. //[sendSocket sendData:testData toHost:@CHANNEL_GROUP port:CHANNEL_PORT withTimeout:-1 tag:0];
  137.  
  138. }
  139.  
  140. - (void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag
  141. {
  142. [self logInfo:@"didSendDataWithTag"];
  143. }
  144.  
  145. - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error
  146. {
  147. [self logInfo:@"didNotSendDataWithTag"];
  148. }
  149.  
  150. - (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
  151. {
  152. [self logInfo:FORMAT(@"didReceiveData tag=%hu", tag)];
  153.  
  154. NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
  155. NSString *msg = [[[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding] autorelease];
  156. if(msg)
  157. {
  158. [self logMessage:msg];
  159. }
  160. else
  161. {
  162. [self logError:@"Error converting received data into UTF-8 String"];
  163. }
  164.  
  165. [listenSocket receiveWithTimeout:-1 tag:0];
  166.  
  167. return YES;
  168. }
  169.  
  170. - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error
  171. {
  172. [self logInfo:@"didNotReceiveDataWithTag"];
  173. }
  174.  
  175. - (void)onUdpSocketDidClose:(AsyncUdpSocket *)sock
  176. {
  177. [self logInfo:@"onUdpSocketDidClose"];
  178. }
  179.  
  180.  
  181. @end
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement