001/**
002 *
003 * Copyright 2020 Florian Schmaus.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.tcp.rce;
018
019import java.net.InetAddress;
020
021import org.jivesoftware.smack.datatypes.UInt16;
022import org.jivesoftware.smack.util.Objects;
023import org.jivesoftware.smack.util.rce.SingleAddressRemoteConnectionEndpoint;
024
025import org.minidns.record.InternetAddressRR;
026
027public final class IpTcpRemoteConnectionEndpoint<IARR extends InternetAddressRR<?>>
028                implements Rfc6120TcpRemoteConnectionEndpoint, SingleAddressRemoteConnectionEndpoint {
029
030    private final CharSequence host;
031
032    private final UInt16 port;
033
034    private final IARR internetAddressResourceRecord;
035
036    public IpTcpRemoteConnectionEndpoint(CharSequence host, UInt16 port, IARR internetAddressResourceRecord) {
037        this.host = Objects.requireNonNull(host);
038        this.port = Objects.requireNonNull(port);
039        this.internetAddressResourceRecord = Objects.requireNonNull(internetAddressResourceRecord);
040    }
041
042    public static IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> from(CharSequence host, UInt16 port,
043                    InetAddress inetAddress) {
044        InternetAddressRR<?> internetAddressResourceRecord = InternetAddressRR.from(inetAddress);
045
046        return new IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>>(host, port,
047                        internetAddressResourceRecord);
048    }
049
050    @Override
051    public CharSequence getHost() {
052        return host;
053    }
054
055    @Override
056    public UInt16 getPort() {
057        return port;
058    }
059
060    @Override
061    public InetAddress getInetAddress() {
062        return internetAddressResourceRecord.getInetAddress();
063    }
064
065    @Override
066    public String getDescription() {
067        return "RFC 6120 A/AAAA Endpoint + [" + host + ":" + port + "]";
068    }
069}