45c1ef99b9cce2032c0eec2f10229c7484e2d55e
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / ScenarioHandler.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.clients;
3
4 import io.netty.channel.ChannelHandlerContext;
5
6 import java.util.Stack;
7 import java.util.concurrent.BlockingQueue;
8 import java.util.concurrent.LinkedBlockingQueue;
9 import java.util.concurrent.TimeUnit;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14
15 public class ScenarioHandler extends Thread {
16
17     private static final Logger LOGGER = LoggerFactory.getLogger(ScenarioHandler.class);
18     private Stack<ClientEvent> scenario;
19     private BlockingQueue<byte[]> ofMsg;
20     private ChannelHandlerContext ctx;
21
22     /**
23      * 
24      * @param scenario
25      */
26     public ScenarioHandler(Stack<ClientEvent> scenario) {
27         this.scenario = scenario;
28         ofMsg = new LinkedBlockingQueue<>();
29     }
30
31     @Override
32     public void run() {
33         int freezeCounter = 0;
34         while (!scenario.isEmpty()) {
35             ClientEvent peek = scenario.peek();
36             if (peek instanceof WaitForMessageEvent) {
37                 LOGGER.debug("WaitForMessageEvent");
38                 try {
39                     WaitForMessageEvent event = (WaitForMessageEvent) peek;
40                     event.setHeaderReceived(ofMsg.poll(2000, TimeUnit.MILLISECONDS));
41                 } catch (InterruptedException e) {
42                     LOGGER.error(e.getMessage(), e);
43                     break;
44                 }
45             } else if (peek instanceof SendEvent) {
46                 LOGGER.debug("Proceed - sendevent");
47                 SendEvent event = (SendEvent) peek;
48                 event.setCtx(ctx);
49             }
50             if (peek.eventExecuted()) {
51                 scenario.pop();
52                 freezeCounter = 0;
53             } else {
54                 freezeCounter++;
55             }
56             if (freezeCounter > 2) {
57                 LOGGER.warn("Scenario freezed: " + freezeCounter);
58                 break;
59             }
60             try {
61                 sleep(100);
62             } catch (InterruptedException e) {
63                 LOGGER.error(e.getMessage(), e);
64             }
65         }
66         LOGGER.info("Scenario finished");
67         synchronized (this) {
68             this.notify();
69         }
70     }
71
72     public boolean isEmpty() {
73         return scenario.isEmpty();
74     }
75
76     public Stack<ClientEvent> getScenario() {
77         return scenario;
78     }
79
80     public void setScenario(Stack<ClientEvent> scenario) {
81         this.scenario = scenario;
82     }
83
84     public void setCtx(ChannelHandlerContext ctx) {
85         this.ctx = ctx;
86     }
87
88     public void addOfMsg(byte[] message) {
89         ofMsg.add(message);
90     }
91 }