net.luminis.quic.server.ApplicationProtocolRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kwik Show documentation
Show all versions of kwik Show documentation
A QUIC implementation in Java
/*
* Copyright © 2020, 2021, 2022, 2023 Peter Doornbosch
*
* This file is part of Kwik, an implementation of the QUIC protocol in Java.
*
* Kwik is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Kwik 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 Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package net.luminis.quic.server;
import net.luminis.quic.QuicConnection;
import java.util.*;
/**
* Registers supported application protocols.
*/
public class ApplicationProtocolRegistry {
Map registeredFactories = new LinkedHashMap<>();
/**
* Select the (server preferred) protocol, given the list of clientProtocols that the client supports.
* @param clientProtocols list of protocols (alpn's) the client advertises
* @return selected protocol (if any)
*/
Optional selectSupportedApplicationProtocol(List clientProtocols) {
Set intersection = new LinkedHashSet<>(registeredFactories.keySet());
intersection.retainAll(clientProtocols);
return intersection.stream().findFirst();
}
/**
* Creates an application protocol connection for the given protocol on top of the given QUIC connection.
* @param protocol protocol alpn
* @param quicConnection underlying QUIC connection
* @return application protocol instance
*/
ApplicationProtocolConnection startApplicationProtocolConnection(String protocol, QuicConnection quicConnection) {
ApplicationProtocolConnection applicationProtocolConnection = registeredFactories.get(protocol).createConnection(protocol, quicConnection);
quicConnection.setPeerInitiatedStreamCallback(applicationProtocolConnection::acceptPeerInitiatedStream);
return applicationProtocolConnection;
}
/**
* Add a protocol with lower preference than the protocols already added.
* So, to set protocols in order of preference, start with adding the most preferred, etc.
*
* @param protocol the protocol alpn
* @param factory factory for creating connections for the given protocol
*/
void registerApplicationProtocol(String protocol, ApplicationProtocolConnectionFactory factory) {
registeredFactories.put(protocol, factory);
}
/**
* Returns the list of APLN's registered.
* @return list of APLN's
*/
Set getRegisteredApplicationProtocols() {
return registeredFactories.keySet();
}
}