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 gnu.io.CommPort;
19  import gnu.io.CommPortIdentifier;
20  import gnu.io.SerialPort;
21  import io.netty.channel.ChannelPromise;
22  import io.netty.channel.oio.OioByteStreamChannel;
23  
24  import java.net.SocketAddress;
25  import java.util.concurrent.TimeUnit;
26  
27  import static io.netty.channel.rxtx.RxtxChannelOption.*;
28  
29  /**
30   * A channel to a serial device using the RXTX library.
31   *
32   * @deprecated this transport will be removed in the next major version.
33   */
34  @Deprecated
35  public class RxtxChannel extends OioByteStreamChannel {
36  
37      private static final RxtxDeviceAddress LOCAL_ADDRESS = new RxtxDeviceAddress("localhost");
38  
39      private final RxtxChannelConfig config;
40  
41      private boolean open = true;
42      private RxtxDeviceAddress deviceAddress;
43      private SerialPort serialPort;
44  
45      public RxtxChannel() {
46          super(null);
47  
48          config = new DefaultRxtxChannelConfig(this);
49      }
50  
51      @Override
52      public RxtxChannelConfig config() {
53          return config;
54      }
55  
56      @Override
57      public boolean isOpen() {
58          return open;
59      }
60  
61      @Override
62      protected AbstractUnsafe newUnsafe() {
63          return new RxtxUnsafe();
64      }
65  
66      @Override
67      protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
68          RxtxDeviceAddress remote = (RxtxDeviceAddress) remoteAddress;
69          final CommPortIdentifier cpi = CommPortIdentifier.getPortIdentifier(remote.value());
70          final CommPort commPort = cpi.open(getClass().getName(), 1000);
71          commPort.enableReceiveTimeout(config().getOption(READ_TIMEOUT));
72          deviceAddress = remote;
73  
74          serialPort = (SerialPort) commPort;
75      }
76  
77      protected void doInit() throws Exception {
78          serialPort.setSerialPortParams(
79              config().getOption(BAUD_RATE),
80              config().getOption(DATA_BITS).value(),
81              config().getOption(STOP_BITS).value(),
82              config().getOption(PARITY_BIT).value()
83          );
84          serialPort.setDTR(config().getOption(DTR));
85          serialPort.setRTS(config().getOption(RTS));
86  
87          activate(serialPort.getInputStream(), serialPort.getOutputStream());
88      }
89  
90      @Override
91      public RxtxDeviceAddress localAddress() {
92          return (RxtxDeviceAddress) super.localAddress();
93      }
94  
95      @Override
96      public RxtxDeviceAddress remoteAddress() {
97          return (RxtxDeviceAddress) super.remoteAddress();
98      }
99  
100     @Override
101     protected RxtxDeviceAddress localAddress0() {
102         return LOCAL_ADDRESS;
103     }
104 
105     @Override
106     protected RxtxDeviceAddress remoteAddress0() {
107         return deviceAddress;
108     }
109 
110     @Override
111     protected void doBind(SocketAddress localAddress) throws Exception {
112         throw new UnsupportedOperationException();
113     }
114 
115     @Override
116     protected void doDisconnect() throws Exception {
117         doClose();
118     }
119 
120     @Override
121     protected void doClose() throws Exception {
122         open = false;
123         try {
124            super.doClose();
125         } finally {
126             if (serialPort != null) {
127                 serialPort.removeEventListener();
128                 serialPort.close();
129                 serialPort = null;
130             }
131         }
132     }
133 
134     private final class RxtxUnsafe extends AbstractUnsafe {
135         @Override
136         public void connect(
137                 final SocketAddress remoteAddress,
138                 final SocketAddress localAddress, final ChannelPromise promise) {
139             if (!promise.setUncancellable() || !ensureOpen(promise)) {
140                 return;
141             }
142 
143             try {
144                 final boolean wasActive = isActive();
145                 doConnect(remoteAddress, localAddress);
146 
147                 int waitTime = config().getOption(WAIT_TIME);
148                 if (waitTime > 0) {
149                     eventLoop().schedule(new Runnable() {
150                         @Override
151                         public void run() {
152                             try {
153                                 doInit();
154                                 safeSetSuccess(promise);
155                                 if (!wasActive && isActive()) {
156                                     pipeline().fireChannelActive();
157                                 }
158                             } catch (Throwable t) {
159                                 safeSetFailure(promise, t);
160                                 closeIfClosed();
161                             }
162                         }
163                    }, waitTime, TimeUnit.MILLISECONDS);
164                 } else {
165                     doInit();
166                     safeSetSuccess(promise);
167                     if (!wasActive && isActive()) {
168                         pipeline().fireChannelActive();
169                     }
170                 }
171             } catch (Throwable t) {
172                 safeSetFailure(promise, t);
173                 closeIfClosed();
174             }
175         }
176     }
177 }