Search in sources :

Example 1 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveBlockingQuerier method readValueFromField.

/**
	 * 
	 * @param querierGuid
	 * @param queriedGuid
	 * @param fields a JS Array is stringified to this string
	 * @param ttl
	 * @return
	 * @throws ActiveException
	 */
private ScriptObjectMirror readValueFromField(String querierGuid, String queriedGuid, String fields, int ttl) throws ActiveException {
    ScriptObjectMirror value = null;
    try {
        ActiveMessage am = new ActiveMessage(ttl, querierGuid, fields, queriedGuid, currentID);
        channel.sendMessage(am);
        ActiveMessage response = (ActiveMessage) channel.receiveMessage();
        if (response == null) {
            throw new ActiveException();
        }
        if (response.getError() != null) {
            throw new ActiveException();
        }
        value = string2JS(response.getValue());
    } catch (IOException e) {
        throw new ActiveException();
    }
    return value;
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 2 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveBlockingRunner method main.

/**
	 * Test throughput with multithread worker with multiple script engine
	 * @param args
	 * @throws JSONException 
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException {
    int numThread = 10;
    final ActiveBlockingRunner[] runners = new ActiveBlockingRunner[numThread];
    for (int i = 0; i < numThread; i++) {
        runners[i] = new ActiveBlockingRunner(null, null);
    }
    final ThreadPoolExecutor executor = new ThreadPoolExecutor(numThread, numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    executor.prestartAllCoreThreads();
    ArrayList<Future<String>> tasks = new ArrayList<Future<String>>();
    String guid = "zhaoyu";
    String field = "gao";
    String noop_code = "";
    try {
        noop_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/noop.js")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    JSONObject value = new JSONObject();
    value.put("string", "hello world");
    ActiveMessage msg = new ActiveMessage(guid, field, noop_code, value.toString(), 0, 500);
    int n = 1000000;
    long t1 = System.currentTimeMillis();
    for (int i = 0; i < n; i++) {
        tasks.add(executor.submit(new SimpleTask(runners[0], msg)));
    }
    for (Future<String> task : tasks) {
        task.get();
    }
    long elapsed = System.currentTimeMillis() - t1;
    System.out.println("It takes " + elapsed + "ms, and the average latency for each operation is " + (elapsed * 1000.0 / n) + "us");
    System.out.println("The throughput is " + n * 1000.0 / elapsed);
    /**
		 * Test runner's protected method
		 */
    ActiveBlockingRunner runner = new ActiveBlockingRunner(null, null);
    String chain_code = null;
    try {
        chain_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/permissionTest.js")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        runner.runCode(guid, field, chain_code, value.toString(), 0, 0);
        // fail here
        assert (false) : "The code should not be here";
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.exit(0);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) JSONException(org.json.JSONException) ScriptException(javax.script.ScriptException) JSONObject(org.json.JSONObject) Future(java.util.concurrent.Future) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 3 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveBlockingWorker method runWorker.

private void runWorker() throws JSONException, IOException {
    while (!Thread.currentThread().isInterrupted()) {
        ActiveMessage msg = null;
        if ((msg = (ActiveMessage) channel.receiveMessage()) != null) {
            if (msg.type == Type.REQUEST) {
                ActiveMessage response = null;
                Future<ActiveMessage> future = executor.submit(new ActiveWorkerBlockingTask(runner, msg));
                try {
                    response = future.get(msg.getBudget(), TimeUnit.MILLISECONDS);
                } catch (InterruptedException | ExecutionException | TimeoutException e) {
                    // e.printStackTrace();
                    // construct a response with an error and cancel this task
                    future.cancel(true);
                    response = new ActiveMessage(msg.getId(), null, e.getMessage());
                }
                // send back response
                channel.sendMessage(response);
                counter.getAndIncrement();
            }
        } else {
            // The client is shutdown, let's exit this loop and return
            break;
        }
    }
}
Also used : ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveDatagramChannel method receiveMessage.

@Override
public Message receiveMessage() throws IOException {
    byte[] buf = new byte[maxPacketSize];
    DatagramPacket packet = new DatagramPacket(buf, maxPacketSize);
    socket.receive(packet);
    ActiveMessage am = null;
    try {
        am = new ActiveMessage(packet.getData());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return am;
}
Also used : DatagramPacket(java.net.DatagramPacket) JSONException(org.json.JSONException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 5 with ActiveMessage

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage in project GNS by MobilityFirst.

the class ActiveNonBlockingQuerier method writeValueIntoField.

private void writeValueIntoField(String querierGuid, String queriedGuid, String value, int ttl) throws ActiveException {
    monitor = new Monitor();
    ActiveMessage am = new ActiveMessage(ttl, querierGuid, null, queriedGuid, value, currentID);
    try {
        channel.sendMessage(am);
        synchronized (monitor) {
            while (!monitor.getDone()) {
                try {
                    monitor.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
        ActiveMessage response = monitor.getResult();
        if (response == null) {
            throw new ActiveException();
        }
        if (response.getError() != null) {
            throw new ActiveException();
        }
    } catch (IOException e) {
        throw new ActiveException();
    }
}
Also used : ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Aggregations

ActiveMessage (edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)13 IOException (java.io.IOException)9 ActiveException (edu.umass.cs.gnsserver.activecode.prototype.ActiveException)6 JSONException (org.json.JSONException)6 ExecutionException (java.util.concurrent.ExecutionException)4 JSONObject (org.json.JSONObject)3 ArrayList (java.util.ArrayList)2 Future (java.util.concurrent.Future)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 TimeoutException (java.util.concurrent.TimeoutException)2 ScriptException (javax.script.ScriptException)2 Message (edu.umass.cs.gnsserver.activecode.prototype.interfaces.Message)1 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)1 DatagramPacket (java.net.DatagramPacket)1 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1