/
PolyphenyControlConnector.java
277 lines (222 loc) · 9.18 KB
/
PolyphenyControlConnector.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
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.polypheny.control.client;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;
import kong.unirest.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
@Slf4j
public class PolyphenyControlConnector {
private final String controlUrl;
private static int clientId = -1;
private final ClientData clientData;
private final HttpConnector httpConnector;
private final Gson gson = new Gson();
private final LogHandler logHandler;
public PolyphenyControlConnector( String controlUrl, ClientData clientData, LogHandler logHandler ) throws URISyntaxException {
this.clientData = clientData;
this.logHandler = logHandler;
this.controlUrl = "http://" + controlUrl;
httpConnector = new HttpConnector();
httpConnector.setSessionTimeoutHandler( () -> {
httpConnector.authenticate( this.controlUrl + "/", clientData.getUsername(), clientData.getPassword() );
// After authenticating, try again.
return true;
} );
httpConnector.authenticate( this.controlUrl + "/", clientData.getUsername(), clientData.getPassword() );
WebSocket webSocket = new WebSocket( new URI( "ws://" + controlUrl + "/socket/" ) );
webSocket.connect();
// Check status of connection and reconnect if necessary
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleWithFixedDelay( () -> {
if ( webSocket.isClosed() ) {
try {
webSocket.reconnectBlocking();
} catch ( InterruptedException e ) {
log.error( "Interrupt while reconnecting", e );
}
}
}, 1, 3L, TimeUnit.SECONDS );
}
public void stopPolypheny() {
setClientType(); // Set the client type (again) - does not hurt and makes sure its set
try {
httpConnector.post( controlUrl + "/control/stop", request -> request.field( "clientId", clientId ) );
} catch ( UnirestException e ) {
log.error( "Error while stopping Polypheny-DB", e );
}
}
public void startPolypheny() {
setClientType(); // Set the client type (again) - does not hurt and makes sure its set
try {
httpConnector.post( controlUrl + "/control/start", request -> request.field( "clientId", clientId ) );
} catch ( UnirestException e ) {
log.error( "Error while starting Polypheny-DB", e );
}
}
public void purgePolyphenyFolder() {
setClientType(); // Set the client type (again) - does not hurt and makes sure its set
try {
httpConnector.post( controlUrl + "/control/purgePolyphenyFolder", request -> request.field( "clientId", clientId ) );
} catch ( UnirestException e ) {
log.error( "Error while purging Polypheny home folder", e );
}
}
public void updatePolypheny() {
// Check if in status idling
String status = getStatus();
if ( !status.equals( "idling" ) ) {
throw new RuntimeException( "Unable to update Polypheny while it is running" );
}
// Trigger update
try {
httpConnector.post( controlUrl + "/control/update", request -> request.field( "clientId", clientId ) );
} catch ( UnirestException e ) {
log.error( "Error while updating Polypheny-DB", e );
}
// Wait for update to finish
status = getStatus();
do {
try {
TimeUnit.SECONDS.sleep( 1 );
} catch ( InterruptedException e ) {
throw new RuntimeException( "Unexpected interrupt", e );
}
} while ( !status.equals( "idling" ) );
}
public void setConfig( Map<String, String> map ) {
JSONObject obj = new JSONObject();
for ( Map.Entry<String, String> entry : map.entrySet() ) {
obj.put( entry.getKey(), entry.getValue() );
}
try {
httpConnector.post(
controlUrl + "/config/set",
request -> request.field( "clientId", clientId ).field( "config", obj.toString() ) );
} catch ( UnirestException e ) {
log.error( "Error while setting client type", e );
}
}
void setClientType() {
try {
httpConnector.post(
controlUrl + "/client/type",
request -> request.field( "clientId", clientId ).field( "clientType", clientData.getClientType().name() ) );
} catch ( UnirestException e ) {
log.error( "Error while setting client type", e );
}
}
public String getConfig() {
return executeGet( "/config/get" );
}
public String getVersion() {
return executeGet( "/control/version" );
}
String getStatus() {
return gson.fromJson( executeGet( "/control/status" ), String.class );
}
boolean checkForAnyRunningPolyphenyInstances() {
return Boolean.parseBoolean( executeGet( "/control/checkAnyRunningPolyphenyInstances" ) );
}
private String executeGet( String command ) {
HttpResponse<String> httpResponse;
try {
httpResponse = httpConnector.get( controlUrl + command );
return httpResponse.getBody();
} catch ( UnirestException e ) {
log.error( "Exception while sending request", e );
}
return null;
}
private void executePost( String command, String data ) {
try {
Unirest.post( controlUrl + command ).body( data ).asString();
} catch ( UnirestException e ) {
log.error( "Exception while sending request", e );
}
}
private class WebSocket extends WebSocketClient {
private final Gson gson = new Gson();
public WebSocket( URI serverUri ) {
super( serverUri );
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
}
@Override
public void onMessage( String message ) {
if ( message.startsWith( "{\"version\":{" ) ) {
return;
}
Type type = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> data = gson.fromJson( message, type );
if ( data.containsKey( "clientId" ) ) {
clientId = Integer.parseInt( data.get( "clientId" ) );
setClientType();
}
if ( data.containsKey( "logOutput" ) ) {
if ( logHandler != null ) {
logHandler.handleLogMessage( data.get( "logOutput" ) );
}
}
if ( data.containsKey( "startOutput" ) ) {
if ( logHandler != null ) {
logHandler.handleStartupMessage( data.get( "startOutput" ) );
}
}
if ( data.containsKey( "stopOutput" ) ) {
if ( logHandler != null ) {
logHandler.handleShutdownMessage( data.get( "stopOutput" ) );
}
}
if ( data.containsKey( "restartOutput" ) ) {
if ( logHandler != null ) {
logHandler.handleRestartMessage( data.get( "restartOutput" ) );
}
}
if ( data.containsKey( "updateOutput" ) ) {
String logStr = data.get( "updateOutput" );
//noinspection StatementWithEmptyBody
if ( logStr.startsWith( "Task :" ) && (logStr.endsWith( "started" ) || logStr.endsWith( "skipped" ) || logStr.endsWith( "UP-TO-DATE" ) || logStr.endsWith( "SUCCESS" )) ) {
// Ignore this to avoid cluttering the log. These are gradle log massage where everything is fine.
} else {
if ( logHandler != null ) {
logHandler.handleUpdateMessage( logStr );
}
}
}
}
@Override
public void onClose( int code, String reason, boolean remote ) {
}
@Override
public void onError( Exception ex ) {
}
}
}