Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SipMessageStreamDecoder needs to be updated to work with the latest pkts.sip code #5

Open
joesaby opened this issue Nov 20, 2018 · 3 comments

Comments

@joesaby
Copy link

joesaby commented Nov 20, 2018

I am not sure if there is a plan to update the versioning dependency with pkts-sip lib. If thats done, the example UAS, UAC will need to be updated because of the change in the builder patterns.

SipMessageStreamDecoder needs to be updated to work with the latest pkts.sip code. I put a small workaround in my repo, putting it in here for my own tracking

/**
 * 
 */
package io.sipstack.netty.codec.sip;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.pkts.packet.sip.impl.*;
import java.net.InetSocketAddress;
import java.util.List;

/**
 * @author jonas
 * 
 */
public class SipMessageStreamDecoder extends ByteToMessageDecoder {

    private final Clock clock;

    private SipMessageStreamBuilder builder;

    public SipMessageStreamDecoder(final Clock clock) {
        this.clock = clock;
        reset();
    }

    public SipMessageStreamDecoder() {
        this(new SystemClock());
    }

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

    @Override
    protected void decode(final ChannelHandlerContext ctx, final ByteBuf buffer, final List<Object> out)
            throws Exception {
        byte[] bytes = new byte[buffer.readableBytes()];
        buffer.readBytes(bytes);
        builder.process(bytes);
        if (builder.isDone()){
            long arrivalTime = this.clock.getCurrentTimeMillis();
            final Channel channel = ctx.channel();
            final Connection connection = new TcpConnection(channel, (InetSocketAddress) channel.remoteAddress());
            out.add(new DefaultSipMessageEvent(connection, builder.build(), arrivalTime));
            while (builder.hasUnprocessData()){
                if (builder.process()) {
                    out.add(new DefaultSipMessageEvent(connection, builder.build(), arrivalTime));
                    reset();
                }
            }
        }
    }

    private void reset() {
        final SipMessageStreamBuilder.Configuration config = new SipMessageStreamBuilder.DefaultConfiguration();
        this.builder = new SipMessageStreamBuilder(config);
    }

}
@joesaby
Copy link
Author

joesaby commented Nov 20, 2018

I didn't get around understanding concurrency issues with using SipMessageStreamBuilder since I haven't made myself overly familiar with netty & pkts libraries. I am guessing the Buffer library is queue based so that a subsequent call of a decode doesn't pollute the internals of SipMessageStreamBuilder from a previous decode thread. Again haven't fully got grips with the threading architecture too to fully comprehend if there is locking to prevent multiple decodes in a pipeline.

@jonbo372
Copy link
Collaborator

Hey again,

As I mentioned in my other reply, I haven't found any time to work on stipstack.io in quite some time and not sure when I'll have time again. As far as the threading goes, this has to do with how Netty is functioning and the SipMessageStreamDecoder is one per channel pipeline, which is guaranteed to only be used by a single thread. Netty doesn't have great documentation but there are some info regarding that on their site (or if I found it elsewhere - or just step debuged at some point, dont remember!)

@joesaby
Copy link
Author

joesaby commented Nov 27, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants