/*
 * 
 * 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 net.jini.discovery;

import java.io.InputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.rmi.MarshalledObject;
import net.jini.core.lookup.ServiceRegistrar;

/**
 * This class encapsulates the details of unmarshaling an incoming
 * unicast response.
 *
 * @author Sun Microsystems, Inc.
 *
 * @see IncomingUnicastRequest
 */
public class IncomingUnicastResponse {
    /**
     * The registrar we have discovered.
     */
    protected ServiceRegistrar registrar;
    /**
     * The groups the lookup service was a member of, at the time
     * discovery was performed.  This may be out of date.
     */
    protected String[] groups;
    
    /**
     * Construct a new object, initialized by unmarshaling the
     * contents of an input stream.
     *
     * @param str the stream from which to unmarshal the response
     * @exception IOException an error occurred while unmarshaling the
     * response
     * @exception ClassNotFoundException some of the lookup service
     * classes could not be found or downloaded
     */
    public IncomingUnicastResponse(InputStream str)
	throws IOException, ClassNotFoundException
    {
	ObjectInputStream istr = new ObjectInputStream(str);
	registrar =
	    (ServiceRegistrar)((MarshalledObject)istr.readObject()).get();
	int grpCount = istr.readInt();
	groups = new String[grpCount];
	for (int i = 0; i < groups.length; i++) {
	    groups[i] = istr.readUTF();
	}
    }

    /**
     * Return the lookup service registrar we have discovered.
     *
     * @return the lookup service registrar we have discovered
     */
    public ServiceRegistrar getRegistrar() {
	return registrar;
    }

    /**
     * Return the set of groups of which the lookup service we
     * discovered was a member when we discovered it.  This set may be
     * out of date.
     *
     * @return the set of groups to which the lookup service
     *         was a member when we discovered it
     *
     * @see net.jini.core.lookup.ServiceRegistrar#getGroups
     */
    public String[] getGroups() {
	return groups;
    }

    public int hashCode() {
	return registrar.hashCode();
    }

    /**
     * Two responses are equal if they have the same registrar.
     */
    public boolean equals(Object o) {
	return o instanceof IncomingUnicastResponse &&
	    ((IncomingUnicastResponse) o).registrar.equals(registrar);
    }
}
