001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.net.events;
020
021import java.net.SocketAddress;
022
023import org.jgrapes.core.Channel;
024import org.jgrapes.core.Components;
025import org.jgrapes.io.events.Opened;
026
027/**
028 * This event signals that a new connection has been made by a client.
029 */
030@SuppressWarnings("PMD.DataClass")
031public class Connected extends Opened {
032
033    private final SocketAddress localAddress;
034    private final SocketAddress remoteAddress;
035
036    /**
037     * Creates a new instance.
038     * 
039     * @param localAddress the local address
040     * @param remoteAddress the remote address
041     * (in case of a TLS connection)
042     */
043    public Connected(SocketAddress localAddress, SocketAddress remoteAddress) {
044        this.localAddress = localAddress;
045        this.remoteAddress = remoteAddress;
046    }
047
048    /**
049     * @return the localAddress
050     */
051    public SocketAddress localAddress() {
052        return localAddress;
053    }
054
055    /**
056     * @return the remoteAddress
057     */
058    public SocketAddress remoteAddress() {
059        return remoteAddress;
060    }
061
062    /*
063     * (non-Javadoc)
064     * 
065     * @see java.lang.Object#toString()
066     */
067    @Override
068    public String toString() {
069        StringBuilder builder = new StringBuilder(50);
070        builder.append(Components.objectName(this))
071            .append(" [")
072            .append(localAddress)
073            .append(" <― ")
074            .append(remoteAddress)
075            .append(", ");
076        if (channels().length > 0) {
077            builder.append("channels=");
078            builder.append(Channel.toString(channels()));
079        }
080        return builder.toString();
081    }
082}