/*
 * AidaTreeServerTest.java
 *
 * Created on October 16, 2003, 10:36 PM
 */

package hep.aida.ref.remote.testRemote;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import hep.aida.dev.IDevMutableStore;

import hep.aida.ref.remote.RemoteConnectionException;
import hep.aida.ref.remote.RemoteUpdateEvent;
import hep.aida.ref.remote.interfaces.AidaTreeClient;
import hep.aida.ref.remote.interfaces.AidaTreeServant;
import hep.aida.ref.remote.interfaces.AidaTreeServer;
import hep.aida.ref.remote.interfaces.AidaUpdateEvent;

/**
 * This is a very simple AidaTreeServer that just delegates
 * most of the methods to the TestUtils class.
 * This class supports duplex mode by default.
 *
 * @author  serbo
 */
public class AidaTreeServerTest implements AidaTreeServer {
    
    private Map hash;
    private boolean supportDuplex;
    private String treeName;
    private long serverUpdateInterval;
    
    /** Creates a new instance of AidaTreeServerTest */
    public AidaTreeServerTest() {
        this(null);
    }
    
    public AidaTreeServerTest(String treeName) {
        this(treeName, true);
    }
    
    public AidaTreeServerTest(String treeName, boolean supportDuplex) {
        this(treeName, supportDuplex, -1);
    }
    
    public AidaTreeServerTest(String treeName, boolean supportDuplex, long serverUpdateInterval) {
        this.treeName = treeName;
        this.supportDuplex = supportDuplex;
        this.serverUpdateInterval = serverUpdateInterval;
        initServerTest();
    }
    
    
    // Service methods
    
    public void initServerTest() {
        if (hash == null) hash = new Hashtable();
        else hash.clear();
    }
    
    public boolean disconnect(Object client) {
        boolean ok = false;
        AidaTreeServant servant = (AidaTreeServant) hash.remove(client);
        if (servant instanceof AidaTreeServantTest) ((AidaTreeServantTest) servant).disconnect();
        ok = true;
        return ok;
    }
    
    
    // AidaTreeServer methods
    
    public AidaTreeServant connectDuplex(AidaTreeClient client) {
        if (hash.containsKey(client))
            throw new RemoteConnectionException("AidaTreeServerTest.connectDuplex This client is already connected: "+client);
        
        if (!supportDuplex)
            throw new RemoteConnectionException("AidaTreeServerTest.connectDuplex This server does not support duplex mode!");
        
        AidaTreeServant servant = new AidaTreeServantTest(client, true, serverUpdateInterval);
        hash.put(client,  servant);
        return servant;
    }
    
    public AidaTreeServant connectNonDuplex(String clientID) {
        if (hash.containsKey(clientID))
            throw new RemoteConnectionException("AidaTreeServerTest.connectNonDuplex This client is already connected: "+clientID);
                
        AidaTreeServant servant = new AidaTreeServantTest(null, false, serverUpdateInterval);
        hash.put(clientID,  servant);
        return servant;        
    }
    
    public boolean disconnectDuplex(AidaTreeClient client) {
        return disconnect(client);
    }
    
    public boolean disconnectNonDuplex(String clientID) {
        return disconnect(clientID);
    }
    
    public boolean supportDuplexMode() {
        return supportDuplex;
    }
    
    public String treeName() {
        if (treeName == null) return TestUtils.treeName();
        else return treeName;
    }
    
}
