Skip to content

Commit

Permalink
Working on App Metadata transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Dec 13, 2023
1 parent 6e22b2a commit ca60ad1
Show file tree
Hide file tree
Showing 26 changed files with 287 additions and 86 deletions.
7 changes: 7 additions & 0 deletions packages/client/src/messaging/MessagePortMessaging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AppIdentifier } from "@finos/fdc3"
import { AgentRequestMessage } from "@finos/fdc3/dist/bridging/BridgingTypes"
import { Messaging } from "da"
import { exchangePostMessage } from "fdc3-common"
import { v4 as uuidv4 } from "uuid"

type ListenerDetail = {
Expand Down Expand Up @@ -60,4 +61,10 @@ export class MessagePortMessaging implements Messaging {
"source": this.getSource()
}
}

exchange<X>(message: object, expectedTypeName: string): Promise<X> {
return exchangePostMessage(this.mp, expectedTypeName, message).then(e => {
return e.data as X
});
}
}
2 changes: 1 addition & 1 deletion packages/client/src/messaging/message-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function messagePortInit(data: APIResponseMessage, options: Options
return new BasicDesktopAgent(
new DefaultChannelSupport(messaging, userChannelState, null),
new DefaultIntentSupport(),
new DefaultAppSupport(),
new DefaultAppSupport(messaging, data.appIdentifier),
data.fdc3Version,
data.provider);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions packages/da/dist/Messaging.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export interface Messaging {
*/
unregister(id: string): void;
createMeta(): object;
/**
*
* @param message Performs a request / response message pass
*/
exchange<X>(message: object, expectedTypeName: string): Promise<X>;
}
7 changes: 6 additions & 1 deletion packages/da/dist/apps/DefaultAppSupport.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { AppIdentifier, AppMetadata, Context } from "@finos/fdc3";
import { AppSupport } from "./AppSupport";
import { Messaging } from "../Messaging";
export declare class DefaultAppSupport implements AppSupport {
readonly messaging: Messaging;
readonly appIdentifier: AppIdentifier;
private thisAppMetadata;
constructor(messaging: Messaging, appIdentifier: AppIdentifier);
hasDesktopAgentBridging(): boolean;
hasOriginatingAppMetadata(): boolean;
findInstances(_app: AppIdentifier): Promise<AppIdentifier[]>;
getAppMetadata(_app: AppIdentifier): Promise<AppMetadata>;
getAppMetadata(app: AppIdentifier): Promise<AppMetadata>;
open(_app: AppIdentifier, _context?: Context | undefined): Promise<AppIdentifier>;
getThisAppMetadata(): Promise<AppMetadata>;
}
70 changes: 66 additions & 4 deletions packages/da/dist/apps/DefaultAppSupport.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/da/src/Messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ export interface Messaging {
unregister(id: string) : void

createMeta() : object

/**
*
* @param message Performs a request / response message pass
*/
exchange<X>(message: object, expectedTypeName: string) : Promise<X>
}
33 changes: 29 additions & 4 deletions packages/da/src/apps/DefaultAppSupport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { AppIdentifier, AppMetadata, Context } from "@finos/fdc3";
import { AppSupport } from "./AppSupport";
import { Messaging } from "../Messaging";
import { AppDestinationIdentifier, GetAppMetadataAgentRequest, GetAppMetadataAgentRequestMeta, GetAppMetadataAgentResponse } from "@finos/fdc3/dist/bridging/BridgingTypes";


export class DefaultAppSupport implements AppSupport {

readonly messaging: Messaging
readonly appIdentifier: AppIdentifier
private thisAppMetadata: AppMetadata | null = null

constructor(messaging: Messaging, appIdentifier: AppIdentifier) {
this.messaging = messaging
this.appIdentifier = appIdentifier
}

hasDesktopAgentBridging(): boolean {
throw new Error("Method not implemented.");
}
Expand All @@ -16,16 +27,30 @@ export class DefaultAppSupport implements AppSupport {
throw new Error("Method not implemented.");
}

getAppMetadata(_app: AppIdentifier): Promise<AppMetadata> {
throw new Error("Method not implemented.");
getAppMetadata(app: AppIdentifier): Promise<AppMetadata> {
const request : GetAppMetadataAgentRequest = {
type: "getAppMetadataRequest",
payload: {
app: app as AppDestinationIdentifier
},
meta: this.messaging.createMeta() as GetAppMetadataAgentRequestMeta
}

return this.messaging.exchange<GetAppMetadataAgentResponse>(request, "getAppMetadataResponse").then(d => {
return d.payload.appMetadata
});
}

open(_app: AppIdentifier, _context?: Context | undefined): Promise<AppIdentifier> {
throw new Error("Method not implemented.");
}

getThisAppMetadata(): Promise<AppMetadata> {
throw new Error("Method not implemented.");
async getThisAppMetadata(): Promise<AppMetadata> {
if (!this.thisAppMetadata) {
this.thisAppMetadata = await this.getAppMetadata(this.appIdentifier)
}

return this.thisAppMetadata
}

}
2 changes: 1 addition & 1 deletion packages/da/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

31 changes: 19 additions & 12 deletions packages/demo/src/dummy-desktop-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,26 @@ window.addEventListener("load", () => {
// for a given window, allows us to determine which app it is (if any)
const appChecker : AppChecker = o => instances.find(i => i.window == o)

const detailsResolver : DesktopAgentDetailResolver = (o) => {
// create the shared web worker
const sw = new SharedWorker('http://localhost:8080/src/server/SimpleServer.ts', {
type: "module"
})

sw.port.start()

const detailsResolver : DesktopAgentDetailResolver = (o, a) => {
const apiKey = "ABC"+ (currentApiInstance++)
sw.port.postMessage({
type: "internalRegisterAppInstance",
apiKey,
appIdentifier: {
appId: a.appId,
instanceId: a.instanceId,
desktopAgent: a.desktopAgent
}
})
return {
apikey: "Abc"
apiKey
}
}

Expand All @@ -86,15 +103,5 @@ window.addEventListener("load", () => {
document.getElementById("app2")?.addEventListener("click", () => launch("http://robs-pro:8080/static/app2/index.html", "2"));
document.getElementById("app3")?.addEventListener("click", () => launch("http://localhost:8080/static/app3/index.html", "3"));

// // listen for desktop agent ports being sent to connect
// bc.addEventListener(
// "message",
// (event) => {
// console.log("Received "+event)
// if (event.data.type == FDC3_PORT_TRANSFER_REQUEST_TYPE) {
// const port = event.data.payload
// server.register(port)
// }
// });
})

19 changes: 15 additions & 4 deletions packages/demo/src/server/SimpleServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* You can register message ports with it and plug in extra functionality to handle new message types.
*/

import { Context } from "@finos/fdc3"
import { AppIdentifier, Context } from "@finos/fdc3"
import { v4 as uuidv4 } from "uuid"
import { handleHandshake } from "./handshake"
import { ConnectionStep2Hello, ConnectionStep3Handshake } from "@finos/fdc3/dist/bridging/BridgingTypes"
import { handleBroadcast } from "./broadcast"
import { handleInternalRegisterDesktopInstance } from "./internal-register"

declare var onconnect : any

Expand All @@ -19,9 +21,10 @@ type ClientData = {
export class SimpleServer {

readonly channelsState: Record<string, Context[]>
readonly clients : Map<MessagePort, ClientData> = new Map()
readonly instances: Record<string, AppIdentifier> = {}
private readonly actions : Record<string, (e: MessageEvent<any>, client: MessagePort, ss: SimpleServer) => void>
private readonly clients : Map<MessagePort, ClientData> = new Map()


constructor(actions) {
this.channelsState = {}
this.actions = actions
Expand Down Expand Up @@ -50,11 +53,19 @@ export class SimpleServer {
hello,
handshake
})

console.log("Added client. "+this.clients.size)
}

addInstance(apiKey: string, appIdentifier: AppIdentifier) {
this.instances[apiKey] = appIdentifier
}
}

const theServer = new SimpleServer({
"hello" : handleHandshake
"hello" : handleHandshake,
"broadcastRequest": handleBroadcast,
"internalRegisterAppInstance": handleInternalRegisterDesktopInstance
});

onconnect = function (event) {
Expand Down
29 changes: 29 additions & 0 deletions packages/demo/src/server/app-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { GetAppMetadataAgentRequest, GetAppMetadataAgentResponse } from "@finos/fdc3/dist/bridging/BridgingTypes";
import { SimpleServer } from "./SimpleServer";

export function handleAppMetadata(e: MessageEvent, client: MessagePort, ss: SimpleServer) {
const message = e.data as GetAppMetadataAgentRequest

const app = message.payload.app

const response : GetAppMetadataAgentResponse = {
type: "getAppMetadataResponse",
meta: {
requestUuid: message.meta.requestUuid,
responseUuid: ss.createUUID(),
timestamp: new Date(),
},
payload: {
appMetadata: {
appId: message.payload.app.appId,
name: "YOYO",
version: "3.2"
}
}
}

client.postMessage(response)



}
20 changes: 20 additions & 0 deletions packages/demo/src/server/broadcast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SimpleServer } from "./SimpleServer";



export function handleBroadcast(e: MessageEvent, client: MessagePort, ss: SimpleServer) {
// in this case, we are going to send the message to every _other_ client that
// is registered.

console.log("Clients: "+ss.clients.size)

Array.from(ss.clients.keys()).forEach(mp => {
if (mp != client) {
try {
mp.postMessage(e.data)
} catch (e) {
console.error("Couldn't send: "+e)
}
}
})
}
7 changes: 7 additions & 0 deletions packages/demo/src/server/internal-register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { SimpleServer } from "./SimpleServer";

export function handleInternalRegisterDesktopInstance(e: MessageEvent, client: MessagePort, ss: SimpleServer) {
console.log("Registering app Identifier: "+e.data.appIdentifier)
ss.addInstance(e.data.apiKey, e.data.appIdentifier)

}
9 changes: 3 additions & 6 deletions packages/fdc3-common/src/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ export function exchangeForMessagePort(p: MessagePort | Window | BroadcastChanne
/**
* Send a message to on a port and wait for one to come back.
*/
export function exchangePostMessage(p: MessagePort | Window | BroadcastChannel, fromType: any, toType: string, payload?: any) : Promise<any> {
export function exchangePostMessage(p: MessagePort | Window | BroadcastChannel, toType: string, contents: any) : Promise<any> {
return exchange(p, toType, () => {
console.log("Posting message: "+fromType)
p.postMessage({
type: fromType,
payload
});
console.log("Posting message: "+JSON.stringify(contents))
p.postMessage(contents);
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/fdc3-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type Loader = (options: Options) => Promise<DesktopAgent>
*/
export type DesktopAgentDetails = { [key: string] : string | number | boolean }

export type DesktopAgentDetailResolver = (o: Window) => DesktopAgentDetails
export type DesktopAgentDetailResolver = (o: Window, a: AppIdentifier) => DesktopAgentDetails

export type Method = (r: APIResponseMessage, options: Options) => Promise<DesktopAgent>

Expand Down
2 changes: 1 addition & 1 deletion packages/fdc3-common/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const supply: Supplier = (checker: AppChecker, detailsResolver: DesktopAg
fdc3Version: "2.0",
type: FDC3_API_RESPONSE_MESSAGE_TYPE,
...staticDetails,
...detailsResolver(source),
...detailsResolver(source, appId),

method: "message-port",

Expand Down
Loading

0 comments on commit ca60ad1

Please sign in to comment.