Skip to content

Commit

Permalink
feat(language-server): supported multiuser support
Browse files Browse the repository at this point in the history
  • Loading branch information
JannisDommer committed Nov 17, 2023
1 parent 95b7090 commit d833422
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
9 changes: 6 additions & 3 deletions WebSocketClient/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import { instance } from "@viz-js/viz";
import { Message } from 'vscode-jsonrpc';

import { buildWorkerDefinition } from 'monaco-editor-workers';
import {randomUUID} from "crypto";
buildWorkerDefinition('./node_modules/monaco-editor-workers/dist/workers', new URL('', window.location.href).href, false);

const languageId = 'uvls';
let languageClient: MonacoLanguageClient;
let fileID;

const createUrl = (hostname: string, port: number, path: string, searchParams: Record<string, any> = {}, secure: boolean): string => {
const protocol = secure ? 'wss' : 'ws';
Expand Down Expand Up @@ -137,7 +139,7 @@ function createDiagramFromDot(res: string): void {
}

function generateDiagram(): void {
vscode.commands.executeCommand("uvls/generate_diagram", 'file:///workspace/fm.uvl')
vscode.commands.executeCommand("uvls/generate_diagram", `file:///workspace/${fileID}.uvl`)
.then((res) => createDiagramFromDot(res as string));
}

Expand Down Expand Up @@ -195,7 +197,8 @@ export const startPythonClient = async () => {
}`);

const fileSystemProvider = new RegisteredFileSystemProvider(false);
fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/workspace/fm.uvl'), 'features\n\tfeature1\n\t\tor\n\t\t\tfeature2\n\t\t\tfeature3\n\nconstraints\n\tfeature1'));
fileID = randomUUID();
fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file(`/workspace/${fileID}.uvl`), 'features\n\tfeature1\n\t\tor\n\t\t\tfeature2\n\t\t\tfeature3\n\nconstraints\n\tfeature1'));
registerFileSystemOverlay(1, fileSystemProvider);

// create the web socket and configure to start the language client on open, can add extra parameters to the url if needed.
Expand All @@ -208,7 +211,7 @@ export const startPythonClient = async () => {


// use the file create before
const modelRef = await createModelReference(monaco.Uri.file('/workspace/fm.uvl'));
const modelRef = await createModelReference(monaco.Uri.file(`/workspace/${fileID}.uvl`));
modelRef.object.setLanguageId(languageId);

// create monaco editor
Expand Down
42 changes: 41 additions & 1 deletion WebSocketLanguageServer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ const initUVLS = () => {

serverConnection.reader.listen((data: Message) => {
const typedData = data as MessageWithId;
if(Message.isNotification(typedData)){
for (const key in typedData.params){
if(key === "uri"){
let uri = typedData.params[key];
const connection = protocolVandalismMap.get(uri);
if(connection){
connection.writer.write(typedData).then(() => console.log("Written to SocketConn")).catch((reason) => console.log("Failed for reason: ", reason));
return;
}
console.log("ALAAAARM, VANDALISM VADILISMED");
}
}
}
if(Message.isResponse(data)){
for (const key in (data.result as unknown as any[])){
if(key === "serverInfo"){
initMessage = data;
}
}
}
if(typedData.id != undefined){
const entry = superMapperMap.get(Number(typedData.id!));
if (entry != undefined) {
Expand All @@ -38,6 +58,7 @@ const initUVLS = () => {
}
const socketConnection: IConnection = connectionMap.get(entry[0].toString())!;
socketConnection.writer.write(typedData).then(() => console.log("Written to SocketConn")).catch((reason) => console.log("Failed for reason: ", reason));
return;
}
}
})
Expand All @@ -52,8 +73,25 @@ function multiplexHandler(socket: IWebSocket) {


socketConnection.reader.listen((message) => {
if (Message.isRequest(message)) {
if (Message.isRequest(message) || Message.isNotification(message)) {
const method = (message as RequestMessage).method;
console.log("MEthod:", method);
console.log(method === "textDocument/didOpen");
for (const key in message.params){
if(key === "textDocument"){
for (const docKey in message.params[key]) {
if (docKey === "uri") {
let uri = message.params[key][docKey];
console.log("Found URI:", uri);
protocolVandalismMap.set(uri, socketConnection);
}
}
}
}
if(method === "initialize" && initMessage){
socketConnection.writer.write(initMessage).then(() => console.log("Written to serverCon")).catch((reason) => console.log("Failed for reason: ", reason));
return;
}
}
const socketNumber = connectionMap.getKey(socketConnection)!;
const jsonrpc: MessageWithId = message as MessageWithId;
Expand All @@ -74,8 +112,10 @@ function multiplexHandler(socket: IWebSocket) {

const superMapperMap = new Map<number, [number, number]>();
const connectionMap = new BiMap<IConnection>();
const protocolVandalismMap = new Map<string, IConnection>();
let serverConnection: IConnection;
let socketConnectionGlobal: IConnection;
let initMessage: Message;
let lastUsedID = -1;

export const runUVLServer = () => {
Expand Down

0 comments on commit d833422

Please sign in to comment.