@@ -2,7 +2,6 @@ import EventEmitter from "events";
|
|||||||
import { Socket } from "net";
|
import { Socket } from "net";
|
||||||
import { Mutex } from "async-mutex";
|
import { Mutex } from "async-mutex";
|
||||||
import log from "./log.js";
|
import log from "./log.js";
|
||||||
import { EOL } from 'os'
|
|
||||||
|
|
||||||
export default class QMPClient extends EventEmitter {
|
export default class QMPClient extends EventEmitter {
|
||||||
socketfile : string;
|
socketfile : string;
|
||||||
@@ -47,52 +46,49 @@ export default class QMPClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async onData(data : Buffer) {
|
private async onData(data : Buffer) {
|
||||||
let msgraw = data.toString().split(EOL).filter(n => n);
|
let msgraw = data.toString();
|
||||||
for(var message in msgraw) {
|
let msg;
|
||||||
let msg;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
msg = JSON.parse(msgraw[message]);
|
msg = JSON.parse(msgraw);
|
||||||
} catch {
|
} catch {
|
||||||
this.emit("qmpreturn", "");
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg.QMP !== undefined) {
|
|
||||||
if (this.sentConnected)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await this.execute({ execute: "qmp_capabilities" });
|
|
||||||
|
|
||||||
this.emit('connected');
|
|
||||||
this.sentConnected = true;
|
|
||||||
}
|
|
||||||
if (msg.return !== undefined)
|
|
||||||
this.emit("qmpreturn", msg.return);
|
|
||||||
else if(msg.event !== undefined) {
|
|
||||||
switch(msg.event) {
|
|
||||||
case "STOP":
|
|
||||||
{
|
|
||||||
log("INFO", "The VM was shut down, restarting...");
|
|
||||||
this.reboot();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "RESET":
|
|
||||||
{
|
|
||||||
log("INFO", "QEMU reset event occured");
|
|
||||||
this.resume();
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
default: {
|
|
||||||
this.emit("qmpreturn", "");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else
|
|
||||||
// for now just return an empty string.
|
|
||||||
// This is a giant hack but avoids a deadlock
|
|
||||||
this.emit("qmpreturn", "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (msg.QMP !== undefined) {
|
||||||
|
if (this.sentConnected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await this.execute({ execute: "qmp_capabilities" });
|
||||||
|
|
||||||
|
this.emit('connected');
|
||||||
|
this.sentConnected = true;
|
||||||
|
}
|
||||||
|
if (msg.return !== undefined)
|
||||||
|
this.emit("qmpreturn", msg.return);
|
||||||
|
else if(msg.event !== undefined) {
|
||||||
|
switch(msg.event) {
|
||||||
|
case "STOP":
|
||||||
|
{
|
||||||
|
log("INFO", "The VM was shut down, restarting...");
|
||||||
|
this.reboot();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "RESET":
|
||||||
|
{
|
||||||
|
log("INFO", "QEMU reset event occured");
|
||||||
|
this.resume();
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
default: {
|
||||||
|
this.emit("qmpreturn", '');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else
|
||||||
|
// for now just return an empty string.
|
||||||
|
// This is a giant hack but avoids a deadlock
|
||||||
|
this.emit("qmpreturn", '');
|
||||||
}
|
}
|
||||||
|
|
||||||
private onClose() {
|
private onClose() {
|
||||||
@@ -154,4 +150,4 @@ export default class QMPClient extends EventEmitter {
|
|||||||
res(await this.execute({execute: "human-monitor-command", arguments: {"command-line": command}}));
|
res(await this.execute({execute: "human-monitor-command", arguments: {"command-line": command}}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user