Skip to content

Commit

Permalink
upload zip and compile from the temp unzip onserver
Browse files Browse the repository at this point in the history
  • Loading branch information
hqnghi88 committed Mar 20, 2022
1 parent b1ff8a1 commit f9f75d7
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 35 deletions.
2 changes: 1 addition & 1 deletion msi.gama.core/src/msi/gaml/operators/Files.java
Expand Up @@ -205,7 +205,7 @@ public static boolean zip_files(final IScope scope, final String zipfile, final
* @param extractFolder
* the extract folder
*/
private static void extractFolder(final IScope scope, final String zipFile, final String extractFolder) {
public static void extractFolder(final IScope scope, final String zipFile, final String extractFolder) {
try {
int BUFFER = 2048;
File file = new File(zipFile);
Expand Down
71 changes: 67 additions & 4 deletions msi.gama.headless/SimpleGUI.html
Expand Up @@ -102,6 +102,43 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
</div>

<button id="create_button">Create a new Widget</button>
<div id="controller_html">
<table>
<tr>
<td colspan=2>
Socket <input type="button" id="sk_open" value="Open" onclick="connect()"> <input type="button"
id="sk_close" disabled value="Close" onclick="disconnect()"> <input type="button" id="sk_exit"
disabled value="Exit" onclick="exit()"></td>
</tr>
<tr>
<td colspan=2>
<input type="file" id="btn_file" name="zip" disabled> <input type="button" id="btn_upload"
onclick="uploadZip()" value="Upload" disabled>
</td>
</tr>
<tr>
<td> Model:</td>
<td> <input type="text" id="gaml_path" value="Traffic\models\Simple Traffic Model.gaml">
<!-- C:\\git\\gama\\msi.gama.models\\models\\Toy Models\\Traffic\\models\\Simple Traffic Model.gaml -->

</td>
</tr>
<tr>
<td> Experiment:</td>
<td> <input type="text" id="exp_name" value="traffic"></td>
</tr>
<tr>
<td colspan=2>
<input type="button" id="exp_launch" value="Launch" onclick="launch()" disabled>
<input type="button" id="exp_play" value="Play" onclick="play()" disabled>
<input type="button" id="exp_step" value="Step" onclick="dostep()" disabled>
<input type="button" id="exp_pause" value="Pause" onclick="pause()" disabled>
<input type="button" id="exp_stop" value="Stop" onclick="stop()" disabled>
</td>
</tr>
</table>
<div id="errors"></div>
</div>
<script>

var _init = $.ui.dialog.prototype._init;
Expand Down Expand Up @@ -134,7 +171,7 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
dialog_element.open();
});
};

$(document).ready(function () {
$('#create_button').button().click(function () {
var create_dialog = $('#dialog_window_1');
Expand Down Expand Up @@ -198,10 +235,26 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
$.fn.scrollBottom = function () {
return $(this).scrollTop($(this)[0].scrollHeight);
};
var sk = new WebSocket("ws://localhost:6868/compile");
function uploadZip() {
let photo = document.getElementById("btn_file").files[0];

sk.binaryType = "arraybuffer";
sk.send(photo);
sk.onmessage = function (event) {
model_path = event.data;
console.log(model_path);
$('#errors').text("File uploaded successful!");
}

// sk.addEventListener('open', (event) => {
// });
}
initDefaultWidget();
function initDefaultWidget() {
$('#new_window_title').val("Controller");
$('#new_window_content').val('Socket <input type="button" id="sk_open" value="Open" onclick="connect()"> <input type="button" id="sk_close" disabled value="Close" onclick="disconnect()"> <input type="button" id="sk_exit" disabled value="Exit" onclick="exit()"> <table><tr><td> Model:</td><td> <input type="text" id="gaml_path" value="C:\\git\\gama\\msi.gama.models\\models\\Toy Models\\Traffic\\models\\Simple Traffic Model.gaml"></td></tr> <tr><td> Experiment:</td><td> <input type="text" id="exp_name" value="traffic"></td></tr> <tr><td colspan=2> <input type="button" id="exp_launch" value="Launch" onclick="launch()" disabled> <input type="button" id="exp_play"value="Play" onclick="play()" disabled> <input type="button" id="exp_step" value="Step" onclick="dostep()" disabled> <input type="button" id="exp_pause" value="Pause" onclick="pause()" disabled> <input type="button" id="exp_stop" value="Stop" onclick="stop()" disabled></td></tr> </table><div id="errors"></div>');
$('#new_window_content').val($('#controller_html').html());
$('#controller_html').html('');
//<input type="button" value="Load"> <input type="button" value="Compile" onclick="compile()">
create1();
$('#' + 'dialog_window_2').parent().css({ left: 8, top: 50 });
Expand Down Expand Up @@ -233,8 +286,15 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
if (!socket) {
socket = new WebSocket("ws://localhost:6868/launch");
socket.binaryType = "arraybuffer";
socket.onerror = function (err) {
socket.close();
socket = null;
$('#errors').text(err);
};
socket.addEventListener('open', (event) => {

$("#btn_file").prop("disabled", false);
$("#btn_upload").prop("disabled", false);
$("#sk_open").prop("disabled", true);
$("#sk_close").prop("disabled", false);
$("#sk_exit").prop("disabled", false);
Expand All @@ -247,6 +307,8 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
function disconnect() {
$('#errors').text('Socket closed!');
$("#sk_open").prop("disabled", false);
$("#btn_file").prop("disabled", true);
$("#btn_upload").prop("disabled", true);
$("#sk_close").prop("disabled", true);
$("#sk_exit").prop("disabled", true);
$("#exp_launch").prop("disabled", true);
Expand All @@ -270,6 +332,7 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
var exp_id = 0;
var exp_out = 0;
var exp_div_out = 0;
var model_path = "";
function compile() {
//setInterval(function(){
// socket.send(""); }, 100);
Expand All @@ -293,8 +356,8 @@ <h3>Create a new <code>Dialog</code> Widget</h3>
if (socket && socket.readyState === 1) {
socket.binaryType = "arraybuffer";
// socket.send(exp_compiled);
console.log("launch@" + $('#gaml_path').val() + "@" + $('#exp_name').val());
socket.send("launch@" + $('#gaml_path').val() + "@" + $('#exp_name').val());
console.log("launch@" + model_path + "\\" + $('#gaml_path').val() + "@" + $('#exp_name').val());
socket.send("launch@" + model_path + "\\" + $('#gaml_path').val() + "@" + $('#exp_name').val());
$("#exp_launch").prop("disabled", true);
$("#exp_play").prop("disabled", false);
$("#exp_step").prop("disabled", false);
Expand Down
5 changes: 4 additions & 1 deletion msi.gama.headless/src/msi/gama/headless/common/Globals.java
Expand Up @@ -18,10 +18,13 @@ public abstract class Globals {

/** The images path. */
public static String IMAGES_PATH;

/** The output path. */
public static String OUTPUT_PATH;

/** The output path. */
public static String TEMP_PATH;

/** The Constant OUTPUT_FILENAME. */
public final static String OUTPUT_FILENAME = "simulation-outputs";

Expand Down
Expand Up @@ -18,15 +18,10 @@

import javax.imageio.ImageIO;

import org.geotools.feature.SchemaException;
import org.java_websocket.WebSocket;

import msi.gama.headless.common.SaveHelper;
import msi.gama.headless.core.RichOutput;
import msi.gama.headless.runtime.GamaWebSocketServer;
import msi.gama.metamodel.shape.IShape;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.IList;

/**
* The Class ExperimentJob.
Expand Down
@@ -1,10 +1,20 @@
package msi.gama.headless.runtime;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.nio.ByteBuffer;

import org.java_websocket.WebSocket;

public class CompileEndPoint implements Endpoint{
import msi.gama.headless.common.Globals;
import msi.gama.headless.core.GamaHeadlessException;
import msi.gama.headless.job.ExperimentJob;
import msi.gaml.operators.Files;

public class CompileEndPoint implements Endpoint {

@Override
public void onOpen(WebSocket socket) {
Expand All @@ -13,14 +23,51 @@ public void onOpen(WebSocket socket) {

@Override
public void onMessage(GamaWebSocketServer server, WebSocket socket, String message) {
socket.send(message);

System.out.println(socket + ": String " + message);
// socket.send(message);

}

public void buildFromZip(final WebSocket socket, final ByteBuffer compiledModel)
throws IOException, GamaHeadlessException {
FileOutputStream fos = new FileOutputStream(Globals.TEMP_PATH+"/tmp"+socket.hashCode()+".zip");
fos.write(compiledModel.array());
fos.close();
System.out.println(Globals.TEMP_PATH+"/tmp"+socket);
Files.extractFolder(null,Globals.TEMP_PATH+"/tmp"+socket.hashCode()+".zip",Globals.TEMP_PATH+"/tmp"+socket.hashCode());
socket.send(Globals.TEMP_PATH+"/tmp"+socket.hashCode());
// ByteArrayInputStream bis = new ByteArrayInputStream(compiledModel.array());
// ObjectInput in = null;
// ExperimentJob selectedJob = null;
// try {
// in = new ObjectInputStream(bis);
// Object o = in.readObject();
// selectedJob = (ExperimentJob) o;
// } catch (ClassNotFoundException ex) {
// ex.printStackTrace();
// } finally {
// try {
// if (in != null) {
// in.close();
// }
// } catch (IOException ex) {
// ex.printStackTrace();
// }
// }

}

@Override
public void onMessage(GamaWebSocketServer server, WebSocket conn, ByteBuffer message) {
// TODO Auto-generated method stub

public void onMessage(GamaWebSocketServer server, WebSocket socket, ByteBuffer message) {

System.out.println(socket + ": " + message);
try {
buildFromZip(socket, message);
} catch (IOException | GamaHeadlessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
Expand Up @@ -16,7 +16,6 @@
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand All @@ -28,14 +27,17 @@
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import msi.gama.headless.common.Globals;
import msi.gama.headless.job.ExperimentJob;

/**
Expand Down Expand Up @@ -63,6 +65,32 @@ public class GamaWebSocketServer extends WebSocketServer {
public GamaWebSocketServer(int port, Application a) {
super(new InetSocketAddress(port));
app = a;
File currentJavaJarFile = new File(
GamaWebSocketServer.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String currentJavaJarFilePath = currentJavaJarFile.getAbsolutePath();

Globals.TEMP_PATH = currentJavaJarFilePath.replace(currentJavaJarFile.getName(), "") + "/temp";

File f = new File(Globals.TEMP_PATH);
deleteFolder(f);
// check if the directory can be created
// using the abstract path name
if (f.mkdir()) {
System.out.println("TEMP Directory is created");
} else {
System.out.println("TEMP Directory cannot be created");
}
}

void deleteFolder(File file) {
for (File subFile : file.listFiles()) {
if (subFile.isDirectory()) {
deleteFolder(subFile);
} else {
subFile.delete();
}
}
file.delete();
}

public Application getDefaultApp() {
Expand All @@ -75,15 +103,16 @@ public Application getDefaultApp() {
* @return single instance of GamaWebSocketServer
*/
public static GamaWebSocketServer newInstance(final int p, final Application a) {
if (instance == null) { createSocketServer(p, a); }
if (instance == null) {
createSocketServer(p, a);
}
return instance;
}

/**
* Creates the socket server.
*
* @throws UnknownHostException
* the unknown host exception
* @throws UnknownHostException the unknown host exception
*/
public static void createSocketServer(final int port, final Application a) {
instance = new GamaWebSocketServer(port, a);
Expand Down Expand Up @@ -128,7 +157,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) {

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
simulations.clear();
// simulations.clear();
broadcast(conn + " has left the room!");
System.out.println(conn + " has left the room!");
}
Expand Down

0 comments on commit f9f75d7

Please sign in to comment.