Skip to content
Snippets Groups Projects
Commit c613695d authored by Jesper Fyhr Knudsen's avatar Jesper Fyhr Knudsen
Browse files

Create simple http server

parent dbe72295
Branches
No related tags found
No related merge requests found
...@@ -3,25 +3,11 @@ ...@@ -3,25 +3,11 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/octopus-agent/octopus-agent.iml" filepath="$PROJECT_DIR$/octopus-agent/octopus-agent.iml" /> <module fileurl="file://$PROJECT_DIR$/octopus-agent/octopus-agent.iml" filepath="$PROJECT_DIR$/octopus-agent/octopus-agent.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-agent/octopus-agent.iml" filepath="$PROJECT_DIR$/octopus-agent/octopus-agent.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-agent/octopus-agent.iml" filepath="$PROJECT_DIR$/octopus-agent/octopus-agent.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-cli/octopus-cli.iml" filepath="$PROJECT_DIR$/octopus-cli/octopus-cli.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-cli/octopus-cli.iml" filepath="$PROJECT_DIR$/octopus-cli/octopus-cli.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-cli/octopus-cli.iml" filepath="$PROJECT_DIR$/octopus-cli/octopus-cli.iml" /> <module fileurl="file://$PROJECT_DIR$/octopus-cli/octopus-cli.iml" filepath="$PROJECT_DIR$/octopus-cli/octopus-cli.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-commons/octopus-commons.iml" filepath="$PROJECT_DIR$/octopus-commons/octopus-commons.iml" /> <module fileurl="file://$PROJECT_DIR$/octopus-commons/octopus-commons.iml" filepath="$PROJECT_DIR$/octopus-commons/octopus-commons.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-commons/octopus-commons.iml" filepath="$PROJECT_DIR$/octopus-commons/octopus-commons.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-commons/octopus-commons.iml" filepath="$PROJECT_DIR$/octopus-commons/octopus-commons.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" filepath="$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" filepath="$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" /> <module fileurl="file://$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" filepath="$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" filepath="$PROJECT_DIR$/octopus-daemon/octopus-daemon.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" filepath="$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" filepath="$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" /> <module fileurl="file://$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" filepath="$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" filepath="$PROJECT_DIR$/octopus-integrationtests/octopus-integrationtests.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-server/octopus-server.iml" filepath="$PROJECT_DIR$/octopus-server/octopus-server.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-server/octopus-server.iml" filepath="$PROJECT_DIR$/octopus-server/octopus-server.iml" /> <module fileurl="file://$PROJECT_DIR$/octopus-server/octopus-server.iml" filepath="$PROJECT_DIR$/octopus-server/octopus-server.iml" />
<module fileurl="file://$PROJECT_DIR$/octopus-server/octopus-server.iml" filepath="$PROJECT_DIR$/octopus-server/octopus-server.iml" />
<module fileurl="file://$PROJECT_DIR$/parent-project.iml" filepath="$PROJECT_DIR$/parent-project.iml" />
<module fileurl="file://$PROJECT_DIR$/parent-project.iml" filepath="$PROJECT_DIR$/parent-project.iml" />
<module fileurl="file://$PROJECT_DIR$/parent-project.iml" filepath="$PROJECT_DIR$/parent-project.iml" /> <module fileurl="file://$PROJECT_DIR$/parent-project.iml" filepath="$PROJECT_DIR$/parent-project.iml" />
</modules> </modules>
</component> </component>
......
This diff is collapsed.
package com.chaos.octopus.commons.http;
import java.io.IOException;
import java.net.*;
/**
* Created by Jesper on 23-06-2016.
*/
public class SimpleServer implements Runnable{
public boolean _isRunning = true;
private ServerSocket _serverSocket;
public SimpleServer(){
try {
_serverSocket = new ServerSocket(8080);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (_isRunning) {
try(Socket socket = _serverSocket.accept()){
new HttpRequestHander(socket).invoke();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class HttpRequestHander {
private Socket socket;
public HttpRequestHander(Socket socket) {
this.socket = socket;
}
public void invoke() throws IOException {
while(socket.getInputStream().available() == 0);
String requestString = "";
while(socket.getInputStream().available() != 0){
byte[] buffer = new byte[socket.getInputStream().available()];
socket.getInputStream().read(buffer);
requestString += new String(buffer);
}
//System.out.println(requestString);
SendResponse("{\"Hello\":\"World\"}");
}
private void SendResponse(String content) throws IOException {
byte[] contentBytes = content.getBytes();
String responseString = "HTTP/1.x 200 OK\n" +
"Connection: close\n" +
"Content-Type: application/json\n" +
"Content-Length: " + contentBytes.length +"\n\n";
socket.getOutputStream().write(responseString.getBytes());
socket.getOutputStream().write(contentBytes);
}
}
}
package com.chaos.octopus.commons.core.http.test;
import com.chaos.octopus.commons.http.SimpleServer;
import org.junit.Test;
/**
* Created by Jesper on 23-06-2016.
*/
public class SimpleServerTest {
@Test
public void nothing(){
SimpleServer ss = new SimpleServer();
ss.run();
}
}
...@@ -10,6 +10,7 @@ import com.chaos.octopus.commons.core.message.ConnectMessage; ...@@ -10,6 +10,7 @@ import com.chaos.octopus.commons.core.message.ConnectMessage;
import com.chaos.octopus.commons.core.message.Message; import com.chaos.octopus.commons.core.message.Message;
import com.chaos.octopus.commons.core.message.TaskMessage; import com.chaos.octopus.commons.core.message.TaskMessage;
import com.chaos.octopus.commons.exception.ConnectException; import com.chaos.octopus.commons.exception.ConnectException;
import com.chaos.octopus.commons.http.SimpleServer;
import com.chaos.octopus.commons.util.Commands; import com.chaos.octopus.commons.util.Commands;
import com.chaos.octopus.commons.util.NetworkingUtil; import com.chaos.octopus.commons.util.NetworkingUtil;
import com.chaos.octopus.commons.util.StreamUtilities; import com.chaos.octopus.commons.util.StreamUtilities;
...@@ -89,6 +90,9 @@ public class OrchestratorImpl implements Orchestrator, Runnable { ...@@ -89,6 +90,9 @@ public class OrchestratorImpl implements Orchestrator, Runnable {
} }
public void run() { public void run() {
SimpleServer ss = new SimpleServer();
ss.run();
while (_isRunning) { while (_isRunning) {
try (Socket socket = _socket.accept()) { try (Socket socket = _socket.accept()) {
String result = StreamUtilities.ReadString(socket.getInputStream()); String result = StreamUtilities.ReadString(socket.getInputStream());
...@@ -134,6 +138,8 @@ public class OrchestratorImpl implements Orchestrator, Runnable { ...@@ -134,6 +138,8 @@ public class OrchestratorImpl implements Orchestrator, Runnable {
e.printStackTrace(); e.printStackTrace();
} }
} }
ss._isRunning = false;
} }
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment