Skip to content

Commit

Permalink
manager: retry QMP connection on error
Browse files Browse the repository at this point in the history
Fixes #172
  • Loading branch information
osy committed Mar 25, 2020
1 parent 21e2745 commit 9749fcb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Managers/UTMJSONStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ - (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
[self disconnect];
break;
}
case NSStreamEventOpenCompleted: {
NSLog(@"Connected to stream");
[self.delegate jsonStream:self connected:(aStream == _inputStream)];
break;
}
default: {
break;
}
Expand Down
1 change: 1 addition & 0 deletions Managers/UTMJSONStreamDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ NS_ASSUME_NONNULL_BEGIN

@protocol UTMJSONStreamDelegate <NSObject>

- (void)jsonStream:(UTMJSONStream *)stream connected:(BOOL)readStream;
- (void)jsonStream:(UTMJSONStream *)stream receivedDictionary:(NSDictionary *)dict;
- (void)jsonStream:(UTMJSONStream *)stream seenError:(NSError *)error;

Expand Down
1 change: 1 addition & 0 deletions Managers/UTMQemuManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly) UTMJSONStream *jsonStream;
@property (nonatomic, weak) id<UTMQemuManagerDelegate> delegate;
@property (nonatomic, assign) int retries;

- (void)connect;
- (void)disconnect;
Expand Down
15 changes: 15 additions & 0 deletions Managers/UTMQemuManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

extern NSString *const kUTMErrorDomain;
const int64_t kRPCTimeout = (int64_t)10*1000000000;
const int64_t kRetryWait = (int64_t)1*1000000000;

static void utm_shutdown_handler(bool guest, ShutdownCause reason, void *ctx) {
UTMQemuManager *self = (__bridge UTMQemuManager *)ctx;
Expand Down Expand Up @@ -163,10 +164,24 @@ - (void)disconnect {
[_jsonStream disconnect];
}

- (void)jsonStream:(UTMJSONStream *)stream connected:(BOOL)readStream {
NSLog(@"QMP connection successful! (readStream:%d)", readStream);
self.retries = 0; // connection was successful
}

- (void)jsonStream:(UTMJSONStream *)stream seenError:(NSError *)error {
NSLog(@"QMP stream error seen: %@", error);
if (_rpc_finish) {
_rpc_finish(nil, error);
}
[self disconnect];
if (self.retries > 0) {
self.retries--;
NSLog(@"QMP connection failed, retries left: %d", self.retries);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kRetryWait), dispatch_get_main_queue(), ^{
[self->_jsonStream connect];
});
}
}

- (void)jsonStream:(UTMJSONStream *)stream receivedDictionary:(NSDictionary *)dict {
Expand Down
1 change: 1 addition & 0 deletions Managers/UTMVirtualMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ - (BOOL)startVM {
if (tries == 0) {
[self errorTriggered:NSLocalizedString(@"Failed to connect to display server.", @"UTMVirtualMachine")];
}
self->_qemu.retries = kMaxConnectionTries;
[self->_qemu connect];
_is_busy = NO;
return YES;
Expand Down

0 comments on commit 9749fcb

Please sign in to comment.