2012-05-04 02:08:55 +00:00
|
|
|
// Copyright 2012 Tony Garnock-Jones <tonygarnockjones@gmail.com>.
|
2012-05-01 15:13:56 +00:00
|
|
|
//
|
2012-05-04 02:08:55 +00:00
|
|
|
// This file is part of Hop.
|
2012-05-01 15:13:56 +00:00
|
|
|
//
|
2012-05-04 02:08:55 +00:00
|
|
|
// Hop is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the
|
|
|
|
// Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
// option) any later version.
|
|
|
|
//
|
|
|
|
// Hop is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Hop. If not, see <http://www.gnu.org/licenses/>.
|
2012-05-01 15:13:56 +00:00
|
|
|
//
|
|
|
|
|
2012-05-01 21:51:56 +00:00
|
|
|
#import "hopOsxAppDelegate.h"
|
2012-05-01 19:39:40 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#define HOP_STARTUP_DEADLINE_SECONDS 30
|
2012-05-01 15:13:56 +00:00
|
|
|
|
2012-05-01 21:51:56 +00:00
|
|
|
@implementation hopOsxAppDelegate
|
2012-05-01 15:13:56 +00:00
|
|
|
|
|
|
|
@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];
|
|
|
|
|
2012-05-01 20:09:43 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(mainWindowClosed:)
|
|
|
|
name: NSWindowWillCloseNotification
|
|
|
|
object: window];
|
|
|
|
|
2012-05-01 17:16:53 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: self
|
|
|
|
selector: @selector(taskStatusChanged:)
|
|
|
|
name: NSTaskDidTerminateNotification
|
|
|
|
object: nil];
|
|
|
|
|
2012-05-01 19:39:40 +00:00
|
|
|
NSString *serverBinaryPath = [[NSBundle mainBundle] resourcePath];
|
|
|
|
[task setCurrentDirectoryPath: serverBinaryPath];
|
2012-05-01 21:38:53 +00:00
|
|
|
NSString *serverBinary = [serverBinaryPath stringByAppendingPathComponent: @"hop_server.native"];
|
2012-05-01 19:39:40 +00:00
|
|
|
[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 {
|
2012-05-01 19:39:40 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-05-01 20:09:43 +00:00
|
|
|
- (void) mainWindowClosed: (NSNotification *) aNotification {
|
|
|
|
[NSApp terminate: self];
|
|
|
|
}
|
|
|
|
|
2012-05-01 15:13:56 +00:00
|
|
|
@end
|