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