View Javadoc

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.serial;
21  
22  import gnu.io.CommPortIdentifier;
23  import gnu.io.PortInUseException;
24  import gnu.io.SerialPort;
25  import gnu.io.UnsupportedCommOperationException;
26  
27  import java.io.IOException;
28  import java.net.SocketAddress;
29  import java.util.Enumeration;
30  import java.util.TooManyListenersException;
31  import java.util.concurrent.Executor;
32  
33  import org.apache.mina.core.service.AbstractIoConnector;
34  import org.apache.mina.core.future.ConnectFuture;
35  import org.apache.mina.core.future.DefaultConnectFuture;
36  import org.apache.mina.core.session.IdleStatusChecker;
37  import org.apache.mina.core.service.IoConnector;
38  import org.apache.mina.core.future.IoFuture;
39  import org.apache.mina.core.session.IoSessionInitializer;
40  import org.apache.mina.core.service.TransportMetadata;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * {@link IoConnector} for serial communication transport.
46   *
47   * @author The Apache MINA Project (dev@mina.apache.org)
48   * @version $Rev: 529576 $, $Date: 2007-04-17 14:25:07 +0200 (mar., 17 avr. 2007) $
49   */
50  public final class SerialConnector extends AbstractIoConnector {
51      private final Logger log;
52  
53      public SerialConnector() {
54          this(null);
55      }
56  
57      public SerialConnector(Executor executor) {
58          super(new DefaultSerialSessionConfig(), executor);
59          log = LoggerFactory.getLogger(SerialConnector.class);
60      }
61  
62      @Override
63      protected ConnectFuture connect0(
64              SocketAddress remoteAddress, SocketAddress localAddress,
65              IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
66  
67          CommPortIdentifier portId;
68          Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
69  
70          SerialAddress portAddress = (SerialAddress) remoteAddress;
71  
72          // looping around found ports
73          while (portList.hasMoreElements()) {
74              portId = (CommPortIdentifier) portList.nextElement();
75              if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
76                  if (log.isDebugEnabled()) {
77                      log.debug("Serial port discovered : " + portId.getName());
78                  }
79                  if (portId.getName().equals(portAddress.getName())) {
80                      try {
81                          if (log.isDebugEnabled()) {
82                              log
83                                      .debug("Serial port found : "
84                                              + portId.getName());
85                          }
86  
87                          SerialPort serialPort = initializePort("Apache MINA",
88                                  portId, portAddress);
89  
90                          ConnectFuture future = new DefaultConnectFuture();
91                          SerialSessionImpl session = new SerialSessionImpl(
92                                  this, getListeners(), portAddress, serialPort);
93                          finishSessionInitialization(session, future, sessionInitializer);
94                          session.start();
95                          return future;
96                      } catch (PortInUseException e) {
97                          if (log.isDebugEnabled()) {
98                              log.debug("Port In Use Exception : ", e);
99                          }
100                         return DefaultConnectFuture.newFailedFuture(e);
101                     } catch (UnsupportedCommOperationException e) {
102                         if (log.isDebugEnabled()) {
103                             log.debug("Comm Exception : ", e);
104                         }
105                         return DefaultConnectFuture.newFailedFuture(e);
106                     } catch (IOException e) {
107                         if (log.isDebugEnabled()) {
108                             log.debug("IOException : ", e);
109                         }
110                         return DefaultConnectFuture.newFailedFuture(e);
111                     } catch (TooManyListenersException e) {
112                         if (log.isDebugEnabled()) {
113                             log.debug("TooManyListenersException : ", e);
114                         }
115                         return DefaultConnectFuture.newFailedFuture(e);
116                     }
117                 }
118             }
119         }
120 
121         return DefaultConnectFuture
122                 .newFailedFuture(new SerialPortUnavailableException(
123                         "Serial port not found"));
124     }
125 
126     @Override
127     protected IoFuture dispose0() throws Exception {
128         return null;
129     }
130 
131     public TransportMetadata getTransportMetadata() {
132         return SerialSessionImpl.METADATA;
133     }
134 
135     private SerialPort initializePort(String user, CommPortIdentifier portId,
136             SerialAddress portAddress)
137             throws UnsupportedCommOperationException, PortInUseException {
138 
139         SerialSessionConfig config = (SerialSessionConfig) getSessionConfig();
140 
141         long connectTimeout = getConnectTimeoutMillis();
142         if (connectTimeout > Integer.MAX_VALUE) {
143             connectTimeout = Integer.MAX_VALUE;
144         }
145 
146         SerialPort serialPort = (SerialPort) portId.open(
147                 user, (int) connectTimeout);
148 
149         serialPort.setSerialPortParams(portAddress.getBauds(), portAddress
150                 .getDataBitsForRXTX(), portAddress.getStopBitsForRXTX(),
151                 portAddress.getParityForRXTX());
152 
153         serialPort.setFlowControlMode(portAddress.getFLowControlForRXTX());
154 
155         serialPort.notifyOnDataAvailable(true);
156 
157         if (config.isLowLatency()) {
158             serialPort.setLowLatency();
159         }
160 
161         serialPort.setInputBufferSize(config.getInputBufferSize());
162 
163         if (config.getReceiveThreshold() >= 0) {
164             serialPort.enableReceiveThreshold(config.getReceiveThreshold());
165         } else {
166             serialPort.disableReceiveThreshold();
167         }
168 
169         return serialPort;
170     }
171 
172     IdleStatusChecker getIdleStatusChecker0() {
173         return super.getIdleStatusChecker();
174     }
175 }