-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
294 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Server } from "http"; | ||
import { APP_HELLO, DA_HELLO, FDC3_APP_EVENT, HelloArgs } from "./message-types"; | ||
import { Socket } from "socket.io"; | ||
import { AppIdentifier } from "@finos/fdc3"; | ||
import { SailFDC3Server } from "./SailFDC3Server"; | ||
import { SailServerContext } from "./SailServerContext"; | ||
|
||
type FDC3Session = { | ||
fdc3Server: SailFDC3Server, | ||
serverContext: SailServerContext | ||
} | ||
|
||
export function initDesktopAgentService(httpServer: Server): void { | ||
|
||
const io = new Server(httpServer) | ||
|
||
const sessions: Map<string, FDC3Session> = new Map() | ||
|
||
io.on('connection', (socket: Socket) => { | ||
|
||
var myInstance: FDC3Session | undefined | ||
var myId: string | undefined | ||
|
||
socket.on(DA_HELLO, function (data: HelloArgs) { | ||
myId = data.id | ||
if (sessions.get(myId)) { | ||
console.log("Need to reconfigure desktop agent") | ||
} | ||
|
||
myInstance = sessions.get(myId) | ||
if (myInstance) { | ||
// reconfiguring current session | ||
myInstance.fdc3Server.getBroadcastHandler().updateChannels(data.channels) | ||
myInstance.fdc3Server.getDirectory().replace(data.directories) | ||
console.log("updated desktop agent channels and directories" + sessions.size) | ||
} else { | ||
// starting session | ||
const serverContext = new SailServerContext(socket) | ||
const fdc3Server = new SailFDC3Server(serverContext, data) | ||
myInstance = { | ||
serverContext, | ||
fdc3Server | ||
} | ||
sessions.set(myId, myInstance) | ||
console.log("created agent session. Running sessions " + sessions.size) | ||
} | ||
}) | ||
|
||
socket.on(APP_HELLO, function (id: string, appID: AppIdentifier) { | ||
myId = appID.instanceId!! | ||
const instance = sessions.get(id) | ||
|
||
if (instance != undefined) { | ||
console.log("An app connected: " + id) | ||
instance.serverContext.connect(myId, socket) | ||
myInstance = instance | ||
} else { | ||
console.log("App Tried Connecting to non-existent DA Instance " + id + " " + appID.appId + " " + appID.instanceId) | ||
} | ||
}) | ||
|
||
socket.on(FDC3_APP_EVENT, function (data, from): void { | ||
// message from app to da | ||
console.log(JSON.stringify(data)) | ||
|
||
// if ((myInstance == null) && (data.type == 'intentResolutionChoice')) { | ||
// // message from app's intent resolver | ||
// myInstance = Array.from(instances.values()).find(cw => cw.apps.get(from.instanceId)) | ||
// } | ||
|
||
// if ((myInstance == null) && (data.type == 'channelSelectionChoice')) { | ||
// // message from app's channelSelector | ||
// myInstance = Array.from(instances.values()).find(cw => cw.apps.get(from.instanceId)) | ||
// } | ||
|
||
if (myInstance != undefined) { | ||
myInstance!!.fdc3Server.receive(data, from) | ||
} | ||
}) | ||
|
||
socket.on("disconnect", function (): void { | ||
if (myInstance) { | ||
if (myInstance.serverContext.serverSocket.id == socket.id) { | ||
sessions.delete(myId!!) | ||
} else { | ||
myInstance.serverContext.disconnect(socket) | ||
console.log(`Apparent disconnect: ${myInstance.serverContext.apps.size} remaining`) | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
packages/sail2/src/server/da/ReconfigurableBroadcastHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { BroadcastHandler, ChannelState } from "da-server/src/handlers/BroadcastHandler"; | ||
import { ChannelMetadata } from "./message-types"; | ||
|
||
|
||
export class ReconfigurableBroadcastHandler extends BroadcastHandler { | ||
|
||
private channelMetadata: ChannelMetadata[] | ||
|
||
constructor(name: string, cm: ChannelMetadata[]) { | ||
const state: ChannelState = {} | ||
cm.forEach(item => { | ||
state[item.id] = [] | ||
}) | ||
|
||
super(name, state) | ||
this.channelMetadata = cm | ||
} | ||
|
||
updateChannels(cm: ChannelMetadata[]) { | ||
cm.forEach(item => { | ||
if (!this.state[item.id]) { | ||
// create the new channel | ||
this.state[item.id] = [] | ||
} | ||
}) | ||
|
||
this.channelMetadata = cm | ||
} | ||
|
||
getChannelMetadata(): ChannelMetadata[] { | ||
return this.channelMetadata | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { BasicFDC3Server, ServerContext } from "da-server"; | ||
import { SailDirectory } from "../appd/SailDirectory"; | ||
import { ReconfigurableBroadcastHandler } from "./ReconfigurableBroadcastHandler"; | ||
import { IntentHandler } from "da-server/src/handlers/IntentHandler"; | ||
import { OpenHandler } from "da-server/src/handlers/OpenHandler"; | ||
import { ChannelMetadata, HelloArgs } from "./message-types"; | ||
|
||
/** | ||
* Extends BasicFDC3Server to allow for more detailed (and changeable) user channel metadata | ||
* as well as user-configurable SailDirectory. | ||
*/ | ||
export class SailFDC3Server extends BasicFDC3Server { | ||
|
||
protected readonly directory: SailDirectory | ||
protected readonly broadcastHandler: ReconfigurableBroadcastHandler | ||
|
||
constructor(sc: ServerContext, helloArgs: HelloArgs) { | ||
const dir = new SailDirectory() | ||
const bh = new ReconfigurableBroadcastHandler("Sail", helloArgs.channels) | ||
const ih = new IntentHandler(dir, 200000) | ||
const oh = new OpenHandler(dir) | ||
|
||
super([bh, ih, oh], sc) | ||
dir.replace(helloArgs.directories) | ||
|
||
this.directory = dir | ||
this.broadcastHandler = bh | ||
} | ||
|
||
getDirectory() { | ||
return this.directory | ||
} | ||
|
||
getBroadcastHandler() { | ||
return this.broadcastHandler | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { AppMetadata } from "@finos/fdc3/dist/bridging/BridgingTypes"; | ||
import { ServerContext } from "da-server"; | ||
import { Socket } from "socket.io"; | ||
import { v4 as uuidv4 } from 'uuid' | ||
import { DA_OPEN, FDC3_APP_EVENT } from "./message-types"; | ||
|
||
|
||
|
||
export class SailServerContext implements ServerContext { | ||
|
||
readonly serverSocket: Socket | ||
readonly apps: Map<AppMetadata, Socket> = new Map() | ||
|
||
constructor(socket: Socket) { | ||
this.serverSocket = socket; | ||
} | ||
|
||
createUUID(): string { | ||
return uuidv4() | ||
} | ||
|
||
async post(message: object, to: AppMetadata): Promise<void> { | ||
const ent = Array.from(this.apps.entries()).find(e => this.matches(e[0], to)) | ||
if (ent) { | ||
ent[1].emit(FDC3_APP_EVENT, message) | ||
} else { | ||
this.log(`Can't find app: ${JSON.stringify(to)}`) | ||
} | ||
} | ||
|
||
async open(appId: string): Promise<AppMetadata> { | ||
const instanceId = uuidv4() | ||
const metadata = { | ||
appId, | ||
instanceId | ||
} as AppMetadata | ||
this.serverSocket.emit(DA_OPEN, metadata) | ||
return metadata | ||
} | ||
|
||
getOpenApps(): Promise<AppMetadata[]> { | ||
return Promise.resolve(Array.from(this.apps.keys())) | ||
} | ||
|
||
matches(a: AppMetadata, b: AppMetadata): boolean { | ||
return (a.appId == b.appId) && (a.instanceId == b.instanceId) | ||
} | ||
|
||
async isAppOpen(app: AppMetadata): Promise<boolean> { | ||
const openApps = await this.getOpenApps() | ||
const found = openApps.find(a => this.matches(app, a)) | ||
return found != null | ||
} | ||
|
||
log(message: string): void { | ||
console.log(message) | ||
} | ||
|
||
provider(): string { | ||
return "FDC3 Sail" | ||
} | ||
|
||
providerVersion(): string { | ||
return "2.0" | ||
} | ||
|
||
fdc3Version(): string { | ||
return "2.0" | ||
} | ||
|
||
/** | ||
* Called when an app connects to the server | ||
*/ | ||
connect(appId: AppMetadata, socket: Socket) { | ||
this.apps.set(appId, socket) | ||
} | ||
|
||
/** | ||
* Called when an app disconnects from the server | ||
*/ | ||
disconnect(socket: Socket) { | ||
const ent = Array.from(this.apps.entries()).find(e => e[1].id == socket.id) | ||
if (ent) { | ||
this.apps.delete(ent[0]) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
export const DA_HELLO = 'da-hello' | ||
export const APP_HELLO = 'app-hello' | ||
export const FDC3_APP_EVENT = 'fdc3-app-event' | ||
export const DA_OPEN = 'da-open' | ||
|
||
export type ChannelMetadata = { | ||
id: string | ||
displayName: string, | ||
color: string, | ||
icon: string | ||
} | ||
|
||
export type HelloArgs = { | ||
id: string, | ||
directories: string[], | ||
channels: ChannelMetadata[] | ||
} |
Oops, something went wrong.