Skip to content

Commit

Permalink
Handling broadcast and private channel broadcast separately
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Mar 12, 2024
1 parent 43e0b19 commit 0838b51
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 46 deletions.
14 changes: 10 additions & 4 deletions packages/da-server/src/handlers/BroadcastHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MessageHandler } from "../BasicFDC3Server";
import { AppMetadata, ConnectionStep2Hello, ConnectionStep3Handshake } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { AppMetadata, BroadcastAgentRequest, ConnectionStep2Hello, ConnectionStep3Handshake } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { ServerContext } from "../ServerContext";
import {
PrivateChannelOnAddContextListenerAgentRequest,
Expand Down Expand Up @@ -49,7 +49,13 @@ export class BroadcastHandler implements MessageHandler {
case 'PrivateChannel.broadcast': return this.handleBroadcast(msg as PrivateChannelBroadcastAgentRequest, sc)
case 'PrivateChannel.onAddContextListener': return this.handleOnAddContextListener(msg as PrivateChannelOnAddContextListenerAgentRequest, sc)
case 'PrivateChannel.onUnsubscribe': return this.handleOnUnsubscribe(msg as PrivateChannelOnUnsubscribeAgentRequest, sc)
case 'broadcast': return this.handleBroadcast(msg as PrivateChannelBroadcastAgentRequest, sc)

// although we don't have messages for these yet, we're going to need them. See: https://github.com/finos/FDC3/issues/1171
case 'onUnsubscribe': return this.handleOnUnsubscribe(msg as PrivateChannelOnUnsubscribeAgentRequest, sc)
case 'onAddContextListener': return this.handleOnAddContextListener(msg as PrivateChannelOnAddContextListenerAgentRequest, sc)
case 'broadcastRequest': return this.handleBroadcast(msg as BroadcastAgentRequest, sc)

// handling state synchronisation of channels
case 'hello': return this.handleHello(msg as ConnectionStep2Hello, sc, from)
}
}
Expand Down Expand Up @@ -93,7 +99,7 @@ export class BroadcastHandler implements MessageHandler {
this.regs.push(lr)
}

handleBroadcast(arg0: PrivateChannelBroadcastAgentRequest, sc: ServerContext) {
handleBroadcast(arg0: PrivateChannelBroadcastAgentRequest | BroadcastAgentRequest, sc: ServerContext) {
const channelId = arg0.payload.channelId
const contextType = arg0.payload.context.type

Expand All @@ -115,7 +121,7 @@ export class BroadcastHandler implements MessageHandler {
},
type: arg0.type,
payload: arg0.payload
} as PrivateChannelBroadcastAgentRequest
} as PrivateChannelBroadcastAgentRequest | BroadcastAgentRequest

sc.post(out, r)
})
Expand Down
5 changes: 5 additions & 0 deletions packages/da-server/test/features/broadcast.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ Feature: Relaying Broadcast messages
| msg.source.AppId | msg.source.instanceId | msg.payload.context.type |

Scenario: Handle disconnection, channel state synchronisation
When "App1/a1" broadcasts "fdc3.instrument" on "channel1"
And "App2/a2" sends hello
Then messaging will have outgoing posts
| msg.type | msg.requestedName | msg.channelsState['channel1'].length | msg.channelsState['channel1'][0].type |
| handshake | Jeff | 1 | fdc3.instrument |
24 changes: 24 additions & 0 deletions packages/da-server/test/features/private-channel.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Feature: Relaying Private Channel Broadcast messages

Background:
Given A newly instantiated FDC3 Server

Scenario: Broadcast message to no-one
When "App1/a1" broadcasts "fdc3.instrument" on private channel "channel1"
Then messaging will have outgoing posts
| msg.source.AppId |
And messaging will have 0 posts

Scenario: Broadcast message sent to one listener
When "App2/a2" adds a context listener on private channel "channel1" with type "fdc3.instrument"
And "App1/a1" broadcasts "fdc3.instrument" on private channel "channel1"
Then messaging will have outgoing posts
| msg.meta.source.appId | msg.meta.source.instanceId | msg.payload.context.type | msg.meta.destination.appId | msg.meta.destination.instanceId |
| App1 | a1 | fdc3.instrument | App2 | a2 |

Scenario: Broadcast message sent but listener has unsubscribed
When "App2/a2" adds a context listener on private channel "channel1" with type "fdc3.instrument"
And "App2/a2" removes context listener on private channel "channel1" with type "fdc3.instrument"
And "App1/a1" broadcasts "fdc3.instrument" on private channel "channel1"
Then messaging will have outgoing posts
| msg.source.AppId | msg.source.instanceId | msg.payload.context.type |
33 changes: 8 additions & 25 deletions packages/da-server/test/step-definitions/broadcast.steps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { DataTable, Then, When } from '@cucumber/cucumber'
import { When } from '@cucumber/cucumber'
import { CustomWorld } from '../world';
import { PrivateChannelOnAddContextListenerAgentRequest, PrivateChannelOnUnsubscribeAgentRequest, PrivateChannelBroadcastAgentRequest } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { matchData } from '../support/matching';
import { BroadcastAgentRequest } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { contextMap, createMeta } from './generic.steps';
import expect from "expect";


When('{string} adds a context listener on {string} with type {string}', function (this: CustomWorld, app: string, channelId: string, contextType: string) {
const meta = createMeta(this, app)
Expand All @@ -14,8 +11,8 @@ When('{string} adds a context listener on {string} with type {string}', function
channelId,
contextType
},
type: 'PrivateChannel.onAddContextListener'
} as PrivateChannelOnAddContextListenerAgentRequest
type: 'onAddContextListener'
}

this.server.receive(message, meta.source)
})
Expand All @@ -28,8 +25,8 @@ When('{string} removes context listener on {string} with type {string}', functio
channelId,
contextType
},
type: 'PrivateChannel.onUnsubscribe'
} as PrivateChannelOnUnsubscribeAgentRequest
type: 'onUnsubscribe'
}

this.server.receive(message, meta.source)
})
Expand All @@ -42,23 +39,9 @@ When('{string} broadcasts {string} on {string}', function (this: CustomWorld, ap
channelId,
context: contextMap[contextType]
},
type: 'PrivateChannel.broadcast'
} as PrivateChannelBroadcastAgentRequest
type: 'broadcastRequest'
} as BroadcastAgentRequest

this.server.receive(message, meta.source)

})

Then('messaging will have outgoing posts', function (this: CustomWorld, dt: DataTable) {
// just take the last few posts and match those
const matching = dt.rows().length
var toUse = this.sc?.postedMessages
if (toUse.length > matching) {
toUse = toUse.slice(toUse.length - matching, toUse.length)
}
matchData(this, toUse, dt)
})

Then('messaging will have {int} posts', function (this: CustomWorld, count: number) {
expect(this.sc.postedMessages.length).toEqual(count)
})
19 changes: 2 additions & 17 deletions packages/da-server/test/step-definitions/generic.steps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Given, When } from '@cucumber/cucumber'
import { Given } from '@cucumber/cucumber'
import { CustomWorld } from '../world';
import { TestServerContext } from '../support/TestServerContext';
import { DefaultFDC3Server } from '../../src/BasicFDC3Server';
import { BasicDirectory } from '../../src/directory/BasicDirectory';
import { ConnectionStep2Hello } from '@finos/fdc3/dist/bridging/BridgingTypes';

export const APP_FIELD = 'apps'

Expand Down Expand Up @@ -49,18 +48,4 @@ Given('A newly instantiated FDC3 Server', function (this: CustomWorld) {
this.sc = new TestServerContext(this)
this.server = new DefaultFDC3Server(this.sc, d, "cucumber-fdc3-server")

});

When('{string} connects', function (this: CustomWorld, appStr: string) {
const meta = createMeta(this, appStr)
const hello: ConnectionStep2Hello = {
type: 'hello',
meta,
payload: {
authRequired: false,
supportedFDC3Versions: ["2.0"],
desktopAgentBridgeVersion: "any"
}
}
this.server.receive(hello, meta.source)
})
});
21 changes: 21 additions & 0 deletions packages/da-server/test/step-definitions/hello.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { When } from '@cucumber/cucumber'
import { CustomWorld } from '../world';
import { ConnectionStep2Hello } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { createMeta } from './generic.steps';

When('{string} sends hello', function (this: CustomWorld, app: string) {
const meta = createMeta(this, app)
const message: ConnectionStep2Hello = {
type: 'hello',
meta: {
timestamp: new Date()
},
payload: {
authRequired: false,
desktopAgentBridgeVersion: "1.0",
supportedFDC3Versions: ["2.0", "2.1"]
}
}

this.server.receive(message, meta.source)
});
20 changes: 20 additions & 0 deletions packages/da-server/test/step-definitions/messaging.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DataTable, Then } from '@cucumber/cucumber'
import { CustomWorld } from '../world';
import { matchData } from '../support/matching';
import expect from "expect";



Then('messaging will have outgoing posts', function (this: CustomWorld, dt: DataTable) {
// just take the last few posts and match those
const matching = dt.rows().length
var toUse = this.sc?.postedMessages
if (toUse.length > matching) {
toUse = toUse.slice(toUse.length - matching, toUse.length)
}
matchData(this, toUse, dt)
})

Then('messaging will have {int} posts', function (this: CustomWorld, count: number) {
expect(this.sc.postedMessages.length).toEqual(count)
})
47 changes: 47 additions & 0 deletions packages/da-server/test/step-definitions/private-channel.steps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { When } from '@cucumber/cucumber'
import { CustomWorld } from '../world';
import { PrivateChannelOnAddContextListenerAgentRequest, PrivateChannelOnUnsubscribeAgentRequest, PrivateChannelBroadcastAgentRequest } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { contextMap, createMeta } from './generic.steps';


When('{string} adds a context listener on private channel {string} with type {string}', function (this: CustomWorld, app: string, channelId: string, contextType: string) {
const meta = createMeta(this, app)
const message = {
meta,
payload: {
channelId,
contextType
},
type: 'PrivateChannel.onAddContextListener'
} as PrivateChannelOnAddContextListenerAgentRequest

this.server.receive(message, meta.source)
})

When('{string} removes context listener on private channel {string} with type {string}', function (this: CustomWorld, app: string, channelId: string, contextType: string) {
const meta = createMeta(this, app)
const message = {
meta,
payload: {
channelId,
contextType
},
type: 'PrivateChannel.onUnsubscribe'
} as PrivateChannelOnUnsubscribeAgentRequest

this.server.receive(message, meta.source)
})

When('{string} broadcasts {string} on private channel {string}', function (this: CustomWorld, app: string, contextType: string, channelId: string) {
const meta = createMeta(this, app)
const message = {
meta,
payload: {
channelId,
context: contextMap[contextType]
},
type: 'PrivateChannel.broadcast'
} as PrivateChannelBroadcastAgentRequest

this.server.receive(message, meta.source)
})

0 comments on commit 0838b51

Please sign in to comment.