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    * http://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  
37  /**
38   * Default configuration class for RXTX device connections.
39   *
40   * @deprecated this transport will be removed in the next major version.
41   */
42  @Deprecated
43  final class DefaultRxtxChannelConfig extends DefaultChannelConfig implements RxtxChannelConfig {
44  
45      private volatile int baudrate = 115200;
46      private volatile boolean dtr;
47      private volatile boolean rts;
48      private volatile Stopbits stopbits = Stopbits.STOPBITS_1;
49      private volatile Databits databits = Databits.DATABITS_8;
50      private volatile Paritybit paritybit = Paritybit.NONE;
51      private volatile int waitTime;
52      private volatile int readTimeout = 1000;
53  
54      DefaultRxtxChannelConfig(RxtxChannel channel) {
55          super(channel);
56          setAllocator(new PreferHeapByteBufAllocator(getAllocator()));
57      }
58  
59      @Override
60      public Map<ChannelOption<?>, Object> getOptions() {
61          return getOptions(super.getOptions(), BAUD_RATE, DTR, RTS, STOP_BITS, DATA_BITS, PARITY_BIT, WAIT_TIME);
62      }
63  
64      @SuppressWarnings("unchecked")
65      @Override
66      public <T> T getOption(ChannelOption<T> option) {
67          if (option == BAUD_RATE) {
68              return (T) Integer.valueOf(getBaudrate());
69          }
70          if (option == DTR) {
71              return (T) Boolean.valueOf(isDtr());
72          }
73          if (option == RTS) {
74              return (T) Boolean.valueOf(isRts());
75          }
76          if (option == STOP_BITS) {
77              return (T) getStopbits();
78          }
79          if (option == DATA_BITS) {
80              return (T) getDatabits();
81          }
82          if (option == PARITY_BIT) {
83              return (T) getParitybit();
84          }
85          if (option == WAIT_TIME) {
86              return (T) Integer.valueOf(getWaitTimeMillis());
87          }
88          if (option == READ_TIMEOUT) {
89              return (T) Integer.valueOf(getReadTimeout());
90          }
91          return super.getOption(option);
92      }
93  
94      @Override
95      public <T> boolean setOption(ChannelOption<T> option, T value) {
96          validate(option, value);
97  
98          if (option == BAUD_RATE) {
99              setBaudrate((Integer) value);
100         } else if (option == DTR) {
101             setDtr((Boolean) value);
102         } else if (option == RTS) {
103             setRts((Boolean) value);
104         } else if (option == STOP_BITS) {
105             setStopbits((Stopbits) value);
106         } else if (option == DATA_BITS) {
107             setDatabits((Databits) value);
108         } else if (option == PARITY_BIT) {
109             setParitybit((Paritybit) value);
110         } else if (option == WAIT_TIME) {
111             setWaitTimeMillis((Integer) value);
112         } else if (option == READ_TIMEOUT) {
113             setReadTimeout((Integer) value);
114         } else {
115             return super.setOption(option, value);
116         }
117         return true;
118     }
119 
120     @Override
121     public RxtxChannelConfig setBaudrate(final int baudrate) {
122         this.baudrate = baudrate;
123         return this;
124     }
125 
126     @Override
127     public RxtxChannelConfig setStopbits(final Stopbits stopbits) {
128         this.stopbits = stopbits;
129         return this;
130     }
131 
132     @Override
133     public RxtxChannelConfig setDatabits(final Databits databits) {
134         this.databits = databits;
135         return this;
136     }
137 
138     @Override
139     public RxtxChannelConfig setParitybit(final Paritybit paritybit) {
140         this.paritybit = paritybit;
141         return  this;
142     }
143 
144     @Override
145     public int getBaudrate() {
146         return baudrate;
147     }
148 
149     @Override
150     public Stopbits getStopbits() {
151         return stopbits;
152     }
153 
154     @Override
155     public Databits getDatabits() {
156         return databits;
157     }
158 
159     @Override
160     public Paritybit getParitybit() {
161         return paritybit;
162     }
163 
164     @Override
165     public boolean isDtr() {
166         return dtr;
167     }
168 
169     @Override
170     public RxtxChannelConfig setDtr(final boolean dtr) {
171         this.dtr = dtr;
172         return this;
173     }
174 
175     @Override
176     public boolean isRts() {
177         return rts;
178     }
179 
180     @Override
181     public RxtxChannelConfig setRts(final boolean rts) {
182         this.rts = rts;
183         return this;
184     }
185 
186     @Override
187     public int getWaitTimeMillis() {
188         return waitTime;
189     }
190 
191     @Override
192     public RxtxChannelConfig setWaitTimeMillis(final int waitTimeMillis) {
193         if (waitTimeMillis < 0) {
194             throw new IllegalArgumentException("Wait time must be >= 0");
195         }
196         waitTime = waitTimeMillis;
197         return this;
198     }
199 
200     @Override
201     public RxtxChannelConfig setReadTimeout(int readTimeout) {
202         if (readTimeout < 0) {
203             throw new IllegalArgumentException("readTime must be >= 0");
204         }
205         this.readTimeout = readTimeout;
206         return this;
207     }
208 
209     @Override
210     public int getReadTimeout() {
211         return readTimeout;
212     }
213 
214     @Override
215     public RxtxChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
216         super.setConnectTimeoutMillis(connectTimeoutMillis);
217         return this;
218     }
219 
220     @Override
221     @Deprecated
222     public RxtxChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
223         super.setMaxMessagesPerRead(maxMessagesPerRead);
224         return this;
225     }
226 
227     @Override
228     public RxtxChannelConfig setWriteSpinCount(int writeSpinCount) {
229         super.setWriteSpinCount(writeSpinCount);
230         return this;
231     }
232 
233     @Override
234     public RxtxChannelConfig setAllocator(ByteBufAllocator allocator) {
235         super.setAllocator(allocator);
236         return this;
237     }
238 
239     @Override
240     public RxtxChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
241         super.setRecvByteBufAllocator(allocator);
242         return this;
243     }
244 
245     @Override
246     public RxtxChannelConfig setAutoRead(boolean autoRead) {
247         super.setAutoRead(autoRead);
248         return this;
249     }
250 
251     @Override
252     public RxtxChannelConfig setAutoClose(boolean autoClose) {
253         super.setAutoClose(autoClose);
254         return this;
255     }
256 
257     @Override
258     public RxtxChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
259         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
260         return this;
261     }
262 
263     @Override
264     public RxtxChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
265         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
266         return this;
267     }
268 
269     @Override
270     public RxtxChannelConfig setWriteBufferWaterMark(WriteBufferWaterMark writeBufferWaterMark) {
271         super.setWriteBufferWaterMark(writeBufferWaterMark);
272         return this;
273     }
274 
275     @Override
276     public RxtxChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
277         super.setMessageSizeEstimator(estimator);
278         return this;
279     }
280 }