Refactor to a connector based concept in lib/link

Remove connection and client subclasses of Link and put the connection
logic into a "connector" class.  The connector deals with sending
and receiving messages, handling the connection and reconnection of lost
connections and giving sequence numbers to messages.

This move the logic of the actual connection away from the link
sub-classes, which in turn means the connection should be a lot easier
to mock in tests.
This commit is contained in:
Hornwitser committed 2019-11-30 00:08:10 +01:00
1 parent a3eb39712c
commit 3bccbac496
12 files changed
+492 -494

No files matched your search

+25 -13
View File
@@ -235,17 +235,36 @@ class Instance {
}
}
class SlaveConnector extends link.SocketIOClientConnector {
constructor(slaveConfig) {
super(slaveConfig.masterURL, slaveConfig.masterAuthToken);
this.id = slaveConfig.id;
this.name = slaveConfig.name;
}
register() {
console.log("SOCKET | registering slave");
this.send('register_slave', {
agent: 'Clusterio Slave',
version,
id: this.id,
name: this.name,
});
}
}
/**
* Handles running the slave
*
* Connects to the master server over the socket.io connection and manages
* intsances.
*/
class Slave extends link.Client {
class Slave extends link.Link {
// I don't like God classes, but the alternative of putting all this state
// into global variables is not much better.
constructor(slaveConfig) {
super('slave', slaveConfig.masterURL, slaveConfig.masterAuthToken);
constructor(connector, slaveConfig) {
super('slave', 'master', connector);
link.attachAllMessages(this);
this.config = {
id: slaveConfig.id,
@@ -256,16 +275,7 @@ class Slave extends link.Client {
masterToken: slaveConfig.masterAuthToken,
publicAddress: slaveConfig.publicIP,
}
}
register() {
console.log("SOCKET | registering slave");
this.send('register_slave', {
agent: 'Clusterio Slave',
version,
id: this.config.id,
name: this.config.name,
});
}
async _findNewInstanceDir(name) {
@@ -626,7 +636,8 @@ async function startClient() {
return;
}
let slave = new Slave(slaveConfig);
let slaveConnector = new SlaveConnector(slaveConfig);
let slave = new Slave(slaveConnector, slaveConfig);
// Handle interrupts
let secondSigint = false
@@ -645,6 +656,7 @@ async function startClient() {
});
});
await slaveConnector.connect();
await slave.start();
/*