All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.prettypb.rpc.server.RpcServerChannelRegistry Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
/**
 *   Copyright 2010-2014 Peter Klauser
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.dongliu.prettypb.rpc.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * An RpcClientRegistry keeps an account of all connected RpcClients of an RpcServer.
 * Note: on the server side, an RpcClient's clientInfo is the server itself, and the
 * serverInfo represents the client.
 *
 * @author Peter Klauser
 */
public class RpcServerChannelRegistry {

    private static Logger logger = LoggerFactory.getLogger(RpcServerChannelRegistry.class);

    private Map clientNameMap = new ConcurrentHashMap<>();

    public RpcServerChannelRegistry() {
    }

    /**
     * A convenience method for a server to get a list of all attached RpcClients.
     * An alternative is that the server keeps its own records by using RpcConnectionEventListener functions.
     *
     * @return an unmodifiable List of connected RpcClients.
     */
    public List getAllServerChannels() {
        List result = new ArrayList<>();
        result.addAll(clientNameMap.values());
        return Collections.unmodifiableList(result);
    }

    /**
     * Attempt to register an RpcClient which has newly connected, during
     * connection handshake. If the client is already connected, return false.
     *
     * @param rpcServerChannel
     * @return true if registration is successful, false if already connected.
     */
    public boolean registerRpcServerChannel(RpcServerChannel rpcServerChannel) {
        RpcServerChannel existingClient = clientNameMap.get(rpcServerChannel.getChannelName());
        if (existingClient == null) {
            clientNameMap.put(rpcServerChannel.getChannelName(), rpcServerChannel);
            return true;
        }
        logger.debug("RpcClient {} is already registered with {}",
                rpcServerChannel.getChannelName(), existingClient.getServerInfo());
        return false;
    }

    /**
     * Remove the RpcClient from the registry, at connection close time.
     *
     * @param rpcClientCallback
     */
    public void removeRpcServerChannel(RpcServerChannel rpcClientCallback) {
        clientNameMap.remove(rpcClientCallback.getChannelName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy