Skip to content

Commit

Permalink
feat: loop check smb connection then prune
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Dec 13, 2023
1 parent 9376bc3 commit 8f83d56
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ describe('api', () => {
const server = api({
title: 'my awesome service',
smbServerBaseUrl: 'http://localhost',
endpointIdleTimeout: '60'
endpointIdleTimeout: '60',
smbPoll: false,
smbPollInterval_s: '60'
});
const response = await server.inject({
method: 'GET',
Expand Down
6 changes: 5 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface ApiOptions {
title: string;
smbServerBaseUrl: string;
endpointIdleTimeout: string;
smbPoll: boolean;
smbPollInterval_s: string;
}

export default (opts: ApiOptions) => {
Expand Down Expand Up @@ -69,7 +71,9 @@ export default (opts: ApiOptions) => {
// register other API routes here
api.register(apiProductions, {
smbServerBaseUrl: opts.smbServerBaseUrl,
endpointIdleTimeout: opts.endpointIdleTimeout
endpointIdleTimeout: opts.endpointIdleTimeout,
smbPoll: opts.smbPoll,
smbPollInterval_s: opts.smbPollInterval_s
});

return api;
Expand Down
63 changes: 62 additions & 1 deletion src/api_productions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, Static } from '@sinclair/typebox';
import { FastifyPluginCallback } from 'fastify';
import { NewProduction, Production, Line } from './models';
import { SmbProtocol, SmbEndpointDescription } from './smb';
import { SmbProtocol, SmbEndpointDescription, DetailedConference } from './smb';
import { ProductionManager } from './production_manager';
import { Connection } from './connection';
import { write, parse } from 'sdp-transform';
Expand Down Expand Up @@ -197,6 +197,8 @@ function getLine(productionLines: Line[], name: string): Line {
export interface ApiProductionsOptions {
smbServerBaseUrl: string;
endpointIdleTimeout: string;
smbPoll: boolean;
smbPollInterval_s: string;
}

const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
Expand Down Expand Up @@ -515,7 +517,66 @@ const apiProductions: FastifyPluginCallback<ApiProductionsOptions> = (
}
);

let parsedDelay_ms: number = parseInt(opts.smbPollInterval_s) * 1000;
if (parsedDelay_ms < 20000) {
parsedDelay_ms = 60000;
}

if (opts.smbPoll) {
startRepeatingPoll(smb, smbServerUrl, parsedDelay_ms);

setTimeout(() => {
stopRepeatingPoll();
}, 6000 * 60 * 10);
}

next();
};

async function poll(smb: SmbProtocol, smbServerUrl: string) {
const productions: Production[] = productionManager.getProductions();

for (const production of productions) {
for (const line of production.lines) {
const endpoints: DetailedConference[] = await smb.getConference(
smbServerUrl,
line.id
);
const activeConnections: Record<string, SmbEndpointDescription> = {};
for (const endpoint of endpoints) {
const activeConnection: SmbEndpointDescription =
line.connections[endpoint['id']];
if (activeConnection) {
activeConnections[endpoint['id']] = activeConnection;
}
}
line.connections = activeConnections;
console.log(line.connections);
}
}
}

let intervalId: NodeJS.Timeout | null = null;

function startRepeatingPoll(
smb: SmbProtocol,
smbServerUrl: string,
delay_ms: number
) {
intervalId = setInterval(() => {
poll(smb, smbServerUrl);
console.log('Repeating poll running...');
}, delay_ms);
}

function stopRepeatingPoll() {
if (intervalId !== null) {
clearInterval(intervalId);
intervalId = null;
console.log('Repeating poll stopped.');
} else {
console.log('No repeating poll is currently running.');
}
}

export default apiProductions;
8 changes: 7 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ if (!process.env.SMB_ADDRESS) {
const ENDPOINT_IDLE_TIMEOUT_S: string =
process.env.ENDPOINT_IDLE_TIMEOUT_S ?? '180';

const SMB_POLL: boolean = process.env.SMB_POLL === 'true';

const SMB_POLL_INTERVAL_S: string = process.env.SMB_POLL_INTERVAL_S ?? '60';

const server = api({
title: 'intercom-manager',
smbServerBaseUrl: SMB_ADDRESS,
endpointIdleTimeout: ENDPOINT_IDLE_TIMEOUT_S
endpointIdleTimeout: ENDPOINT_IDLE_TIMEOUT_S,
smbPoll: SMB_POLL,
smbPollInterval_s: SMB_POLL_INTERVAL_S
});

const PORT = process.env.PORT ? Number(process.env.PORT) : 8000;
Expand Down
30 changes: 30 additions & 0 deletions src/smb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ interface BaseAllocationRequest {
idleTimeout?: number;
}

export interface DetailedConference {
dtlsState: string;
iceState: string;
id: string;
isActiveTalker: boolean;
isDominantSpeaker: boolean;
ActiveTalker?: {
noiseLevel: number;
ptt: boolean;
score: number;
};
}

export class SmbProtocol {
async allocateConference(smbUrl: string): Promise<string> {
const allocateResponse = await fetch(smbUrl, {
Expand Down Expand Up @@ -202,4 +215,21 @@ export class SmbProtocol {
const responseBody: string[] = await response.json();
return responseBody;
}

async getConference(
smbUrl: string,
conferenceId: string
): Promise<DetailedConference[]> {
const url = smbUrl + conferenceId;
const response = await fetch(url, {
method: 'GET'
});

if (!response.ok) {
return [];
}

const responseBody: DetailedConference[] = await response.json();
return responseBody;
}
}

0 comments on commit 8f83d56

Please sign in to comment.