Example usage for io.netty.channel.local LocalChannel closeFuture

List of usage examples for io.netty.channel.local LocalChannel closeFuture

Introduction

In this page you can find the example usage for io.netty.channel.local LocalChannel closeFuture.

Prototype

@Override
    public ChannelFuture closeFuture() 

Source Link

Usage

From source file:org.opendaylight.usc.plugin.UscSessionManager.java

License:Open Source License

/**
 * Add a session (channel) to this session manager.
 * // w w w  .  j ava2 s . c o m
 * @param port
 * @param channel
 * @return
 */
public UscSessionImpl addSession(int port, LocalChannel channel) {
    // generate unique session ID for this agent
    // valid session ID range is 1 to 65535
    // always assign in sequential order for ease of testing
    for (int sessionId = 1; sessionId <= Character.MAX_VALUE; ++sessionId) {
        // standard idiom for double-checked locking
        if (!sessions.containsKey(sessionId)) {
            final UscSessionImpl session = createSession(sessionId, port, channel);
            if (sessions.putIfAbsent(sessionId, session) == null) {
                final int sId = sessionId;
                channel.closeFuture().addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        removeSession(sId);
                        log.trace("serverChannel for session " + sId + " closed");
                    }
                });

                plugin.sendEvent(new UscSessionCreateEvent(session));

                return session;
            }
        }
    }

    throw new RuntimeException("out of available session IDs");
}