org.opendaylight.ocpjava.protocol.impl.clients.ScenarioHandler Maven / Gradle / Ivy
/*
* Copyright (c) 2016 Foxconn Corporation and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.ocpjava.protocol.impl.clients;
import io.netty.channel.ChannelHandlerContext;
import java.util.Deque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Marko Lai
*
*/
public class ScenarioHandler extends Thread {
private static final Logger LOGGER = LoggerFactory.getLogger(ScenarioHandler.class);
private Deque scenario;
private BlockingQueue ocpMsg;
private ChannelHandlerContext ctx;
private int eventNumber;
private boolean scenarioFinished = false;
/**
*
* @param scenario
*/
public ScenarioHandler(Deque scenario) {
this.scenario = scenario;
ocpMsg = new LinkedBlockingQueue<>();
}
@Override
public void run() {
int freezeCounter = 0;
while (!scenario.isEmpty()) {
LOGGER.debug("Running event #{}", eventNumber);
ClientEvent peek = scenario.peekLast();
if (peek instanceof WaitForMessageEvent) {
LOGGER.debug("WaitForMessageEvent");
try {
WaitForMessageEvent event = (WaitForMessageEvent) peek;
event.setHeaderReceived(ocpMsg.poll(2000, TimeUnit.MILLISECONDS));
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
break;
}
} else if (peek instanceof SendEvent) {
LOGGER.debug("Proceed - sendevent");
SendEvent event = (SendEvent) peek;
event.setCtx(ctx);
}
if (peek.eventExecuted()) {
scenario.removeLast();
eventNumber++;
freezeCounter = 0;
} else {
freezeCounter++;
}
if (freezeCounter > 2) {
LOGGER.warn("Scenario frozen: {}", freezeCounter);
break;
}
try {
sleep(100);
} catch (InterruptedException e) {
LOGGER.error(e.getMessage(), e);
}
}
LOGGER.debug("Scenario finished");
synchronized (this) {
scenarioFinished = true;
this.notify();
}
}
/**
* @return true if scenario is done / empty
*/
public boolean isEmpty() {
return scenario.isEmpty();
}
/**
* @return scenario
*/
public Deque getScenario() {
return scenario;
}
/**
* @param scenario scenario filled with desired events
*/
public void setScenario(Deque scenario) {
this.scenario = scenario;
}
/**
* @param ctx context which will be used for sending messages (SendEvents)
*/
public void setCtx(ChannelHandlerContext ctx) {
this.ctx = ctx;
}
/**
* @param message received message that is compared to expected message
*/
public void addOcpMsg(byte[] message) {
ocpMsg.add(message);
}
/**
* @return true is scenario is finished
*/
public boolean isScenarioFinished() {
return scenarioFinished;
}
}