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

tuwien.auto.calimero.log.LogNetWriter 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, 2011 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.log;

import java.io.IOException;
import java.net.Socket;

/**
 * A LogNetWriter is used to log information over a socket network connection.
 * 

* A destination host is supplied on creation of this log writer, a {@link Socket} TCP * connection is opened and used for further logging. After {@link #close()}ing the log * writer, it cannot be connected again.
* For output on the socket the platform's default character set is used. * * @author B. Malinowsky * @see Socket */ public class LogNetWriter extends LogStreamWriter { /** * Socket connection used as logging destination. *

*/ protected Socket s; /** * Creates a log writer and opens a socket connection to destination host * and port. *

* * @param host destination host name or IP address in textual presentation; if * null or an empty string is specified, an address of the * loopback interface is used * @param port destination port, 0 <= port <= 65535 * @throws KNXLogException if IP address of host could not be determined or socket * binding / connecting failed * @see Socket */ public LogNetWriter(final String host, final int port) throws KNXLogException { try { s = new Socket(host, port); createWriter(s.getOutputStream()); } catch (final IOException e) { throw new KNXLogException("log to " + host + ":" + port + ": " + e.getMessage(), e); } } /** * Like {@link #LogNetWriter(String, int)} with the ability to set the filter log * level for information logged by LogNetWriter. *

* * @param level log level used by this LogWriter to filter log information * @param host destination host name or IP address in textual presentation; if * null or an empty string is specified, an address of the * loopback interface is used * @param port destination port, 0 <= port <= 65535 * @throws KNXLogException if IP address of host could not be determined or socket * binding / connecting failed */ public LogNetWriter(final LogLevel level, final String host, final int port) throws KNXLogException { this(host, port); setLogLevel(level); } /** * Returns the remote host IP address of this log writer, or "" if the connection was * closed. *

* * @return IP address as String * @see java.net.InetAddress#getHostAddress() */ public final String getHostAddress() { final Socket socket = s; return socket == null ? "" : socket.getInetAddress().getHostAddress(); } /** * Returns the remote host name of this log writer, or "" if the connection was * closed. *

* Note that this might involve a reverse name lookup. It is possible that only the IP * address string is returned. * * @return host name (or IP address) as String * @see java.net.InetAddress#getHostName() */ public final String getHostName() { final Socket socket = s; return socket == null ? "" : socket.getInetAddress().getHostName(); } /** * Returns the destination port of this log writer, or 0 if the connection was closed. *

* * @return port number as unsigned */ public final int getPort() { final Socket socket = s; return socket == null ? 0 : socket.getPort(); } /* (non-Javadoc) * @see tuwien.auto.calimero.log.LogStreamWriter#close() */ public synchronized void close() { if (s != null) { super.close(); try { s.close(); } catch (final IOException e) {} s = null; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy