Author: jvermillard
Date: Wed Apr 18 06:44:43 2007
New Revision: 530020
URL: http://svn.apache.org/viewvc?view=rev&rev=530020
Log:
Added transport-serial module (see http://rxtx.org).
Until rxtx lib is not uploaded to maven repository, you need to provide the lib
manualy and use mvn with the option : -Dwith-serial
Added:
mina/trunk/transport-serial/
mina/trunk/transport-serial/pom.xml
mina/trunk/transport-serial/src/
mina/trunk/transport-serial/src/main/
mina/trunk/transport-serial/src/main/java/
mina/trunk/transport-serial/src/main/java/org/
mina/trunk/transport-serial/src/main/java/org/apache/
mina/trunk/transport-serial/src/main/java/org/apache/mina/
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/DefaultSerialSessionConfig.java
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialAddress.java
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialFilterChain.java
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialPortUnavailableException.java
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSession.java
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSessionConfig.java
Modified:
mina/trunk/pom.xml
Modified: mina/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/mina/trunk/pom.xml?view=diff&rev=530020&r1=530019&r2=530020
==============================================================================
--- mina/trunk/pom.xml (original)
+++ mina/trunk/pom.xml Wed Apr 18 06:44:43 2007
@@ -92,10 +92,28 @@
<module>filter-codec-netty</module>
<module>filter-compression</module>
<module>integration-spring</module>
- <module>integration-jmx</module>
+ <module>integration-jmx</module>
<module>example</module>
</modules>
+ <profiles>
+ <profile>
+ <id>default-profile</id>
+ <activation>
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ </profile>
+ <profile>
+ <id>with-serial</id>
+ <activation>
+ <property><name>with-serial</name></property>
+ </activation>
+ <modules>
+ <module>transport-serial</module>
+ </modules>
+ </profile>
+ </profiles>
+
<build>
<plugins>
<plugin>
Added: mina/trunk/transport-serial/pom.xml
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/pom.xml?view=auto&rev=530020
==============================================================================
--- mina/trunk/transport-serial/pom.xml (added)
+++ mina/trunk/transport-serial/pom.xml Wed Apr 18 06:44:43 2007
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.mina</groupId>
+ <artifactId>build</artifactId>
+ <version>2.0.0-M1-SNAPSHOT</version>
+ </parent>
+ <artifactId>mina-transport-serial</artifactId>
+ <name>Apache MINA Serial Communication support</name>
+ <packaging>jar</packaging>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.mina</groupId>
+ <artifactId>mina-core</artifactId>
+ <version>${pom.version}</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+</project>
+
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/DefaultSerialSessionConfig.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/DefaultSerialSessionConfig.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/DefaultSerialSessionConfig.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/DefaultSerialSessionConfig.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.transport.serial;
+
+/**
+ * The default configuration for a serial session [EMAIL PROTECTED]
SerialSessionConfig}.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: 529576 $, $Date: 2007-04-17 14:25:07 +0200 (mar., 17 avr.
2007) $
+ */
+public class DefaultSerialSessionConfig implements SerialSessionConfig {
+
+ private int receiveThreshold = -1;
+
+ private int inputBufferSize = 8;
+
+ private int receiveTimeout = -1;
+
+ private boolean lowLatency = false;
+
+ public DefaultSerialSessionConfig() {
+
+ }
+
+ @Override
+ public Object clone() {
+ return new DefaultSerialSessionConfig();
+ }
+
+ public int getInputBufferSize() {
+ return inputBufferSize;
+ }
+
+ public boolean isLowLantecy() {
+ return lowLatency;
+ }
+
+ public void setInputBufferSize(int bufferSize) {
+ this.inputBufferSize = bufferSize;
+ }
+
+ public void setLowLatency(boolean lowLatency) {
+ this.lowLatency = lowLatency;
+ }
+
+ public int getReceiveThreshold() {
+ return receiveThreshold;
+ }
+
+ public void setReceiveThreshold(int bytes) {
+ receiveThreshold = bytes;
+ }
+
+ public int getReceiveTimeout() {
+ return receiveTimeout;
+ }
+
+ public void setReceiveTimeout(int milliseconds) {
+ receiveTimeout = milliseconds;
+ }
+}
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialAddress.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialAddress.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialAddress.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialAddress.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.mina.transport.serial;
+
+import gnu.io.SerialPort;
+
+import java.net.SocketAddress;
+import java.security.InvalidParameterException;
+
+/**
+ * An address for a serial port communication.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: 529576 $, $Date: 2007-04-17 14:25:07 +0200 (mar., 17 avr.
2007) $
+ */
+public class SerialAddress extends SocketAddress {
+
+ private static final long serialVersionUID = 1735370510442384505L;
+
+ public enum DataBits {
+ DATABITS_5, DATABITS_6, DATABITS_7, DATABITS_8
+ }
+
+ public enum Parity {
+ NONE, ODD, EVEN, MARK, SPACE
+ }
+
+ public enum StopBits {
+ BITS_1, BITS_2, BITS_1_5
+ }
+
+ public enum FlowControl {
+ NONE, RTSCTS_IN, RTSCTS_OUT, XONXOFF_IN, XONXOFF_OUT
+ }
+
+ private String name;
+
+ private int bauds;
+
+ private int dataBits;
+
+ private StopBits stopBits;
+
+ private Parity parity;
+
+ private FlowControl flowControl;
+
+ /**
+ * Create an address for a serial communication, associating a serial
interface and
+ * various serial signal carcteristics.
+ * @param name name of the device, COM1 COM2 for Windows, /dev/ttyS0 for
Unix
+ * @param bauds baud rate for the communication
+ * @param dataBits number of data bits per bytes
+ * @param stopBits number of stop bits
+ * @param parity parity used
+ * @param flowControl flow control used
+ */
+ public SerialAddress(String name, int bauds, int dataBits,
+ StopBits stopBits, Parity parity, FlowControl flowControl) {
+ super();
+ this.name = name;
+ this.bauds = bauds;
+ this.dataBits = dataBits;
+ this.stopBits = stopBits;
+ this.parity = parity;
+ this.flowControl = flowControl;
+ }
+
+ /**
+ * Bauds rate for the communication.
+ * @return the bauds (bits per seconds) for this serial link
+ */
+ public int getBauds() {
+ return bauds;
+ }
+
+ /**
+ * Number of data bits for each communicated bytes.
+ * @return the data bits
+ */
+ public int getDataBits() {
+ return dataBits;
+ }
+
+ /**
+ * The flow control policie used for this communication.
+ * @return the flow control
+ */
+ public FlowControl getFlowControl() {
+ return flowControl;
+ }
+
+ /**
+ * The name of the device. Can be COM1, COM2, /dev/ttyS0, /dev/ttyUSB1,
etc..
+ * @return name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * The parity check for this communication.
+ * @return parity type
+ */
+ public Parity getParity() {
+ return parity;
+ }
+
+ /**
+ * Number of stop bits used.
+ * @return stop bits number
+ */
+ public StopBits getStopBits() {
+ return stopBits;
+ }
+ /**
+ * Convert this serial address to a human readable string.
+ */
+ public String toString() {
+ return "serial(" + name + ",bauds:" + bauds + ",databits:" + dataBits
+ + ",stopbits:" + stopBits + ",parity:" + parity
+ + ",flowcontrol:" + flowControl + ")";
+ }
+
+ int getDataBitsForRXTX() {
+ switch (dataBits) {
+ case 5:
+ return SerialPort.DATABITS_5;
+ case 6:
+ return SerialPort.DATABITS_6;
+ case 7:
+ return SerialPort.DATABITS_7;
+ case 8:
+ return SerialPort.DATABITS_8;
+ }
+ throw new InvalidParameterException("broken databits");
+ }
+
+ int getStopBitsForRXTX() {
+ switch (stopBits) {
+ case BITS_1:
+ return SerialPort.STOPBITS_1;
+ case BITS_1_5:
+ return SerialPort.STOPBITS_1_5;
+ case BITS_2:
+ return SerialPort.STOPBITS_2;
+ }
+ throw new InvalidParameterException("broken stopbits");
+ }
+
+ int getParityForRXTX() {
+ switch (parity) {
+ case EVEN:
+ return SerialPort.PARITY_EVEN;
+ case MARK:
+ return SerialPort.PARITY_MARK;
+ case NONE:
+ return SerialPort.PARITY_NONE;
+ case ODD:
+ return SerialPort.PARITY_ODD;
+ case SPACE:
+ return SerialPort.PARITY_SPACE;
+ }
+ throw new InvalidParameterException("broken parity");
+ }
+
+ int getFLowControlForRXTX() {
+ switch (flowControl) {
+ case NONE:
+ return SerialPort.FLOWCONTROL_NONE;
+ case RTSCTS_IN:
+ return SerialPort.FLOWCONTROL_RTSCTS_IN;
+ case RTSCTS_OUT:
+ return SerialPort.FLOWCONTROL_RTSCTS_OUT;
+ case XONXOFF_IN:
+ return SerialPort.FLOWCONTROL_XONXOFF_IN;
+ case XONXOFF_OUT:
+ return SerialPort.FLOWCONTROL_XONXOFF_OUT;
+ }
+ throw new InvalidParameterException("broken stopbits");
+ }
+}
\ No newline at end of file
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialConnector.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.transport.serial;
+
+import gnu.io.CommPortIdentifier;
+import gnu.io.PortInUseException;
+import gnu.io.SerialPort;
+import gnu.io.UnsupportedCommOperationException;
+
+import java.io.IOException;
+import java.net.SocketAddress;
+import java.util.Enumeration;
+import java.util.TooManyListenersException;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.TransportType;
+import org.apache.mina.common.support.BaseIoConnector;
+import org.apache.mina.common.support.DefaultConnectFuture;
+import org.apache.mina.common.support.IoServiceListenerSupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * [EMAIL PROTECTED] IoConnector} for serial communication transport.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: 529576 $, $Date: 2007-04-17 14:25:07 +0200 (mar., 17 avr.
2007) $
+ */
+public class SerialConnector extends BaseIoConnector {
+ private Logger log;
+
+ public SerialConnector() {
+ super(new DefaultSerialSessionConfig());
+ log = LoggerFactory.getLogger(SerialConnector.class);
+ }
+
+ @Override
+ protected IoServiceListenerSupport getListeners() {
+ return super.getListeners();
+ }
+
+ @Override
+ protected ConnectFuture doConnect(SocketAddress remoteAddress,
+ SocketAddress localAddress) {
+ if (!(remoteAddress instanceof SerialAddress)) {
+ throw new IllegalArgumentException(
+ "Bad SocketAddress, need a SerialPortAddress");
+ }
+
+ CommPortIdentifier portId;
+ Enumeration portList = CommPortIdentifier.getPortIdentifiers();
+
+ SerialAddress portAddress = (SerialAddress) remoteAddress;
+
+ // looping around found ports
+ while (portList.hasMoreElements()) {
+ portId = (CommPortIdentifier) portList.nextElement();
+ if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
+ if (log.isDebugEnabled()) {
+ log.debug("Serial port discovered : " + portId.getName());
+ }
+ if (portId.getName().equals(portAddress.getName())) {
+ try {
+ if (log.isDebugEnabled()) {
+ log
+ .debug("Serial port found : "
+ + portId.getName());
+ }
+
+ SerialPort serialPort = initalizePort("Apache MINA",
+ 2000, portId, portAddress);
+
+ ConnectFuture future = new DefaultConnectFuture();
+ SerialSession session = new SerialSession(this,
+ portAddress, serialPort);
+ session.start();
+ future.setSession(session);
+ return future;
+ } catch (PortInUseException e) {
+ if (log.isDebugEnabled())
+ log.debug("Port In Use Exception : ", e);
+ return DefaultConnectFuture.newFailedFuture(e);
+ } catch (UnsupportedCommOperationException e) {
+ if (log.isDebugEnabled())
+ log.debug("Comm Exception : ", e);
+ return DefaultConnectFuture.newFailedFuture(e);
+ } catch (IOException e) {
+ if (log.isDebugEnabled())
+ log.debug("IOException : ", e);
+ return DefaultConnectFuture.newFailedFuture(e);
+ } catch (TooManyListenersException e) {
+ if (log.isDebugEnabled())
+ log.debug("TooManyListenersException : ", e);
+ return DefaultConnectFuture.newFailedFuture(e);
+ }
+ }
+ }
+ }
+
+ return DefaultConnectFuture.newFailedFuture(new
SerialPortUnavailableException(
+ "Serial port not found"));
+ }
+
+ public TransportType getTransportType() {
+ return SerialSession.serialTransportType;
+ }
+
+ private SerialPort initalizePort(String user, int timeout,
+ CommPortIdentifier portId, SerialAddress portAddress)
+ throws UnsupportedCommOperationException, PortInUseException {
+ SerialPort serialPort = (SerialPort) portId.open("Apache MINA", 2000);
// TODO: need a parameter for millisec. timeout
+
+ serialPort.setSerialPortParams(portAddress.getBauds(), portAddress
+ .getDataBitsForRXTX(), portAddress.getStopBitsForRXTX(),
+ portAddress.getParityForRXTX());
+
+ serialPort.setFlowControlMode(portAddress.getFLowControlForRXTX());
+
+ serialPort.notifyOnDataAvailable(true);
+ SerialSessionConfig config = (SerialSessionConfig) getSessionConfig();
+
+ if (config.isLowLantecy())
+ serialPort.setLowLatency();
+
+ serialPort.setInputBufferSize(config.getInputBufferSize());
+
+ if (config.getReceiveThreshold() >= 0) {
+ serialPort.enableReceiveThreshold(config.getReceiveThreshold());
+ } else
+ serialPort.disableReceiveThreshold();
+
+ if (config.getReceiveTimeout() >= 0) {
+ serialPort.enableReceiveTimeout(config.getReceiveTimeout());
+ } else
+ serialPort.disableReceiveTimeout();
+
+ serialPort.enableReceiveThreshold(8);
+
+ return serialPort;
+ }
+}
\ No newline at end of file
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialFilterChain.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialFilterChain.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialFilterChain.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialFilterChain.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.transport.serial;
+
+import java.util.Queue;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.IoFilterChain;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.WriteRequest;
+import org.apache.mina.common.support.AbstractIoFilterChain;
+
+/**
+ * An [EMAIL PROTECTED] IoFilterChain} for serial communication transport.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: 529590 $, $Date: 2007-04-17 15:14:17 +0200 (mar., 17 avr.
2007) $
+ */
+public class SerialFilterChain extends AbstractIoFilterChain {
+
+ protected SerialFilterChain(IoSession session) {
+ super(session);
+ }
+
+ @Override
+ protected void doClose(IoSession session) throws Exception {
+ ((SerialSession) session).closeSerialPort();
+ }
+
+ @Override
+ protected void doWrite(IoSession session, WriteRequest writeRequest)
+ throws Exception {
+ SerialSession s = (SerialSession) session;
+ Queue<WriteRequest> queue = s.getWriteRequestQueue();
+
+ // SocketIoProcessor.doFlush() will reset it after write is finished
+ // because the buffer will be passed with messageSent event.
+ ((ByteBuffer) writeRequest.getMessage()).mark();
+ synchronized (queue) {
+ queue.offer(writeRequest);
+ if (queue.size() == 1 && session.getTrafficMask().isWritable()) {
+ // Notify serial session worker only when writeRequestQueue
was empty.
+ s.notifyWriteWorker();
+ }
+ }
+ }
+
+}
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialPortUnavailableException.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialPortUnavailableException.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialPortUnavailableException.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialPortUnavailableException.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.transport.serial;
+
+import org.apache.mina.common.RuntimeIOException;
+
+/**
+ * Exception thrown when the serial port can't be open because
+ * it doesn't exists.
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: $, $Date: $
+ */
+public class SerialPortUnavailableException extends RuntimeIOException {
+
+ private static final long serialVersionUID = 1L;
+
+ public SerialPortUnavailableException(String details) {
+ super(details);
+ }
+
+}
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSession.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSession.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSession.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSession.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,306 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.transport.serial;
+
+import gnu.io.SerialPort;
+import gnu.io.SerialPortEvent;
+import gnu.io.SerialPortEventListener;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.SocketAddress;
+import java.util.LinkedList;
+import java.util.Queue;
+import java.util.TooManyListenersException;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.ExceptionMonitor;
+import org.apache.mina.common.IoFilterChain;
+import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoService;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.IoSessionConfig;
+import org.apache.mina.common.TransportType;
+import org.apache.mina.common.WriteRequest;
+import org.apache.mina.common.support.BaseIoSession;
+import org.apache.mina.common.support.DefaultTransportType;
+import org.apache.mina.common.support.SessionIdleStatusChecker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An [EMAIL PROTECTED] IoSession} for serial communication transport.
+ *
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: 529590 $, $Date: 2007-04-17 15:14:17 +0200 (mar., 17 avr.
2007) $
+ */
+public class SerialSession extends BaseIoSession implements
+ SerialPortEventListener {
+
+ private SerialSessionConfig config;
+
+ private IoHandler ioHandler;
+
+ private IoFilterChain filterChain;
+
+ private IoService service;
+
+ private SerialAddress address;
+
+ private final Queue<WriteRequest> writeRequestQueue;
+
+ private InputStream inputStream;
+
+ private OutputStream outputStream;
+
+ private SerialPort port;
+
+ private Logger log;
+
+ public static final TransportType serialTransportType = new
DefaultTransportType(
+ "serial communication", false, SerialAddress.class,
+ ByteBuffer.class, SerialSessionConfig.class);
+
+ SerialSession(IoService service, SerialAddress address, SerialPort port) {
+ this.service = service;
+ this.ioHandler = service.getHandler();
+ this.filterChain = new SerialFilterChain(this);
+ this.writeRequestQueue = new LinkedList<WriteRequest>();
+ this.port = port;
+
+ log = LoggerFactory.getLogger(SerialSession.class);
+ }
+
+ @Override
+ protected void updateTrafficMask() {
+ throw new UnsupportedOperationException();
+ }
+
+ public IoSessionConfig getConfig() {
+ return config;
+ }
+
+ public IoFilterChain getFilterChain() {
+ return filterChain;
+ }
+
+ public IoHandler getHandler() {
+ return ioHandler;
+ }
+
+ public SocketAddress getLocalAddress() {
+ return null; // not applicable
+ }
+
+ public SocketAddress getRemoteAddress() {
+ return address;
+ }
+
+ Queue<WriteRequest> getWriteRequestQueue() {
+ return writeRequestQueue;
+ }
+
+ public int getScheduledWriteMessages() {
+ synchronized (writeRequestQueue) {
+ return writeRequestQueue.size();
+ }
+ }
+
+ public int getScheduledWriteBytes() {
+ int size = 0;
+ synchronized (writeRequestQueue) {
+ for (Object o : writeRequestQueue) {
+ if (o instanceof ByteBuffer) {
+ size += ((ByteBuffer) o).remaining();
+ }
+ }
+ }
+ return size;
+ }
+
+ public IoService getService() {
+ return service;
+ }
+
+ public TransportType getTransportType() {
+ return serialTransportType;
+ }
+
+ protected void close0() {
+ filterChain.fireFilterClose(this);
+ }
+
+ protected void write0(WriteRequest writeRequest) {
+ filterChain.fireFilterWrite(this, writeRequest);
+ }
+
+ /**
+ * start handling streams
+ *
+ * @throws IOException
+ * @throws TooManyListenersException
+ */
+ void start() throws IOException, TooManyListenersException {
+ inputStream = port.getInputStream();
+ outputStream = port.getOutputStream();
+ ReadWorker w = new ReadWorker();
+ w.start();
+ port.addEventListener(this);
+ SessionIdleStatusChecker.getInstance().addSession(this);
+ ((SerialConnector) getService()).getListeners()
+ .fireSessionCreated(this);
+ }
+
+ private Object writeMonitor = new Object();
+
+ private WriteWorker writeWorker;
+
+ private class WriteWorker extends Thread {
+ public void run() {
+ while (isConnected() && !isClosing()) {
+ flushWrites();
+
+ // wait for more data
+ synchronized (writeMonitor) {
+ try {
+ writeMonitor.wait();
+ } catch (InterruptedException e) {
+ log.error("InterruptedException", e);
+ }
+ }
+ }
+ }
+ }
+
+ private void flushWrites() {
+ for (;;) {
+ WriteRequest req;
+
+ synchronized (writeRequestQueue) {
+ req = (WriteRequest) writeRequestQueue.peek();
+ }
+
+ if (req == null)
+ break;
+
+ ByteBuffer buf = (ByteBuffer) req.getMessage();
+ if (buf.remaining() == 0) {
+ synchronized (writeRequestQueue) {
+ writeRequestQueue.poll();
+ }
+ this.increaseWrittenMessages();
+
+ buf.reset();
+
+ this.getFilterChain().fireMessageSent(this, req);
+ continue;
+ }
+
+ int writtenBytes = buf.remaining();
+ try {
+ outputStream.write(buf.array());
+ buf.position(buf.position() + writtenBytes);
+ this.increaseWrittenBytes(writtenBytes);
+ } catch (IOException e) {
+ this.getFilterChain().fireExceptionCaught(this, e);
+ }
+ }
+ }
+
+ void notifyWriteWorker() {
+ if (writeWorker == null) {
+ writeWorker = new WriteWorker();
+ writeWorker.start();
+ } else {
+ synchronized (writeMonitor) {
+ writeMonitor.notifyAll();
+ }
+ }
+ }
+
+ private Object readReadyMonitor = new Object();
+
+ private class ReadWorker extends Thread {
+ @Override
+ public void run() {
+ while (isConnected() && !isClosing()) {
+ synchronized (readReadyMonitor) {
+ try {
+ readReadyMonitor.wait();
+ } catch (InterruptedException e) {
+ log.error("InterruptedException", e);
+ }
+ if (isClosing() || !isConnected())
+ break;
+ int dataSize;
+ try {
+ dataSize = inputStream.available();
+ byte[] data = new byte[dataSize];
+ int readBytes = inputStream.read(data);
+
+ if (readBytes > 0) {
+ increaseReadBytes(readBytes);
+ // TODO : check if it's the good allocation way
+ ByteBuffer buf = ByteBuffer.allocate(readBytes);
+ buf.put(data, 0, readBytes);
+ buf.flip();
+ getFilterChain().fireMessageReceived(
+ SerialSession.this, buf);
+ }
+ } catch (IOException e) {
+ getFilterChain().fireExceptionCaught(
+ SerialSession.this, e);
+ }
+ }
+ }
+ }
+ }
+
+ public void serialEvent(SerialPortEvent evt) {
+ if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
+ synchronized (readReadyMonitor) {
+ readReadyMonitor.notifyAll();
+ }
+ }
+ }
+
+ public void closeSerialPort() {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ ExceptionMonitor.getInstance().exceptionCaught(e);
+ }
+ try {
+ outputStream.close();
+ } catch (IOException e) {
+ ExceptionMonitor.getInstance().exceptionCaught(e);
+ }
+
+ port.close();
+ notifyWriteWorker();
+ synchronized (readReadyMonitor) {
+ readReadyMonitor.notifyAll();
+ }
+
+ ((SerialConnector) getService()).getListeners().fireSessionDestroyed(
+ this);
+ }
+}
\ No newline at end of file
Added:
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSessionConfig.java
URL:
http://svn.apache.org/viewvc/mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSessionConfig.java?view=auto&rev=530020
==============================================================================
---
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSessionConfig.java
(added)
+++
mina/trunk/transport-serial/src/main/java/org/apache/mina/transport/serial/SerialSessionConfig.java
Wed Apr 18 06:44:43 2007
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mina.transport.serial;
+
+import org.apache.mina.common.IoSessionConfig;
+
+/**
+ * An [EMAIL PROTECTED] IoSessionConfig} for serial transport type.
+ * All those parameters are extracted from rxtx.org API for more details :
+ * http://www.rxtx.org
+ * @author The Apache MINA Project ([EMAIL PROTECTED])
+ * @version $Rev: 529576 $, $Date: 2007-04-17 14:25:07 +0200 (mar., 17 avr.
2007) $
+ */
+public interface SerialSessionConfig extends IoSessionConfig {
+
+ /**
+ * Gets the input buffer size. Note that this method is advisory and the
underlying OS
+ * may choose not to report correct values for the buffer size.
+ * @return input buffer size in bytes
+ */
+ int getInputBufferSize();
+
+ /**
+ * Sets the input buffer size. Note that this is advisory and memory
availability may
+ * determine the ultimate buffer size used by the driver.
+ * @param bufferSize the buffer size in bytes
+ */
+ void setInputBufferSize(int bufferSize);
+
+ /**
+ * Is the low latency mode is enabled.
+ * @return low latency on
+ */
+ boolean isLowLantecy();
+
+ /**
+ * Set the low latency mode, be carefull it's not supported by all the
OS/hardware.
+ * @param lowLatency
+ */
+ void setLowLatency(boolean lowLatency);
+
+ /**
+ * TODO : to test if it's usefull for MINA
+ * @return
+ */
+ int getReceiveThreshold();
+
+ /**
+ * TODO : to test if it's usefull for MINA
+ * @param bytes
+ */
+ void setReceiveThreshold(int bytes);
+
+ /**
+ * TODO : to test if it's usefull for MINA
+ * @param bytes
+ */
+ int getReceiveTimeout();
+
+ /**
+ * TODO : to test if it's usefull for MINA
+ * @param bytes
+ */
+ void setReceiveTimeout(int milliseconds);
+}