Skip to content

Commit 65974d2

Browse files
committed
Fix process launcher
1 parent 4a8d3e8 commit 65974d2

28 files changed

Lines changed: 3030 additions & 50 deletions

snap-agent/src/main/java/org/snapscript/agent/ConsoleConnector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static void connect(ProcessEventChannel channel, String process) {
1313
try {
1414
ProcessEventStream errorAdapter = new ProcessEventStream(WRITE_ERROR, channel, System.err, process);
1515
ProcessEventStream outputAdapter = new ProcessEventStream(WRITE_OUTPUT, channel, System.out, process);
16-
PrintStream outputStream = new PrintStream(outputAdapter, false, "UTF-8");
17-
PrintStream errorStream = new PrintStream(errorAdapter, false, "UTF-8");
16+
PrintStream outputStream = new PrintStream(outputAdapter, true, "UTF-8");
17+
PrintStream errorStream = new PrintStream(errorAdapter, true, "UTF-8");
1818

1919
// redirect all output to the streams
2020
System.setOut(outputStream);

snap-develop/src/main/java/org/snapscript/develop/ProcessLauncher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ public ProcessLauncher(ProcessEventChannel channel, ConsoleLogger logger, Worksp
3131
public ProcessDefinition launch(ProcessConfiguration configuration) throws Exception {
3232
int remote = channel.port();
3333
int httpPort = configuration.getPort();
34+
String httpHost = configuration.getHost();
3435
String level = logger.getLevel();
3536
String name = generator.generate();
3637
String port = String.valueOf(remote);
3738
String home = System.getProperty("java.home");
3839
String java = String.format("%s%sbin%sjava", home, File.separatorChar, File.separatorChar);
39-
String resources = String.format("http://localhost:%s/resource/", httpPort);
40-
String classes = String.format("http://localhost:%s/class/", httpPort);
40+
String resources = String.format("http://%s:%s/resource/", httpHost, httpPort);
41+
String classes = String.format("http://%s:%s/class/", httpHost, httpPort);
4142
Map<String, String> variables = configuration.getVariables();
4243
List<String> arguments = configuration.getArguments();
4344
String launcher = RemoteProcessLauncher.class.getCanonicalName();

snap-develop/src/main/java/org/snapscript/develop/ProcessManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ public boolean ping(String process, long time) {
133133
return false;
134134
}
135135

136-
public void start(int port) {
136+
public void start(String host, int port) {
137137
loader.load(configuration);
138+
configuration.setHost(host);
138139
configuration.setPort(port);
139-
pool.start(port);
140+
pool.start(host, port);
140141
}
141142

142143
public void launch() {

snap-develop/src/main/java/org/snapscript/develop/ProcessPool.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ public void remove(ProcessEventListener listener) {
106106
}
107107
}
108108

109-
public void start(int port) { // http://host:port/project
109+
public void start(String host, int port) { // http://host:port/project
110110
try {
111111
manager.start();
112112
server.start();
113-
pinger.start(port);
113+
pinger.start(host, port);
114114
} catch(Exception e) {
115115
logger.info("Could not start pool on port " + port, e);
116116
}
@@ -313,10 +313,11 @@ public ProcessAgentPinger(long frequency) {
313313
this.frequency = frequency;
314314
}
315315

316-
public void start(int port) {
316+
public void start(String host, int port) {
317317
if(listen.compareAndSet(0, port)) {
318318
Thread thread = factory.newThread(this);
319319

320+
configuration.setHost(host);
320321
configuration.setPort(port);
321322
thread.start();
322323
}

snap-develop/src/main/java/org/snapscript/develop/ProcessServer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.snapscript.develop;
22

3+
import java.net.InetSocketAddress;
4+
35
import org.snapscript.develop.http.WebServer;
46

57
public class ProcessServer {
@@ -14,15 +16,17 @@ public ProcessServer(ProcessManager engine, WebServer server) {
1416

1517
public void start() {
1618
try {
17-
int port = server.start();
18-
String project = String.format("http://localhost:%s/", port);
19+
InetSocketAddress address = server.start();
20+
int port = address.getPort();
21+
String host = address.getHostString();
22+
String project = String.format("http://%s:%s/", host, port);
1923
String script = CommandLineArgument.SCRIPT.getValue();
2024

2125
if(script != null) {
2226
engine.launch(); // start a new process
2327
}
2428
System.err.println(project);
25-
engine.start(port);
29+
engine.start(host, port);
2630
} catch(Exception e) {
2731
e.printStackTrace();
2832
}

snap-develop/src/main/java/org/snapscript/develop/configuration/ProcessConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ProcessConfiguration {
1010
private Map<String, String> variables;
1111
private List<String> arguments;
1212
private String classPath;
13+
private String host;
1314
private int port;
1415

1516
public ProcessConfiguration() {
@@ -41,6 +42,14 @@ public void setClassPath(String classPath) {
4142
this.classPath = classPath;
4243
}
4344

45+
public String getHost() {
46+
return host;
47+
}
48+
49+
public void setHost(String host) {
50+
this.host = host;
51+
}
52+
4453
public int getPort() {
4554
return port;
4655
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.snapscript.develop.http;
2+
3+
import java.net.InetAddress;
4+
import java.net.InetSocketAddress;
5+
import java.net.SocketAddress;
6+
7+
public class WebAddress {
8+
9+
private final int port;
10+
11+
public WebAddress(int port) {
12+
this.port = port;
13+
}
14+
15+
public InetSocketAddress getLocalAddress() {
16+
try {
17+
return new InetSocketAddress(port);
18+
}catch(Exception e){
19+
return new InetSocketAddress(port);
20+
}
21+
}
22+
23+
public InetSocketAddress getExternalAddress() {
24+
try {
25+
String host = InetAddress.getLocalHost().getHostAddress();
26+
return new InetSocketAddress(host, port);
27+
}catch(Exception e){
28+
return new InetSocketAddress(port);
29+
}
30+
}
31+
}

snap-develop/src/main/java/org/snapscript/develop/http/WebServer.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,32 @@
1313
public class WebServer {
1414

1515
private final SocketProcessor server;
16-
private final SocketAddress address;
1716
private final Connection connection;
17+
private final WebAddress address;
1818

1919
public WebServer(Container container, int port) throws IOException {
2020
this.server = new ContainerSocketProcessor(container, 10);
2121
this.connection = new SocketConnection(server);
22-
this.address = new InetSocketAddress(port);
22+
this.address = new WebAddress(port);
2323
}
2424

25-
public int start() throws IOException {
25+
public InetSocketAddress start() throws IOException {
2626
try {
27-
InetSocketAddress local = (InetSocketAddress)connection.connect(address);
28-
return local.getPort();
27+
InetSocketAddress external = address.getExternalAddress();
28+
InetSocketAddress internal = address.getLocalAddress();
29+
InetSocketAddress local = (InetSocketAddress)connection.connect(internal);
30+
String host = external.getHostString();
31+
int port = local.getPort();
32+
33+
return new InetSocketAddress(host, port);
2934
} catch (IOException ex) {
30-
System.err.println("Failed to connect to: " + address);
31-
throw ex;
35+
try {
36+
SocketAddress local = address.getLocalAddress();
37+
return (InetSocketAddress)connection.connect(local);
38+
}catch(Exception e) {
39+
System.err.println("Failed to connect to: " + address);
40+
throw ex;
41+
}
3242
}
3343
}
3444

snap-develop/src/main/resources/context/develop.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229

230230
<bean id="WebContainer" class="org.snapscript.develop.http.WebContainer">
231231
<constructor-arg ref="WebSocketContainer" />
232-
<constructor-arg value="Snap/1.0" />
232+
<constructor-arg value="Apache/2.2.14 (Win32)" />
233233
</bean>
234234

235235
<bean id="WebServer" class="org.snapscript.develop.http.WebServer">

snap-develop/src/main/resources/resource/css/breakpoints.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
font-family: "Lucida Console", sans-serif;
33
font-size: 0.9em;
44
background-color: #ffffff;
5-
border-radius: 4px;
5+
/*border-radius: 4px;*/
66
border-style: solid;
77
border-width: 1px;
88
border-color: #c0c0c0;

0 commit comments

Comments
 (0)