|
@@ -18,10 +18,25 @@ if (SERVERS.length === 0)
|
|
class Server {
|
|
class Server {
|
|
host: string;
|
|
host: string;
|
|
#secret: string;
|
|
#secret: string;
|
|
|
|
+ static listServer: Server[] = [];
|
|
|
|
+
|
|
|
|
+ static #currentServerIndex = 0;
|
|
|
|
+
|
|
|
|
+ static get currentServer() {
|
|
|
|
+ return Server.listServer[Server.#currentServerIndex];
|
|
|
|
+ }
|
|
|
|
|
|
constructor(host: string, secret: string) {
|
|
constructor(host: string, secret: string) {
|
|
this.host = host;
|
|
this.host = host;
|
|
this.#secret = secret;
|
|
this.#secret = secret;
|
|
|
|
+ this.test().then(_ => Server.listServer.push(this)).catch(_ => Deno.exit(1));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static getNextServer() {
|
|
|
|
+ Server.#currentServerIndex++;
|
|
|
|
+ if (Server.#currentServerIndex === Server.listServer.length)
|
|
|
|
+ Server.#currentServerIndex = 0;
|
|
|
|
+ return Server.listServer[Server.#currentServerIndex];
|
|
}
|
|
}
|
|
|
|
|
|
static async hashCreate(params: string) {
|
|
static async hashCreate(params: string) {
|
|
@@ -66,31 +81,19 @@ class Server {
|
|
if (!ok)
|
|
if (!ok)
|
|
throw Error;
|
|
throw Error;
|
|
console.log(`${this.host} is ${green('ok')}`);
|
|
console.log(`${this.host} is ${green('ok')}`);
|
|
- } catch (_e) {
|
|
|
|
|
|
+ } catch (e) {
|
|
console.log(`${this.host} is ${red('misconfigured. Please check your secret in servers.json')}`);
|
|
console.log(`${this.host} is ${red('misconfigured. Please check your secret in servers.json')}`);
|
|
- Deno.exit(1)
|
|
|
|
|
|
+ throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-const listServer: Server[] = [];
|
|
|
|
const queue: Map<string, Promise<unknown>> = new Map();
|
|
const queue: Map<string, Promise<unknown>> = new Map();
|
|
|
|
|
|
for (const server of SERVERS) {
|
|
for (const server of SERVERS) {
|
|
- const s = new Server(server.host, server.secret);
|
|
|
|
- s.test();
|
|
|
|
- listServer.push(s);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-let currentServerIndex = 0;
|
|
|
|
-function getNextServer() {
|
|
|
|
- currentServerIndex++;
|
|
|
|
- if (currentServerIndex === listServer.length)
|
|
|
|
- currentServerIndex = 0;
|
|
|
|
- return listServer[currentServerIndex];
|
|
|
|
|
|
+ new Server(server.host, server.secret);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
Deno.serve({ port: PORT,
|
|
Deno.serve({ port: PORT,
|
|
onListen({ port, hostname }) {
|
|
onListen({ port, hostname }) {
|
|
console.log(green(`Starting tinyscale ${VERSION} on Deno ${Deno.version.deno}`));
|
|
console.log(green(`Starting tinyscale ${VERSION} on Deno ${Deno.version.deno}`));
|
|
@@ -129,7 +132,7 @@ Deno.serve({ port: PORT,
|
|
}
|
|
}
|
|
queue.set(meetingID, promise);
|
|
queue.set(meetingID, promise);
|
|
}
|
|
}
|
|
- for (const server of listServer) {
|
|
|
|
|
|
+ for (const server of Server.listServer) {
|
|
const meetings = await server.getResponse('getMeetings');
|
|
const meetings = await server.getResponse('getMeetings');
|
|
if (meetings.includes(meetingID)) {
|
|
if (meetings.includes(meetingID)) {
|
|
selectedServer = server;
|
|
selectedServer = server;
|
|
@@ -138,7 +141,7 @@ Deno.serve({ port: PORT,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (recordingID !== null)
|
|
else if (recordingID !== null)
|
|
- for (const server of listServer) {
|
|
|
|
|
|
+ for (const server of Server.listServer) {
|
|
const recordings = await server.getResponse('getRecordings');
|
|
const recordings = await server.getResponse('getRecordings');
|
|
if (recordings.includes(recordingID)) {
|
|
if (recordings.includes(recordingID)) {
|
|
selectedServer = server;
|
|
selectedServer = server;
|
|
@@ -148,7 +151,7 @@ Deno.serve({ port: PORT,
|
|
log = `${green(`${call}`)} found, reply with`;
|
|
log = `${green(`${call}`)} found, reply with`;
|
|
|
|
|
|
if (call === 'create' && selectedServer === undefined && meetingID !== null) {
|
|
if (call === 'create' && selectedServer === undefined && meetingID !== null) {
|
|
- selectedServer = getNextServer();
|
|
|
|
|
|
+ selectedServer = Server.getNextServer();
|
|
console.log(green('create')+' '+yellow('not found')+", opening a new room on "+green(selectedServer.host));
|
|
console.log(green('create')+' '+yellow('not found')+", opening a new room on "+green(selectedServer.host));
|
|
const body = await selectedServer.getResponse('create', params);
|
|
const body = await selectedServer.getResponse('create', params);
|
|
resolve(meetingID);
|
|
resolve(meetingID);
|
|
@@ -159,7 +162,7 @@ Deno.serve({ port: PORT,
|
|
queue.delete(meetingID);
|
|
queue.delete(meetingID);
|
|
}
|
|
}
|
|
if (selectedServer === undefined)
|
|
if (selectedServer === undefined)
|
|
- selectedServer = listServer[currentServerIndex];
|
|
|
|
|
|
+ selectedServer = Server.currentServer;
|
|
log = log + ' ' + selectedServer.host;
|
|
log = log + ' ' + selectedServer.host;
|
|
console.log(log);
|
|
console.log(log);
|
|
|
|
|