Skip to content

Commit

Permalink
NETTY-386 Support for serial connections using RXTX
Browse files Browse the repository at this point in the history
Contributed by Daniel Bimschas and Dennis Pfisterer
Initial import after fixing compiler warnings and removing log
messages and shut-down hooks
  • Loading branch information
trustin committed Feb 24, 2011
1 parent 7ab5ec5 commit 4144b43
Show file tree
Hide file tree
Showing 6 changed files with 733 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>

<!-- RXTX - completely optional -->
<!-- Used for serial port communication transport -->
<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>

<!-- Servlet API - completely optional -->
<!-- Used for HTTP tunneling transport -->
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/org/jboss/netty/channel/rxtx/RXTXChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2011 Red Hat, Inc.
*
* Red Hat 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.jboss.netty.channel.rxtx;


import java.net.SocketAddress;

import org.jboss.netty.channel.AbstractChannel;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelConfig;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelSink;

/**
* A channel to a serial device using the RXTX library.
*
* @author Daniel Bimschas
* @author Dennis Pfisterer
*/
public class RXTXChannel extends AbstractChannel {

RXTXChannel(final Channel parent, final ChannelFactory factory, final ChannelPipeline pipeline,
final ChannelSink sink) {
super(parent, factory, pipeline, sink);
}

@Override
public ChannelConfig getConfig() {
return ((RXTXChannelSink) getPipeline().getSink()).getConfig();
}

@Override
public boolean isBound() {
return ((RXTXChannelSink) getPipeline().getSink()).isBound();
}

@Override
public boolean isConnected() {
return ((RXTXChannelSink) getPipeline().getSink()).isConnected();
}

@Override
public SocketAddress getLocalAddress() {
return null;
}

@Override
public SocketAddress getRemoteAddress() {
return ((RXTXChannelSink) getPipeline().getSink()).getRemoteAddress();
}

@Override
public ChannelFuture bind(final SocketAddress localAddress) {
throw new UnsupportedOperationException();
}

@Override
public ChannelFuture unbind() {
throw new UnsupportedOperationException();
}

void doSetClosed() {
setClosed();
}
}
204 changes: 204 additions & 0 deletions src/main/java/org/jboss/netty/channel/rxtx/RXTXChannelConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright 2011 Red Hat, Inc.
*
* Red Hat 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.jboss.netty.channel.rxtx;

import gnu.io.SerialPort;

import org.jboss.netty.channel.DefaultChannelConfig;
import org.jboss.netty.util.internal.ConversionUtil;

/**
* A configuration class for RXTX device connections.
*
* @author Daniel Bimschas
* @author Dennis Pfisterer
*/
public class RXTXChannelConfig extends DefaultChannelConfig {

public static enum Stopbits {

STOPBITS_1(SerialPort.STOPBITS_1),
STOPBITS_2(SerialPort.STOPBITS_2),
STOPBITS_1_5(SerialPort.STOPBITS_1_5);

private int value;

private Stopbits(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static Stopbits ofValue(int value) {
for (Stopbits stopbit : Stopbits.values()) {
if (stopbit.value == value) {
return stopbit;
}
}
throw new IllegalArgumentException("Unknown value for Stopbits: " + value + ".");
}
}

public static enum Databits {

DATABITS_5(SerialPort.DATABITS_5),
DATABITS_6(SerialPort.DATABITS_6),
DATABITS_7(SerialPort.DATABITS_7),
DATABITS_8(SerialPort.DATABITS_8);

private int value;

private Databits(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static Databits ofValue(int value) {
for (Databits databit : Databits.values()) {
if (databit.value == value) {
return databit;
}
}
throw new IllegalArgumentException("Unknown value for Databits: " + value + ".");
}
}

public static enum Paritybit {

NONE(SerialPort.PARITY_NONE),
ODD(SerialPort.PARITY_ODD),
EVEN(SerialPort.PARITY_EVEN),
MARK(SerialPort.PARITY_MARK),
SPACE(SerialPort.PARITY_SPACE);

private int value;

private Paritybit(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static Paritybit ofValue(int value) {
for (Paritybit paritybit : Paritybit.values()) {
if (paritybit.value == value) {
return paritybit;
}
}
throw new IllegalArgumentException("Unknown value for paritybit: " + value + ".");
}
}

private int baudrate = 115200;

private boolean dtr = false;

private boolean rts = false;

private Stopbits stopbits = RXTXChannelConfig.Stopbits.STOPBITS_1;

private Databits databits = RXTXChannelConfig.Databits.DATABITS_8;

private Paritybit paritybit = RXTXChannelConfig.Paritybit.NONE;

public RXTXChannelConfig() {
// work with defaults ...
}

public RXTXChannelConfig(final int baudrate, final boolean dtr, final boolean rts, final Stopbits stopbits,
final Databits databits, final Paritybit paritybit) {
this.baudrate = baudrate;
this.dtr = dtr;
this.rts = rts;
this.stopbits = stopbits;
this.databits = databits;
this.paritybit = paritybit;
}

@Override
public boolean setOption(final String key, final Object value) {
if (key.equals("baudrate")) {
setBaudrate(ConversionUtil.toInt(value));
return true;
} else if (key.equals("stopbits")) {
setStopbits((Stopbits) value);
return true;
} else if (key.equals("databits")) {
setDatabits((Databits) value);
return true;
} else if (key.equals("paritybit")) {
setParitybit((Paritybit) value);
return true;
} else {
return super.setOption(key, value);
}
}

public void setBaudrate(final int baudrate) {
this.baudrate = baudrate;
}

public void setStopbits(final Stopbits stopbits) {
this.stopbits = stopbits;
}

public void setDatabits(final Databits databits) {
this.databits = databits;
}

private void setParitybit(final Paritybit paritybit) {
this.paritybit = paritybit;
}

public int getBaudrate() {
return baudrate;
}

public Stopbits getStopbits() {
return stopbits;
}

public Databits getDatabits() {
return databits;
}

public Paritybit getParitybit() {
return paritybit;
}

public boolean isDtr() {
return dtr;
}

public void setDtr(final boolean dtr) {
this.dtr = dtr;
}

public boolean isRts() {
return rts;
}

public void setRts(final boolean rts) {
this.rts = rts;
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/jboss/netty/channel/rxtx/RXTXChannelFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2011 Red Hat, Inc.
*
* Red Hat 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.jboss.netty.channel.rxtx;


import java.util.concurrent.ExecutorService;

import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.ChannelGroupFuture;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.util.internal.ExecutorUtil;

/**
* A {@link ChannelFactory} for creating {@link RXTXChannel} instances.
*
* @author Daniel Bimschas
* @author Dennis Pfisterer
*/
public class RXTXChannelFactory implements ChannelFactory {

private final ChannelGroup channels = new DefaultChannelGroup("RXTXChannelFactory-ChannelGroup");

private final ExecutorService executor;

public RXTXChannelFactory(ExecutorService executor) {
this.executor = executor;
}

@Override
public Channel newChannel(final ChannelPipeline pipeline) {
RXTXChannelSink sink = new RXTXChannelSink(executor);
RXTXChannel channel = new RXTXChannel(null, this, pipeline, sink);
sink.setChannel(channel);
channels.add(channel);
return channel;
}

@Override
public void releaseExternalResources() {
ChannelGroupFuture close = channels.close();
close.awaitUninterruptibly();
ExecutorUtil.terminate(executor);
}
}
Loading

0 comments on commit 4144b43

Please sign in to comment.