/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */

package com.sun.jini.jeri.internal.http;

import com.sun.jini.collection.SoftCache;
import com.sun.jini.thread.Executor;
import com.sun.jini.thread.GetThreadPoolAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Class for managing client-side functions shared among multiple connections
 * (e.g., tracking of unsent response acknowledgments, caching of information
 * about contacted HTTP servers).
 *
 * @author Sun Microsystems, Inc.
 * 
 */
public class HttpClientManager {
    
    private static final Executor systemThreadPool = (Executor)
	java.security.AccessController.doPrivileged(
	    new GetThreadPoolAction(false));

    private final SoftCache rolodex = new SoftCache();
    private final TimedMap unsentAcks;

    /**
     * Creates new HttpClientManager which expires unsent acknowledgments after
     * the specified timeout.
     */
    public HttpClientManager(long ackTimeout) {
	unsentAcks = new TimedMap(systemThreadPool, ackTimeout);
    }
    
    /**
     * Forgets all cached information about contacted HTTP servers.
     */
    public void clearServerInfo() {
	rolodex.clear();
    }

    /**
     * Adds to list of unsent acknowledgments for server at given host/port.
     */
    void addUnsentAcks(String host, int port, String[] cookies) {
	if (cookies == null) {
	    throw new NullPointerException();
	}
	synchronized (unsentAcks) {
	    ServerKey key = new ServerKey(host, port);
	    Set set = (Set) unsentAcks.get(key);
	    if (set == null) {
		set = new HashSet();
		unsentAcks.put(key, set);
	    }
	    for (int i = 0; i < cookies.length; i++) {
		set.add(cookies[i]);
	    }
	}
    }

    /**
     * Removes cookies from list of unsent acknowledgments for server at given
     * host/port.
     */
    void clearUnsentAcks(String host, int port, String[] cookies) {
	if (cookies == null) {
	    throw new NullPointerException();
	}
	synchronized (unsentAcks) {
	    ServerKey key = new ServerKey(host, port);
	    Set set = (Set) unsentAcks.get(key);
	    if (set == null) {
		return;
	    }
	    for (int i = 0; i < cookies.length; i++) {
		set.remove(cookies[i]);
	    }
	    if (set.isEmpty()) {
		unsentAcks.remove(key);
	    }
	}
    }

    /**
     * Returns list of unsent acknowledgments for server at given host/port.
     */
    String[] getUnsentAcks(String host, int port) {
	synchronized (unsentAcks) {
	    Set set = (Set) unsentAcks.get(new ServerKey(host, port));
	    return (set != null) ? 
		(String[]) set.toArray(new String[set.size()]) : new String[0];
	}
    }

    /**
     * Returns cached information about specified HTTP server, or ServerInfo
     * struct with default values if no entry found.
     */
    ServerInfo getServerInfo(String host, int port) {
	ServerKey key = new ServerKey(host, port);
	synchronized (rolodex) {
	    ServerInfo info = (ServerInfo) rolodex.get(key);
	    return (info != null) ? 
		(ServerInfo) info.clone() : new ServerInfo(host, port);
	}
    }

    /**
     * Caches HTTP server information, overwriting any previously registered
     * information for server if timestamp is more recent.
     */
    void cacheServerInfo(ServerInfo info) {
	if (info.timestamp == ServerInfo.NO_TIMESTAMP) {
	    return;
	}
	ServerKey key = new ServerKey(info.host, info.port);
	synchronized (rolodex) {
	    ServerInfo oldInfo = (ServerInfo) rolodex.get(key);
	    if (oldInfo == null || info.timestamp > oldInfo.timestamp) {
		rolodex.put(key, info.clone());
	    }
	}
    }
    
    /**
     * Server lookup key.
     */
    private static class ServerKey {
	
	private final String host;
	private final int port;
	private final int hash;
	
	ServerKey(String host, int port) {
	    this.host = host;
	    this.port = port;
	    hash = (host.hashCode() << 10) | (port & 0x3FF);
	}
	
	public int hashCode() {
	    return hash;
	}
	
	public boolean equals(Object obj) {
	    if (obj instanceof ServerKey) {
		ServerKey key = (ServerKey) obj;
		return host.equals(key.host) && port == key.port;
	    }
	    return false;
	}
    }
}
