Skip to content

Commit

Permalink
Added external API to contribute start/stop trace server implementation
Browse files Browse the repository at this point in the history
Added new external api for trace server startup and shutdown
The new API allows adopting extension to register server startup handler and shutdown handlers and a validator function to check whether a trace file at given location is applicable

Fixes #159

Signed-off-by: Alex Doan <[email protected]>
  • Loading branch information
AlexDoanBB authored and PatrickTasse committed Oct 18, 2023
1 parent 8add693 commit b2c8f77
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 6 deletions.
20 changes: 15 additions & 5 deletions vscode-trace-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import { VSCODE_MESSAGES } from 'vscode-trace-common/lib/messages/vscode-message
import { TraceViewerPanel } from './trace-viewer-panel/trace-viewer-webview-panel';
import { TspClientProvider } from 'vscode-trace-common/lib/client/tsp-client-provider-impl';
import { TraceServerUrlProvider } from 'vscode-trace-common/lib/server/trace-server-url-provider';
import { TraceServerManager } from './utils/trace-server-manager';

export let traceLogger: TraceExtensionLogger;
export const traceExtensionWebviewManager: TraceExtensionWebviewManager = new TraceExtensionWebviewManager();
export const traceServerManager: TraceServerManager = new TraceServerManager();
const tspClientProvider = new TspClientProvider(getTspClientUrl(), undefined, new TraceServerUrlProvider());

export function activate(context: vscode.ExtensionContext): ExternalAPI {
Expand Down Expand Up @@ -70,8 +72,8 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
// TODO: For now, a different command opens traces from file explorer. Remove when we have a proper trace finder
const fileOpenHandler = fileHandler(analysisProvider);
context.subscriptions.push(
vscode.commands.registerCommand('traces.openTraceFile', async file => {
await startTraceServerIfAvailable();
vscode.commands.registerCommand('traces.openTraceFile', async (file: vscode.Uri) => {
await startTraceServerIfAvailable(file.fsPath);
if (await isUp()) {
fileOpenHandler(context, file);
}
Expand Down Expand Up @@ -187,15 +189,23 @@ export function activate(context: vscode.ExtensionContext): ExternalAPI {
return traceExtensionAPI;
}

export function deactivate(): void {
export async function deactivate(): Promise<void> {
await traceServerManager.stopServer();
traceServerManager.dispose();
traceLogger.disposeChannel();
traceExtensionWebviewManager.dispose();
}

async function startTraceServerIfAvailable(): Promise<void> {
async function startTraceServerIfAvailable(pathToTrace?: string): Promise<void> {
const extensionId = 'vscode-trace-server';
if (await isUp()) {
return;
}
if (pathToTrace) {
await traceServerManager.startServer(pathToTrace);
}
const traceServerExtension = vscode.extensions.getExtension('tracecompass-community.' + extensionId);
if (!traceServerExtension || (await isUp())) {
if (!traceServerExtension) {
return;
}
await vscode.commands.executeCommand(extensionId + '.start-if-stopped');
Expand Down
13 changes: 12 additions & 1 deletion vscode-trace-extension/src/external-api/external-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import { Experiment } from 'tsp-typescript-client/lib/models/experiment';
import { TraceViewerPanel } from '../trace-viewer-panel/trace-viewer-webview-panel';
import * as vscode from 'vscode';
import { traceExtensionWebviewManager } from '../extension';
import { traceExtensionWebviewManager, traceServerManager } from '../extension';
import { TraceServerContributor } from '../utils/trace-server-manager';

export interface ExternalAPI {
getActiveExperiment(): Experiment | undefined;
getActiveWebviewPanels(): { [key: string]: TraceViewerPanel | undefined };
getActiveWebviews(): vscode.WebviewView[];
onWebviewCreated(listener: (data: vscode.WebviewView) => void): void;
onWebviewPanelCreated(listener: (data: vscode.WebviewPanel) => void): void;
addTraceServerContributor(contributor: TraceServerContributor): void;
}

export const traceExtensionAPI: ExternalAPI = {
Expand Down Expand Up @@ -60,5 +62,14 @@ export const traceExtensionAPI: ExternalAPI = {
*/
onWebviewPanelCreated(listener: (data: vscode.WebviewPanel) => void): void {
traceExtensionWebviewManager.onWebviewPanelCreated(listener);
},

/**
* Registers a server contributor
*
* @param contributor Contributor object that contains startServer, stopServer handlers and a traceValidator
*/
addTraceServerContributor(contributor: TraceServerContributor): void {
traceServerManager.addTraceServerContributor(contributor);
}
};
84 changes: 84 additions & 0 deletions vscode-trace-extension/src/utils/trace-server-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/***************************************************************************************
* Copyright (c) 2023 BlackBerry Limited and others.
*
* Licensed under the MIT license. See LICENSE file in the project root for details.
***************************************************************************************/
export interface TraceServerContributor {
startServer: () => Promise<void>;
stopServer?: () => Promise<void>;
isApplicable?: (pathToTrace: string) => boolean;
}

export class TraceServerManager {
private traceServersContributors: TraceServerContributor[] = [];
private isManagerDisposed = false;
private indexOfTraceServerContributor = -1;

/**
* Add new contributor (groups of functions that runs on adopting extension to start/stop its trace server) to the manager
*
* @param contributor Contributor object that contains startServer, stopServer handlers and a traceValidator
*/
addTraceServerContributor(contributor: TraceServerContributor): void {
if (!this.isDisposed()) {
this.traceServersContributors.push(contributor);
}
}

/**
* Look for appropriate startServer handler and execute it, also assign the index of current contributor
*
* @param pathToTrace path to trace file
*/
async startServer(pathToTrace: string): Promise<void> {
this.indexOfTraceServerContributor = -1;
if (!this.isDisposed()) {
// find an adopting extension that has successfully validated the trace
let index = this.traceServersContributors.findIndex(
traceServerContributor => traceServerContributor.isApplicable?.(pathToTrace)
);
if (index === -1) {
// find an adopting extension with no validator
index = this.traceServersContributors.findIndex(
traceServerContributor => !traceServerContributor.isApplicable
);
}
// if found
if (index !== -1) {
await this.traceServersContributors[index].startServer();
this.indexOfTraceServerContributor = index;
return;
}
}
}

/**
* execute server stopping handler
*/
async stopServer(): Promise<void> {
if (this.indexOfTraceServerContributor !== -1) {
await this.traceServersContributors[this.indexOfTraceServerContributor].stopServer?.();
this.indexOfTraceServerContributor = -1;
}
}

/**
* remove all contributors, set manager to disposed status
*/
dispose(): void {
if (!this.isDisposed()) {
this.traceServersContributors = [];
this.isManagerDisposed = true;
this.indexOfTraceServerContributor = -1;
}
}

/**
* whether manager is disposed
*
* @returns disposed status
*/
isDisposed(): boolean {
return this.isManagerDisposed;
}
}

0 comments on commit b2c8f77

Please sign in to comment.