package org.opendaylight.controller.ping.plugin.internal;
-import java.io.IOException;
import java.net.InetAddress;
import java.util.Collections;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.opendaylight.controller.sal.common.util.Rpcs;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResult;
-import com.google.common.util.concurrent.Futures;
-
public class PingImpl implements PingService {
-
- private EchoResult pingHost(InetAddress destination) throws IOException {
- if (destination.isReachable(5000)) {
- return EchoResult.Reachable;
- } else {
- return EchoResult.Unreachable;
- }
+ private final ExecutorService pool = Executors.newFixedThreadPool(2);
+
+ private Future<RpcResult<SendEchoOutput>> startPingHost(final SendEchoInput destination) {
+ return pool.submit(new Callable<RpcResult<SendEchoOutput>>() {
+ @Override
+ public RpcResult<SendEchoOutput> call() throws Exception {
+ SendEchoOutputBuilder ob = new SendEchoOutputBuilder();
+ try {
+ InetAddress dst = InetAddress.getByName(destination.getDestination().getValue());
+ /* Build the result and return it. */
+ ob.setEchoResult(dst.isReachable(5000) ? EchoResult.Reachable : EchoResult.Unreachable);
+ } catch (Exception e) {
+ /* Return error result. */
+ ob.setEchoResult(EchoResult.Error);
+ }
+ return Rpcs.<SendEchoOutput>getRpcResult(true, ob.build(), Collections.<RpcError>emptySet());
+ }
+ });
}
@Override
public Future<RpcResult<SendEchoOutput>> sendEcho(SendEchoInput destination) {
- try {
- InetAddress dst = InetAddress.getByName(destination
- .getDestination().getValue());
- EchoResult result = this.pingHost(dst);
-
- /* Build the result and return it. */
- SendEchoOutputBuilder ob = new SendEchoOutputBuilder();
- ob.setEchoResult(result);
- RpcResult<SendEchoOutput> rpcResult =
- Rpcs.<SendEchoOutput> getRpcResult(true, ob.build(),
- Collections.<RpcError> emptySet());
-
- return Futures.immediateFuture(rpcResult);
- } catch (Exception e) {
-
- /* Return error result. */
- SendEchoOutputBuilder ob = new SendEchoOutputBuilder();
- ob.setEchoResult(EchoResult.Error);
- RpcResult<SendEchoOutput> rpcResult =
- Rpcs.<SendEchoOutput> getRpcResult(true, ob.build(),
- Collections.<RpcError> emptySet());
- return Futures.immediateFuture(rpcResult);
- }
+ return this.startPingHost(destination);
}
}