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