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

com.zusmart.base.network.support.AbstractChannelContext 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 com.zusmart.base.future.FutureListener;
import com.zusmart.base.network.ChannelContext;
import com.zusmart.base.network.ChannelContextFuture;
import com.zusmart.base.network.handler.ChannelContextHandlerChain;
import com.zusmart.base.network.handler.support.DefaultChannelContextHandlerChain;
import com.zusmart.base.network.message.Message;
import com.zusmart.base.network.message.MessageFuture;
import com.zusmart.base.util.Assert;

public abstract class AbstractChannelContext implements ChannelContext {

	private long registTime;
	private long activeTime;

	private long readerDataTotal;
	private long readerDataCount;
	private long readerActiveTime;

	private long writerDataTotal;
	private long writerDataCount;
	private long writerActiveTime;

	private final boolean serverSide;
	private final String contextCode;
	private final ChannelContextHandlerChain handlerChain;

	private ChannelContextFuture closeFuture;

	protected AbstractChannelContext(boolean serverSide, String contextCode) {
		Assert.isBlank(contextCode, "context code must not be blank");
		this.serverSide = serverSide;
		this.contextCode = contextCode;
		this.handlerChain = this.createChannelContextHandlerChain();
	}

	@Override
	public long getRegistTime() {
		return this.registTime;
	}

	@Override
	public long getActiveTime() {
		return this.activeTime;
	}

	@Override
	public long getReaderActiveTime() {
		return this.readerActiveTime;
	}

	@Override
	public long getWriterActiveTime() {
		return this.writerActiveTime;
	}

	@Override
	public long getReaderDataTotal() {
		return this.readerDataTotal;
	}

	@Override
	public long getWriterDataTotal() {
		return this.writerDataTotal;
	}

	@Override
	public long getReaderDataCount() {
		return this.readerDataCount;
	}

	@Override
	public long getWriterDataCount() {
		return this.writerDataCount;
	}

	@Override
	public boolean isServerSide() {
		return this.serverSide == true;
	}

	@Override
	public boolean isClientSide() {
		return this.serverSide == false;
	}

	@Override
	public String getContextCode() {
		return this.contextCode;
	}

	@Override
	public ChannelContextHandlerChain getChannelContextHandlerChain() {
		return this.handlerChain;
	}

	@Override
	public synchronized ChannelContextFuture close() {
		if (null == this.closeFuture) {
			this.closeFuture = new ChannelContextFuture(this);
		}
		return this.closeFuture;
	}

	@Override
	public MessageFuture writeAndFlush(Message message) {
		final MessageFuture messageFuture = this.write(message);
		final ChannelContextFuture contextFuture = this.flush();
		final MessageFuture future = new MessageFuture(this, message);
		messageFuture.attachListener(new FutureListener() {
			@Override
			public void execute(MessageFuture f) throws Exception {
				if (!f.isSuccessed()) {
					future.setFailure(f.getFailureCause());
				}
			}
		});
		contextFuture.attachListener(new FutureListener() {
			@Override
			public void execute(ChannelContextFuture f) throws Exception {
				if (f.isSuccessed()) {
					future.setSuccess();
				} else {
					future.setFailure(f.getFailureCause());
				}
			}
		});
		return future;
	}

	@Override
	public String toString() {
		if (this.isServerSide()) {
			return String.format("[Opened:%s][ServerSide:%s] %s -> %s", this.isOpen(), true, this.getServerAddress(), this.getClientAddress());
		} else {
			return String.format("[Opened:%s][ServerSide:%s] %s -> %s", this.isOpen(), false, this.getClientAddress(), this.getServerAddress());
		}
	}

	protected boolean isClosing() {
		return null != this.closeFuture && (!this.closeFuture.isCompleted());
	}

	protected void updateOnRegist() {
		this.registTime = System.currentTimeMillis();
		this.activeTime = this.registTime;
	}

	protected void updateOnReader(long total) {
		this.activeTime = System.currentTimeMillis();
		this.readerActiveTime = this.activeTime;
		this.readerDataTotal += total;
		this.readerDataCount++;
	}

	protected void updateOnWriter(long total) {
		this.activeTime = System.currentTimeMillis();
		this.writerActiveTime = this.activeTime;
		this.writerDataTotal += total;
		this.writerDataCount++;
	}

	protected ChannelContextHandlerChain createChannelContextHandlerChain() {
		return new DefaultChannelContextHandlerChain(this);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy