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;
022import org.jgrapes.core.Channel;
023import org.jgrapes.core.Components;
024import org.jgrapes.io.events.Opened;
025
026/**
027 * This event signals that a new connection has been established.
028 */
029@SuppressWarnings("PMD.DataClass")
030public class Connected<T> extends Opened<T> {
031
032    private final SocketAddress localAddress;
033    private final SocketAddress remoteAddress;
034
035    /**
036     * Creates a new instance.
037     * 
038     * @param localAddress the local address
039     * @param remoteAddress the remote address
040     * (in case of a TLS connection)
041     */
042    public Connected(SocketAddress localAddress, SocketAddress remoteAddress) {
043        this.localAddress = localAddress;
044        this.remoteAddress = remoteAddress;
045    }
046
047    /**
048     * @return the localAddress
049     */
050    public SocketAddress localAddress() {
051        return localAddress;
052    }
053
054    /**
055     * @return the remoteAddress
056     */
057    public SocketAddress remoteAddress() {
058        return remoteAddress;
059    }
060
061    /*
062     * (non-Javadoc)
063     * 
064     * @see java.lang.Object#toString()
065     */
066    @Override
067    public String toString() {
068        StringBuilder builder = new StringBuilder(50);
069        builder.append(Components.objectName(this))
070            .append(" [")
071            .append(localAddress)
072            .append(" <― ")
073            .append(remoteAddress)
074            .append(", ");
075        if (channels().length > 0) {
076            builder.append("channels=");
077            builder.append(Channel.toString(channels()));
078        }
079        return builder.toString();
080    }
081}