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