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

com.firefly.net.support.wrap.client.TcpConnection Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.support.wrap.client;

import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

import com.firefly.net.Session;
import com.firefly.utils.log.Log;
import com.firefly.utils.log.LogFactory;

abstract public class TcpConnection {
	protected static Log log = LogFactory.getInstance().getLog("firefly-system");
	
	protected Session session;
	protected long timeout;
	
	public TcpConnection(Session session) {
		this(session, 0);
	}
	
	public TcpConnection(Session session, long timeout) {
		this.session = session;
		this.timeout = timeout > 0 ? timeout : 5000L;
	}

	public Future send(Object obj) {
		final ResultCallable callable = new ResultCallable();
		final FutureTask future = new FutureTask(callable);
		
		send(obj, new MessageReceivedCallback() {

			@Override
			public void messageRecieved(TcpConnection connection, Object resultObject) {
				callable.setValue(resultObject);
				future.run();
			}
		});

		return future;
	}

	abstract public void send(Object obj, MessageReceivedCallback callback);


	public int getId() {
		return session.getSessionId();
	}

	public void close() {
		session.close();
	}
	
	public void closeNow() {
		session.closeNow();
	}

	public boolean isOpen() {
		return session.isOpen();
	}

	public Session getSession() {
		return session;
	}
}