Queue yang model
[controller.git] / opendaylight / hosttracker / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / HostTrackerCallable.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 /*
10  * Provides a mechanism for applications to do an inline Host Discovery as opposed
11  * to a delayed discovery
12  */
13 package org.opendaylight.controller.hosttracker.internal;
14
15 /**
16  * This Class provides methods to discover Host through a blocking call
17  * mechanism. Applications can make use of these methods if they don't
18  * find a host in HostTracker's database and want to discover the host
19  * in the same thread without being called by a callback function.
20  */
21 import java.net.InetAddress;
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.CountDownLatch;
24
25 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
26
27 public class HostTrackerCallable implements Callable<HostNodeConnector> {
28
29     InetAddress trackedHost;
30     HostTracker hostTracker;
31     protected CountDownLatch latch;
32
33     public HostTrackerCallable(HostTracker tracker, InetAddress inet) {
34         trackedHost = inet;
35         hostTracker = tracker;
36         latch = new CountDownLatch(1);
37     }
38
39     @Override
40     public HostNodeConnector call() throws Exception {
41         HostNodeConnector h = hostTracker.hostFind(trackedHost);
42         if (h != null)
43             return h;
44         hostTracker.setCallableOnPendingARP(trackedHost, this);
45         Thread.sleep(2000); // wait 2sec to see if the host responds
46         return hostTracker.hostQuery(trackedHost);
47     }
48
49     public void wakeup() {
50         this.latch.countDown();
51     }
52 }