1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.river.discovery;
20
21 import java.io.IOException;
22 import java.io.InvalidObjectException;
23 import java.io.ObjectInputStream;
24 import java.io.Serializable;
25 import net.jini.core.constraint.InvocationConstraint;
26 import org.apache.river.api.io.AtomicSerial;
27 import org.apache.river.api.io.AtomicSerial.GetArg;
28
29 /**
30 * Represents a constraint on the time to live (TTL) value set on outgoing
31 * multicast request and multicast announcement packets. Lookup services and
32 * discovery clients can use this constraint to specify the range of multicast
33 * transmissions used in discovery.
34 *
35 * @author Sun Microsystems, Inc.
36 * @since 2.0
37 */
38 @AtomicSerial
39 public final class MulticastTimeToLive
40 implements InvocationConstraint, Serializable
41 {
42 private static final long serialVersionUID = 8899039913861829419L;
43
44 /** The maximum permissible time to live value. */
45 public static final int MAX_TIME_TO_LIVE = 0xFF;
46
47 /**
48 * The time to live value.
49 *
50 * @serial
51 */
52 private final int ttl;
53
54 /**
55 * Creates a <code>MulticastTimeToLive</code> constraint for the given time
56 * to live value.
57 *
58 * @param ttl the time to live value
59 * @throws IllegalArgumentException if the given value is negative or
60 * greater than {@link #MAX_TIME_TO_LIVE}.
61 */
62 public MulticastTimeToLive(int ttl) {
63 this(ttl, check(ttl));
64 }
65
66 MulticastTimeToLive(GetArg arg)throws IOException{
67 this(arg.get("ttl", -1));
68 }
69
70 private MulticastTimeToLive(int ttl, boolean check){
71 this.ttl = ttl;
72 }
73
74 private static boolean check(int ttl){
75 if (ttl < 0 || ttl > MAX_TIME_TO_LIVE) {
76 throw new IllegalArgumentException("invalid time to live");
77 }
78 return true;
79 }
80
81 /**
82 * Returns the time to live value.
83 *
84 * @return the time to live value
85 */
86 public int getTimeToLive() {
87 return ttl;
88 }
89
90 public int hashCode() {
91 return MulticastTimeToLive.class.hashCode() + ttl;
92 }
93
94 public boolean equals(Object obj) {
95 return obj instanceof MulticastTimeToLive &&
96 ttl == ((MulticastTimeToLive) obj).ttl;
97 }
98
99 public String toString() {
100 return "MulticastTimeToLive[" + ttl + "]";
101 }
102
103 private void readObject(ObjectInputStream in)
104 throws IOException, ClassNotFoundException
105 {
106 in.defaultReadObject();
107 if (ttl < 0 || ttl > MAX_TIME_TO_LIVE) {
108 throw new InvalidObjectException("invalid time to live");
109 }
110 }
111 }