io.flood.rpc.network.ConnectionManager.java Source code

Java tutorial

Introduction

Here is the source code for io.flood.rpc.network.ConnectionManager.java

Source

/*
 * Copyright 2015 The RPC Project
 *
 * The RPC Project licenses this file to you 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 io.flood.rpc.network;

import io.netty.channel.Channel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConnectionManager {

    private static final Map<Integer, Connection> connectionMap = new ConcurrentHashMap<Integer, Connection>();

    public static void put(final int id, Connection.Type type, Channel channel) {
        channel.closeFuture().addListener(new GenericFutureListener() {
            public void operationComplete(Future future) throws Exception {
                remove(id);
            }
        });
        connectionMap.put(id, new Connection(id, type, channel));
    }

    private static void remove(Integer id) {
        connectionMap.remove(id);
    }

}