1)renamed sctp payload to sctp frame 2)added sctp codec, handler classes

This commit is contained in:
Jestan Nirojan 2012-02-11 18:48:06 +05:30
parent a29d887c34
commit fe3a480fb9
15 changed files with 325 additions and 15 deletions

View File

@ -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

View File

@ -35,5 +35,10 @@
<artifactId>netty-transport</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-codec</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -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;
}

View File

@ -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=").

View File

@ -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();

View File

@ -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());

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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.
*/
/**
* <a href="http://en.wikipedia.org/wiki/New_I/O">NIO</a>-based socket channel
* API implementation - recommended for a large number of connections (&gt;= 1000).
*/
package io.netty.channel.sctp.codec;

View File

@ -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);
}
}

View File

@ -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 {
}

View File

@ -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);
}
}

View File

@ -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.
*/
/**
* <a href="http://en.wikipedia.org/wiki/New_I/O">NIO</a>-based socket channel
* API implementation - recommended for a large number of connections (&gt;= 1000).
*/
package io.netty.channel.sctp.handler;