Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AppDelegate.h
- #import <Cocoa/Cocoa.h>
- @class GCDAsyncUdpSocket;
- @interface AppDelegate : NSObject <NSApplicationDelegate> {
- long tagz;
- GCDAsyncUdpSocket *socket;
- NSString *stream;
- }
- @property (assign) IBOutlet NSWindow *window;
- @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
- @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- @end
- // AppDelegate.m
- #import "AppDelegate.h"
- #import "GCDAsyncUdpSocket.h"
- @implementation AppDelegate
- #define FORMAT(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__]
- @synthesize window = _window;
- @synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
- @synthesize managedObjectModel = __managedObjectModel;
- @synthesize managedObjectContext = __managedObjectContext;
- - (void)dealloc
- {
- [__persistentStoreCoordinator release];
- [__managedObjectModel release];
- [__managedObjectContext release];
- [super dealloc];
- }
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
- socket = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
- [socket connectToHost:@"ip" onPort:<port> error:nil];
- NSError *error = nil;
- [socket beginReceiving:&error];
- char *bytes = "\xff\xffcmd command variable";
- NSData* data = [[NSData alloc] initWithBytes:bytes length:strlen(bytes)];
- [socket sendData:data withTimeout:9999 tag:tagz];
- NSLog(@"%@", FORMAT(@"SENT (%i): %@", (int)tagz, data));
- tagz++;
- }
- - (void)udpSocket:(GCDAsyncUdpSocket *)sock didSendDataWithTag:(long)tags {
- NSLog(@"Did Send");
- }
- - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data fromAddress:(NSData *)address withFilterContext:(id)filterContext {
- NSLog(@"Hello");
- }
- - (void)socket:(GCDAsyncUdpSocket *)sender didReadData:(NSData *)data withTag:(long)tagz
- {
- NSLog(@"%@", data);
- }
- - (void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error {
- NSLog(@"Didnt Send");
- }
- //CoreData Sturf
- /**
- 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.
- */
- - (NSURL *)applicationFilesDirectory {
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSURL *libraryURL = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
- return [libraryURL URLByAppendingPathComponent:@"codRCMac"];
- }
- /**
- Creates if necessary and returns the managed object model for the application.
- */
- - (NSManagedObjectModel *)managedObjectModel {
- if (__managedObjectModel) {
- return __managedObjectModel;
- }
- NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"codRCMac" withExtension:@"momd"];
- __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
- return __managedObjectModel;
- }
- /**
- 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.)
- */
- - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
- if (__persistentStoreCoordinator) {
- return __persistentStoreCoordinator;
- }
- NSManagedObjectModel *mom = [self managedObjectModel];
- if (!mom) {
- NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
- return nil;
- }
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
- NSError *error = nil;
- NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsDirectoryKey] error:&error];
- if (!properties) {
- BOOL ok = NO;
- if ([error code] == NSFileReadNoSuchFileError) {
- ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
- }
- if (!ok) {
- [[NSApplication sharedApplication] presentError:error];
- return nil;
- }
- }
- else {
- if ([[properties objectForKey:NSURLIsDirectoryKey] boolValue] != YES) {
- // Customize and localize this error.
- NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
- error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];
- [[NSApplication sharedApplication] presentError:error];
- return nil;
- }
- }
- NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"codRCMac.storedata"];
- NSPersistentStoreCoordinator *coordinator = [[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom] autorelease];
- if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
- [[NSApplication sharedApplication] presentError:error];
- return nil;
- }
- __persistentStoreCoordinator = [coordinator retain];
- return __persistentStoreCoordinator;
- }
- /**
- Returns the managed object context for the application (which is already
- bound to the persistent store coordinator for the application.)
- */
- - (NSManagedObjectContext *)managedObjectContext {
- if (__managedObjectContext) {
- return __managedObjectContext;
- }
- NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
- if (!coordinator) {
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- [dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
- [dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
- NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
- [[NSApplication sharedApplication] presentError:error];
- return nil;
- }
- __managedObjectContext = [[NSManagedObjectContext alloc] init];
- [__managedObjectContext setPersistentStoreCoordinator:coordinator];
- return __managedObjectContext;
- }
- /**
- Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
- */
- - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
- return [[self managedObjectContext] undoManager];
- }
- /**
- 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.
- */
- - (IBAction)saveAction:(id)sender {
- NSError *error = nil;
- if (![[self managedObjectContext] commitEditing]) {
- NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
- }
- if (![[self managedObjectContext] save:&error]) {
- [[NSApplication sharedApplication] presentError:error];
- }
- }
- - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
- // Save changes in the application's managed object context before the application terminates.
- if (!__managedObjectContext) {
- return NSTerminateNow;
- }
- if (![[self managedObjectContext] commitEditing]) {
- NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
- return NSTerminateCancel;
- }
- if (![[self managedObjectContext] hasChanges]) {
- return NSTerminateNow;
- }
- NSError *error = nil;
- if (![[self managedObjectContext] save:&error]) {
- // Customize this code block to include application-specific recovery steps.
- BOOL result = [sender presentError:error];
- if (result) {
- return NSTerminateCancel;
- }
- NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
- NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
- NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
- NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
- NSAlert *alert = [[[NSAlert alloc] init] autorelease];
- [alert setMessageText:question];
- [alert setInformativeText:info];
- [alert addButtonWithTitle:quitButton];
- [alert addButtonWithTitle:cancelButton];
- NSInteger answer = [alert runModal];
- if (answer == NSAlertAlternateReturn) {
- return NSTerminateCancel;
- }
- }
- return NSTerminateNow;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement