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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.mina.transport.socket;
21
22 import org.apache.mina.core.session.AbstractIoSessionConfig;
23 import org.apache.mina.core.session.IoSessionConfig;
24
25 /**
26 * TODO Add documentation
27 *
28 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
29 */
30 public abstract class AbstractDatagramSessionConfig extends AbstractIoSessionConfig implements DatagramSessionConfig {
31
32 private static final boolean DEFAULT_CLOSE_ON_PORT_UNREACHABLE = true;
33
34 private boolean closeOnPortUnreachable = DEFAULT_CLOSE_ON_PORT_UNREACHABLE;
35
36 protected AbstractDatagramSessionConfig() {
37 // Do nothing
38 }
39
40 @Override
41 protected void doSetAll(IoSessionConfig config) {
42 if (!(config instanceof DatagramSessionConfig)) {
43 return;
44 }
45
46 if (config instanceof AbstractDatagramSessionConfig) {
47 // Minimize unnecessary system calls by checking all 'propertyChanged' properties.
48 AbstractDatagramSessionConfig cfg = (AbstractDatagramSessionConfig) config;
49 if (cfg.isBroadcastChanged()) {
50 setBroadcast(cfg.isBroadcast());
51 }
52 if (cfg.isReceiveBufferSizeChanged()) {
53 setReceiveBufferSize(cfg.getReceiveBufferSize());
54 }
55 if (cfg.isReuseAddressChanged()) {
56 setReuseAddress(cfg.isReuseAddress());
57 }
58 if (cfg.isSendBufferSizeChanged()) {
59 setSendBufferSize(cfg.getSendBufferSize());
60 }
61 if (cfg.isTrafficClassChanged() && getTrafficClass() != cfg.getTrafficClass()) {
62 setTrafficClass(cfg.getTrafficClass());
63 }
64 } else {
65 DatagramSessionConfig cfg = (DatagramSessionConfig) config;
66 setBroadcast(cfg.isBroadcast());
67 setReceiveBufferSize(cfg.getReceiveBufferSize());
68 setReuseAddress(cfg.isReuseAddress());
69 setSendBufferSize(cfg.getSendBufferSize());
70 if (getTrafficClass() != cfg.getTrafficClass()) {
71 setTrafficClass(cfg.getTrafficClass());
72 }
73 }
74 }
75
76 /**
77 * Returns <tt>true</tt> if and only if the <tt>broadcast</tt> property
78 * has been changed by its setter method. The system call related with
79 * the property is made only when this method returns <tt>true</tt>. By
80 * default, this method always returns <tt>true</tt> to simplify implementation
81 * of subclasses, but overriding the default behavior is always encouraged.
82 */
83 protected boolean isBroadcastChanged() {
84 return true;
85 }
86
87 /**
88 * Returns <tt>true</tt> if and only if the <tt>receiveBufferSize</tt> property
89 * has been changed by its setter method. The system call related with
90 * the property is made only when this method returns <tt>true</tt>. By
91 * default, this method always returns <tt>true</tt> to simplify implementation
92 * of subclasses, but overriding the default behavior is always encouraged.
93 */
94 protected boolean isReceiveBufferSizeChanged() {
95 return true;
96 }
97
98 /**
99 * Returns <tt>true</tt> if and only if the <tt>reuseAddress</tt> property
100 * has been changed by its setter method. The system call related with
101 * the property is made only when this method returns <tt>true</tt>. By
102 * default, this method always returns <tt>true</tt> to simplify implementation
103 * of subclasses, but overriding the default behavior is always encouraged.
104 */
105 protected boolean isReuseAddressChanged() {
106 return true;
107 }
108
109 /**
110 * Returns <tt>true</tt> if and only if the <tt>sendBufferSize</tt> property
111 * has been changed by its setter method. The system call related with
112 * the property is made only when this method returns <tt>true</tt>. By
113 * default, this method always returns <tt>true</tt> to simplify implementation
114 * of subclasses, but overriding the default behavior is always encouraged.
115 */
116 protected boolean isSendBufferSizeChanged() {
117 return true;
118 }
119
120 /**
121 * Returns <tt>true</tt> if and only if the <tt>trafficClass</tt> property
122 * has been changed by its setter method. The system call related with
123 * the property is made only when this method returns <tt>true</tt>. By
124 * default, this method always returns <tt>true</tt> to simplify implementation
125 * of subclasses, but overriding the default behavior is always encouraged.
126 */
127 protected boolean isTrafficClassChanged() {
128 return true;
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 public boolean isCloseOnPortUnreachable() {
135 return closeOnPortUnreachable;
136 }
137
138 /**
139 * {@inheritDoc}
140 */
141 public void setCloseOnPortUnreachable(boolean closeOnPortUnreachable) {
142 this.closeOnPortUnreachable = closeOnPortUnreachable;
143 }
144 }