Proper negotiation with server at startup time.
This commit is contained in:
parent
ad80bf2fca
commit
73ef7f9d7d
|
@ -7,6 +7,9 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#import "gui_ocamlmsgAppDelegate.h"
|
#import "gui_ocamlmsgAppDelegate.h"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
#define HOP_STARTUP_DEADLINE_SECONDS 30
|
||||||
|
|
||||||
@implementation gui_ocamlmsgAppDelegate
|
@implementation gui_ocamlmsgAppDelegate
|
||||||
|
|
||||||
|
@ -22,11 +25,48 @@
|
||||||
name: NSTaskDidTerminateNotification
|
name: NSTaskDidTerminateNotification
|
||||||
object: nil];
|
object: nil];
|
||||||
|
|
||||||
[task setCurrentDirectoryPath: @"/Users/tonyg/src/ocamlmsg"];
|
NSString *serverBinaryPath = [[NSBundle mainBundle] resourcePath];
|
||||||
[task setLaunchPath: @"/Users/tonyg/src/ocamlmsg/ocamlmsg.native"];
|
[task setCurrentDirectoryPath: serverBinaryPath];
|
||||||
[task setArguments: [NSArray new]];
|
NSString *serverBinary = [serverBinaryPath stringByAppendingPathComponent: @"ocamlmsg.native"];
|
||||||
[task launch];
|
[task setLaunchPath: serverBinary];
|
||||||
sleep(1);
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
[[webview mainFrame] loadRequest:
|
[[webview mainFrame] loadRequest:
|
||||||
[NSURLRequest requestWithURL:
|
[NSURLRequest requestWithURL:
|
||||||
|
@ -34,7 +74,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) applicationWillTerminate: (NSNotification *) notification {
|
- (void) applicationWillTerminate: (NSNotification *) notification {
|
||||||
[task terminate];
|
if ([task isRunning]) {
|
||||||
|
[task terminate];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) taskStatusChanged: (NSNotification *) aNotification {
|
- (void) taskStatusChanged: (NSNotification *) aNotification {
|
||||||
|
|
Loading…
Reference in New Issue