001    package org.apache.jcs.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    
022    import java.io.Serializable;
023    import java.util.ArrayList;
024    
025    /**
026     * The message sent by the discovery mechanism.
027     */
028    public class UDPDiscoveryMessage
029        implements Serializable
030    {
031        /** Don't change */
032        private static final long serialVersionUID = -5332377899560951793L;
033    
034        /**
035         * This is the periodic broadcast of a servers location. This type of message is also sent in
036         * response to a REQUEST_BROADCAST.
037         */
038        public static final int PASSIVE_BROADCAST = 0;
039    
040        /**
041         * This asks recipients to broadcast their location. This is used on startup.
042         */
043        public static final int REQUEST_BROADCAST = 1;
044    
045        /**
046         * This message instructs the receiver to remove this service from its list.
047         */
048        public static final int REMOVE_BROADCAST = 2;
049    
050        /** The message type */
051        private int messageType = PASSIVE_BROADCAST;
052    
053        /** udp port */
054        private int port = 6789;
055    
056        /** UDP host */
057        private String host = "228.5.6.7";
058    
059        /** Id of the requestor, allows self-filtration */
060        private long requesterId;
061    
062        /** Names of regions */
063        private ArrayList<String> cacheNames = new ArrayList<String>();
064    
065        /**
066         * @param port The port to set.
067         */
068        public void setPort( int port )
069        {
070            this.port = port;
071        }
072    
073        /**
074         * @return Returns the port.
075         */
076        public int getPort()
077        {
078            return port;
079        }
080    
081        /**
082         * @param host The host to set.
083         */
084        public void setHost( String host )
085        {
086            this.host = host;
087        }
088    
089        /**
090         * @return Returns the host.
091         */
092        public String getHost()
093        {
094            return host;
095        }
096    
097        /**
098         * @param requesterId The requesterId to set.
099         */
100        public void setRequesterId( long requesterId )
101        {
102            this.requesterId = requesterId;
103        }
104    
105        /**
106         * @return Returns the requesterId.
107         */
108        public long getRequesterId()
109        {
110            return requesterId;
111        }
112    
113        /**
114         * @param messageType The messageType to set.
115         */
116        public void setMessageType( int messageType )
117        {
118            this.messageType = messageType;
119        }
120    
121        /**
122         * @return Returns the messageType.
123         */
124        public int getMessageType()
125        {
126            return messageType;
127        }
128    
129        /**
130         * @param cacheNames The cacheNames to set.
131         */
132        public void setCacheNames( ArrayList<String> cacheNames )
133        {
134            this.cacheNames = cacheNames;
135        }
136    
137        /**
138         * @return Returns the cacheNames.
139         */
140        public ArrayList<String> getCacheNames()
141        {
142            return cacheNames;
143        }
144    
145        /**
146         * @return debugging string
147         */
148        @Override
149        public String toString()
150        {
151            StringBuffer buf = new StringBuffer();
152            buf.append( "\n host = [" + host + "]" );
153            buf.append( "\n port = [" + port + "]" );
154            buf.append( "\n requesterId = [" + requesterId + "]" );
155            buf.append( "\n messageType = [" + messageType + "]" );
156            buf.append( "\n Cache Names" );
157            for (String name : cacheNames)
158            {
159                buf.append( " cacheName = [" + name + "]" );
160            }
161            return buf.toString();
162        }
163    }