/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2004 Gregor Koukkoullis ( phex <at> kouk <dot> de )
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package phex.msg;

import java.io.IOException;
import java.net.NoRouteToHostException;
import java.util.*;

import phex.common.*;
import phex.connection.NetworkManager;
import phex.download.*;
import phex.host.*;
import phex.msg.vendor.TCPConnectBackVMsg;
import phex.msg.vendor.VendorMsg;
import phex.query.*;
import phex.utils.*;

/**
 * <p>Implementates the management of Gnutella messages through this node.</p>
 *
 * <p>This class will co-ordinate message sending, forwarding and query result
 * collection. In principle, it appears to be possible to instantiate muliple
 * instances. This could potentially be used to manage multiple Gnutella-style
 * networks independently of one another. In practice, I think some of the
 * configuration parameters may be stored in global static fields, so it may be
 * more tricky.</p>
 */
final public class MsgManager implements Manager
{
    private HostManager hostMgr;
    private NetworkHostsContainer hostsContainer;
    private QueryManager queryMgr;
    
    /**
     * Responsible to monitor query results for hits that we might like to use
     * for downloads or passive searching.
     */
    private QueryResultMonitor queryResultMonitor;
    
    /**
     * The last sent query routing table, used for dynamic query
     * for a fast check for matches. 
     */
    private QueryRoutingTable lastSentQueryRoutingTable;

    /**
     * Holds ping GUID routings to Host.
     */
    private GUIDRoutingTable pingRoutingTable;

    /**
     * Holds query GUID routings to Host.
     */
    private QueryGUIDRoutingTable queryRoutingTable;

    /**
     * Holds query reply GUID routings to Host.
     */
    private GUIDRoutingTable pushRoutingTable;

    private static MsgManager instance;

    /**
     * Create a new message manager.
     */
    private MsgManager()
    {
        // holds from 2-4 minutes of ping GUIDs
        pingRoutingTable = new GUIDRoutingTable( 2 * 60 * 1000 );
        // holds from 5-10 minutes of query GUIDs
        queryRoutingTable = new QueryGUIDRoutingTable( 5 * 60 * 1000 );
        // holds from 7-14 minutes of QueryReply GUIDs for push routes.
        pushRoutingTable = new GUIDRoutingTable( 7 * 60 * 1000 );
    }

    public static MsgManager getInstance()
    {
        if ( instance == null )
        {
            instance = new MsgManager();
        }
        return instance;
    }

    /**
     * This method is called in order to initialize the manager. This method
     * includes all tasks that must be done to intialize all the several manager.
     * Like instantiating the singleton instance of the manager. Inside
     * this method you can't rely on the availability of other managers.
     * @return true is initialization was successful, false otherwise.
     */
    public boolean initialize()
    {
        return true;
    }

    /**
     * This method is called in order to perform post initialization of the
     * manager. This method includes all tasks that must be done after initializing
     * all the several managers. Inside this method you can rely on the
     * availability of other managers.
     * @return true is initialization was successful, false otherwise.
     */
    public boolean onPostInitialization()
    {
        hostMgr = HostManager.getInstance();
        hostsContainer = hostMgr.getNetworkHostsContainer();
        queryMgr = QueryManager.getInstance();
		Environment.getInstance().scheduleTimerTask( 
			new QRPUpdateTimer(), QRPUpdateTimer.TIMER_PERIOD,
			QRPUpdateTimer.TIMER_PERIOD );
        queryResultMonitor = new QueryResultMonitor();
        return true;
    }
    
    /**
     * This method is called after the complete application including GUI completed
     * its startup process. This notification must be used to activate runtime
     * processes that needs to be performed once the application has successfully
     * completed startup.
     */
    public void startupCompletedNotify()
    {
    }

    /**
     * This method is called in order to cleanly shutdown the manager. It
     * should contain all cleanup operations to ensure a nice shutdown of Phex.
     */
    public void shutdown(){}
    
    
    /**
     * @return
     */
    public QueryResultMonitor getQueryResultMonitor()
    {
        return queryResultMonitor;
    }
    
    /**
     * Returns the last sent query routing table that contains
     * my and my leafs entries.
     * @return the last sent query routing table.
     */
    public QueryRoutingTable getLastSentQueryRoutingTable()
    {
        return lastSentQueryRoutingTable;
    }

    /**
     * <p>Checks if a route for the GUID is already available. If not associates
     * the Host with the GUID.</p>
     *
     * @param clientID  the GUID to route.
     * @param sender  the Host sending information
     */
    public synchronized boolean checkAndAddToPingRoutingTable( GUID pingGUID,
        Host sender )
    {
        boolean state = pingRoutingTable.checkAndAddRouting( pingGUID, sender );
        return state;
    }

    /**
     * <p>Checks if a route for the GUID is already available. If not associates
     * the Host with the GUID.</p>
     *
     * @param clientID  the GUID to route.
     * @param sender  the Host sending information
     */
    public synchronized boolean checkAndAddToQueryRoutingTable( GUID queryGUID,
        Host sender )
    {
        boolean state = queryRoutingTable.checkAndAddRouting( queryGUID, sender );
        return state;
    }

    /**
     * <p>Associate a Host with the GUID for the servent serving a file.</p>
     *
     * @param clientID  the GUID of a servent publishing a file
     * @param sender  the Host sending information
     */
    public synchronized void addToPushRoutingTable( GUID clientID, Host sender )
    {
        pushRoutingTable.addRouting( clientID, sender );
    }

    public synchronized void removeHost( Host host )
    {
        queryMgr.removeHostQueries( host );
        pingRoutingTable.removeHost( host );
        queryRoutingTable.removeHost( host );
        pushRoutingTable.removeHost( host );
    }

    /**
     * Returns the push routing host for the given GUID or null
     * if no push routing is available or the host is not anymore
     * connected.
     */
    public synchronized Host getPushRouting( GUID clientID )
    {
        return pushRoutingTable.findRouting( clientID );
    }

    /**
     * Returns the ping routing host for the given GUID or null
     * if no push routing is available or the host is not anymore
     * connected.
     */
    public synchronized Host getPingRouting( GUID pingGUID )
    {
        return pingRoutingTable.findRouting( pingGUID );
    }

    /**
     * Returns the query routing pair with host for the given GUID or null
     * if no push routing is available or the host is not anymore
     * connected.
     * 
     * @param queryGUID the GUID of the query reply route to find.
     * @param resultCount the number of results routed together with the query reply of
     *        this query GUID.
     * @return the QueryGUIDRoutingPair that contains the host and routed result count to 
     * 		route the reply or null.
     */
    public synchronized QueryGUIDRoutingPair getQueryRouting( GUID queryGUID, int resultCount )
    {
        return queryRoutingTable.findRoutingForQuerys( queryGUID, resultCount );
    }

    /**
     * Process the query response after IP filtering is done.
     */
    public synchronized void processQueryResponse(Host remoteHost, MsgQueryResponse msg)
    {
        queryMgr.getSearchContainer().processQueryResponse( msg );
        queryMgr.getBackgroundSearchContainer().processQueryResponse( msg );
        
        // snoop download candidates and monitor passive searches.
        queryResultMonitor.processResponse( msg );
    }
    

    /**
     * <p>Called to forward a query to all connected neighbours. This is only
     * done under special conditions.<br>
     * When we are in Leaf mode we hold connections to Ultrapeers (we are there
     * leaf) and usual peers therefore we:<br>
     * - Never forward a query comming from a Ultrapper.
     * - Never forward a query to a ultrapeer.<br>
     * <br>
     * When we are in Ultrapeer mode we hold connections to other Ultrapeers,
     * Leafs and usual peers. The leaf connection usually forward us
     * there QueryRoutingTable, therefore:<br>
     * - Never forward a query that does not match a QRT entry.<br>
     * <br>
     * This strategy is used to separate the broadcast traffic of the peer
     * network from the Ultrapeer/Leaf network and is essential for a correct
     * Ultrapeer proposal support.</p>
     *
     * <p>This does not affect the TTL or hops fields of the message.</p>
     *
     * @param msg the IMsg to forward
     * @param fromHost the Host that originated this message
     */
    public void forwardQuery( MsgQuery query, Host fromHost )
    {
        boolean isShieldedLeaf = hostMgr.isShieldedLeafNode();
        // Never broadcast a message comming from a ultrapeer when in leaf mode!
        if ( isShieldedLeaf && fromHost != null
            && fromHost.isLeafUltrapeerConnection() )
        {
            return;
        }
        
        if ( fromHost.isUltrapeerLeafConnection() )
        {
            queryMgr.sendDynamicQuery( query,
                DynamicQueryConstants.DESIRED_LEAF_RESULTS );
        }
        else if ( !isShieldedLeaf )
        {
            // only forward to ultrapeers if I'm not a leaf.
            if ( query.getHeader().getTTL() > 0 )
            {
                forwardQueryToUltrapeers( query, fromHost );
            }

            // Only forward to leafs if I'm not a leaf itself.
            // Forward query to Leafs regardless of TTL
            // see section 2.4 Ultrapeers and Leaves Single Unit of
            // Gnutella Ultrapeer Query Routing v0.1
            forwardQueryToLeaves( query, fromHost );
        }

        // forward to usual peers.
        if ( query.getHeader().getTTL() > 0 )
        {
            Host[] hosts = hostsContainer.getPeerConnections();
            forwardQuery( query, fromHost, hosts );
        }
    }

    /**
     * Forward query to Leafs regardless of TTL
     * see section 2.4 Ultrapeers and Leaves Single Unit of
     * Gnutella Ultrapeer Query Routing v0.1
     * @param msg query to forward.
     * @param fromHost the host the query comes from and
     *        query is not forwarded to
     */
    public void forwardQueryToLeaves( MsgQuery msg, Host fromHost )
    {
        Host[] hosts = hostsContainer.getLeafConnections();
        forwardQuery(msg, fromHost, hosts);
    }

    /**
     * Forwards a query to the given hosts but never to the from Host.
     * @param msg the query to forward
     * @param fromHost the host the query came from.
     * @param hosts the hosts to forward to.
     */
    private void forwardQuery( MsgQuery msg, Host fromHost, Host[] hosts )
    {
        for ( int i = 0; i < hosts.length; i++ )
        {
            if ( hosts[i] == fromHost )
            {
                continue;
            }
            QueryRoutingTable qrt = hosts[i].getLastReceivedRoutingTable();
            if ( qrt != null && !qrt.containsQuery( msg ) )
            {
                continue;
            }
            hosts[i].queueMessageToSend( msg );
        }
    }

    /**
     * Forwards a query to the given hosts but never to the from Host.
     * @param msg the query to forward
     * @param fromHost the host the query came from.
     * @param hosts the hosts to forward to.
     */
    public void forwardQueryToUltrapeers( MsgQuery msg, Host fromHost )
    {
        Host[] ultrapeers = hostsContainer.getUltrapeerConnections();
        boolean lastHop = msg.getHeader().getTTL() == 1;
        for ( int i = 0; i < ultrapeers.length; i++ )
        {
            if ( ultrapeers[i] == fromHost )
            {
                continue;
            }
            // a query on last hop is forwarded to other Ultrapeers
            // with the use of a possibly available qrt.
            if ( lastHop && ultrapeers[i].isUPQueryRoutingSupported() )
            {
                QueryRoutingTable qrt = ultrapeers[i].
                    getLastReceivedRoutingTable();
                if ( qrt != null && !qrt.containsQuery( msg ) )
                {
                    continue;
                }
            }
            ultrapeers[i].queueMessageToSend( msg );
        }
    }


    /**
     * <p>Called to forward a Ping to all connected neighbours. This is only
     * done under special conditions.<br>
     * When we are in Leaf mode we hold connections to Ultrapeers (we are there
     * leaf) and usuall peers therefore we:<br>
     * - Never broadcast a message comming from a Ultrapper.
     * - Never broadcast a message to a ultrapeer.<br>
     * This strategy is used to separate the broadcast traffic of the peer
     * network from the Ultrapeer/Leaf network and is essential for a correct
     * Ultrapeer proposal support.</p>
     *
     * @param msg the MsgPing to forward
     * @param fromHost the Host that originated this message
     */
    public void forwardPing( MsgPing msg, Host fromHost )
    {
        boolean isShieldedLeaf = hostMgr.isShieldedLeafNode();
        // Never broadcast a message comming from a ultrapeer when in leaf mode!
        if ( isShieldedLeaf && fromHost != null
            && fromHost.isLeafUltrapeerConnection() )
        {
            return;
        }

        Host[] hosts;

        if ( !isShieldedLeaf )
        {   // only forward to ultrapeers if I'm not a leaf.
            hosts = hostsContainer.getUltrapeerConnections();
            forwardPing(msg, fromHost, hosts);

            // only forward to leafs if I'm not a leaf itself.
            hosts = hostsContainer.getLeafConnections();
            forwardPing(msg, fromHost, hosts);
        }

        // forward to usual peers.
        hosts = hostsContainer.getPeerConnections();
        forwardPing(msg, fromHost, hosts);
    }

    /**
     * Forwards a ping to the given hosts but never to the from Host.
     * @param msg the ping to forward
     * @param fromHost the host the ping came from.
     * @param hosts the hosts to forward to.
     */
    private void forwardPing( MsgPing msg, Host fromHost, Host[] hosts )
    {
        for ( int i = 0; i < hosts.length; i++ )
        {
            if ( hosts[i] == fromHost )
            {
                continue;
            }
            hosts[i].queueMessageToSend( msg );
        }
    }

    /**
     * Ping a host
     */
    public void pingHost( Host host )
    {
        // Send ping msg.
        MsgPing pingMsg = new MsgPing();
        pingMsg.getHeader().setTTL( (byte)1);
        checkAndAddToPingRoutingTable(
            pingMsg.getHeader().getMsgID(), Host.LOCAL_HOST );
        host.queueMessageToSend( pingMsg );
    }
    
    public void requestTCPConnectBack()
    {
        HostAddress localAddress = NetworkManager.getInstance().getLocalAddress();
        VendorMsg tcpConnectBack = new TCPConnectBackVMsg( localAddress.getPort() );
        Host[] hosts = hostsContainer.getUltrapeerConnections();
        int sentCount = 0;
        for ( int i = 0; sentCount <= 3 && i < hosts.length; i++ )
        {
            if ( hosts[i].isTCPConnectBackSupported() )
            {
                hosts[i].queueMessageToSend( tcpConnectBack );
                sentCount ++;
            }
        }
    }


    /**
     * <p>Prepares and sends a push request.</p>
     *
     * <p>This will attempt to queue a push message to send back in response to
     * a query hit that needs push to fetch the file. This is used by
     * PushRequestSleeper to help obtain a socket to download a file from.</p>
     *
     * @param sleeper  the PushRequestSleeper that is requesting the push
     *                 message be sent
     */
    public void sendPushRequest( PushRequestSleeper sleeper )
        throws IOException
    {
        GUID clientGUID = sleeper.getGUID();
        long fileIndex = sleeper.getFileIndex();
        HostAddress localAddress = NetworkManager.getInstance().getLocalAddress();
        // according to the_gdf it is all right to send a push with a private
        // local address
        // http://groups.yahoo.com/group/the_gdf/message/14305
        MsgPushRequest push = new MsgPushRequest( clientGUID, fileIndex,
            localAddress );

        // Route the PushRequest msg.
        Host returnHost = getPushRouting( clientGUID );
        Logger.logMessage( Logger.FINER, Logger.DOWNLOAD, "Push route for "
            + clientGUID + " is " + returnHost );
        // no push route it will not work...
        if (returnHost == null)
        {
            throw new NoRouteToHostException(
                "Routing table has no info to route back to sharing host." );
        }
        returnHost.queueMessageToSend( push );
    }
    
    public void updateLocalQueryRoutingTable()
    {
        lastSentQueryRoutingTable = QueryRoutingTable.createLocalQueryRoutingTable();
    }
    
    private class QRPUpdateTimer extends TimerTask
    {
        private static final long TIMER_PERIOD = 1000 * 10;

    	public void run()
    	{
            try
            {
                sendQueryRoutingTable();
            }
            catch ( Throwable th )
            {
                Logger.logError( th );
            }
    	}
    	
		/**
		 * Sends the query routing table to all network connections that haven't
		 * been updated for a while.
		 */
		private void sendQueryRoutingTable()
		{
			boolean isUltrapeer = hostMgr.isUltrapeer();
			// check if we are a shilded leaf node or a Ultrapeer.
			// Forwarding QRT is not wanted otherwise.
			if ( !( hostMgr.isShieldedLeafNode() || isUltrapeer ) )
			{
				return;
			}

			Host[] hosts = hostsContainer.getUltrapeerConnections();

			// lazy initialize if needed.
			QueryRoutingTable currentTable = null;
			QueryRoutingTable lastSentTable;
			for (int i = 0; i < hosts.length; i++)
			{
				// first check if we are a UP or leaf    supports QRP
				if ( isUltrapeer )
				{
					if ( !hosts[i].isUPQueryRoutingSupported() )
					{
						continue;
					}
				}
				else 
				{
					if ( !hosts[i].isQueryRoutingSupported() )
					{
						continue;
					}
				}
				
				if ( !hosts[i].isQRTableUpdateRequired() )
				{
					continue;
				}
				
				Logger.logMessage( Logger.FINER, Logger.NETWORK,
					"Updating QRTable for: " + hosts[i] );
				if ( currentTable == null )
				{// lazy initialize
					updateLocalQueryRoutingTable();
                    currentTable = lastSentQueryRoutingTable;
				}
				lastSentTable = hosts[i].getLastSentRoutingTable();

				Iterator msgIterator = QueryRoutingTable.buildRouteTableUpdateMsgIterator(
					currentTable, lastSentTable );
				RouteTableUpdateMsg msg;
				while ( msgIterator.hasNext() )
				{
					msg = (RouteTableUpdateMsg) msgIterator.next();
					hosts[i].queueMessageToSend( msg );
				}
				// when setting the last sent routing table the next routing
				// table update time is set.
				hosts[i].setLastSentRoutingTable( currentTable );
			}
		}
    }
}