1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.future.ConnectFuture;
34 import org.apache.mina.core.future.DefaultConnectFuture;
35 import org.apache.mina.core.service.AbstractIoConnector;
36 import org.apache.mina.core.service.IoConnector;
37 import org.apache.mina.core.service.TransportMetadata;
38 import org.apache.mina.core.session.IdleStatusChecker;
39 import org.apache.mina.core.session.IoSessionInitializer;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45
46
47
48 public final class SerialConnector extends AbstractIoConnector {
49 private final Logger log;
50
51 private IdleStatusChecker idleChecker;
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 idleChecker = new IdleStatusChecker();
62
63
64 executeWorker(idleChecker.getNotifyingTask(), "idleStatusChecker");
65
66 }
67
68 @Override
69 protected synchronized ConnectFuture connect0(
70 SocketAddress remoteAddress, SocketAddress localAddress,
71 IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
72
73 CommPortIdentifier portId;
74 Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
75
76 SerialAddress portAddress = (SerialAddress) remoteAddress;
77
78
79 while (portList.hasMoreElements()) {
80 portId = (CommPortIdentifier) portList.nextElement();
81 if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
82 if (log.isDebugEnabled()) {
83 log.debug("Serial port discovered : " + portId.getName());
84 }
85 if (portId.getName().equals(portAddress.getName())) {
86 try {
87 if (log.isDebugEnabled()) {
88 log
89 .debug("Serial port found : "
90 + portId.getName());
91 }
92
93 SerialPort serialPort = initializePort("Apache MINA",
94 portId, portAddress);
95
96 ConnectFuture future = new DefaultConnectFuture();
97 SerialSessionImpl session = new SerialSessionImpl(
98 this, getListeners(), portAddress, serialPort);
99 initSession(session, future, sessionInitializer);
100 session.start();
101 return future;
102 } catch (PortInUseException e) {
103 if (log.isDebugEnabled()) {
104 log.debug("Port In Use Exception : ", e);
105 }
106 return DefaultConnectFuture.newFailedFuture(e);
107 } catch (UnsupportedCommOperationException e) {
108 if (log.isDebugEnabled()) {
109 log.debug("Comm Exception : ", e);
110 }
111 return DefaultConnectFuture.newFailedFuture(e);
112 } catch (IOException e) {
113 if (log.isDebugEnabled()) {
114 log.debug("IOException : ", e);
115 }
116 return DefaultConnectFuture.newFailedFuture(e);
117 } catch (TooManyListenersException e) {
118 if (log.isDebugEnabled()) {
119 log.debug("TooManyListenersException : ", e);
120 }
121 return DefaultConnectFuture.newFailedFuture(e);
122 }
123 }
124 }
125 }
126
127 return DefaultConnectFuture
128 .newFailedFuture(new SerialPortUnavailableException(
129 "Serial port not found"));
130 }
131
132 @Override
133 protected void dispose0() throws Exception {
134
135 idleChecker.getNotifyingTask().cancel();
136 }
137
138 public TransportMetadata getTransportMetadata() {
139 return SerialSessionImpl.METADATA;
140 }
141
142 private SerialPort initializePort(String user, CommPortIdentifier portId,
143 SerialAddress portAddress)
144 throws UnsupportedCommOperationException, PortInUseException {
145
146 SerialSessionConfig config = (SerialSessionConfig) getSessionConfig();
147
148 long connectTimeout = getConnectTimeoutMillis();
149 if (connectTimeout > Integer.MAX_VALUE) {
150 connectTimeout = Integer.MAX_VALUE;
151 }
152
153 SerialPort serialPort = (SerialPort) portId.open(
154 user, (int) connectTimeout);
155
156 serialPort.setSerialPortParams(portAddress.getBauds(), portAddress
157 .getDataBitsForRXTX(), portAddress.getStopBitsForRXTX(),
158 portAddress.getParityForRXTX());
159
160 serialPort.setFlowControlMode(portAddress.getFLowControlForRXTX());
161
162 serialPort.notifyOnDataAvailable(true);
163
164 if (config.isLowLatency()) {
165 serialPort.setLowLatency();
166 }
167
168 serialPort.setInputBufferSize(config.getInputBufferSize());
169 serialPort.setOutputBufferSize(config.getOutputBufferSize());
170
171 if (config.getReceiveThreshold() >= 0) {
172 serialPort.enableReceiveThreshold(config.getReceiveThreshold());
173 } else {
174 serialPort.disableReceiveThreshold();
175 }
176
177 return serialPort;
178 }
179
180 IdleStatusChecker getIdleStatusChecker0() {
181 return idleChecker;
182 }
183 }