use of edu.umass.cs.gnsserver.activecode.prototype.ActiveException in project GNS by MobilityFirst.
the class ActiveBlockingClient method runCode.
/**
* This runCode method sends the request to worker, and
* wait for worker to finish the request. If the worker
* crashed during the request execution, this method
* will resend the request to a new created worker, and
* the new worker will execute this request again.
* <p>If the worker fails to execute the request, it will
* send back an error to inform this method that the execution
* gets accomplished with an error. This method will raise
* an ActiveException, and the method which calls this method
* needs to handle this exception.
*
* @param guid
* @param accessor
* @param code
* @param value
* @param ttl
* @return executed result sent back from worker
* @throws edu.umass.cs.gnsserver.activecode.prototype.ActiveException
*/
@Override
public synchronized JSONObject runCode(InternalRequestHeader header, String guid, String accessor, String code, JSONObject value, int ttl, long budget) throws ActiveException {
ActiveMessage msg = new ActiveMessage(guid, accessor, code, value.toString(), ttl, budget);
sendMessage(msg);
ActiveMessage response = null;
while (true) {
try {
response = (ActiveMessage) channel.receiveMessage();
} catch (IOException e) {
// do nothing, as the worker is crashed, the response is null
}
if (response == null) {
/**
* The worker is crashed, restart the
* worker.
*/
if (!isRestarting.getAndSet(true)) {
this.shutdown();
this.initializeChannelAndStartWorker();
isRestarting.set(false);
}
break;
} else if (response.type != Type.RESPONSE) {
ActiveCodeHandler.getLogger().log(Level.FINE, "receive a query from worker:{0}", new Object[] { response });
ActiveMessage result = queryHandler.handleQuery(response, header);
sendMessage(result);
ActiveCodeHandler.getLogger().log(Level.FINE, "send a response to worker:{0} for the query: {1}", new Object[] { result, response });
} else {
assert (response.type == Type.RESPONSE) : "The message type is not RESPONSE";
break;
}
}
ActiveCodeHandler.getLogger().log(Level.FINE, "receive a response from the worker:{0}", new Object[] { response });
if (response == null) {
throw new ActiveException("Worker crashed!");
}
if (response.getError() != null) {
throw new ActiveException(msg.toString());
}
counter.getAndIncrement();
try {
return new JSONObject(response.getValue());
} catch (JSONException e) {
return value;
}
}
use of edu.umass.cs.gnsserver.activecode.prototype.ActiveException in project GNS by MobilityFirst.
the class ActiveBlockingQuerier method writeValueIntoField.
private void writeValueIntoField(String querierGuid, String targetGuid, String value, int ttl) throws ActiveException {
ActiveMessage am = new ActiveMessage(ttl, querierGuid, null, targetGuid, value, currentID);
try {
channel.sendMessage(am);
ActiveMessage response = (ActiveMessage) channel.receiveMessage();
if (response == null) {
throw new ActiveException();
}
if (response.getError() != null) {
throw new ActiveException();
}
} catch (IOException e) {
throw new ActiveException();
}
}
use of edu.umass.cs.gnsserver.activecode.prototype.ActiveException in project GNS by MobilityFirst.
the class ActiveNonBlockingQuerier method getLocations.
@Override
public ScriptObjectMirror getLocations(ScriptObjectMirror ipList) throws ActiveException {
// convert ipList to a JSONArray
JSONArray arr = null;
try {
arr = new JSONArray("[" + ipList.callMember("toString") + "]");
} catch (JSONException e) {
e.printStackTrace();
throw new ActiveException(e.getMessage());
}
//System.out.println(">>>>>>>>>>>> The query is "+arr.toString());
// resolve ip one by one
JSONObject obj = new JSONObject();
for (int i = 0; i < arr.length(); i++) {
try {
String ip = arr.getString(i);
CityResponse loc = GeoIPUtils.getLocation_City(ip, dbReader);
if (loc != null) {
JSONObject value = new JSONObject();
value.put("latitude", loc.getLocation().getLatitude());
value.put("longitude", loc.getLocation().getLongitude());
// continent of the location
value.put("continent", loc.getContinent().getCode());
obj.put(ip, value);
}
} catch (JSONException e) {
continue;
}
}
//System.out.println("The result is "+obj.toString());
return string2JS(obj.toString());
}
Aggregations