All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coos.messaging.transport.UDPTransportManager Maven / Gradle / Ivy

The newest version!
/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.transport;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.Hashtable;
import java.util.Iterator;

import org.coos.messaging.LinkManager;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;


/**
 * @author Morten Morten Versvik, Tellu As, Tellu AS UDP transport manager
 */
public class UDPTransportManager extends DefaultChannelServer {

    private static final String PROPERTY_LISTEN_PORT = "port";
    private static final Log logger = LogFactory.getLog(UDPTransportManager.class.getName());

    private int listenPort = 15666;
    private DatagramSocket serverSocket;
    private boolean running;
    Thread thread;
    Hashtable connectionTable = new Hashtable();

    public UDPTransportManager() {
    }

    public UDPTransportManager(int listenPort, LinkManager linkManager) {
        this.listenPort = listenPort;
        setLinkManager(linkManager);
    }

    public int getListenPort() {
        return listenPort;
    }

    public void start() throws Exception {

        if (properties.get(PROPERTY_LISTEN_PORT) != null) {
            listenPort = Integer.valueOf((String) properties.get(PROPERTY_LISTEN_PORT));
        }

        serverSocket = new DatagramSocket(listenPort);
        listenPort = serverSocket.getLocalPort();
        logger.info(" Listening on port " + listenPort);
        serverSocket.setSoTimeout(500);

        running = true;

        thread = new Thread(new Runnable() {

                    public void run() {

                        try {

                            while (running) {

                                try {
                                    byte[] data = new byte[UDPTransport.MAX_UDP_SIZE];
                                    DatagramPacket dp = new DatagramPacket(data, data.length);

                                    serverSocket.receive(dp);

                                    if (!running) {
                                        serverSocket.close();

                                        return;
                                    }

                                    UDPTransport ut = connectionTable.get(dp.getSocketAddress());

                                    if (ut == null) {
                                        ut = new UDPTransport(serverSocket, dp.getAddress().getHostAddress(), dp.getPort());
                                        initializeChannel(ut);
                                        ut.start();

                                        connectionTable.put(dp.getSocketAddress(), ut);
                                    }

                                    ut.receivedMessage(dp.getData());

                                } catch (SocketTimeoutException e) {

                                    if (!running) {

                                        if ((serverSocket != null) && !serverSocket.isClosed()) {
                                            serverSocket.close();
                                        }

                                        return;
                                    }
                                } catch (SocketException e) {

                                    if (e.getMessage().equals("socket closed")) {
                                        logger.info("Server connection closing");
                                        running = false;
                                    }
                                }
                            }
                        } catch (Exception e1) {
                            logger.warn("Exception ignored", e1);
                        }
                    }
                });
        thread.start();
    }

    public void stop() throws Exception {

        if (running) {
            running = false;

            Iterator e = connectionTable.values().iterator();

            while (e.hasNext()) {
                UDPTransport ut = e.next();
                ut.stop();
            }

            connectionTable.clear();

            serverSocket.close();
            thread.interrupt();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy