-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoSocketServer.java
111 lines (97 loc) · 3.18 KB
/
AcceleoSocketServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
* Copyright (c) 2020, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ls;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.launch.LSPLauncher;
import org.eclipse.lsp4j.services.LanguageClient;
/**
* {@link AcceleoLanguageServer} over a socket.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class AcceleoSocketServer {
/**
* Backlog for the socket server.
*/
private static final int BACKLOG = 50;
/**
* The {@link IAcceleoLanguageServerContext} of this server.
*/
private final IAcceleoLanguageServerContext acceleoLanguageServerContext;
/**
* The {@link ServerSocket}.
*/
private ServerSocket serverSocket;
/**
* The server {@link Thread}.
*/
private Thread serverThread;
/**
* Constructor.
*
* @param acceleoLanguageServerContext
* the (non-{@code null}) root {@link IAcceleoLanguageServerContext} of this server.
*/
public AcceleoSocketServer(IAcceleoLanguageServerContext acceleoLanguageServerContext) {
this.acceleoLanguageServerContext = acceleoLanguageServerContext;
}
/**
* Starts this server.
*
* @param host
* @throws UnknownHostException
* @throws IOException
*/
public synchronized void start(String host) throws UnknownHostException, IOException {
this.serverSocket = new ServerSocket(0, BACKLOG, InetAddress.getByName(host));
this.serverThread = new Thread(new Runnable() {
@Override
public void run() {
try {
final Socket client = serverSocket.accept();
final AcceleoLanguageServer acceleoLanguageServer = new AcceleoLanguageServer(
AcceleoSocketServer.this.acceleoLanguageServerContext);
final Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
acceleoLanguageServer, client.getInputStream(), client.getOutputStream());
launcher.startListening(); // the thread is created inside this method
acceleoLanguageServer.connect(launcher.getRemoteProxy());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, "Acceleo LS: " + serverSocket.getInetAddress().getHostName() + ":" + serverSocket.getLocalPort());
this.serverThread.start();
}
/**
* Provides the port currently in use by this socket server.
*
* @return the port currently in use.
*/
public int getPort() {
return this.serverSocket.getLocalPort();
}
/**
* Stops this server.
*
* @throws IOException
* if an I/O error occurs when stopping the server.
*/
public synchronized void stop() throws IOException {
serverSocket.close();
serverThread.interrupt();
}
}