001package org.apache.commons.jcs3.auxiliary.lateral.socket.tcp;
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 org.apache.commons.jcs3.auxiliary.lateral.LateralCacheAttributes;
023import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
024
025/**
026 * This interface defines functions that are particular to the TCP Lateral Cache plugin. It extends
027 * the generic LateralCacheAttributes interface which in turn extends the AuxiliaryCache interface.
028 */
029public class TCPLateralCacheAttributes
030    extends LateralCacheAttributes
031    implements ITCPLateralCacheAttributes
032{
033    /** Don't change. */
034    private static final long serialVersionUID = 1077889204513905220L;
035
036    /** default */
037    private static final String DEFAULT_UDP_DISCOVERY_ADDRESS = "228.5.6.7";
038
039    /** default */
040    private static final int DEFAULT_UDP_DISCOVERY_PORT = 6789;
041
042    /** default */
043    private static final boolean DEFAULT_UDP_DISCOVERY_ENABLED = true;
044
045    /** default */
046    private static final boolean DEFAULT_ALLOW_GET = true;
047
048    /** default */
049    private static final boolean DEFAULT_ALLOW_PUT = true;
050
051    /** default */
052    private static final boolean DEFAULT_ISSUE_REMOVE_FOR_PUT = false;
053
054    /** default */
055    private static final boolean DEFAULT_FILTER_REMOVE_BY_HASH_CODE = true;
056
057    /** default - Only block for 1 second before timing out on a read.*/
058    private static final int DEFAULT_SOCKET_TIME_OUT = 1000;
059
060    /** default - Only block for 2 seconds before timing out on startup.*/
061    private static final int DEFAULT_OPEN_TIMEOUT = 2000;
062
063    /** TCP -------------------------------------------- */
064    private String tcpServers = "";
065
066    /** used to identify the service that this manager will be operating on */
067    private String tcpServer = "";
068
069    /** The port */
070    private int tcpListenerPort;
071
072    /** The host */
073    private String tcpListenerHost = "";
074
075    /** udp discovery for tcp server */
076    private String udpDiscoveryAddr = DEFAULT_UDP_DISCOVERY_ADDRESS;
077
078    /** discovery port */
079    private int udpDiscoveryPort = DEFAULT_UDP_DISCOVERY_PORT;
080
081    /** discovery switch */
082    private boolean udpDiscoveryEnabled = DEFAULT_UDP_DISCOVERY_ENABLED;
083
084    /** udp datagram TTL */
085    private int udpTTL = 0;
086
087    /** can we put */
088    private boolean allowPut = DEFAULT_ALLOW_GET;
089
090    /** can we go laterally for a get */
091    private boolean allowGet = DEFAULT_ALLOW_PUT;
092
093    /** call remove when there is a put */
094    private boolean issueRemoveOnPut = DEFAULT_ISSUE_REMOVE_FOR_PUT;
095
096    /** don't remove it the hash code is the same */
097    private boolean filterRemoveByHashCode = DEFAULT_FILTER_REMOVE_BY_HASH_CODE;
098
099    /** Only block for socketTimeOut seconds before timing out on a read.  */
100    private int socketTimeOut = DEFAULT_SOCKET_TIME_OUT;
101
102    /** Only block for openTimeOut seconds before timing out on startup. */
103    private int openTimeOut = DEFAULT_OPEN_TIMEOUT;
104
105    /**
106     * Sets the tcpServer attribute of the ILateralCacheAttributes object
107     * <p>
108     * @param val The new tcpServer value
109     */
110    @Override
111    public void setTcpServer( final String val )
112    {
113        this.tcpServer = val;
114    }
115
116    /**
117     * Gets the tcpServer attribute of the ILateralCacheAttributes object
118     * <p>
119     * @return The tcpServer value
120     */
121    @Override
122    public String getTcpServer()
123    {
124        return this.tcpServer;
125    }
126
127    /**
128     * Sets the tcpServers attribute of the ILateralCacheAttributes object
129     * <p>
130     * @param val The new tcpServers value
131     */
132    @Override
133    public void setTcpServers( final String val )
134    {
135        this.tcpServers = val;
136    }
137
138    /**
139     * Gets the tcpServers attribute of the ILateralCacheAttributes object
140     * <p>
141     * @return The tcpServers value
142     */
143    @Override
144    public String getTcpServers()
145    {
146        return this.tcpServers;
147    }
148
149    /**
150     * Sets the tcpListenerPort attribute of the ILateralCacheAttributes object
151     * <p>
152     * @param val The new tcpListenerPort value
153     */
154    @Override
155    public void setTcpListenerPort( final int val )
156    {
157        this.tcpListenerPort = val;
158    }
159
160    /**
161     * Gets the tcpListenerPort attribute of the ILateralCacheAttributes object
162     * <p>
163     * @return The tcpListenerPort value
164     */
165    @Override
166    public int getTcpListenerPort()
167    {
168        return this.tcpListenerPort;
169    }
170
171    /**
172     * Sets the tcpListenerHost attribute of the ILateralCacheAttributes object
173     * <p>
174     * @param val
175     *            The new tcpListenerHost value
176     */
177    @Override
178    public void setTcpListenerHost( final String val )
179    {
180        this.tcpListenerHost = val;
181    }
182
183    /**
184     * Gets the tcpListenerHost attribute of the ILateralCacheAttributes object
185     * <p>
186     * @return The tcpListenerHost value
187     */
188    @Override
189    public String getTcpListenerHost()
190    {
191        return this.tcpListenerHost;
192    }
193
194    /**
195     * Can setup UDP Discovery. This only works for TCp laterals right now. It allows TCP laterals
196     * to find each other by broadcasting to a multicast port.
197     * <p>
198     * @param udpDiscoveryEnabled The udpDiscoveryEnabled to set.
199     */
200    @Override
201    public void setUdpDiscoveryEnabled( final boolean udpDiscoveryEnabled )
202    {
203        this.udpDiscoveryEnabled = udpDiscoveryEnabled;
204    }
205
206    /**
207     * Whether or not TCP laterals can try to find each other by multicast communication.
208     * <p>
209     * @return Returns the udpDiscoveryEnabled.
210     */
211    @Override
212    public boolean isUdpDiscoveryEnabled()
213    {
214        return this.udpDiscoveryEnabled;
215    }
216
217    /**
218     * The port to use if UDPDiscovery is enabled.
219     * <p>
220     * @return Returns the udpDiscoveryPort.
221     */
222    @Override
223    public int getUdpDiscoveryPort()
224    {
225        return this.udpDiscoveryPort;
226    }
227
228    /**
229     * Sets the port to use if UDPDiscovery is enabled.
230     * <p>
231     * @param udpDiscoveryPort The udpDiscoveryPort to set.
232     */
233    @Override
234    public void setUdpDiscoveryPort( final int udpDiscoveryPort )
235    {
236        this.udpDiscoveryPort = udpDiscoveryPort;
237    }
238
239    /**
240     * The address to broadcast to if UDPDiscovery is enabled.
241     * <p>
242     * @return Returns the udpDiscoveryAddr.
243     */
244    @Override
245    public String getUdpDiscoveryAddr()
246    {
247        return this.udpDiscoveryAddr;
248    }
249
250    /**
251     * Sets the address to broadcast to if UDPDiscovery is enabled.
252     * <p>
253     * @param udpDiscoveryAddr The udpDiscoveryAddr to set.
254     */
255    @Override
256    public void setUdpDiscoveryAddr( final String udpDiscoveryAddr )
257    {
258        this.udpDiscoveryAddr = udpDiscoveryAddr;
259    }
260
261    /**
262     * The time-to-live for the UDP multicast packets
263     * <p>
264     * @return Returns the udpTTL.
265     * @since 3.1
266     */
267    @Override
268    public int getUdpTTL()
269    {
270        return udpTTL;
271    }
272
273    /**
274     * Sets the time-to-live for the UDP multicast packet
275     * <p>
276     * @param udpTTL The udpTTL to set.
277     * @since 3.1
278     */
279    @Override
280    public void setUdpTTL( final int udpTTL )
281    {
282        this.udpTTL = udpTTL;
283    }
284
285    /**
286     * Is the lateral allowed to try and get from other laterals.
287     * <p>
288     * This replaces the old putOnlyMode
289     * <p>
290     * @param allowGet
291     */
292    @Override
293    public void setAllowGet( final boolean allowGet )
294    {
295        this.allowGet = allowGet;
296    }
297
298    /**
299     * Is the lateral allowed to try and get from other laterals.
300     * <p>
301     * @return true if the lateral will try to get
302     */
303    @Override
304    public boolean isAllowGet()
305    {
306        return this.allowGet;
307    }
308
309    /**
310     * Is the lateral allowed to put objects to other laterals.
311     * <p>
312     * @param allowPut
313     */
314    @Override
315    public void setAllowPut( final boolean allowPut )
316    {
317        this.allowPut = allowPut;
318    }
319
320    /**
321     * Is the lateral allowed to put objects to other laterals.
322     * <p>
323     * @return true if puts are allowed
324     */
325    @Override
326    public boolean isAllowPut()
327    {
328        return this.allowPut;
329    }
330
331    /**
332     * Should the client send a remove command rather than a put when update is called. This is a
333     * client option, not a receiver option. This allows you to prevent the lateral from serializing
334     * objects.
335     * <p>
336     * @param issueRemoveOnPut
337     */
338    @Override
339    public void setIssueRemoveOnPut( final boolean issueRemoveOnPut )
340    {
341        this.issueRemoveOnPut = issueRemoveOnPut;
342    }
343
344    /**
345     * Should the client send a remove command rather than a put when update is called. This is a
346     * client option, not a receiver option. This allows you to prevent the lateral from serializing
347     * objects.
348     * <p>
349     * @return true if updates will result in a remove command being sent.
350     */
351    @Override
352    public boolean isIssueRemoveOnPut()
353    {
354        return this.issueRemoveOnPut;
355    }
356
357    /**
358     * Should the receiver try to match hash codes. If true, the receiver will see if the client
359     * supplied a hash code. If it did, then it will try to get the item locally. If the item exists,
360     * then it will compare the hash code. if they are the same, it will not remove. This isn't
361     * perfect since different objects can have the same hash code, but it is unlikely of objects of
362     * the same type.
363     * <p>
364     * @return boolean
365     */
366    @Override
367    public boolean isFilterRemoveByHashCode()
368    {
369        return this.filterRemoveByHashCode;
370    }
371
372    /**
373     * Should the receiver try to match hash codes. If true, the receiver will see if the client
374     * supplied a hash code. If it did, then it will try to get the item locally. If the item exists,
375     * then it will compare the hash code. if they are the same, it will not remove. This isn't
376     * perfect since different objects can have the same hash code, but it is unlikely of objects of
377     * the same type.
378     * <p>
379     * @param filter
380     */
381    @Override
382    public void setFilterRemoveByHashCode( final boolean filter )
383    {
384        this.filterRemoveByHashCode = filter;
385    }
386
387    /**
388     * @param socketTimeOut the socketTimeOut to set
389     */
390    @Override
391    public void setSocketTimeOut( final int socketTimeOut )
392    {
393        this.socketTimeOut = socketTimeOut;
394    }
395
396    /**
397     * @return the socketTimeOut
398     */
399    @Override
400    public int getSocketTimeOut()
401    {
402        return socketTimeOut;
403    }
404
405    /**
406     * @param openTimeOut the openTimeOut to set
407     */
408    @Override
409    public void setOpenTimeOut( final int openTimeOut )
410    {
411        this.openTimeOut = openTimeOut;
412    }
413
414    /**
415     * @return the openTimeOut
416     */
417    @Override
418    public int getOpenTimeOut()
419    {
420        return openTimeOut;
421    }
422
423    /**
424     * Used to key the instance TODO create another method for this and use toString for debugging
425     * only.
426     * <p>
427     * @return String
428     */
429    @Override
430    public String toString()
431    {
432        return this.getTcpServer() + ":" + this.getTcpListenerPort();
433    }
434}