001package org.apache.commons.jcs.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.jcs.auxiliary.lateral.LateralCacheAttributes;
023import org.apache.commons.jcs.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 pot */
070    private int tcpListenerPort = 0;
071
072    /** udp discovery for tcp server */
073    private String udpDiscoveryAddr = DEFAULT_UDP_DISCOVERY_ADDRESS;
074
075    /** discovery port */
076    private int udpDiscoveryPort = DEFAULT_UDP_DISCOVERY_PORT;
077
078    /** discovery switch */
079    private boolean udpDiscoveryEnabled = DEFAULT_UDP_DISCOVERY_ENABLED;
080
081    /** can we put */
082    private boolean allowPut = DEFAULT_ALLOW_GET;
083
084    /** can we go laterally for a get */
085    private boolean allowGet = DEFAULT_ALLOW_PUT;
086
087    /** call remove when there is a put */
088    private boolean issueRemoveOnPut = DEFAULT_ISSUE_REMOVE_FOR_PUT;
089
090    /** don't remove it the hashcode is the same */
091    private boolean filterRemoveByHashCode = DEFAULT_FILTER_REMOVE_BY_HASH_CODE;
092
093    /** Only block for socketTimeOut seconds before timing out on a read.  */
094    private int socketTimeOut = DEFAULT_SOCKET_TIME_OUT;
095
096    /** Only block for openTimeOut seconds before timing out on startup. */
097    private int openTimeOut = DEFAULT_OPEN_TIMEOUT;
098
099    /**
100     * Sets the tcpServer attribute of the ILateralCacheAttributes object
101     * <p>
102     * @param val The new tcpServer value
103     */
104    @Override
105    public void setTcpServer( String val )
106    {
107        this.tcpServer = val;
108    }
109
110    /**
111     * Gets the tcpServer attribute of the ILateralCacheAttributes object
112     * <p>
113     * @return The tcpServer value
114     */
115    @Override
116    public String getTcpServer()
117    {
118        return this.tcpServer;
119    }
120
121    /**
122     * Sets the tcpServers attribute of the ILateralCacheAttributes object
123     * <p>
124     * @param val The new tcpServers value
125     */
126    @Override
127    public void setTcpServers( String val )
128    {
129        this.tcpServers = val;
130    }
131
132    /**
133     * Gets the tcpServers attribute of the ILateralCacheAttributes object
134     * <p>
135     * @return The tcpServers value
136     */
137    @Override
138    public String getTcpServers()
139    {
140        return this.tcpServers;
141    }
142
143    /**
144     * Sets the tcpListenerPort attribute of the ILateralCacheAttributes object
145     * <p>
146     * @param val The new tcpListenerPort value
147     */
148    @Override
149    public void setTcpListenerPort( int val )
150    {
151        this.tcpListenerPort = val;
152    }
153
154    /**
155     * Gets the tcpListenerPort attribute of the ILateralCacheAttributes object
156     * <p>
157     * @return The tcpListenerPort value
158     */
159    @Override
160    public int getTcpListenerPort()
161    {
162        return this.tcpListenerPort;
163    }
164
165    /**
166     * Can setup UDP Discovery. This only works for TCp laterals right now. It allows TCP laterals
167     * to find each other by broadcasting to a multicast port.
168     * <p>
169     * @param udpDiscoveryEnabled The udpDiscoveryEnabled to set.
170     */
171    @Override
172    public void setUdpDiscoveryEnabled( boolean udpDiscoveryEnabled )
173    {
174        this.udpDiscoveryEnabled = udpDiscoveryEnabled;
175    }
176
177    /**
178     * Whether or not TCP laterals can try to find each other by multicast communication.
179     * <p>
180     * @return Returns the udpDiscoveryEnabled.
181     */
182    @Override
183    public boolean isUdpDiscoveryEnabled()
184    {
185        return this.udpDiscoveryEnabled;
186    }
187
188    /**
189     * The port to use if UDPDiscovery is enabled.
190     * <p>
191     * @return Returns the udpDiscoveryPort.
192     */
193    @Override
194    public int getUdpDiscoveryPort()
195    {
196        return this.udpDiscoveryPort;
197    }
198
199    /**
200     * Sets the port to use if UDPDiscovery is enabled.
201     * <p>
202     * @param udpDiscoveryPort The udpDiscoveryPort to set.
203     */
204    @Override
205    public void setUdpDiscoveryPort( int udpDiscoveryPort )
206    {
207        this.udpDiscoveryPort = udpDiscoveryPort;
208    }
209
210    /**
211     * The address to broadcast to if UDPDiscovery is enabled.
212     * <p>
213     * @return Returns the udpDiscoveryAddr.
214     */
215    @Override
216    public String getUdpDiscoveryAddr()
217    {
218        return this.udpDiscoveryAddr;
219    }
220
221    /**
222     * Sets the address to broadcast to if UDPDiscovery is enabled.
223     * <p>
224     * @param udpDiscoveryAddr The udpDiscoveryAddr to set.
225     */
226    @Override
227    public void setUdpDiscoveryAddr( String udpDiscoveryAddr )
228    {
229        this.udpDiscoveryAddr = udpDiscoveryAddr;
230    }
231
232    /**
233     * Is the lateral allowed to try and get from other laterals.
234     * <p>
235     * This replaces the old putOnlyMode
236     * <p>
237     * @param allowGet
238     */
239    @Override
240    public void setAllowGet( boolean allowGet )
241    {
242        this.allowGet = allowGet;
243    }
244
245    /**
246     * Is the lateral allowed to try and get from other laterals.
247     * <p>
248     * @return true if the lateral will try to get
249     */
250    @Override
251    public boolean isAllowGet()
252    {
253        return this.allowGet;
254    }
255
256    /**
257     * Is the lateral allowed to put objects to other laterals.
258     * <p>
259     * @param allowPut
260     */
261    @Override
262    public void setAllowPut( boolean allowPut )
263    {
264        this.allowPut = allowPut;
265    }
266
267    /**
268     * Is the lateral allowed to put objects to other laterals.
269     * <p>
270     * @return true if puts are allowed
271     */
272    @Override
273    public boolean isAllowPut()
274    {
275        return this.allowPut;
276    }
277
278    /**
279     * Should the client send a remove command rather than a put when update is called. This is a
280     * client option, not a receiver option. This allows you to prevent the lateral from serializing
281     * objects.
282     * <p>
283     * @param issueRemoveOnPut
284     */
285    @Override
286    public void setIssueRemoveOnPut( boolean issueRemoveOnPut )
287    {
288        this.issueRemoveOnPut = issueRemoveOnPut;
289    }
290
291    /**
292     * Should the client send a remove command rather than a put when update is called. This is a
293     * client option, not a receiver option. This allows you to prevent the lateral from serializing
294     * objects.
295     * <p>
296     * @return true if updates will result in a remove command being sent.
297     */
298    @Override
299    public boolean isIssueRemoveOnPut()
300    {
301        return this.issueRemoveOnPut;
302    }
303
304    /**
305     * Should the receiver try to match hashcodes. If true, the receiver will see if the client
306     * supplied a hashcode. If it did, then it will try to get the item locally. If the item exists,
307     * then it will compare the hashcode. if they are the same, it will not remove. This isn't
308     * perfect since different objects can have the same hashcode, but it is unlikely of objects of
309     * the same type.
310     * <p>
311     * @return boolean
312     */
313    @Override
314    public boolean isFilterRemoveByHashCode()
315    {
316        return this.filterRemoveByHashCode;
317    }
318
319    /**
320     * Should the receiver try to match hashcodes. If true, the receiver will see if the client
321     * supplied a hashcode. If it did, then it will try to get the item locally. If the item exists,
322     * then it will compare the hashcode. if they are the same, it will not remove. This isn't
323     * perfect since different objects can have the same hashcode, but it is unlikely of objects of
324     * the same type.
325     * <p>
326     * @param filter
327     */
328    @Override
329    public void setFilterRemoveByHashCode( boolean filter )
330    {
331        this.filterRemoveByHashCode = filter;
332    }
333
334    /**
335     * @param socketTimeOut the socketTimeOut to set
336     */
337    @Override
338    public void setSocketTimeOut( int socketTimeOut )
339    {
340        this.socketTimeOut = socketTimeOut;
341    }
342
343    /**
344     * @return the socketTimeOut
345     */
346    @Override
347    public int getSocketTimeOut()
348    {
349        return socketTimeOut;
350    }
351
352    /**
353     * @param openTimeOut the openTimeOut to set
354     */
355    @Override
356    public void setOpenTimeOut( int openTimeOut )
357    {
358        this.openTimeOut = openTimeOut;
359    }
360
361    /**
362     * @return the openTimeOut
363     */
364    @Override
365    public int getOpenTimeOut()
366    {
367        return openTimeOut;
368    }
369
370    /**
371     * Used to key the instance TODO create another method for this and use toString for debugging
372     * only.
373     * <p>
374     * @return String
375     */
376    @Override
377    public String toString()
378    {
379        return this.getTcpServer() + ":" + this.getTcpListenerPort();
380    }
381}