UDP support implementation
[openflowjava.git] / simple-client / src / main / java / org / opendaylight / openflowjava / protocol / impl / clients / UdpSimpleClient.java
diff --git a/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/UdpSimpleClient.java b/simple-client/src/main/java/org/opendaylight/openflowjava/protocol/impl/clients/UdpSimpleClient.java
new file mode 100644 (file)
index 0000000..685dbd2
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.clients;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioDatagramChannel;
+import io.netty.util.concurrent.Future;
+
+import java.net.InetAddress;
+import java.util.concurrent.ExecutionException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.util.concurrent.SettableFuture;
+
+/**
+ * Simple client for testing purposes
+ *
+ * @author michal.polkorab
+ */
+public class UdpSimpleClient implements OFClient {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(UdpSimpleClient.class);
+    private final String host;
+    private final int port;
+    private EventLoopGroup group;
+    private SettableFuture<Boolean> isOnlineFuture;
+    private SettableFuture<Boolean> scenarioDone;
+    private UdpSimpleClientInitializer clientInitializer;
+    private ScenarioHandler scenarioHandler;
+    
+    /**
+     * Constructor of class
+     *
+     * @param host address of host
+     * @param port host listening port
+     */
+    public UdpSimpleClient(String host, int port) {
+        this.host = host;
+        this.port = port;
+        init();
+    }
+
+    private void init() {
+        isOnlineFuture = SettableFuture.create();
+        scenarioDone = SettableFuture.create();
+    }
+    
+    /**
+     * Starting class of {@link UdpSimpleClient}
+     */
+    @Override
+    public void run() {
+        group = new NioEventLoopGroup();
+        clientInitializer = new UdpSimpleClientInitializer(isOnlineFuture);
+        clientInitializer.setScenario(scenarioHandler);
+        try {
+            Bootstrap b = new Bootstrap();
+            b.group(group)
+                .channel(NioDatagramChannel.class)
+                .option(ChannelOption.SO_BROADCAST, false)
+                .handler(clientInitializer);
+
+            b.connect(host, port).sync();
+
+            synchronized (scenarioHandler) {
+                LOGGER.debug("WAITING FOR SCENARIO");
+                scenarioHandler.wait();
+            }
+        } catch (Exception ex) {
+            LOGGER.error(ex.getMessage(), ex);
+        } finally {
+            LOGGER.debug("shutting down");
+            try {
+                group.shutdownGracefully().get();
+                LOGGER.debug("shutdown succesful");
+            } catch (InterruptedException | ExecutionException e) {
+                LOGGER.error(e.getMessage(), e);
+            }
+        }
+        scenarioDone.set(true);
+    }
+
+    /**
+     * @return close future
+     */
+    public Future<?> disconnect() {
+        LOGGER.debug("disconnecting client");
+        return group.shutdownGracefully();
+    }
+
+    /**
+     * Sets up {@link UdpSimpleClient} and fires run()
+     *
+     * @param args
+     * @throws Exception
+     */
+    public static void main(String[] args) throws Exception {
+        String host;
+        int port;
+        UdpSimpleClient sc;
+        if (args.length != 2) {
+            LOGGER.error("Usage: " + UdpSimpleClient.class.getSimpleName()
+                    + " <host> <port>");
+            LOGGER.error("Trying to use default setting.");
+            InetAddress ia = InetAddress.getLocalHost();
+            InetAddress[] all = InetAddress.getAllByName(ia.getHostName());
+            host = all[0].getHostAddress();
+            port = 6633;
+            sc = new UdpSimpleClient(host, port);
+        } else {
+            host = args[0];
+            port = Integer.parseInt(args[1]);
+            sc = new UdpSimpleClient(host, port);
+        }
+        sc.run();
+        
+    }
+
+    @Override
+    public SettableFuture<Boolean> getIsOnlineFuture() {
+        return isOnlineFuture;
+    }
+
+    @Override
+    public SettableFuture<Boolean> getScenarioDone() {
+        return scenarioDone;
+    }
+
+    @Override
+    public void setScenarioHandler(ScenarioHandler scenario) {
+        this.scenarioHandler = scenario;
+    }
+
+    @Override
+    public void setSecuredClient(boolean securedClient) {
+        // TODO: Finish implementation when DTLS is supported
+    }
+}
\ No newline at end of file