Search in sources :

Example 1 with Notification

use of it.unibo.arces.wot.sepa.commons.response.Notification in project SEPA by arces-wot.

the class SPUNaive method processInternal.

@Override
public Response processInternal(UpdateResponse update, int timeout) {
    logger.debug("* PROCESSING *" + request);
    Response ret;
    try {
        // Query the SPARQL processing service
        ret = queryProcessor.process(request, timeout);
        if (ret.getClass().equals(ErrorResponse.class)) {
            logger.error(ret);
            return ret;
        }
        // Current and previous bindings
        BindingsResults results = ((QueryResponse) ret).getBindingsResults();
        BindingsResults currentBindings = new BindingsResults(results);
        // Initialize the results with the current bindings
        BindingsResults added = new BindingsResults(results.getVariables(), null);
        BindingsResults removed = new BindingsResults(results.getVariables(), null);
        // Create empty bindings if null
        if (lastBindings == null)
            lastBindings = new BindingsResults(null, null);
        logger.debug("Current bindings: " + currentBindings);
        logger.debug("Last bindings: " + lastBindings);
        // Find removed bindings
        long start = System.nanoTime();
        for (Bindings solution : lastBindings.getBindings()) {
            if (!results.contains(solution) && !solution.isEmpty())
                removed.add(solution);
            else
                results.remove(solution);
        }
        long stop = System.nanoTime();
        logger.debug("Removed bindings: " + removed + " found in " + (stop - start) + " ns");
        // Find added bindings
        start = System.nanoTime();
        for (Bindings solution : results.getBindings()) {
            if (!lastBindings.contains(solution) && !solution.isEmpty())
                added.add(solution);
        }
        stop = System.nanoTime();
        logger.debug("Added bindings: " + added + " found in " + (stop - start) + " ns");
        // Update the last bindings with the current ones
        lastBindings = currentBindings;
        // Send notification (or end processing indication)
        if (!added.isEmpty() || !removed.isEmpty())
            ret = new Notification(getUUID(), new ARBindingsResults(added, removed), sequence++);
    } catch (Exception e) {
        ret = new ErrorResponse(500, e.getMessage());
    }
    return ret;
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) ARBindingsResults(it.unibo.arces.wot.sepa.commons.sparql.ARBindingsResults) BindingsResults(it.unibo.arces.wot.sepa.commons.sparql.BindingsResults) ARBindingsResults(it.unibo.arces.wot.sepa.commons.sparql.ARBindingsResults) QueryResponse(it.unibo.arces.wot.sepa.commons.response.QueryResponse) Bindings(it.unibo.arces.wot.sepa.commons.sparql.Bindings) Notification(it.unibo.arces.wot.sepa.commons.response.Notification) SEPAProtocolException(it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Example 2 with Notification

use of it.unibo.arces.wot.sepa.commons.response.Notification in project SEPA by arces-wot.

the class SPU method run.

@Override
public void run() {
    while (running) {
        // Poll the request from the queue
        UpdateResponse updateResponse;
        while ((updateResponse = updateQueue.poll()) != null && running) {
            // Processing update
            logger.debug("* PROCESSING *");
            // Asynchronous processing and waiting for result
            notify = processInternal(updateResponse, SPUManagerBeans.getSPUProcessingTimeout());
            // Notify event handler
            if (handler != null) {
                if (notify.isNotification())
                    try {
                        handler.notifyEvent((Notification) notify);
                    } catch (IOException e) {
                        logger.error("Failed to notify " + notify);
                    }
                else
                    logger.debug("Not a notification: " + notify);
            } else
                logger.error("Handler is null");
            // Notify SPU manager
            logger.debug("Notify SPU manager. Running: " + running);
            synchronized (queue) {
                queue.remove(this);
                logger.debug("SPUs left: " + queue.size());
                queue.notify();
            }
        }
        // Wait next request...
        if (running)
            synchronized (updateQueue) {
                try {
                    updateQueue.wait();
                } catch (InterruptedException e) {
                    return;
                }
            }
    }
}
Also used : UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) IOException(java.io.IOException) Notification(it.unibo.arces.wot.sepa.commons.response.Notification)

Example 3 with Notification

use of it.unibo.arces.wot.sepa.commons.response.Notification in project SEPA by arces-wot.

the class SEPAWebsocketClient method onMessage.

@Override
public void onMessage(String message) {
    logger.debug("@onMessage " + message);
    // Parse message
    JsonObject notify = new JsonParser().parse(message).getAsJsonObject();
    if (notify.get("ping") != null) {
        if (handler != null)
            handler.onPing();
    } else if (notify.get("subscribed") != null) {
        response = new SubscribeResponse(notify);
        setResponse();
    } else if (notify.get("unsubscribed") != null) {
        response = new UnsubscribeResponse(notify);
        setResponse();
    } else if (notify.get("results") != null) {
        if (handler != null)
            handler.onSemanticEvent(new Notification(notify));
    } else if (notify.get("code") != null) {
        if (handler != null)
            handler.onError(new ErrorResponse(notify));
    } else
        logger.error("Unknown message: " + message);
}
Also used : JsonObject(com.google.gson.JsonObject) SubscribeResponse(it.unibo.arces.wot.sepa.commons.response.SubscribeResponse) UnsubscribeResponse(it.unibo.arces.wot.sepa.commons.response.UnsubscribeResponse) Notification(it.unibo.arces.wot.sepa.commons.response.Notification) JsonParser(com.google.gson.JsonParser) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Aggregations

Notification (it.unibo.arces.wot.sepa.commons.response.Notification)3 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)2 UpdateResponse (it.unibo.arces.wot.sepa.commons.response.UpdateResponse)2 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 SEPAProtocolException (it.unibo.arces.wot.sepa.commons.exceptions.SEPAProtocolException)1 QueryResponse (it.unibo.arces.wot.sepa.commons.response.QueryResponse)1 Response (it.unibo.arces.wot.sepa.commons.response.Response)1 SubscribeResponse (it.unibo.arces.wot.sepa.commons.response.SubscribeResponse)1 UnsubscribeResponse (it.unibo.arces.wot.sepa.commons.response.UnsubscribeResponse)1 ARBindingsResults (it.unibo.arces.wot.sepa.commons.sparql.ARBindingsResults)1 Bindings (it.unibo.arces.wot.sepa.commons.sparql.Bindings)1 BindingsResults (it.unibo.arces.wot.sepa.commons.sparql.BindingsResults)1 IOException (java.io.IOException)1