/*
* $Id$
*/
package edu.jas.util;
import java.io.IOException;
import java.util.Iterator;
//import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.log4j.Logger;
//import edu.unima.ky.parallel.ChannelFactory;
//import edu.unima.ky.parallel.SocketChannel;
/**
* Distributed version of a List.
* Implemented with a SortedMap / TreeMap to keep the sequence order of elements.
* @author Heinz Kredel
*/
public class DistributedList /* implements List not jet */ {
private static final Logger logger = Logger.getLogger(DistributedList.class);
protected final SortedMap theList;
protected final ChannelFactory cf;
protected SocketChannel channel = null;
protected Listener listener = null;
/**
* Constructor for DistributedList.
* @param host name or IP of server host.
*/
public DistributedList(String host) {
this(host,DistributedListServer.DEFAULT_PORT);
}
/**
* Constructor for DistributedList.
* @param host name or IP of server host.
* @param port of server.
*/
public DistributedList(String host,int port) {
this(new ChannelFactory(port+1),host,port);
}
/**
* Constructor for DistributedList.
* @param cf ChannelFactory to use.
* @param host name or IP of server host.
* @param port of server.
*/
public DistributedList(ChannelFactory cf,String host,int port) {
this.cf = cf;
cf.init();
try {
channel = cf.getChannel(host,port);
} catch (IOException e) {
e.printStackTrace();
}
logger.debug("dl channel = " + channel);
theList = new TreeMap();
}
/**
* Constructor for DistributedList.
* @param sc SocketChannel to use.
*/
public DistributedList(SocketChannel sc) {
cf = null;
channel = sc;
theList = new TreeMap();
}
/**
* List thread initialization and start.
*/
public void init() {
listener = new Listener(channel,theList);
listener.start();
}
/**
* Terminate the list thread.
*/
public void terminate() {
if ( cf != null ) {
cf.terminate();
logger.warn("terminating " + cf);
}
if ( channel != null ) {
channel.close();
}
//theList.clear();
if ( listener == null ) {
return;
}
logger.debug("terminate " + listener);
listener.setDone();
try {
while ( listener.isAlive() ) {
listener.interrupt();
listener.join(100);
}
} catch (InterruptedException u) {
Thread.currentThread().interrupt();
}
listener = null;
}
/**
* Get the internal list, convert from Collection.
*/
public List
Ads help maintain this website.