001package org.apache.commons.jcs3.utils.discovery;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.Objects;
025
026/**
027 * This contains info about a discovered service. These objects are stored in a set in the
028 * UDPDiscoveryService.
029 */
030public class DiscoveredService
031    implements Serializable
032{
033    /** For serialization. Don't change. */
034    private static final long serialVersionUID = -7810164772089509751L;
035
036    /** region names */
037    private ArrayList<String> cacheNames;
038
039    /** service address */
040    private String serviceAddress;
041
042    /** service port */
043    private int servicePort;
044
045    /** last time we heard from this service? */
046    private long lastHearFromTime;
047
048    /**
049     * Default constructor
050     */
051    public DiscoveredService()
052    {
053        // empty
054    }
055
056    /**
057     * Constructor
058     *
059     * @param message incoming message
060     * @since 3.1
061     */
062    public DiscoveredService(UDPDiscoveryMessage message)
063    {
064        setServiceAddress( message.getHost() );
065        setCacheNames( message.getCacheNames() );
066        setServicePort( message.getPort() );
067        setLastHearFromTime( System.currentTimeMillis() );
068    }
069
070    /**
071     * @param cacheNames the cacheNames to set
072     */
073    public void setCacheNames( final ArrayList<String> cacheNames )
074    {
075        this.cacheNames = cacheNames;
076    }
077
078    /**
079     * @return the cacheNames
080     */
081    public ArrayList<String> getCacheNames()
082    {
083        return cacheNames;
084    }
085
086    /**
087     * @param serviceAddress The serviceAddress to set.
088     */
089    public void setServiceAddress( final String serviceAddress )
090    {
091        this.serviceAddress = serviceAddress;
092    }
093
094    /**
095     * @return Returns the serviceAddress.
096     */
097    public String getServiceAddress()
098    {
099        return serviceAddress;
100    }
101
102    /**
103     * @param servicePort The servicePort to set.
104     */
105    public void setServicePort( final int servicePort )
106    {
107        this.servicePort = servicePort;
108    }
109
110    /**
111     * @return Returns the servicePort.
112     */
113    public int getServicePort()
114    {
115        return servicePort;
116    }
117
118    /**
119     * @param lastHearFromTime The lastHearFromTime to set.
120     */
121    public void setLastHearFromTime( final long lastHearFromTime )
122    {
123        this.lastHearFromTime = lastHearFromTime;
124    }
125
126    /**
127     * @return Returns the lastHearFromTime.
128     */
129    public long getLastHearFromTime()
130    {
131        return lastHearFromTime;
132    }
133
134    /** @return hash code based on address/port */
135        @Override
136        public int hashCode()
137        {
138                return Objects.hash(serviceAddress, servicePort);
139        }
140
141        /**
142     * NOTE - this object is often put into sets, so equals needs to be overridden.
143     * <p>
144     * We can't use cache names as part of the equals unless we manually only use the address and
145     * port in a contains check. So that we can use normal set functionality, I've kept the cache
146     * names out.
147     * <p>
148     * @param otherArg other
149     * @return equality based on the address/port
150     */
151        @Override
152        public boolean equals(final Object otherArg)
153        {
154                if (this == otherArg)
155                {
156                        return true;
157                }
158                if (otherArg == null)
159                {
160                        return false;
161                }
162                if (!(otherArg instanceof DiscoveredService))
163                {
164                        return false;
165                }
166                final DiscoveredService other = (DiscoveredService) otherArg;
167                if (!Objects.equals(serviceAddress, other.serviceAddress))
168                {
169                        return false;
170                }
171        return servicePort == other.servicePort;
172    }
173
174    /**
175     * @return string for debugging purposes.
176     */
177    @Override
178    public String toString()
179    {
180        final StringBuilder buf = new StringBuilder();
181        buf.append( "\n DiscoveredService" );
182        buf.append( "\n CacheNames = [" + getCacheNames() + "]" );
183        buf.append( "\n ServiceAddress = [" + getServiceAddress() + "]" );
184        buf.append( "\n ServicePort = [" + getServicePort() + "]" );
185        buf.append( "\n LastHearFromTime = [" + getLastHearFromTime() + "]" );
186        return buf.toString();
187    }
188}