Search in sources :

Example 1 with Request

use of it.unibo.arces.wot.sepa.commons.request.Request in project SEPA by arces-wot.

the class Processor method run.

@Override
public void run() {
    while (true) {
        // WAIT NEW REQUEST
        ScheduledRequest scheduledRequest;
        try {
            scheduledRequest = queue.waitRequest();
        } catch (InterruptedException e1) {
            return;
        }
        Request request = scheduledRequest.getRequest();
        if (request.isUpdateRequest()) {
            logger.info("Update request #" + request.getToken());
            logger.debug(request);
            // Process update request
            Response ret = updateProcessor.process((UpdateRequest) request, ProcessorBeans.getUpdateTimeout());
            // // Notify update result
            // setChanged();
            // notifyObservers(ret);
            queue.addResponse(ret);
            if (ret.isUpdateResponse()) {
                // updateProcessing = true;
                spuManager.process((UpdateResponse) ret);
                try {
                    updateProcessingQueue.waitUpdateEOP();
                } catch (InterruptedException e1) {
                    return;
                }
            // while (updateProcessing) {
            // // Wait for SPUs processing end
            // synchronized (updateProcessor) {
            // try {
            // updateProcessor.wait();
            // } catch (InterruptedException e) {
            // return;
            // }
            // }
            // }
            }
        } else if (request.isQueryRequest()) {
            logger.info("Query request #" + request.getToken());
            logger.debug(request);
            Thread queryProcessing = new Thread() {

                public void run() {
                    Response ret = queryProcessor.process((QueryRequest) request, ProcessorBeans.getQueryTimeout());
                    // setChanged();
                    // notifyObservers(ret);
                    queue.addResponse(ret);
                }
            };
            queryProcessing.setName("SEPA Query Processing Thread-" + request.getToken());
            queryProcessing.start();
        } else if (request.isSubscribeRequest()) {
            logger.info("Subscribe request #" + request.getToken());
            logger.debug(request);
            Response ret = spuManager.subscribe((SubscribeRequest) request, (EventHandler) scheduledRequest.getHandler());
            // setChanged();
            // notifyObservers(ret);
            queue.addResponse(ret);
        } else if (request.isUnsubscribeRequest()) {
            logger.info("Unsubscribe request #" + request.getToken());
            logger.debug(request);
            Response ret = spuManager.unsubscribe((UnsubscribeRequest) request);
            // setChanged();
            // notifyObservers(ret);
            queue.addResponse(ret);
        }
    // 
    // UpdateRequest request;
    // while ((request = updateRequestQueue.poll()) != null) {
    // logger.debug("New request: " + request);
    // 
    // // Process update request
    // Response ret = updateProcessor.process(request,
    // ProcessorBeans.getUpdateTimeout());
    // 
    // // Notify update result
    // setChanged();
    // notifyObservers(ret);
    // 
    // if (ret.isUpdateResponse()) {
    // updateProcessing = true;
    // 
    // spuManager.process((UpdateResponse) ret);
    // 
    // while (updateProcessing) {
    // // Wait for SPUs processing end
    // synchronized (updateProcessor) {
    // try {
    // updateProcessor.wait();
    // } catch (InterruptedException e) {
    // return;
    // }
    // }
    // }
    // }
    // }
    // 
    // synchronized (updateRequestQueue) {
    // try {
    // updateRequestQueue.wait();
    // } catch (InterruptedException e) {
    // logger.error(e.getMessage());
    // return;
    // }
    // }
    }
}
Also used : Response(it.unibo.arces.wot.sepa.commons.response.Response) UpdateResponse(it.unibo.arces.wot.sepa.commons.response.UpdateResponse) ScheduledRequest(it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) UnsubscribeRequest(it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest) Request(it.unibo.arces.wot.sepa.commons.request.Request) UpdateRequest(it.unibo.arces.wot.sepa.commons.request.UpdateRequest) QueryRequest(it.unibo.arces.wot.sepa.commons.request.QueryRequest) SubscribeRequest(it.unibo.arces.wot.sepa.commons.request.SubscribeRequest) UnsubscribeRequest(it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest) ScheduledRequest(it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest)

Example 2 with Request

use of it.unibo.arces.wot.sepa.commons.request.Request in project SEPA by arces-wot.

the class SPARQL11Handler method handle.

@Override
public void handle(HttpRequest request, HttpAsyncExchange httpExchange, HttpContext context) throws HttpException, IOException {
    Instant start = Instant.now();
    // CORS
    if (!corsHandling(httpExchange)) {
        jmx.corsFailed();
        return;
    }
    // Parsing SPARQL 1.1 request and attach a token
    Request sepaRequest = parse(httpExchange);
    // Parsing failed
    if (sepaRequest == null) {
        logger.error("Parsing failed: " + httpExchange.getRequest());
        HttpUtilities.sendFailureResponse(httpExchange, HttpStatus.SC_BAD_REQUEST, "Parsing failed: " + httpExchange.getRequest());
        jmx.parsingFailed();
        return;
    }
    // Validate
    if (!validate(httpExchange.getRequest())) {
        logger.error("Validation failed SPARQL: " + sepaRequest.getSPARQL());
        HttpUtilities.sendFailureResponse(httpExchange, HttpStatus.SC_BAD_REQUEST, "Validation failed SPARQL: " + sepaRequest.getSPARQL());
        jmx.validatingFailed();
        return;
    }
    // Authorize
    if (!authorize(httpExchange.getRequest())) {
        logger.error("Authorization failed SPARQL: " + sepaRequest.getSPARQL());
        HttpUtilities.sendFailureResponse(httpExchange, HttpStatus.SC_UNAUTHORIZED, "Authorization failed SPARQL: " + sepaRequest.getSPARQL());
        jmx.authorizingFailed();
        return;
    }
    // Schedule request
    scheduler.schedule(sepaRequest, new SPARQL11ResponseHandler(httpExchange, jmx, start));
}
Also used : Instant(java.time.Instant) Request(it.unibo.arces.wot.sepa.commons.request.Request) HttpRequest(org.apache.http.HttpRequest)

Example 3 with Request

use of it.unibo.arces.wot.sepa.commons.request.Request in project SEPA by arces-wot.

the class WebsocketServer method onMessage.

@Override
public void onMessage(WebSocket conn, String message) {
    jmx.onMessage();
    logger.debug("Message from: " + conn.getRemoteSocketAddress() + " [" + message + "]");
    Request req = parseRequest(message);
    if (req == null) {
        logger.debug("Failed to parse: " + message);
        ErrorResponse response = new ErrorResponse(HttpStatus.SC_BAD_REQUEST, "Failed to parse: " + message);
        conn.send(response.toString());
        return;
    }
    scheduler.schedule(req, new WebsocketEventHandler(conn, jmx));
}
Also used : Request(it.unibo.arces.wot.sepa.commons.request.Request) SubscribeRequest(it.unibo.arces.wot.sepa.commons.request.SubscribeRequest) UnsubscribeRequest(it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest) ErrorResponse(it.unibo.arces.wot.sepa.commons.response.ErrorResponse)

Aggregations

Request (it.unibo.arces.wot.sepa.commons.request.Request)3 SubscribeRequest (it.unibo.arces.wot.sepa.commons.request.SubscribeRequest)2 UnsubscribeRequest (it.unibo.arces.wot.sepa.commons.request.UnsubscribeRequest)2 QueryRequest (it.unibo.arces.wot.sepa.commons.request.QueryRequest)1 UpdateRequest (it.unibo.arces.wot.sepa.commons.request.UpdateRequest)1 ErrorResponse (it.unibo.arces.wot.sepa.commons.response.ErrorResponse)1 Response (it.unibo.arces.wot.sepa.commons.response.Response)1 UpdateResponse (it.unibo.arces.wot.sepa.commons.response.UpdateResponse)1 ScheduledRequest (it.unibo.arces.wot.sepa.engine.scheduling.ScheduledRequest)1 Instant (java.time.Instant)1 HttpRequest (org.apache.http.HttpRequest)1