Skip to content

Commit

Permalink
Channel switching, initial synchronization works
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Apr 18, 2024
1 parent dc1aadd commit 78ab479
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 34 deletions.
12 changes: 3 additions & 9 deletions packages/client/src/messaging/message-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ function openFrame(url: string): Window {
return ifrm.contentWindow!!
}

function buildUserChannelState(messaging: MessagePortMessaging) {
// TODO: Figure out how to set initial user channels.
// Should probably be in the message from the server.
return [
new DefaultChannel(messaging, "one", "user", {
color: "red",
name: "THE RED CHANNEL"
})
]
function buildUserChannelState(_messaging: MessagePortMessaging) {
// we start with no channels and receive from the server after handshake.
return []
}
18 changes: 18 additions & 0 deletions packages/da-proxy/src/channels/DefaultChannelSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class DefaultChannelSupport implements ChannelSupport {

mergeChannelState(newState: { [key: string]: ContextElement[]; }): void {
this.userChannel = null
// update known channels
this.userChannelState.forEach(uc => {
const incoming = newState[uc.id] ?? []
incoming.forEach((context) => {
Expand All @@ -54,6 +55,23 @@ export class DefaultChannelSupport implements ChannelSupport {
}
});
})

// ensure we have new channels
for (const [id, contexts] of Object.entries(newState)) {
const existing = this.userChannelState.find(c => c.id == id)
if (!existing) {
const newChannel = new DefaultChannel(this.messaging, id, 'user', {
// todo - figure out how to source these
name: 'channel named ' + id,
color: '#abc',
glyph: "circle.png"
})
contexts.forEach(c => {
newChannel.latestContextMap.set(c.type, c)
})
this.userChannelState.push(newChannel)
}
}
}

hasUserChannelMembershipAPIs(): boolean {
Expand Down
6 changes: 3 additions & 3 deletions packages/da-server/src/BasicFDC3Server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AppMetadata } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { FDC3Server } from "./FDC3Server";
import { ServerContext } from "./ServerContext";
import { BroadcastHandler } from "./handlers/BroadcastHandler";
import { BroadcastHandler, ChannelState } from "./handlers/BroadcastHandler";
import { IntentHandler } from "./handlers/IntentHandler";
import { Directory } from "./directory/DirectoryInterface";
import { OpenHandler } from "./handlers/OpenHandler";
Expand Down Expand Up @@ -35,9 +35,9 @@ export class BasicFDC3Server implements FDC3Server {

export class DefaultFDC3Server extends BasicFDC3Server {

constructor(sc: ServerContext, directory: Directory, name: string, timeoutMs: number = 20000) {
constructor(sc: ServerContext, directory: Directory, name: string, userChannels: ChannelState, timeoutMs: number = 20000) {
super([
new BroadcastHandler(name),
new BroadcastHandler(name, userChannels),
new IntentHandler(directory, timeoutMs),
new OpenHandler(directory)
], sc)
Expand Down
15 changes: 8 additions & 7 deletions packages/da-server/src/handlers/BroadcastHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PrivateChannelBroadcastAgentRequest
} from "@finos/fdc3/dist/bridging/BridgingTypes";
import { ContextElement } from "@finos/fdc3";
import { OnAddContextListenerAgentRequest, OnUnsubscribeAgentRequest, ChannelSectionChoiceAgentRequest, ChannelSectionChoiceAgentResponse } from "fdc3-common";
import { OnAddContextListenerAgentRequest, OnUnsubscribeAgentRequest, ChannelSelectionChoiceAgentRequest, ChannelSelectionChoiceAgentResponse } from "fdc3-common";

type ListenerRegistration = {
appId: string,
Expand Down Expand Up @@ -35,16 +35,17 @@ function createListenerRegistration(msg:
}
}

type ChannelState = { [channelId: string]: ContextElement[] }
export type ChannelState = { [channelId: string]: ContextElement[] }

export class BroadcastHandler implements MessageHandler {

private regs: ListenerRegistration[] = []
private state: ChannelState = {}
private readonly state: ChannelState = {}
private readonly desktopAgentName: string

constructor(name: string) {
constructor(name: string, initialChannelState: ChannelState) {
this.desktopAgentName = name
this.state = initialChannelState
}

accept(msg: any, sc: ServerContext, from: AppMetadata) {
Expand All @@ -60,13 +61,13 @@ export class BroadcastHandler implements MessageHandler {

// handling state synchronisation of channels
case 'hello': return this.handleHello(msg as ConnectionStep2Hello, sc, from)
case 'channelSelectionChoice': return this.handleChannelSelectionChoice(msg as ChannelSectionChoiceAgentRequest, from, sc)
case 'channelSelectionChoice': return this.handleChannelSelectionChoice(msg as ChannelSelectionChoiceAgentRequest, from, sc)
}
}

handleChannelSelectionChoice(arg0: ChannelSectionChoiceAgentRequest, from: AppMetadata, sc: ServerContext): void | PromiseLike<void> {
handleChannelSelectionChoice(arg0: ChannelSelectionChoiceAgentRequest, from: AppMetadata, sc: ServerContext): void | PromiseLike<void> {
// currently, this is a no-op, just pass the same message to the app
const out = arg0 as ChannelSectionChoiceAgentResponse
const out = arg0 as ChannelSelectionChoiceAgentResponse
sc.post(out, from)
}

Expand Down
12 changes: 8 additions & 4 deletions packages/demo/src/client/apps/app1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ function createContext(i: number) {
*/
async function startBroadcasting() {
console.log("starting...")
const fdc3 = await getClientAPI();
const fdc3 = await getClientAPI();
console.log("got api...")
const channels = await fdc3.getUserChannels()
const channel = channels[0]
const cc = await fdc3.getCurrentChannel()

if (cc == null) {
const channels = await fdc3.getUserChannels()
await fdc3.joinUserChannel(channels[0].id)
}
for (let index = 0; index < 50; index++) {
setTimeout(() => channel.broadcast(createContext(index)), index*1000);
setTimeout(() => fdc3.broadcast(createContext(index)), index * 1000);
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/demo/src/client/da/DemoServerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Socket, io } from 'socket.io-client';
import { v4 as uuid } from 'uuid'
import { APP_HELLO, FDC3_DA_EVENT } from '../../message-types';
import { AppIdentifier, OpenError } from '@finos/fdc3';
import { AppChecker, DesktopAgentDetailResolver, DesktopAgentDetails, DesktopAgentPortResolver, IntentResolver, ChannelSelector } from 'fdc3-common';
import { AppChecker, DesktopAgentDetailResolver, DesktopAgentDetails, DesktopAgentPortResolver, IntentResolver, ChannelSelector, ChannelSelectorDetails, IntentResolverDetails } from 'fdc3-common';
import { link } from './util';

enum Opener { Tab, Frame, Nested }
Expand Down Expand Up @@ -142,11 +142,11 @@ export class DemoServerContext implements ServerContext {
detailsResolver: DesktopAgentDetailResolver = (o: Window, a: AppIdentifier) => {
const apiKey = "ABC"

const resolver: IntentResolver = {
const intentResolver: IntentResolverDetails = {
uri: window.location.origin + "/static/da/intent-resolver.html"
}

const channelSelector: ChannelSelector = {
const channelSelector: ChannelSelectorDetails = {
icon: {
src: window.location.origin + "/static/da/noun-mailbox-6010513.png",
css: {
Expand All @@ -168,14 +168,14 @@ export class DemoServerContext implements ServerContext {
apiKey,
uri: window.location.origin + "/static/da/embed.html",
desktopAgentId: this.desktopAgentUUID,
resolver,
intentResolver,
channelSelector
}
} as DesktopAgentDetails
} else {
return {
apiKey,
desktopAgentId: this.desktopAgentUUID,
resolver,
intentResolver,
channelSelector
} as DesktopAgentDetails
}
Expand Down
9 changes: 8 additions & 1 deletion packages/demo/src/client/da/dummy-desktop-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DA_HELLO, FDC3_APP_EVENT } from "../../message-types";
import { DemoServerContext } from "./DemoServerContext";
import { FDC3_2_1_JSONDirectory } from "./FDC3_2_1_JSONDirectory";
import { DefaultFDC3Server, DirectoryApp, ServerContext } from "da-server";
import { ChannelState } from "da-server/src/handlers/BroadcastHandler";



Expand Down Expand Up @@ -36,7 +37,13 @@ window.addEventListener("load", () => {
const directory = new FDC3_2_1_JSONDirectory()
await directory.load("/static/da/appd.json")
const sc = new DemoServerContext(socket, directory, desktopAgentUUID)
const fdc3Server = new DefaultFDC3Server(sc, directory, "FDC3-Web-Demo")
const initialChannels: ChannelState = {
"one": [],
"two": [],
"three": [],
"four": []
}
const fdc3Server = new DefaultFDC3Server(sc, directory, "FDC3-Web-Demo", initialChannels)

socket.on(FDC3_APP_EVENT, (msg, from) => {
fdc3Server.receive(msg, from)
Expand Down
6 changes: 3 additions & 3 deletions packages/demo/src/client/ui/channel-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ socket.on("connect", async () => {
const list = document.getElementById("channel-list")!!
const close = document.getElementById("close")!!

function sendChosenChannel(channelId: string, cancel: boolean) {
function sendChosenChannel(channelId: string, cancelled: boolean) {
const out: ChannelSelectionChoiceAgentRequest = {
type: "channelSelectionChoice",
payload: {
cancel,
cancelled,
channelId
},
meta: {
Expand All @@ -76,7 +76,7 @@ socket.on("connect", async () => {
li.style.backgroundColor = channel.displayMetadata.color
const a = document.createElement("a")
const description = document.createElement("em")
description.textContent = channel.displayMetadata.name
description.textContent = channel.displayMetadata.name = (channel.id == currentChannelId ? " CURRENT CHANNEL " : "")
a.textContent = channel.id

li.appendChild(a)
Expand Down
4 changes: 3 additions & 1 deletion packages/fdc3-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export type Loader = (options: Options) => Promise<DesktopAgent>
/**
* These are details such as login information sent from the desktop back to the
* app in order to initialise the api.
*
* TODO: remove this type
*/
export type DesktopAgentDetails = { [key: string]: string | number | boolean }
export type DesktopAgentDetails = { [key: string]: any }

/**
* Use these to return details specific to the window/app needing a connection
Expand Down

0 comments on commit 78ab479

Please sign in to comment.