Search in sources :

Example 1 with RefinementElementApiData

use of org.eclipse.winery.repository.rest.resources.apiData.RefinementElementApiData in project winery by eclipse.

the class RefinementWebSocket method onMessage.

@Override
@OnMessage
public void onMessage(String message, Session session) throws IOException {
    RefinementWebSocketApiData data = JacksonProvider.mapper.readValue(message, RefinementWebSocketApiData.class);
    switch(data.task) {
        case START:
            if (!running) {
                Thread thread = new Thread(() -> {
                    RefinementElementApiData element = new RefinementElementApiData();
                    element.serviceTemplateContainingRefinements = refinement.refineServiceTemplate(new ServiceTemplateId(data.serviceTemplate));
                    try {
                        this.sendAsync(element);
                        this.session.close();
                        this.session = null;
                    } catch (JsonProcessingException e) {
                        LOGGER.error("Error while sending refinement result", e);
                    } catch (IOException e) {
                        LOGGER.error("Error while closing the session", e);
                    }
                    running = false;
                });
                this.running = true;
                thread.start();
            }
            break;
        case REFINE_WITH:
            this.future.complete(data.refineWith);
            break;
        case STOP:
            this.future.complete(-1);
            this.sendAsync(new RefinementElementApiData(null, this.refinementServiceTemplate, null));
            this.onClose(this.session);
            break;
    }
}
Also used : IOException(java.io.IOException) ServiceTemplateId(org.eclipse.winery.model.ids.definitions.ServiceTemplateId) RefinementElementApiData(org.eclipse.winery.repository.rest.resources.apiData.RefinementElementApiData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) OnMessage(javax.websocket.OnMessage)

Example 2 with RefinementElementApiData

use of org.eclipse.winery.repository.rest.resources.apiData.RefinementElementApiData in project winery by eclipse.

the class RefinementWebSocket method chooseRefinement.

@Override
public RefinementCandidate chooseRefinement(List<RefinementCandidate> candidates, ServiceTemplateId refinementServiceTemplate, TTopologyTemplate topology) {
    this.refinementServiceTemplate = refinementServiceTemplate;
    try {
        this.future = new CompletableFuture<>();
        RefinementElementApiData element = new RefinementElementApiData(candidates, refinementServiceTemplate, topology);
        this.sendAsync(element);
        int id = future.get();
        if (id >= 0) {
            return candidates.stream().filter(candidate -> candidate.getId() == id).findFirst().orElseThrow(NotFoundException::new);
        }
    } catch (InterruptedException | ExecutionException e) {
        LOGGER.error("Error while retrieving ");
    } catch (JsonProcessingException e) {
        LOGGER.error("Error while creating JSON request", e);
    }
    return null;
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) ExecutionException(java.util.concurrent.ExecutionException) RefinementElementApiData(org.eclipse.winery.repository.rest.resources.apiData.RefinementElementApiData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 RefinementElementApiData (org.eclipse.winery.repository.rest.resources.apiData.RefinementElementApiData)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 OnMessage (javax.websocket.OnMessage)1 ServerEndpoint (javax.websocket.server.ServerEndpoint)1 NotFoundException (javax.ws.rs.NotFoundException)1 ServiceTemplateId (org.eclipse.winery.model.ids.definitions.ServiceTemplateId)1