Skip to content

Commit

Permalink
feat: smb api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
oshinongit committed Nov 10, 2023
1 parent 76b1d24 commit 4f80a3c
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import swaggerUI from '@fastify/swagger-ui';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { Static, Type } from '@sinclair/typebox';
import { FastifyPluginCallback } from 'fastify';
import apiProductions from './api_productions';

const HelloWorld = Type.String({
description: 'The magical words!'
Expand Down Expand Up @@ -64,6 +65,7 @@ export default (opts: ApiOptions) => {

api.register(healthcheck, { title: opts.title });
// register other API routes here
api.register(apiProductions);

return api;
};
92 changes: 79 additions & 13 deletions src/api_productions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
import { Type } from '@sinclair/typebox';
import { Type, Static } from '@sinclair/typebox';
import { FastifyPluginCallback, FastifyRequest } from 'fastify';
import { NewProduction, Production, SdpOffer, SdpAnswer } from './models';
import { NewProduction, Production, SdpOffer, SdpAnswer, Line } from './models';
import { SmbProtocol, SmbEndpointDescription } from './smb';


type NewProduction = Static<typeof NewProduction>
type Production = Static<typeof Production>
type SdpOffer = Static<typeof SdpOffer>
type SdpAnswer = Static<typeof SdpAnswer>
type Line = Static<typeof Line>

async function createProduction(smb: SmbProtocol, smbServerUrl: string, newProduction: NewProduction): Promise<Production | undefined> {
console.log("hej2");

let production: Production;
let newProductionLines = [];

for (const line of newProduction.lines) {
const id = await smb.allocateConference(smbServerUrl);
let newProductionLine: Line = {name:line.name, id:id};
newProductionLines.push(newProductionLine);
}
production = {name: newProduction.name, lines: newProductionLines, id: "serverGeneratedId"};
if(production){
console.log(production);
return production;
}
else {
return
}
}

async function createEndpoint(smb: SmbProtocol, smbServerUrl: string, lineId: string, clientName: string, audio: boolean, video: boolean, data: boolean): Promise<any> {

const endpoint: SmbEndpointDescription = await smb.allocateEndpoint(smbServerUrl, lineId, clientName, audio, video, data);
return endpoint;
}

const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {

const smbServerUrl = 'http://localhost:8080/conferences/';
const smb = new SmbProtocol();

fastify.post<{
Body: typeof NewProduction;
Reply: typeof Production;
Body: NewProduction;
Reply: Production | string;
}>(
'/production',
{
Expand All @@ -19,10 +58,18 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
},
async (request, reply) => {
try {
//prod = new Production(name, [line1, line2, line3])
//Production construction will make calls to smb and set up each line.
const production: Production | undefined = await createProduction(smb, smbServerUrl, request.body);
if(production){
reply.code(201).send(production);
}
else {
reply.code(500).send('Failed to create production');
}

} catch (err) {
//error handling
reply
.code(500)
.send('Exception thrown when trying to create production: ' + err);
}
}
);
Expand All @@ -34,7 +81,7 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
'/production',
{
schema: {
description: 'Retrieves all Production resource.',
//description: 'Retrieves all Production resources.',
response: {
200: Type.Array(Production)
}
Expand All @@ -56,7 +103,7 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
'/productions/:id',
{
schema: {
description: 'Retrieves a Production resource.',
//description: 'Retrieves a Production resource.',
response: {
200: Production
}
Expand All @@ -78,7 +125,7 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {
'/productions/:id',
{
schema: {
description: 'Delete a Production resource.',
//description: 'Delete a Production resource.',
response: {
200: Type.Object({
message: Type.Literal('removed')
Expand All @@ -97,13 +144,13 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {

fastify.post<{
Params: { id: string; name: string };
Body: typeof SdpOffer;
Reply: typeof SdpAnswer;
Body: SdpOffer;
Reply: SdpAnswer;
}>(
'/productions/:id/lines/:name',
{
schema: {
description: 'Join a Production line.',
//description: 'Join a Production line.',
body: SdpOffer,
response: {
200: SdpAnswer
Expand All @@ -121,3 +168,22 @@ const apiProductions: FastifyPluginCallback = (fastify, opts, next) => {

next();
};

export default apiProductions;

// async function dummyMain(): Promise<any> {

// const smbServerUrl = 'http://localhost:8080/conferences/';
// const smb = new SmbProtocol();

// const newProduction: NewProduction = {name: "NewProduction", lines:[{name:"line1"}, {name:"line2"}, {name:"line3"}]};
// const production = await createProduction(smb, smbServerUrl, newProduction);
// let lineId ="";
// if(production){
// lineId = production.lines[0].id;
// }
// const endpoint = await createEndpoint(smb, smbServerUrl, lineId, "myClientEndpoint", true, false, false);
// console.log(endpoint);
// }

// dummyMain();
58 changes: 34 additions & 24 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import { Type } from '@sinclair/typebox';
export const SdpOffer = Type.String();
export const SdpAnswer = Type.String();

//Allocation Models
export const NewProduction = Type.Object({
name: Type.String(),
lines: Type.Array(
Type.Object({
name: Type.String()
})
export const Audio = Type.Object({
'relay-type': Type.Array(
Type.Union([
Type.Literal('forwarder'),
Type.Literal('mixed'),
Type.Literal('ssrc-rewrite')
])
)
});
})
export const Video = Type.Object({
'relay-type': Type.Union([
Type.Literal('forwarder'),
Type.Literal('ssrc-rewrite')
])
})

//Allocation Models
export const AllocateConference = Type.Object({
'last-n': Type.Integer(),
'global-port': Type.Boolean()
Expand All @@ -27,36 +34,39 @@ export const AllocateEndpoint = Type.Object({
dtls: Type.Boolean(),
sdes: Type.Boolean()
}),
audio: Type.Object({
'relay-type': Type.Array(
Type.Union([
Type.Literal('forwarder'),
Type.Literal('mixed'),
Type.Literal('ssrc-rewrite')
])
)
}),
video: Type.Object({
'relay-type': Type.Union([
Type.Literal('forwarder'),
Type.Literal('ssrc-rewrite')
])
}),
audio: Audio,
video: Video,
data: Type.Object({}),
idleTimeout: Type.Integer()
});

//Production
export const NewProduction = Type.Object({
name: Type.String(),
lines: Type.Array(
Type.Object({
name: Type.String()
})
)
});

export const Production = Type.Object({
id: Type.String(),
name: Type.String(),
lines: Type.Array(
Type.Object({
name: Type.String()
name: Type.String(),
id: Type.String()
})
)
});

//Line
export const Line = Type.Object({
name: Type.String(),
id: Type.String()
})

//Conference
export const ConferenceId = Type.Object({
id: Type.Integer()
Expand Down
Loading

0 comments on commit 4f80a3c

Please sign in to comment.