View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.rxtx;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelOption;
20  import io.netty.channel.DefaultChannelConfig;
21  import io.netty.channel.MessageSizeEstimator;
22  import io.netty.channel.PreferHeapByteBufAllocator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.channel.WriteBufferWaterMark;
25  
26  import java.util.Map;
27  
28  import static io.netty.channel.rxtx.RxtxChannelOption.BAUD_RATE;
29  import static io.netty.channel.rxtx.RxtxChannelOption.DATA_BITS;
30  import static io.netty.channel.rxtx.RxtxChannelOption.DTR;
31  import static io.netty.channel.rxtx.RxtxChannelOption.PARITY_BIT;
32  import static io.netty.channel.rxtx.RxtxChannelOption.READ_TIMEOUT;
33  import static io.netty.channel.rxtx.RxtxChannelOption.RTS;
34  import static io.netty.channel.rxtx.RxtxChannelOption.STOP_BITS;
35  import static io.netty.channel.rxtx.RxtxChannelOption.WAIT_TIME;
36  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
37  
38  /**
39   * Default configuration class for RXTX device connections.
40   *
41   * @deprecated this transport will be removed in the next major version.
42   */
43  @Deprecated
44  final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements RxtxChannelConfig {
45  
46      private volatile int baudrate = 115200;
47      private volatile boolean dtr;
48      private volatile boolean rts;
49      private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
50      private volatile Databits databits = Databits.DATABITS_8;
51      private volatile Paritybit paritybit = Paritybit.NONE;
52      private volatile int waitTime;
53      private volatile int readTimeout = 1000;
54  
55      DefaultRxtxChannelConfig(RxtxChannel channel) {
56          super(channel);
57          setAllocator(new PreferHeapByteBufAllocator(getAllocator()));
58      }
59  
60      @Override
61      public Map<ChannelOption<?>, Object> getOptions() {
62          return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT, WAIT_TIME);
63      }
64  
65      @SuppressWarnings("unchecked")
66      @Override
67      public <T> T getOption(ChannelOption<T> option) {
68          if (option == BAUD_RATE) {
69              return (T) Integer.valueOf(getBaudrate());
70          }
71          if (option == DTR) {
72              return (T) Boolean.valueOf(isDtr());
73          }
74          if (option == RTS) {
75              return (T) Boolean.valueOf(isRts());
76          }
77          if (option == STOP_BITS) {
78              return (T) getStopbits();
79          }
80          if (option == DATA_BITS) {
81              return (T) getDatabits();
82          }
83          if (option == PARITY_BIT) {
84              return (T) getParitybit();
85          }
86          if (option == WAIT_TIME) {
87              return (T) Integer.valueOf(getWaitTimeMillis());
88          }
89          if (option == READ_TIMEOUT) {
90              return (T) Integer.valueOf(getReadTimeout());
91          }
92          return super.getOption(option);
93      }
94  
95      @Override
96      public <T> boolean setOption(ChannelOption<T> option, T value) {
97          validate(option, value);
98  
99          if (option == BAUD_RATE) {
100             setBaudrate((Integer) value);
101         } else if (option == DTR) {
102             setDtr((Boolean) value);
103         } else if (option == RTS) {
104             setRts((Boolean) value);
105         } else if (option == STOP_BITS) {
106             setStopbits((Stopbits) value);
107         } else if (option == DATA_BITS) {
108             setDatabits((Databits) value);
109         } else if (option == PARITY_BIT) {
110             setParitybit((Paritybit) value);
111         } else if (option == WAIT_TIME) {
112             setWaitTimeMillis((Integer) value);
113         } else if (option == READ_TIMEOUT) {
114             setReadTimeout((Integer) value);
115         } else {
116             return super.setOption(option, value);
117         }
118         return true;
119     }
120 
121     @Override
122     public RxtxChannelConfig setBaudrate(final int baudrate) {
123         this.baudrate = baudrate;
124         return this;
125     }
126 
127     @Override
128     public RxtxChannelConfig setStopbits(final Stopbits stopbits) {
129         this.stopbits = stopbits;
130         return this;
131     }
132 
133     @Override
134     public RxtxChannelConfig setDatabits(final Databits databits) {
135         this.databits = databits;
136         return this;
137     }
138 
139     @Override
140     public RxtxChannelConfig setParitybit(final Paritybit paritybit) {
141         this.paritybit = paritybit;
142         return  this;
143     }
144 
145     @Override
146     public int getBaudrate() {
147         return baudrate;
148     }
149 
150     @Override
151     public Stopbits getStopbits() {
152         return stopbits;
153     }
154 
155     @Override
156     public Databits getDatabits() {
157         return databits;
158     }
159 
160     @Override
161     public Paritybit getParitybit() {
162         return paritybit;
163     }
164 
165     @Override
166     public boolean isDtr() {
167         return dtr;
168     }
169 
170     @Override
171     public RxtxChannelConfig setDtr(final boolean dtr) {
172         this.dtr = dtr;
173         return this;
174     }
175 
176     @Override
177     public boolean isRts() {
178         return rts;
179     }
180 
181     @Override
182     public RxtxChannelConfig setRts(final boolean rts) {
183         this.rts = rts;
184         return this;
185     }
186 
187     @Override
188     public int getWaitTimeMillis() {
189         return waitTime;
190     }
191 
192     @Override
193     public RxtxChannelConfig setWaitTimeMillis(final int waitTimeMillis) {
194         this.waitTime = checkPositiveOrZero(waitTimeMillis, "waitTimeMillis");
195         return this;
196     }
197 
198     @Override
199     public RxtxChannelConfig setReadTimeout(int readTimeout) {
200         this.readTimeout = checkPositiveOrZero(readTimeout, "readTimeout");
201         return this;
202     }
203 
204     @Override
205     public int getReadTimeout() {
206         return readTimeout;
207     }
208 
209     @Override
210     public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
211         super.setConnectTimeoutMillis(connectTimeoutMillis);
212         return this;
213     }
214 
215     @Override
216     @Deprecated
217     public RxtxChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
218         super.setMaxMessagesPerRead(maxMessagesPerRead);
219         return this;
220     }
221 
222     @Override
223     public RxtxChannelConfig setWriteSpinCount(int writeSpinCount) {
224         super.setWriteSpinCount(writeSpinCount);
225         return this;
226     }
227 
228     @Override
229     public RxtxChannelConfig setAllocator(ByteBufAllocator allocator) {
230         super.setAllocator(allocator);
231         return this;
232     }
233 
234     @Override
235     public RxtxChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
236         super.setRecvByteBufAllocator(allocator);
237         return this;
238     }
239 
240     @Override
241     public RxtxChannelConfig setAutoRead(boolean autoRead) {
242         super.setAutoRead(autoRead);
243         return this;
244     }
245 
246     @Override
247     public RxtxChannelConfig setAutoClose(boolean autoClose) {
248         super.setAutoClose(autoClose);
249         return this;
250     }
251 
252     @Override
253     public RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
254         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
255         return this;
256     }
257 
258     @Override
259     public RxtxChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
260         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
261         return this;
262     }
263 
264     @Override
265     public RxtxChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
266         super.setWriteBufferWaterMark(writeBufferWaterMark);
267         return this;
268     }
269 
270     @Override
271     public RxtxChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
272         super.setMessageSizeEstimator(estimator);
273         return this;
274     }
275 }