Skip to content

Commit

Permalink
[netty#561] [netty#912] Add Rxtx transport
Browse files Browse the repository at this point in the history
  • Loading branch information
lw346 authored and Norman Maurer committed Jan 9, 2013
1 parent d5a7057 commit c094aba
Show file tree
Hide file tree
Showing 10 changed files with 771 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<artifactId>netty-transport-udt</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport-rxtx</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
61 changes: 61 additions & 0 deletions example/src/main/java/io/netty/example/rxtx/RxtxClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2013 The Netty Project
*
* The Netty Project 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 io.netty.example.rxtx;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.oio.OioEventLoopGroup;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.transport.rxtx.RxtxChannel;
import io.netty.transport.rxtx.RxtxDeviceAddress;

/**
* Sends one message to a serial device
*/
public final class RxtxClient {

public static void main(String[] args) throws Exception {
Bootstrap b = new Bootstrap();
try {
b.group(new OioEventLoopGroup())
.channel(RxtxChannel.class)
.remoteAddress(new RxtxDeviceAddress("/dev/ttyUSB0"))
.handler(new ChannelInitializer<RxtxChannel>() {
@Override
public void initChannel(RxtxChannel ch) throws Exception {
ch.pipeline().addLast(
new LineBasedFrameDecoder(32768),
new StringEncoder(),
new StringDecoder(),
new RxtxClientHandler()
);
}
});

ChannelFuture f = b.connect().sync();

f.channel().closeFuture().sync();
} finally {
b.shutdown();
}
}

private RxtxClient() {
}
}
37 changes: 37 additions & 0 deletions example/src/main/java/io/netty/example/rxtx/RxtxClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2013 The Netty Project
*
* The Netty Project 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 io.netty.example.rxtx;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;

public class RxtxClientHandler extends ChannelInboundMessageHandlerAdapter<String> {

@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.write("AT\n");
}

@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
if ("OK".equals(msg)) {
System.out.println("Serial port responded to AT");
} else {
System.out.println("Serial port responded with not-OK: " + msg);
}
ctx.close();
}
}
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<module>codec-http</module>
<module>codec-socks</module>
<module>transport</module>
<module>transport-rxtx</module>
<module>transport-sctp</module>
<module>transport-udt</module>
<module>handler</module>
Expand Down Expand Up @@ -106,6 +107,12 @@
<version>2.4.1</version>
</dependency>

<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand Down
54 changes: 54 additions & 0 deletions transport-rxtx/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2013 The Netty Project
~
~ The Netty Project 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.netty</groupId>
<artifactId>netty-parent</artifactId>
<version>4.0.0.Beta1-SNAPSHOT</version>
</parent>

<artifactId>netty-transport-rxtx</artifactId>
<packaging>jar</packaging>

<name>Netty/RXTX Transport</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-buffer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-codec</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-transport</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
</dependency>
</dependencies>

</project>

178 changes: 178 additions & 0 deletions transport-rxtx/src/main/java/io/netty/transport/rxtx/RxtxChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright 2013 The Netty Project
*
* The Netty Project 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 io.netty.transport.rxtx;

import static io.netty.transport.rxtx.RxtxChannelOptions.*;

import io.netty.buffer.BufType;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelConfig;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.socket.oio.AbstractOioByteChannel;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.nio.channels.NotYetConnectedException;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

/**
* A channel to a serial device using the RXTX library.
*/
public class RxtxChannel extends AbstractOioByteChannel {
private static final ChannelMetadata METADATA = new ChannelMetadata(BufType.BYTE, true);

private final ChannelConfig config;

private RxtxDeviceAddress deviceAddress;
private SerialPort serialPort;
private InputStream in;
private OutputStream out;

public RxtxChannel() {
super(null, null);

config = new RxtxChannelConfig(this);
}

@Override
public ChannelConfig config() {
return config;
}

@Override
public ChannelMetadata metadata() {
return METADATA;
}

@Override
public boolean isOpen() {
return true;
}

@Override
public boolean isActive() {
return in != null && out != null;
}

@Override
protected int available() {
try {
return in.available();
} catch (IOException e) {
return 0;
}
}

@Override
protected int doReadBytes(ByteBuf buf) throws Exception {
try {
return buf.writeBytes(in, buf.writableBytes());
} catch (SocketTimeoutException e) {
return 0;
}
}

@Override
protected void doWriteBytes(ByteBuf buf) throws Exception {
if (out == null) {
throw new NotYetConnectedException();
}
buf.readBytes(out, buf.readableBytes());
}

@Override
protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
RxtxDeviceAddress remote = (RxtxDeviceAddress) remoteAddress;
final CommPortIdentifier cpi =
CommPortIdentifier.getPortIdentifier(remote.getDeviceAddress());
final CommPort commPort = cpi.open(getClass().getName(), 1000);

deviceAddress = remote;

serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(
config().getOption(BAUD_RATE),
config().getOption(DATA_BITS).getValue(),
config().getOption(STOP_BITS).getValue(),
config().getOption(PARITY_BIT).getValue()
);
serialPort.setDTR(config().getOption(DTR));
serialPort.setRTS(config().getOption(RTS));

out = serialPort.getOutputStream();
in = serialPort.getInputStream();
}

@Override
protected SocketAddress localAddress0() {
return null;
}

@Override
protected SocketAddress remoteAddress0() {
return deviceAddress;
}

@Override
protected void doBind(SocketAddress localAddress) throws Exception {
throw new UnsupportedOperationException();
}

@Override
protected void doDisconnect() throws Exception {
doClose();
}

@Override
protected void doClose() throws Exception {
IOException ex = null;

try {
if (in != null) {
in.close();
}
} catch (IOException e) {
ex = e;
}

try {
if (out != null) {
out.close();
}
} catch (IOException e) {
ex = e;
}

if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}

in = null;
out = null;
serialPort = null;

if (ex != null) {
throw ex;
}
}
}
Loading

0 comments on commit c094aba

Please sign in to comment.