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;
}
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);
}
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;
}
}
}
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;
}
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();
}
}
Aggregations