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 size (in bytes) of multicast packets used in
31 * the multicast request and multicast announcement discovery protocols.
32 * Lookup services and discovery clients can use this constraint to limit the
33 * size of multicast request and announcement packets sent, so as to avoid
34 * fragmentation or loss when the packets traverse routers. This constraint
35 * can also be used to control the size of the buffers used to receive incoming
36 * multicast request and announcement packets.
37 *
38 * @author Sun Microsystems, Inc.
39 * @since 2.0
40 */
41 @AtomicSerial
42 public final class MulticastMaxPacketSize
43 implements InvocationConstraint, Serializable
44 {
45 private static final long serialVersionUID = 2277375127808559673L;
46
47 /** The minimum allowable multicast packet size limit. */
48 public static final int MIN_MAX_PACKET_SIZE = 512;
49
50 /**
51 * The multicast packet size limit.
52 *
53 * @serial
54 */
55 private final int size;
56
57 /**
58 * Creates a <code>MulticastMaxPacketSize</code> constraint for the given
59 * multicast packet size limit.
60 *
61 * @param size the multicast packet size limit
62 * @throws IllegalArgumentException if the given size is less than
63 * {@link #MIN_MAX_PACKET_SIZE}.
64 */
65 public MulticastMaxPacketSize(int size) {
66 this(size, check(size));
67 }
68
69 MulticastMaxPacketSize(GetArg arg) throws IOException {
70 this(arg.get("size",0));
71 }
72
73 private MulticastMaxPacketSize(int size, boolean check){
74 this.size = size;
75 }
76
77 private static boolean check(int size){
78 if (size < MIN_MAX_PACKET_SIZE) {
79 throw new IllegalArgumentException("invalid size");
80 }
81 return true;
82 }
83
84 /**
85 * Returns the multicast packet size limit.
86 *
87 * @return the multicast packet size limit
88 */
89 public int getSize() {
90 return size;
91 }
92
93 public int hashCode() {
94 return MulticastMaxPacketSize.class.hashCode() + size;
95 }
96
97 public boolean equals(Object obj) {
98 return obj instanceof MulticastMaxPacketSize &&
99 size == ((MulticastMaxPacketSize) obj).size;
100 }
101
102 public String toString() {
103 return "MulticastMaxPacketSize[" + size + "]";
104 }
105
106 private void readObject(ObjectInputStream in)
107 throws IOException, ClassNotFoundException
108 {
109 in.defaultReadObject();
110 if (size <= MIN_MAX_PACKET_SIZE) {
111 throw new InvalidObjectException("invalid size");
112 }
113 }
114 }