Search in sources :

Example 6 with ActiveException

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;
    }
}
Also used : JSONObject(org.json.JSONObject) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 7 with ActiveException

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();
    }
}
Also used : ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 8 with 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());
}
Also used : CityResponse(com.maxmind.geoip2.model.CityResponse) JSONObject(org.json.JSONObject) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Aggregations

ActiveException (edu.umass.cs.gnsserver.activecode.prototype.ActiveException)8 ActiveMessage (edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)6 IOException (java.io.IOException)5 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 CityResponse (com.maxmind.geoip2.model.CityResponse)2 JSONArray (org.json.JSONArray)2 ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)1