blob: 60b932fa0e8e653b89cc3ee200c7e9c327c53ef1 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2011, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.core.internal;
19
20import java.util.concurrent.CountDownLatch;
21import java.util.concurrent.ExecutionException;
22import java.util.concurrent.Future;
23import java.util.concurrent.TimeUnit;
24import java.util.concurrent.TimeoutException;
25
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026import net.floodlightcontroller.core.IOFSwitch;
27import net.floodlightcontroller.threadpool.IThreadPoolService;
28
Jonathan Harta99ec672014-04-03 11:30:34 -070029import org.openflow.protocol.OFMessage;
30import org.openflow.protocol.OFType;
31
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080032/**
33 * A Future object used to retrieve asynchronous OFMessage replies. Unregisters
34 * and cancels itself by default after 60 seconds. This class is meant to be
35 * sub-classed and proper behavior added to the handleReply method, and
36 * termination of the Future to be handled in the isFinished method.
37 *
38 * @author David Erickson (daviderickson@cs.stanford.edu)
39 */
40public abstract class OFMessageFuture<V> implements Future<V> {
41
42 protected IThreadPoolService threadPool;
43 protected volatile boolean canceled;
44 protected CountDownLatch latch;
45 protected OFType responseType;
46 protected volatile V result;
47 protected IOFSwitch sw;
48 protected Runnable timeoutTimer;
49 protected int transactionId;
50 protected static final long DEFAULT_TIMEOUT = 60;
51 protected static final TimeUnit DEFAULT_TIMEOUT_UNIT = TimeUnit.SECONDS;
52
53 public OFMessageFuture(IThreadPoolService tp,
54 IOFSwitch sw, OFType responseType, int transactionId) {
55 this(tp, sw, responseType, transactionId,
56 DEFAULT_TIMEOUT, DEFAULT_TIMEOUT_UNIT);
57 }
58
59 public OFMessageFuture(IThreadPoolService tp,
60 IOFSwitch sw, OFType responseType, int transactionId, long timeout, TimeUnit unit) {
61 this.threadPool = tp;
62 this.canceled = false;
63 this.latch = new CountDownLatch(1);
64 this.responseType = responseType;
65 this.sw = sw;
66 this.transactionId = transactionId;
67
68 final OFMessageFuture<V> future = this;
69 timeoutTimer = new Runnable() {
70 @Override
71 public void run() {
72 if (timeoutTimer == this)
73 future.cancel(true);
74 }
75 };
76 threadPool.getScheduledExecutor().schedule(timeoutTimer, timeout, unit);
77 }
78
79 protected void unRegister() {
80 this.timeoutTimer = null;
81 }
82
83
84 public void deliverFuture(IOFSwitch sw, OFMessage msg) {
85 if (transactionId == msg.getXid()) {
86 handleReply(sw, msg);
87 if (isFinished()) {
88 unRegister();
89 this.latch.countDown();
90 }
91 }
92 }
93
94 /**
95 * Used to handle the specific expected message this Future was reigstered
96 * for, the specified msg parameter is guaranteed to match the type and
97 * transaction id specified.
98 * @param sw
99 * @param msg
100 * @return
101 */
102 protected abstract void handleReply(IOFSwitch sw, OFMessage msg);
103
104 /**
105 * Called directly after handleReply, subclasses implement this method to
106 * indicate when the future can deregister itself from receiving future
107 * messages, and when it is safe to return the results to any waiting
108 * threads.
109 * @return when this Future has completed its work
110 */
111 protected abstract boolean isFinished();
112
113 /* (non-Javadoc)
114 * @see java.util.concurrent.Future#cancel(boolean)
115 */
116 @Override
117 public boolean cancel(boolean mayInterruptIfRunning) {
118 if (isDone()) {
119 return false;
120 } else {
121 unRegister();
122 canceled = true;
123 this.latch.countDown();
124 return !isDone();
125 }
126 }
127
128 /* (non-Javadoc)
129 * @see java.util.concurrent.Future#isCancelled()
130 */
131 @Override
132 public boolean isCancelled() {
133 return canceled;
134 }
135
136 /* (non-Javadoc)
137 * @see java.util.concurrent.Future#isDone()
138 */
139 @Override
140 public boolean isDone() {
141 return this.latch.getCount() == 0;
142 }
143
144 /* (non-Javadoc)
145 * @see java.util.concurrent.Future#get()
146 */
147 @Override
148 public V get() throws InterruptedException, ExecutionException {
149 this.latch.await();
150 return result;
151 }
152
153 /* (non-Javadoc)
154 * @see java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)
155 */
156 @Override
157 public V get(long timeout, TimeUnit unit) throws InterruptedException,
158 ExecutionException, TimeoutException {
159 this.latch.await(timeout, unit);
160 return result;
161 }
162
163 public int getTransactionId() {
164 return transactionId;
165 }
166
167 public void setTransactionId(int transactionId) {
168 this.transactionId = transactionId;
169 }
170}