-
Notifications
You must be signed in to change notification settings - Fork 24
/
StreamBrowserServer.java
367 lines (326 loc) · 10.7 KB
/
StreamBrowserServer.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* (c) 2011, Alejandro Serrano
* Released under the terms of the EPL.
*/
package net.sf.eclipsefp.haskell.browser.client;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.zip.InflaterInputStream;
import net.sf.eclipsefp.haskell.browser.BrowserEvent;
import net.sf.eclipsefp.haskell.browser.BrowserPlugin;
import net.sf.eclipsefp.haskell.browser.BrowserServer;
import net.sf.eclipsefp.haskell.browser.Database;
import net.sf.eclipsefp.haskell.browser.DatabaseLoadedEvent;
import net.sf.eclipsefp.haskell.browser.DatabaseType;
import net.sf.eclipsefp.haskell.browser.items.Declaration;
import net.sf.eclipsefp.haskell.browser.items.DeclarationId;
import net.sf.eclipsefp.haskell.browser.items.HaskellPackage;
import net.sf.eclipsefp.haskell.browser.items.HoogleResult;
import net.sf.eclipsefp.haskell.browser.items.HoogleStatus;
import net.sf.eclipsefp.haskell.browser.items.Module;
import net.sf.eclipsefp.haskell.browser.items.Packaged;
import net.sf.eclipsefp.haskell.browser.util.BrowserText;
import net.sf.eclipsefp.haskell.util.FileUtil;
import net.sf.eclipsefp.haskell.util.LangUtil;
import net.sf.eclipsefp.haskell.util.NullWriter;
import net.sf.eclipsefp.haskell.util.StreamRedirect;
import org.eclipse.core.runtime.IPath;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Class used for communicating with a Scion Browser instance.
*
* @author Alejandro Serrano
*/
public class StreamBrowserServer extends BrowserServer {
private IPath serverExecutable;
private Process process = null;
private BufferedWriter in = null;
// private BufferedReader err = null;
private InputStream out = null;
private boolean localDbLoaded = false;
private boolean hackageDbLoaded = false;
private boolean hoogleLoaded = false;
private StreamRedirect errorRedirect;
public Object lock;
//private DatabaseType currentDatabase;
private HashMap<String, Packaged<Declaration>[]> declCache;
private boolean logError;
public StreamBrowserServer(IPath serverExecutable,boolean logError) throws Exception {
this.serverExecutable = serverExecutable;
this.declCache = new HashMap<String, Packaged<Declaration>[]>();
lock = new Object();
this.logError=logError;
startServer(logError);
}
@Override
public void setLogStream(Writer logStream) {
super.setLogStream(logStream);
if (logError){
errorRedirect.setOutput(logStream);
}
}
public void setLogError(boolean logError) {
this.logError = logError;
}
public void startServer(boolean logError) throws Exception {
ProcessBuilder builder = new ProcessBuilder(
serverExecutable.toOSString());
builder.redirectErrorStream(false);
try {
process = builder.start();
out = process.getInputStream();
Writer errorWriter=logError && logStream!=null?logStream:new NullWriter();
errorRedirect=new StreamRedirect(new InputStreamReader(process.getErrorStream(),FileUtil.UTF8), errorWriter);
errorRedirect.start();
/*out = new BufferedReader(new InputStreamReader(
process.getInputStream(), FileUtil.UTF8));*/
/*err = new BufferedReader(new InputStreamReader(
process.getErrorStream(), FileUtil.UTF8)); */
in = new BufferedWriter(new OutputStreamWriter(
process.getOutputStream(),FileUtil.UTF8));
} catch (Throwable ex) {
throw new Exception("Could not load");
}
}
public String sendAndReceive(JSONObject input)
throws IOException {
synchronized(lock) {
String jsonInput = input.toString();
log(">> " + jsonInput);
in.write(jsonInput + "\n");
in.flush();
String response=getALine();
// String response = out.readLine();
//log(response);
if (response==null){
response=new JSONArray().toString();
}
return response;
}
}
public boolean sendAndReceiveOk(JSONObject input)
throws IOException {
synchronized(lock) {
String jsonInput = input.toString();
log(">> " + jsonInput);
in.write(jsonInput + "\n");
in.flush();
String response = null;
do {
response = getALine(); // out.readLine();
log(response);
} while (response!=null && !response.equals("\"ok\""));
return "\"ok\"".equals(response);
}
}
public synchronized boolean sendAndReceiveBoolean(JSONObject input)
throws IOException {
synchronized(lock) {
String jsonInput = input.toString();
log(">> " + jsonInput);
in.write(jsonInput + "\n");
in.flush();
String response = null;
do {
response = getALine(); // out.readLine();
log(response);
} while (response!=null && !response.equals("true") && !response.equals("false"));
return "true".equals(response);
}
}
public synchronized HoogleStatus sendAndReceiveStatus(JSONObject input)
throws IOException {
synchronized(lock) {
String jsonInput = input.toString();
log(">> " + jsonInput);
in.write(jsonInput + "\n");
in.flush();
HoogleStatus st=null;
String response = null;
do {
response = getALine(); // out.readLine();
log(response);
try {
st=HoogleStatus.valueOf(LangUtil.unquote(response).toUpperCase());
} catch (IllegalArgumentException iae){
// NOOP
}
} while (st==null);
return st;
}
}
public String getALine() throws IOException{
try {
InflaterInputStream gzip = new InflaterInputStream(out);
BufferedReader reader = new BufferedReader(
new InputStreamReader(gzip, FileUtil.UTF8));
return reader.readLine();
} catch (IOException e) {
BrowserPlugin.logError(BrowserText.error_read, e);
throw e;
}
}
@Override
public boolean isLocalDatabaseLoaded() {
return localDbLoaded;
}
@Override
public boolean isHackageDatabaseLoaded() {
return hackageDbLoaded;
}
@Override
public boolean isHoogleLoaded() {
return hoogleLoaded;
}
@Override
protected void loadLocalDatabaseInternal(String path, boolean rebuild)
throws IOException, JSONException {
if (sendAndReceiveOk(Commands.createLoadLocalDatabase(path, rebuild))){
// Notify listeners
DatabaseLoadedEvent e = new DatabaseLoadedEvent(this, path,
DatabaseType.LOCAL);
localDbLoaded = true;
notifyDatabaseLoaded(e);
}
}
@Override
protected void loadHackageDatabaseInternal(String path, boolean rebuild)
throws IOException, JSONException {
if (sendAndReceiveOk(Commands.createLoadHackageDatabase(path, rebuild))){
// Notify listeners
DatabaseLoadedEvent e = new DatabaseLoadedEvent(this, path,
DatabaseType.HACKAGE);
hackageDbLoaded = true;
notifyDatabaseLoaded(e);
}
}
// @Override
// public void setCurrentDatabase(DatabaseType current, PackageIdentifier id)
// throws IOException, JSONException {
// if (this.currentDatabase==null || !this.currentDatabase.equals(current) || id!=null){
// this.currentDatabase = current;
// sendAndReceiveOk(Commands.createSetCurrentDatabase(current, id));
// }
// }
@Override
public HaskellPackage[] getPackages(Database db) throws IOException, JSONException {
String response = sendAndReceive(Commands.createGetPackages(db));
return Commands.responseGetPackages(response);
}
@Override
public Module[] getAllModules(Database db) throws IOException, JSONException {
String response = sendAndReceive(Commands.createGetAllModules(db));
return Commands.responseGetModules(response);
}
@Override
public Module[] getModules(Database db,String module) throws IOException, JSONException {
String response = sendAndReceive(Commands.createGetModules(db,module));
return Commands.responseGetModules(response);
}
@SuppressWarnings("unchecked")
@Override
public Packaged<Declaration>[] getDeclarations(Database db,String module)
throws Exception {
// Try to find in cache
//if (this.currentDatabase == DatabaseType.ALL) {
Packaged<Declaration>[] decls=this.declCache.get(module);
if (decls!=null){
return decls;
}
//}
// If not, search
String response = sendAndReceive(Commands.createGetDeclarations(db,module));
decls = Commands.responseGetDeclarations(response);
if (decls==null){
decls=new Packaged[0];
}
// Check if we need to save in cache
//if (this.currentDatabase == DatabaseType.ALL) {
this.declCache.put(module, decls);
//}
return decls;
}
@SuppressWarnings("unchecked")
@Override
public Packaged<Declaration>[] getDeclarationsFromPrefix(Database db,String prefix)
throws Exception {
String response = sendAndReceive(Commands.createGetDeclarationsFromPrefix(db, prefix));
Packaged<Declaration>[] decls = Commands.responseGetDeclarationsFromPrefix(response);
if (decls==null){
decls=new Packaged[0];
}
return decls;
}
@Override
public DeclarationId[] findModulesForDeclaration(Database db,String decl) throws IOException, JSONException {
String response = sendAndReceive(Commands.createFindModulesForDeclaration(db,decl));
return Commands.responseGetDeclarationId(response);
}
@Override
public void setExtraHooglePath(String newPath) throws IOException, JSONException {
sendAndReceiveOk(Commands.createSetExtraHooglePath(newPath));
}
@Override
public HoogleResult[] queryHoogle(Database db,String query) throws Exception {
String response = sendAndReceive(Commands.createHoogleQuery(db,query));
return Commands.responseHoogleQuery(response);
}
@Override
public void downloadHoogleData() throws IOException, JSONException {
sendAndReceiveStatus(Commands.createDownloadHoogleData());
}
@Override
public HoogleStatus checkHoogle() throws Exception {
HoogleStatus st = sendAndReceiveStatus(Commands.createCheckHoogleData());
// If is present, notify the views
if (HoogleStatus.OK.equals(st)) {
hoogleLoaded = true;
notifyHoogleLoaded(new BrowserEvent(this));
}
return st;
}
@Override
public void stop() {
// Nothing is loaded
localDbLoaded = false;
hoogleLoaded = false;
// Tell we no longer have a database
BrowserEvent e = new BrowserEvent(this);
notifyDatabaseUnloaded(e);
// Nor a Hoogle connection
notifyHoogleUnloaded(e);
try {
if (out!=null){
out.close();
}
} catch (Exception ignore){
// noop
}
try {
if (in!=null){
in.close();
}
} catch (Exception ignore){
//noop
}
try {
sendAndReceiveOk(Commands.createQuit());
if (process!=null){
process.destroy();
}
} catch (Exception ex) {
if (process!=null){
process.destroy();
}
}
process = null;
}
}