Skip to content

Commit 7ecc4da

Browse files
committed
Issue #46: Add socket namespace to the URL
1 parent fcd00c8 commit 7ecc4da

File tree

4 files changed

+78
-18
lines changed

4 files changed

+78
-18
lines changed

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/connection/MicroclimateConnection.java

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public class MicroclimateConnection {
5656
private IPath localWorkspacePath;
5757
private String versionStr;
5858
private String connectionErrorMsg = null;
59+
private String socketNamespace = null;
5960

60-
public final MicroclimateSocket mcSocket;
61+
private MicroclimateSocket mcSocket;
6162

6263
private volatile boolean isConnected = true;
6364

@@ -76,14 +77,7 @@ public MicroclimateConnection (URI uri) throws IOException, URISyntaxException,
7677
if (MicroclimateConnectionManager.getActiveConnection(uri.toString()) != null) {
7778
onInitFail(NLS.bind(Messages.MicroclimateConnection_ErrConnection_AlreadyExists, baseUrl));
7879
}
79-
80-
// Must set host, port fields before doing this
81-
mcSocket = new MicroclimateSocket(this);
82-
if(!mcSocket.blockUntilFirstConnection()) {
83-
close();
84-
throw new MicroclimateConnectionException(mcSocket.socketUri);
85-
}
86-
80+
8781
JSONObject env = getEnvData(this.baseUrl);
8882

8983
this.versionStr = getMCVersion(env);
@@ -104,11 +98,27 @@ public MicroclimateConnection (URI uri) throws IOException, URISyntaxException,
10498
// This should never happen since we have already determined it is a supported version of microclimate.
10599
onInitFail(Messages.MicroclimateConnection_ErrConnection_WorkspaceErr);
106100
}
101+
102+
this.socketNamespace = getSocketNamespace(env);
103+
104+
mcSocket = new MicroclimateSocket(this);
105+
if(!mcSocket.blockUntilFirstConnection()) {
106+
close();
107+
throw new MicroclimateConnectionException(mcSocket.socketUri);
108+
}
107109

108110
refreshApps(null);
109111

110112
MCLogger.log("Created " + this); //$NON-NLS-1$
111113
}
114+
115+
public String getSocketNamespace() {
116+
return socketNamespace;
117+
}
118+
119+
public MicroclimateSocket getMCSocket() {
120+
return mcSocket;
121+
}
112122

113123
private void onInitFail(String msg) throws ConnectException {
114124
MCLogger.log("Initializing MicroclimateConnection failed: " + msg); //$NON-NLS-1$
@@ -121,11 +131,8 @@ private void onInitFail(String msg) throws ConnectException {
121131
*/
122132
public void close() {
123133
MCLogger.log("Closing " + this); //$NON-NLS-1$
124-
if (mcSocket != null && mcSocket.socket != null) {
125-
if (mcSocket.socket.connected()) {
126-
mcSocket.socket.disconnect();
127-
}
128-
mcSocket.socket.close();
134+
if (mcSocket != null) {
135+
mcSocket.close();
129136
}
130137
for (MicroclimateApplication app : appMap.values()) {
131138
app.dispose();
@@ -250,6 +257,19 @@ private static Path getWorkspacePath(JSONObject env) throws JSONException {
250257
}
251258
return new Path(workspaceLoc);
252259
}
260+
261+
private static String getSocketNamespace(JSONObject env) throws JSONException {
262+
if (env.has(MCConstants.KEY_ENV_MC_SOCKET_NAMESPACE)) {
263+
Object nsObj = env.get(MCConstants.KEY_ENV_MC_SOCKET_NAMESPACE);
264+
if (nsObj instanceof String) {
265+
String namespace = (String)nsObj;
266+
if (!namespace.isEmpty()) {
267+
return namespace;
268+
}
269+
}
270+
}
271+
return null;
272+
}
253273

254274
/**
255275
* Refresh this connection's apps using the Microclimate project list endpoint.
@@ -547,6 +567,21 @@ public synchronized void clearConnectionError() {
547567
return;
548568
}
549569
this.localWorkspacePath = path;
570+
571+
String socketNS = getSocketNamespace(envData);
572+
if ((socketNS != null && !socketNS.equals(this.socketNamespace)) || (this.socketNamespace != null && !this.socketNamespace.equals(socketNS))) {
573+
// The socket namespace has changed so need to recreate the socket
574+
this.socketNamespace = socketNS;
575+
mcSocket.close();
576+
mcSocket = new MicroclimateSocket(this);
577+
if(!mcSocket.blockUntilFirstConnection()) {
578+
// Still not connected
579+
MCLogger.logError("Failed to create a new socket with updated URI: " + mcSocket.socketUri);
580+
// Clear the message so that it just shows the basic disconnected message
581+
this.connectionErrorMsg = null;
582+
return;
583+
}
584+
}
550585
} catch (Exception e) {
551586
MCLogger.logError("An exception occurred while trying to update the connection information", e);
552587
this.connectionErrorMsg = Messages.MicroclimateConnection_ErrConnection_UpdateCacheException;

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/connection/MicroclimateSocket.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018 IBM Corporation and others.
2+
* Copyright (c) 2018, 2019 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -72,10 +72,14 @@ public class MicroclimateSocket {
7272
public MicroclimateSocket(MicroclimateConnection mcConnection) throws URISyntaxException {
7373
this.mcConnection = mcConnection;
7474

75-
socketUri = mcConnection.baseUrl;
75+
URI uri = mcConnection.baseUrl;
76+
if (mcConnection.getSocketNamespace() != null) {
77+
uri = uri.resolve(mcConnection.getSocketNamespace());
78+
}
79+
socketUri = uri;
7680

7781
socket = IO.socket(socketUri);
78-
82+
7983
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
8084
@Override
8185
public void call(Object... arg0) {
@@ -232,6 +236,15 @@ public void call(Object... arg0) {
232236
MCLogger.log("Created MicroclimateSocket connected to " + socketUri); //$NON-NLS-1$
233237
}
234238

239+
public void close() {
240+
if (socket != null) {
241+
if (socket.connected()) {
242+
socket.disconnect();
243+
}
244+
socket.close();
245+
}
246+
}
247+
235248
private void onProjectCreation(JSONObject event) throws JSONException {
236249
String projectID = event.getString(MCConstants.KEY_PROJECT_ID);
237250
mcConnection.refreshApps(projectID);

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/console/SocketConsole.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018, 2019 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
112
package com.ibm.microclimate.core.internal.console;
213

314
import java.io.IOException;
@@ -29,7 +40,7 @@ public SocketConsole(String name, MicroclimateApplication app) {
2940

3041
this.projectID = app.projectID;
3142
this.outputStream = newOutputStream();
32-
this.socket = app.mcConnection.mcSocket;
43+
this.socket = app.mcConnection.getMCSocket();
3344
socket.registerSocketConsole(this);
3445

3546
try {

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/constants/MCConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ private MCConstants() {}
6767

6868
KEY_ENV_WORKSPACE_LOC = "workspace_location",
6969
KEY_ENV_MC_VERSION = "microclimate_version",
70+
KEY_ENV_MC_SOCKET_NAMESPACE = "socket_namespace",
7071

7172
KEY_LANGUAGE = "language",
7273
KEY_FRAMEWORK = "framework",

0 commit comments

Comments
 (0)