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

com.zusmart.base.network.support.AbstractChannelContextManager Maven / Gradle / Ivy

Go to download

提供基础的工具类及方法类,Logging,Scanner,Buffer,NetWork,Future,Thread

There is a newer version: 1.0.6
Show newest version
package com.zusmart.base.network.support;

import java.nio.channels.SocketChannel;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.zusmart.base.activity.support.AbstractActivity;
import com.zusmart.base.network.ChannelContext;
import com.zusmart.base.network.ChannelContextManager;
import com.zusmart.base.network.handler.ChannelContextHandlerContext;
import com.zusmart.base.network.handler.support.DefaultChannelContextHandler;

public abstract class AbstractChannelContextManager extends AbstractActivity implements ChannelContextManager {

	private static final String HANDLER_NAME = "__CHANNEL_CONTEXT_MONITOR__";

	private final ConcurrentMap contexts = new ConcurrentHashMap();

	@Override
	public ChannelContext createChannelContext(SocketChannel socketChannel) {
		String contextCode = this.createChannelContextCode();
		ChannelContext channelContext = this.doCreateChannelContext(contextCode, socketChannel);
		channelContext.getChannelContextHandlerChain().addFirst(HANDLER_NAME, new DefaultChannelContextHandler() {
			@Override
			public void onRegister(ChannelContext channelContext, ChannelContextHandlerContext handlerContext) {
				contexts.putIfAbsent(channelContext.getContextCode(), channelContext);
				super.onRegister(channelContext, handlerContext);
			}

			@Override
			public void unRegister(ChannelContext channelContext, ChannelContextHandlerContext handlerContext) {
				super.unRegister(channelContext, handlerContext);
				contexts.remove(channelContext.getContextCode(), channelContext);
			}
		});
		return channelContext;
	}

	@Override
	public ChannelContext getChannelContext(String contextCode) {
		return this.contexts.get(contextCode);
	}

	@Override
	public Map getChannelContexts() {
		return Collections.unmodifiableMap(this.contexts);
	}

	@Override
	protected ChannelContextManager getActivity() {
		return this;
	}

	protected String createChannelContextCode() {
		return UUID.randomUUID().toString().toUpperCase();
	}

	protected abstract ChannelContext doCreateChannelContext(String contextCode, SocketChannel socketChannel);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy