/*
 netjanswer.java - answer handling of the  VRML net java server

 Copyright (C) 2002    Peter Graf

 This file is part of VRMLNETJ - The VRML net java server.
 VRMLNETJ is free software.

    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

   For more information on the VRML net java server or Peter Graf,
   please see: http://www.mission-base.com/.

   $Log: vrmlnetj.java,v $

------------------------------------------------------------------------------
*/
//package vrmlnetj;

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

/**
 * A class containing answers received
 */
public class Netjanswer
{
    public Netjanswer( DatagramPacket packet )
    {
        // parse the answer received
        //
        vrmlnetj.packetParse( packet.getData(), packet.getLength() );

        // ignore packets that are not ANSWERS
        //
        String tag = vrmlnetj.strargs[ 0 ];
        if( !tag.equals( "AN" ))
        {
            return;
        }

        // ignore packets that do not contain a valid header
        //
        if( vrmlnetj.nstrargs < 3 )
        {
            return;
        }

        // look up the connection the answer is for
        //
        Netjconn conn;

        String connectionId = vrmlnetj.strargs[ 2 ];
        if(( conn = Netjconn.find( connectionId )) == null )
        {
            // ignore the answer by the unknown connection
            //
            return;
        }

        // remember the internetadress and port used for connection
        //
        conn.setAddress( packet.getAddress(), packet.getPort() );

        // remember the last time we received a packet from the connection
        //
        conn.setLastRecvTime();

        // tell the send packet class that we received an answer
        //
        Netjsendpacket.answer( connectionId, vrmlnetj.strargs[ 1 ] );

        return;
    }
}
