Advertisement
Guest User

Untitled

a guest
Dec 19th, 2011
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // AppDelegate.h
  2. #import <Cocoa/Cocoa.h>
  3.  
  4. @class GCDAsyncUdpSocket;
  5.  
  6. @interface AppDelegate : NSObject <NSApplicationDelegate> {
  7.     long tagz;
  8.     GCDAsyncUdpSocket *socket;
  9.     NSString *stream;
  10. }
  11.  
  12. @property (assign) IBOutlet NSWindow *window;
  13.  
  14. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  15. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
  16. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
  17.  
  18. @end
  19.  
  20. // AppDelegate.m
  21.  
  22. #import "AppDelegate.h"
  23. #import "GCDAsyncUdpSocket.h"
  24.  
  25. @implementation AppDelegate
  26.  
  27. #define FORMAT(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__]
  28.  
  29. @synthesize window = _window;
  30. @synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
  31. @synthesize managedObjectModel = __managedObjectModel;
  32. @synthesize managedObjectContext = __managedObjectContext;
  33.  
  34. - (void)dealloc
  35. {
  36.     [__persistentStoreCoordinator release];
  37.     [__managedObjectModel release];
  38.     [__managedObjectContext release];
  39.     [super dealloc];
  40. }
  41.  
  42. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  43.      
  44.       socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
  45.    
  46.       [socket connectToHost:@"ip" onPort:<port> error:nil];
  47.    
  48.       NSError *error = nil;
  49.    
  50.       [socket beginReceiving:&error];
  51.    
  52.       char *bytes = "\xff\xffcmd command variable";
  53.       NSData* data = [[NSData alloc] initWithBytes:bytes length:strlen(bytes)];
  54.       [socket sendData:data withTimeout:9999 tag:tagz];
  55.    
  56.       NSLog(@"%@", FORMAT(@"SENT (%i): %@", (int)tagz, data));
  57.    
  58.       tagz++;
  59.      
  60. }
  61.  
  62. - (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tags {
  63.     NSLog(@"Did Send");
  64. }
  65.  
  66. - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
  67.     NSLog(@"Hello");
  68. }
  69.  
  70. - (void)socket:(GCDAsyncUdpSocket *)sender didReadData:(NSData *)data withTag:(long)tagz
  71. {
  72.     NSLog(@"%@", data);
  73. }
  74.  
  75. - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error {
  76.     NSLog(@"Didnt Send");
  77. }
  78.  
  79.  
  80. //CoreData Sturf
  81.  
  82. /**
  83.     Returns the directory the application uses to store the Core Data store file. This code uses a directory named "codRCMac" in the user's Library directory.
  84.  */
  85. - (NSURL *)applicationFilesDirectory {
  86.  
  87.     NSFileManager *fileManager = [NSFileManager defaultManager];
  88.     NSURL *libraryURL = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
  89.     return [libraryURL URLByAppendingPathComponent:@"codRCMac"];
  90. }
  91.  
  92. /**
  93.     Creates if necessary and returns the managed object model for the application.
  94.  */
  95. - (NSManagedObjectModel *)managedObjectModel {
  96.     if (__managedObjectModel) {
  97.         return __managedObjectModel;
  98.     }
  99.    
  100.     NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"codRCMac" withExtension:@"momd"];
  101.     __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
  102.     return __managedObjectModel;
  103. }
  104.  
  105. /**
  106.     Returns the persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. (The directory for the store is created, if necessary.)
  107.  */
  108. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  109.     if (__persistentStoreCoordinator) {
  110.         return __persistentStoreCoordinator;
  111.     }
  112.  
  113.     NSManagedObjectModel *mom = [self managedObjectModel];
  114.     if (!mom) {
  115.         NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
  116.         return nil;
  117.     }
  118.  
  119.     NSFileManager *fileManager = [NSFileManager defaultManager];
  120.     NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
  121.     NSError *error = nil;
  122.    
  123.     NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsDirectoryKey] error:&error];
  124.        
  125.     if (!properties) {
  126.         BOOL ok = NO;
  127.         if ([error code] == NSFileReadNoSuchFileError) {
  128.             ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
  129.         }
  130.         if (!ok) {
  131.             [[NSApplication sharedApplication] presentError:error];
  132.             return nil;
  133.         }
  134.     }
  135.     else {
  136.         if ([[properties objectForKey:NSURLIsDirectoryKey] boolValue] != YES) {
  137.             // Customize and localize this error.
  138.             NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];
  139.            
  140.             NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  141.             [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
  142.             error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];
  143.            
  144.             [[NSApplication sharedApplication] presentError:error];
  145.             return nil;
  146.         }
  147.     }
  148.    
  149.     NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"codRCMac.storedata"];
  150.     NSPersistentStoreCoordinator *coordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom] autorelease];
  151.     if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
  152.         [[NSApplication sharedApplication] presentError:error];
  153.         return nil;
  154.     }
  155.     __persistentStoreCoordinator = [coordinator retain];
  156.  
  157.     return __persistentStoreCoordinator;
  158. }
  159.  
  160. /**
  161.     Returns the managed object context for the application (which is already
  162.     bound to the persistent store coordinator for the application.)
  163.  */
  164. - (NSManagedObjectContext *)managedObjectContext {
  165.     if (__managedObjectContext) {
  166.         return __managedObjectContext;
  167.     }
  168.  
  169.     NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
  170.     if (!coordinator) {
  171.         NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  172.         [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
  173.         [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
  174.         NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
  175.         [[NSApplication sharedApplication] presentError:error];
  176.         return nil;
  177.     }
  178.     __managedObjectContext = [[NSManagedObjectContext alloc] init];
  179.     [__managedObjectContext setPersistentStoreCoordinator:coordinator];
  180.  
  181.     return __managedObjectContext;
  182. }
  183.  
  184. /**
  185.     Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
  186.  */
  187. - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
  188.     return [[self managedObjectContext] undoManager];
  189. }
  190.  
  191. /**
  192.     Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user.
  193.  */
  194. - (IBAction)saveAction:(id)sender {
  195.     NSError *error = nil;
  196.    
  197.     if (![[self managedObjectContext] commitEditing]) {
  198.         NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
  199.     }
  200.  
  201.     if (![[self managedObjectContext] save:&error]) {
  202.         [[NSApplication sharedApplication] presentError:error];
  203.     }
  204. }
  205.  
  206. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
  207.  
  208.     // Save changes in the application's managed object context before the application terminates.
  209.  
  210.     if (!__managedObjectContext) {
  211.         return NSTerminateNow;
  212.     }
  213.  
  214.     if (![[self managedObjectContext] commitEditing]) {
  215.         NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
  216.         return NSTerminateCancel;
  217.     }
  218.  
  219.     if (![[self managedObjectContext] hasChanges]) {
  220.         return NSTerminateNow;
  221.     }
  222.  
  223.     NSError *error = nil;
  224.     if (![[self managedObjectContext] save:&error]) {
  225.  
  226.         // Customize this code block to include application-specific recovery steps.              
  227.         BOOL result = [sender presentError:error];
  228.         if (result) {
  229.             return NSTerminateCancel;
  230.         }
  231.  
  232.         NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
  233.         NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
  234.         NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
  235.         NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
  236.         NSAlert *alert = [[[NSAlert alloc] init] autorelease];
  237.         [alert setMessageText:question];
  238.         [alert setInformativeText:info];
  239.         [alert addButtonWithTitle:quitButton];
  240.         [alert addButtonWithTitle:cancelButton];
  241.  
  242.         NSInteger answer = [alert runModal];
  243.        
  244.         if (answer == NSAlertAlternateReturn) {
  245.             return NSTerminateCancel;
  246.         }
  247.     }
  248.  
  249.     return NSTerminateNow;
  250. }
  251.  
  252. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement