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

tuwien.auto.calimero.link.KNXNetworkLinkIP Maven / Gradle / Ivy

There is a newer version: 2.6-rc1
Show newest version
/*
    Calimero 2 - A library for KNX network access
    Copyright (c) 2006, 2015 B. Malinowsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Linking this library statically or dynamically with other modules is
    making a combined work based on this library. Thus, the terms and
    conditions of the GNU General Public License cover the whole
    combination.

    As a special exception, the copyright holders of this library give you
    permission to link this library with independent modules to produce an
    executable, regardless of the license terms of these independent
    modules, and to copy and distribute the resulting executable under terms
    of your choice, provided that you also meet, for each linked independent
    module, the terms and conditions of the license of that module. An
    independent module is a module which is not derived from or based on
    this library. If you modify this library, you may extend this exception
    to your version of the library, but you are not obligated to do so. If
    you do not wish to do so, delete this exception statement from your
    version.
*/

package tuwien.auto.calimero.link;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

import tuwien.auto.calimero.FrameEvent;
import tuwien.auto.calimero.KNXAddress;
import tuwien.auto.calimero.Priority;
import tuwien.auto.calimero.cemi.CEMILData;
import tuwien.auto.calimero.exception.KNXException;
import tuwien.auto.calimero.exception.KNXIllegalArgumentException;
import tuwien.auto.calimero.exception.KNXIllegalStateException;
import tuwien.auto.calimero.exception.KNXTimeoutException;
import tuwien.auto.calimero.knxnetip.KNXConnectionClosedException;
import tuwien.auto.calimero.knxnetip.KNXnetIPConnection;
import tuwien.auto.calimero.knxnetip.KNXnetIPRouting;
import tuwien.auto.calimero.knxnetip.KNXnetIPTunnel;
import tuwien.auto.calimero.link.medium.KNXMediumSettings;
import tuwien.auto.calimero.log.LogLevel;

/**
 * Implementation of the KNX network link based on the KNXnet/IP protocol, using a
 * {@link KNXnetIPConnection}.
 * 

* Once a link has been closed, it is not available for further link communication, i.e. it can't be * reopened. *

* If KNXnet/IP routing is used as base protocol, the send methods with wait for confirmation behave * equally like without wait specified, since routing is an unconfirmed protocol. This implies that * no confirmation frames are generated, thus {@link NetworkLinkListener#confirmation(FrameEvent)} * is not used. *

* IP address considerations:
* On more IP addresses assigned to the local host (on possibly several local network interfaces), * the default chosen local host address can differ from the expected. In this situation, the local * endpoint has to be specified manually during instantiation.
* Network Address Translation (NAT) aware communication can only be used, if the KNXnet/IP server * of the remote endpoint supports it. Otherwise, connection timeouts will occur. With NAT enabled, * KNXnet/IP accepts IPv6 addresses. By default, the KNXnet/IP protocol only works with IPv4 * addresses.
* * @author B. Malinowsky */ public class KNXNetworkLinkIP extends AbstractLink { /** * Service mode for link layer tunneling. */ public static final int TUNNELING = 1; /** * Service mode for routing. */ public static final int ROUTING = 2; private final int mode; private final KNXnetIPConnection conn; /** * Creates a new network link based on the KNXnet/IP protocol, using a * {@link KNXnetIPConnection}. *

* For more details on KNXnet/IP connections, refer to the various KNXnet/IP implementations. * * @param serviceMode mode of communication to open, serviceMode is one of the * service mode constants (e.g. {@link #TUNNELING}); depending on the mode set, the * expected local/remote endpoints might differ * @param localEP the local endpoint of the link to use;
* - in tunneling mode (point-to-point), this is the client control endpoint, use * null for the default local host and an ephemeral port number
* - in {@link #ROUTING} mode, specifies the multicast interface, i.e., the local network * interface is taken that has the IP address bound to it (if IP address is bound more * than once, it's undefined which interface is returned), the port is not used; use * null for localEP or an unresolved IP address to take the * host's default multicast interface * @param remoteEP the remote endpoint of the link to communicate with;
* - in tunneling mode (point-to-point), this is the server control endpoint
* - in {@link #ROUTING} mode, the IP address specifies the multicast group to join, the * port is not used; use null for remoteEP or an unresolved IP * address to take the default multicast group * @param useNAT true to use network address translation in tunneling service mode, * false to use the default (non aware) mode; parameter is ignored for * routing * @param settings medium settings defining device and medium specifics needed for communication * @throws KNXException on failure establishing link using the KNXnet/IP connection * @throws InterruptedException on interrupted thread while establishing link */ public KNXNetworkLinkIP(final int serviceMode, final InetSocketAddress localEP, final InetSocketAddress remoteEP, final boolean useNAT, final KNXMediumSettings settings) throws KNXException, InterruptedException { super(null, createLinkName(remoteEP), settings); switch (serviceMode) { case TUNNELING: InetSocketAddress local = localEP; if (local == null) try { local = new InetSocketAddress(InetAddress.getLocalHost(), 0); } catch (final UnknownHostException e) { throw new KNXException("no local host available"); } conn = new KNXnetIPTunnel(KNXnetIPTunnel.LINK_LAYER, local, remoteEP, useNAT); break; case ROUTING: NetworkInterface netIf = null; if (localEP != null && !localEP.isUnresolved()) try { netIf = NetworkInterface.getByInetAddress(localEP.getAddress()); } catch (final SocketException e) { throw new KNXException("error getting network interface: " + e.getMessage()); } final InetAddress mcast = remoteEP != null ? remoteEP.getAddress() : null; conn = new KNXnetIPRouting(netIf, mcast); break; default: throw new KNXIllegalArgumentException("unknown service mode"); } cEMI = true; mode = serviceMode; conn.addConnectionListener(notifier); } /** * Creates a new network link based on the KNXnet/IP tunneling protocol, using a * {@link KNXnetIPTunnel} with default communication settings. *

* The link is established using a KNXnet/IP tunnel, the local endpoint is the default local * host, the remote endpoint uses the default KNXnet/IP port number, network address translation * (NAT) is disabled. * * @param remoteHost remote host name * @param settings medium settings defining device and medium specifics needed for communication * @throws KNXException on failure establishing link using the KNXnet/IP connection * @throws InterruptedException on interrupted thread while establishing link */ public KNXNetworkLinkIP(final String remoteHost, final KNXMediumSettings settings) throws KNXException, InterruptedException { this(TUNNELING, null, new InetSocketAddress(remoteHost, KNXnetIPConnection.DEFAULT_PORT), false, settings); } /** * Creates a new network link based on the KNXnet/IP routing protocol, using a * {@link KNXnetIPRouting}. * * @param netIf local network interface used to join the multicast group and for sending, use * null for the host's default multicast interface * @param mcGroup address of the multicast group to join, use null for the default * KNXnet/IP multicast address * @param settings medium settings defining device and medium specifics needed for communication * @throws KNXException on failure establishing link using the KNXnet/IP connection */ public KNXNetworkLinkIP(final NetworkInterface netIf, final InetAddress mcGroup, final KNXMediumSettings settings) throws KNXException { this(ROUTING, new KNXnetIPRouting(netIf, mcGroup), settings); } /** * Creates a new network link with serviceMode based on the supplied KNXnet/IP * connection. * * @param c a KNXnet/IP tunneling or routing connection in open state * @param settings medium settings defining device and medium specifics needed for communication */ protected KNXNetworkLinkIP(final int serviceMode, final KNXnetIPConnection c, final KNXMediumSettings settings) { super(null, createLinkName(c.getRemoteAddress()), settings); cEMI = true; mode = serviceMode; conn = c; conn.addConnectionListener(notifier); } /** * {@inheritDoc} When communicating with a KNX network which uses open medium, messages are * broadcasted within domain (as opposite to system broadcast) by default. Specify * dst = null for system broadcast. */ public void sendRequest(final KNXAddress dst, final Priority p, final byte[] nsdu) throws KNXLinkClosedException, KNXTimeoutException { final int mc = mode == TUNNELING ? CEMILData.MC_LDATA_REQ : CEMILData.MC_LDATA_IND; send(mc, dst, p, nsdu, false); } /** * {@inheritDoc} When communicating with a KNX network which uses open medium, messages are * broadcasted within domain (as opposite to system broadcast) by default. Specify * dst null for system broadcast. */ public void sendRequestWait(final KNXAddress dst, final Priority p, final byte[] nsdu) throws KNXTimeoutException, KNXLinkClosedException { final int mc = mode == TUNNELING ? CEMILData.MC_LDATA_REQ : CEMILData.MC_LDATA_IND; send(mc, dst, p, nsdu, true); } public String toString() { return (mode == TUNNELING ? "tunneling" : "routing") + " link " + super.toString(); } protected void onSend(final KNXAddress dst, final byte[] msg, final boolean waitForCon) { throw new KNXIllegalStateException("KNXnet/IP uses cEMI only"); } protected void onSend(final CEMILData msg, final boolean waitForCon) throws KNXTimeoutException, KNXLinkClosedException { try { if (logger.isLoggable(LogLevel.INFO)) logger.info("send message to " + msg.getDestination() + (waitForCon ? ", wait for confirmation" : "")); final boolean trace = logger.isLoggable(LogLevel.TRACE); if (trace) logger.trace("cEMI " + msg); conn.send(msg, waitForCon ? KNXnetIPConnection.WAIT_FOR_CON : KNXnetIPConnection.WAIT_FOR_ACK); if (trace) logger.trace("send to " + msg.getDestination() + " succeeded"); } catch (final KNXConnectionClosedException e) { logger.error("send error, closing link", e); close(); throw new KNXLinkClosedException("link closed, " + e.getMessage()); } } protected void onClose() { conn.close(); } private static String createLinkName(final InetSocketAddress endpt) { if (endpt == null) return KNXnetIPRouting.DEFAULT_MULTICAST; // do our own IP:port string, since InetAddress.toString() always prepends a '/' final String host = (endpt.isUnresolved() ? endpt.getHostString() : endpt.getAddress().getHostAddress()); final int p = endpt.getPort(); if (p > 0) return host + ":" + p; return host; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy