Search in sources :

Example 1 with ActiveException

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

the class ActiveBlockingQuerier 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());
    }
    // 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;
        }
    }
    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)

Example 2 with ActiveException

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveException 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 3 with ActiveException

use of edu.umass.cs.gnsserver.activecode.prototype.ActiveException 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)

Example 4 with ActiveException

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

the class ActiveNonBlockingQuerier method readValueFromField.

/**
	 * 
	 * @param querierGuid
	 * @param queriedGuid
	 * @param fields a JS Array is stringified to this string
	 * @param ttl
	 * @return
	 * @throws ActiveException
	 */
private String readValueFromField(String querierGuid, String queriedGuid, String fields, int ttl) throws ActiveException {
    monitor = new Monitor();
    String value = null;
    try {
        ActiveMessage am = new ActiveMessage(ttl, querierGuid, fields, queriedGuid, currentID);
        channel.sendMessage(am);
        synchronized (monitor) {
            while (!monitor.getDone()) {
                try {
                    monitor.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
        ActiveMessage response = monitor.getResult();
        if (response == null) {
            throw new ActiveException();
        }
        if (response.getError() != null) {
            throw new ActiveException();
        }
        value = response.getValue();
    } catch (IOException e) {
        throw new ActiveException();
    }
    return value;
}
Also used : ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 5 with ActiveException

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

the class ActiveNonBlockingClient 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 valuesMap
	 * @param ttl
	 * @return executed result sent back from worker
   * @throws edu.umass.cs.gnsserver.activecode.prototype.ActiveException
	 */
@Override
public JSONObject runCode(InternalRequestHeader header, String guid, String accessor, String code, JSONObject valuesMap, int ttl, long budget) throws ActiveException {
    ActiveMessage msg = new ActiveMessage(guid, accessor, code, valuesMap.toString(), ttl, budget);
    Monitor monitor = new Monitor();
    tasks.put(msg.getId(), monitor);
    ActiveMessage response = null;
    synchronized (monitor) {
        while (!monitor.getDone()) {
            try {
                if (!monitor.getWait()) {
                    sendMessage(msg);
                    monitor.setWait();
                }
                monitor.wait();
            } catch (InterruptedException e) {
                // this thread is interrupted, do nothing
                e.printStackTrace();
            }
            response = monitor.getResult();
            /**
				 * If it's a query, queryHandler needs to handle it.
				 * Otherwise, exit the while loop to process the response.
				 */
            if (response != null && response.type != Type.RESPONSE) {
                // submit the task to the worker and wait for the response
                queryHandler.handleQueryAsync(response, header, monitor);
                try {
                    monitor.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (!monitor.getDone()) {
                    // If it's not because of the timeout event, then send back the response  
                    ActiveMessage result = monitor.getResult();
                    sendMessage(result);
                } else {
                    // If it's because of timeout, then clean up the state and exit the loop
                    break;
                }
            }
        }
    }
    response = monitor.getResult();
    ActiveCodeHandler.getLogger().log(ActiveCodeHandler.DEBUG_LEVEL, "receive a response from the worker:{0}", new Object[] { response });
    if (response == null) {
        /**
			 * No need to resend the request, as it is much likely
			 * a malicious request. 
			 */
        throw new ActiveException("Worker crashes!");
    }
    if (response.getError() != null) {
        throw new ActiveException("Message: " + msg.toString() + " Response: " + response.toString());
    }
    counter.getAndIncrement();
    tasks.remove(response.getId());
    try {
        // FIXED: it is possible that the returned value is null which causes a NullPointerException when initializing a JSONObject
        if (response.getValue() == null) {
            // the methods will use the original value. See NSFieldAccess, NSUpdateSupport
            return null;
        }
        return new JSONObject(response.getValue());
    } catch (JSONException e) {
        throw new ActiveException("Bad JSON value returned from active code!");
    }
}
Also used : JSONObject(org.json.JSONObject) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) JSONException(org.json.JSONException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

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