hop-2012/gui_ocamlmsgAppDelegate.m

87 lines
2.1 KiB
Mathematica
Raw Normal View History

2012-05-01 15:13:56 +00:00
//
// gui_ocamlmsgAppDelegate.m
// gui-ocamlmsg
//
// Created by Tony Garnock-Jones on 2012-05-01.
// Copyright 2012 n/a. All rights reserved.
//
#import "gui_ocamlmsgAppDelegate.h"
#include <sys/stat.h>
#define HOP_STARTUP_DEADLINE_SECONDS 30
2012-05-01 15:13:56 +00:00
@implementation gui_ocamlmsgAppDelegate
@synthesize window;
2012-05-01 17:16:53 +00:00
@synthesize webview;
2012-05-01 15:13:56 +00:00
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
2012-05-01 17:16:53 +00:00
task = [NSTask new];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(taskStatusChanged:)
name: NSTaskDidTerminateNotification
object: nil];
NSString *serverBinaryPath = [[NSBundle mainBundle] resourcePath];
[task setCurrentDirectoryPath: serverBinaryPath];
NSString *serverBinary = [serverBinaryPath stringByAppendingPathComponent: @"ocamlmsg.native"];
[task setLaunchPath: serverBinary];
NSString *readyFile = [NSTemporaryDirectory() stringByAppendingPathComponent: @"hop.ready."];
readyFile = [readyFile stringByAppendingFormat: @"%d", getpid()];
char const *readyFileUtf8 = [readyFile UTF8String];
unlink(readyFileUtf8);
[task setArguments: [NSArray arrayWithObjects: @"--ready-file", readyFile, nil]];
@try {
[task launch];
}
@catch (NSException *e) {
NSLog(@"Could not launch server %@: %@", serverBinary, [e reason]);
[NSApp terminate: self];
@throw;
}
int foundReadyFile = 0;
time_t startTime = time(NULL);
while ([task isRunning] && time(NULL) - startTime < HOP_STARTUP_DEADLINE_SECONDS) {
struct stat s;
if (lstat(readyFileUtf8, &s) != -1) {
foundReadyFile = 1;
break;
}
if (errno != ENOENT) {
perror("lstat of readyFileUtf8");
exit(EXIT_FAILURE);
}
usleep(100000);
}
unlink(readyFileUtf8);
if (!foundReadyFile) {
NSLog(@"Server did not start up within %d seconds.", HOP_STARTUP_DEADLINE_SECONDS);
[NSApp terminate: self];
return;
}
2012-05-01 17:16:53 +00:00
[[webview mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString: @"http://localhost:5678/"]]];
}
- (void) applicationWillTerminate: (NSNotification *) notification {
if ([task isRunning]) {
[task terminate];
}
2012-05-01 17:16:53 +00:00
}
- (void) taskStatusChanged: (NSNotification *) aNotification {
[NSApp terminate: self];
2012-05-01 15:13:56 +00:00
}
@end