From fe3a480fb9ebfbb6698a9ddc4636943e7f4533d9 Mon Sep 17 00:00:00 2001 From: Jestan Nirojan Date: Sat, 11 Feb 2012 18:48:06 +0530 Subject: [PATCH] 1)renamed sctp payload to sctp frame 2)added sctp codec, handler classes --- .../netty/example/sctp/SctpClientHandler.java | 4 +- transport-sctp/pom.xml | 5 ++ .../netty/channel/sctp/SctpChannelImpl.java | 5 +- .../sctp/{SctpPayload.java => SctpFrame.java} | 10 ++-- .../channel/sctp/SctpSendBufferPool.java | 8 +-- .../io/netty/channel/sctp/SctpWorker.java | 2 +- .../codec/DefaultSctpWriteStreamSelector.java | 25 ++++++++ .../channel/sctp/codec/SctpFrameDecoder.java | 37 ++++++++++++ .../channel/sctp/codec/SctpFrameEncoder.java | 59 +++++++++++++++++++ .../sctp/codec/SctpWriteStreamSelector.java | 22 +++++++ .../channel/sctp/codec/package-info.java | 21 +++++++ .../handler/SimpleSctpChannelHandler.java | 50 ++++++++++++++++ .../handler/SimpleSctpDownstreamHandler.java | 24 ++++++++ .../handler/SimpleSctpUpstreamHandler.java | 47 +++++++++++++++ .../channel/sctp/handler/package-info.java | 21 +++++++ 15 files changed, 325 insertions(+), 15 deletions(-) rename transport-sctp/src/main/java/io/netty/channel/sctp/{SctpPayload.java => SctpFrame.java} (92%) create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/codec/DefaultSctpWriteStreamSelector.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameDecoder.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameEncoder.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpWriteStreamSelector.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/codec/package-info.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpChannelHandler.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpDownstreamHandler.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpUpstreamHandler.java create mode 100644 transport-sctp/src/main/java/io/netty/channel/sctp/handler/package-info.java diff --git a/example/src/main/java/io/netty/example/sctp/SctpClientHandler.java b/example/src/main/java/io/netty/example/sctp/SctpClientHandler.java index 561fda3d69..4c0d3b62f4 100644 --- a/example/src/main/java/io/netty/example/sctp/SctpClientHandler.java +++ b/example/src/main/java/io/netty/example/sctp/SctpClientHandler.java @@ -25,7 +25,7 @@ import io.netty.channel.ChannelStateEvent; import io.netty.channel.ExceptionEvent; import io.netty.channel.MessageEvent; import io.netty.channel.SimpleChannelUpstreamHandler; -import io.netty.channel.sctp.SctpPayload; +import io.netty.channel.sctp.SctpFrame; /** * Handler implementation for the echo client. It initiates the message @@ -47,7 +47,7 @@ public class SctpClientHandler extends SimpleChannelUpstreamHandler { */ @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent stateEvent) { - stateEvent.getChannel().write(new SctpPayload(0, 0, ChannelBuffers.wrappedBuffer("SCTP ECHO".getBytes()))); + stateEvent.getChannel().write(new SctpFrame(0, 0, ChannelBuffers.wrappedBuffer("SCTP ECHO".getBytes()))); } @Override diff --git a/transport-sctp/pom.xml b/transport-sctp/pom.xml index c14941d722..95aab04293 100644 --- a/transport-sctp/pom.xml +++ b/transport-sctp/pom.xml @@ -35,5 +35,10 @@ netty-transport ${project.version} + + ${project.groupId} + netty-codec + ${project.version} + diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpChannelImpl.java b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpChannelImpl.java index 7e6498b231..187197f790 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpChannelImpl.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpChannelImpl.java @@ -29,7 +29,6 @@ import java.util.concurrent.atomic.AtomicInteger; import com.sun.nio.sctp.Association; -import io.netty.buffer.ChannelBuffer; import io.netty.channel.AbstractChannel; import io.netty.channel.Channel; import io.netty.channel.ChannelFactory; @@ -297,8 +296,8 @@ class SctpChannelImpl extends AbstractChannel implements SctpChannel { private int getMessageSize(MessageEvent e) { Object m = e.getMessage(); - if (m instanceof SctpPayload) { - return ((SctpPayload) m).getPayloadBuffer().readableBytes(); + if (m instanceof SctpFrame) { + return ((SctpFrame) m).getPayloadBuffer().readableBytes(); } return 0; } diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpPayload.java b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpFrame.java similarity index 92% rename from transport-sctp/src/main/java/io/netty/channel/sctp/SctpPayload.java rename to transport-sctp/src/main/java/io/netty/channel/sctp/SctpFrame.java index e773a34083..ddea344751 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpPayload.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpFrame.java @@ -20,20 +20,20 @@ import io.netty.buffer.ChannelBuffers; /** */ -public final class SctpPayload { +public final class SctpFrame { private final int streamIdentifier; private final int protocolIdentifier; private final ChannelBuffer payloadBuffer; /** * Essential data that is being carried within SCTP Data Chunk - * @param streamIdentifier that you want to send the payload * @param protocolIdentifier of payload + * @param streamIdentifier that you want to send the payload * @param payloadBuffer channel buffer */ - public SctpPayload(int streamIdentifier, int protocolIdentifier, ChannelBuffer payloadBuffer) { - this.streamIdentifier = streamIdentifier; + public SctpFrame(int protocolIdentifier, int streamIdentifier, ChannelBuffer payloadBuffer) { this.protocolIdentifier = protocolIdentifier; + this.streamIdentifier = streamIdentifier; this.payloadBuffer = payloadBuffer; } @@ -56,7 +56,7 @@ public final class SctpPayload { @Override public String toString() { return new StringBuilder(). - append("SctpPayload{"). + append("SctpFrame{"). append("streamIdentifier="). append(streamIdentifier). append(", protocolIdentifier="). diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpSendBufferPool.java b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpSendBufferPool.java index 0ff9600116..c920195414 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpSendBufferPool.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpSendBufferPool.java @@ -39,15 +39,15 @@ final class SctpSendBufferPool { } SendBuffer acquire(Object message) { - if (message instanceof SctpPayload) { - return acquire((SctpPayload) message); + if (message instanceof SctpFrame) { + return acquire((SctpFrame) message); } else { throw new IllegalArgumentException( - "unsupported message type: " + message.getClass() + " required io.netty.channel.sctp.SctpPayload instance"); + "unsupported message type: " + message.getClass() + " required io.netty.channel.sctp.SctpFrame instance"); } } - private SendBuffer acquire(SctpPayload message) { + private SendBuffer acquire(SctpFrame message) { final ChannelBuffer src = message.getPayloadBuffer(); final int streamNo = message.getStreamIdentifier(); final int protocolId = message.getProtocolIdentifier(); diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpWorker.java b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpWorker.java index 4b0df5eff4..3f01e3a9f5 100644 --- a/transport-sctp/src/main/java/io/netty/channel/sctp/SctpWorker.java +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/SctpWorker.java @@ -343,7 +343,7 @@ class SctpWorker implements Runnable { // Fire the event. fireMessageReceived(channel, - new SctpPayload(messageInfo.streamNumber(), + new SctpFrame(messageInfo.streamNumber(), messageInfo.payloadProtocolID(), buffer), messageInfo.address()); diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/codec/DefaultSctpWriteStreamSelector.java b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/DefaultSctpWriteStreamSelector.java new file mode 100644 index 0000000000..f089bf5164 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/DefaultSctpWriteStreamSelector.java @@ -0,0 +1,25 @@ +/* + * Copyright 2011 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.channel.sctp.codec; + +import io.netty.channel.sctp.SctpChannel; + +public class DefaultSctpWriteStreamSelector implements SctpWriteStreamSelector { + @Override + public int streamIdentifier(SctpChannel sctpChannel, Object msg) { + return 1; + } +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameDecoder.java b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameDecoder.java new file mode 100644 index 0000000000..4722e91b77 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameDecoder.java @@ -0,0 +1,37 @@ +/* + * Copyright 2011 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.channel.sctp.codec; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.sctp.SctpFrame; +import io.netty.handler.codec.oneone.OneToOneDecoder; + +/** + * SCTP Frame Decoder which just extract payload channel buffer + */ + +public class SctpFrameDecoder extends OneToOneDecoder { + + @Override + protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { + if (!(msg instanceof SctpFrame)) { + return msg; + } + + return ((SctpFrame) msg).getPayloadBuffer(); + } +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameEncoder.java b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameEncoder.java new file mode 100644 index 0000000000..7864cbbd71 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpFrameEncoder.java @@ -0,0 +1,59 @@ +/* + * Copyright 2011 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.channel.sctp.codec; + +import io.netty.buffer.ChannelBuffer; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.sctp.SctpChannel; +import io.netty.channel.sctp.SctpFrame; +import io.netty.handler.codec.oneone.OneToOneEncoder; + +/** + * SCTP Frame Encoder which encode a channel buffer to SctpFrame object + */ +public class SctpFrameEncoder extends OneToOneEncoder { + + private final int protocolIdentifier; + private final SctpWriteStreamSelector sctpWriteStreamSelector; + + public SctpFrameEncoder() { + this(0); + } + + public SctpFrameEncoder(int protocolIdentifier) { + this(protocolIdentifier, new DefaultSctpWriteStreamSelector()); + } + + public SctpFrameEncoder(final int protocolIdentifier, final SctpWriteStreamSelector sctpWriteStreamSelector) { + if (sctpWriteStreamSelector == null) { + throw new NullPointerException("sctpWriteStreamSelector"); + } + this.protocolIdentifier = Math.max(0, protocolIdentifier); + this.sctpWriteStreamSelector = sctpWriteStreamSelector; + } + + @Override + protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { + if (!(msg instanceof ChannelBuffer)) { + return msg; + } else { + SctpChannel sctpChannel = (SctpChannel) channel; + final int streamIdentifier = sctpWriteStreamSelector.streamIdentifier(sctpChannel, msg); + return new SctpFrame(protocolIdentifier, streamIdentifier, (ChannelBuffer) msg); + } + } +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpWriteStreamSelector.java b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpWriteStreamSelector.java new file mode 100644 index 0000000000..54574889f6 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/SctpWriteStreamSelector.java @@ -0,0 +1,22 @@ +/* + * Copyright 2011 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.channel.sctp.codec; + +import io.netty.channel.sctp.SctpChannel; + +public interface SctpWriteStreamSelector { + int streamIdentifier(SctpChannel sctpChannel, Object msg); +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/codec/package-info.java b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/package-info.java new file mode 100644 index 0000000000..25918fec9e --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/codec/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2011 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. + */ + +/** + * NIO-based socket channel + * API implementation - recommended for a large number of connections (>= 1000). + */ +package io.netty.channel.sctp.codec; diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpChannelHandler.java b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpChannelHandler.java new file mode 100644 index 0000000000..1a14464999 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpChannelHandler.java @@ -0,0 +1,50 @@ +/* + * Copyright 2011 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.channel.sctp.handler; + +import io.netty.channel.ChannelEvent; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelHandler; +import io.netty.channel.sctp.SctpNotificationEvent; +import io.netty.logging.InternalLogger; +import io.netty.logging.InternalLoggerFactory; + +/** + * SCTP Channel Handler (upstream + downstream) with SCTP notification handling + */ + +public class SimpleSctpChannelHandler extends SimpleChannelHandler { + + private static final InternalLogger logger = + InternalLoggerFactory.getInstance(SimpleSctpUpstreamHandler.class.getName()); + + + @Override + public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent event) throws Exception { + if (!(event instanceof SctpNotificationEvent)) { + super.handleUpstream(ctx, event); + + } + if (event instanceof SctpNotificationEvent) { + sctpNotificationReceived(ctx, (SctpNotificationEvent) event); + } + } + + public void sctpNotificationReceived(ChannelHandlerContext ctx, SctpNotificationEvent event) { + ctx.sendUpstream(event); + } + +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpDownstreamHandler.java b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpDownstreamHandler.java new file mode 100644 index 0000000000..7a70491a37 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpDownstreamHandler.java @@ -0,0 +1,24 @@ +/* + * Copyright 2011 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.channel.sctp.handler; + +import io.netty.channel.SimpleChannelDownstreamHandler; + +/** + * Sctp Downstream handler for sake of completeness + */ +public class SimpleSctpDownstreamHandler extends SimpleChannelDownstreamHandler { +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpUpstreamHandler.java b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpUpstreamHandler.java new file mode 100644 index 0000000000..cf69c86301 --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/SimpleSctpUpstreamHandler.java @@ -0,0 +1,47 @@ +/* + * Copyright 2011 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.channel.sctp.handler; + +import io.netty.channel.ChannelEvent; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelUpstreamHandler; +import io.netty.channel.sctp.SctpNotificationEvent; +import io.netty.logging.InternalLogger; +import io.netty.logging.InternalLoggerFactory; + +/** + * SCTP Upstream Channel Handler with SCTP notification handling + */ +public class SimpleSctpUpstreamHandler extends SimpleChannelUpstreamHandler { + private static final InternalLogger logger = + InternalLoggerFactory.getInstance(SimpleSctpUpstreamHandler.class.getName()); + + + @Override + public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent event) throws Exception { + if (!(event instanceof SctpNotificationEvent)) { + super.handleUpstream(ctx, event); + + } + if (event instanceof SctpNotificationEvent) { + sctpNotificationReceived(ctx, (SctpNotificationEvent) event); + } + } + + public void sctpNotificationReceived(ChannelHandlerContext ctx, SctpNotificationEvent event) { + ctx.sendUpstream(event); + } +} diff --git a/transport-sctp/src/main/java/io/netty/channel/sctp/handler/package-info.java b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/package-info.java new file mode 100644 index 0000000000..790d7ec30b --- /dev/null +++ b/transport-sctp/src/main/java/io/netty/channel/sctp/handler/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2011 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. + */ + +/** + * NIO-based socket channel + * API implementation - recommended for a large number of connections (>= 1000). + */ +package io.netty.channel.sctp.handler;