2012-04-26 5 views
2

すでにTCP/IPに実装されているサーバーがありますが、UDPをサポートするためのプロトコルが必要です。UDPデータグラムごとに異なるネットワークのパイプ

送信された各UDPデータグラムには、デコードする必要があるすべてのデータグラムが含まれているため、データグラム内のデータが改行で区切られた非常に簡単な応答および応答システムです。

サーバが起動されるブートストラップのためのコードを以下に示す:

//SETUP UDP SERVER 
    DatagramChannelFactory udpFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool()); 

    ConnectionlessBootstrap udpBootstrap = new ConnectionlessBootstrap(udpFactory); 

    udpBootstrap.setOption("sendBufferSize", 65536); 
    udpBootstrap.setOption("receiveBufferSize", 65536); 
    udpBootstrap.setOption("receiveBufferSizePredictorFactory", new AdaptiveReceiveBufferSizePredictorFactory()); 

    udpBootstrap.setOption("broadcast", "true"); 
    udpBootstrap.setPipelineFactory(new ServerPipeLineFactoryUDP()); 
    udpBootstrap.bind(new InetSocketAddress(hostIp, 4000)); 

パイプラインコード:

class ServerPipeLineFactoryUDP implements ChannelPipelineFactory 
{ 

    private final static ExecutionHandler EXECUTION_HANDLER = new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(ScorpionFMS.THREAD_POOL_COUNT, 0, 0)); 

    public ServerPipeLineFactoryUDP() 
    { 

    } 

    @Override 
    public ChannelPipeline getPipeline() throws Exception 
    { 

    ChannelPipeline pipeline = pipeline(); 
    pipeline.addLast("debugup", new DebugUpstreamHandler("UDP")); 
    pipeline.addLast("debugdown", new DebugDownstreamHandler("UDP")); 

    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(256, Delimiters.lineDelimiter())); 

    pipeline.addLast("decoder", new UDPRequestDecoder(true)); 
    pipeline.addLast("encoder", new StringEncoder()); 
    pipeline.addLast("executor", EXECUTION_HANDLER); 
    pipeline.addLast("handler", new UDPRequestHandler(

    return pipeline; 
    } 
} 

有する問題Imは各データグラムが同一のインスタンスを使用しているありますこのパイプライン(私は各データグラムがパイプラインの新しいインスタンスを使用することを望んでいました)ので、データグラムの内容を処理している間に保存した状態はすべて保存され、次のデータグラムもそれを使用します自分のチャンネル、それゆえに私自身の

これは、ドキュメントを読むことで期待される動作だと思いますが、Nettyに各データグラムのパイプラインを再作成させるにはどうしたらいいですか?または、私はこれについて完全に間違った方法をとっていますか?

簡潔に言えば、私は各データグラムは、パイプラインの新しいインスタンスを持っていると思います(TCPと同じ)

+0

*状態*をハンドラに保存するのはなぜですか?あなたのメッセージングの種類のための接続の概念が必要ですか?まず、あなたがそれを必要としないように思えますが、後であなたの心が変わっているように見えます(あなたがそれを必要としないなら、なぜあなたはそれを保管しますか)。 。 。 – MartinK

答えて

5

私がIRCで言ったように、私はあなたが望むやり方をするか、少なくともあなたに何か考えを与えることができると思います。

public class Example { 

    public static void main(String[] args) { 
     final ChannelPipelineHandlerImpl perDatagramFactory = new ChannelPipelineHandlerImpl(); 

     DatagramChannelFactory udpFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool()); 

     ConnectionlessBootstrap udpBootstrap = new ConnectionlessBootstrap(udpFactory); 

     udpBootstrap.setPipelineFactory(new ChannelPipelineFactory() { 

      public ChannelPipeline getPipeline() throws Exception { 
       return Channels.pipeline(new DistinctChannelPipelineHandler(perDatagramFactory)); 
      } 
     }); 

    } 

    private static final class DistinctChannelPipelineHandler implements ChannelDownstreamHandler, ChannelUpstreamHandler { 
     private ChannelPipelineFactory factory; 

     public DistinctChannelPipelineHandler(ChannelPipelineFactory factory) { 
      this.factory = factory; 
     } 

     public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { 
      ChannelPipeline pipeline = factory.getPipeline(); 
      pipeline.attach(ctx.getChannel(), ctx.getPipeline().getSink()); 
      pipeline.sendUpstream(e); 

      ctx.sendUpstream(e); 

     } 

     public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { 
      ChannelPipeline pipeline = factory.getPipeline(); 
      pipeline.attach(ctx.getChannel(), ctx.getPipeline().getSink()); 
      pipeline.sendDownstream(e); 

      ctx.sendDownstream(e); 
     } 

    } 

    private static final class ChannelPipelineHandlerImpl implements ChannelPipelineFactory { 

     public ChannelPipeline getPipeline() throws Exception { 
      // Add your handlers here 
      return Channels.pipeline(); 
     } 

    } 
} 
+0

いくつかの変更を加えれば、それはまさに私が必要としていたものでした。再度、感謝します! TCP/IPとUDPが完璧に並んでいます。 –

+0

クール..フィードバックに感謝! –

+2

@NormanMaurer Netty 4にも同様の例がありますか?それは非常に有用な発見! – Abe

0

私はUDPチャネルがどのように扱われるかわからないんだけど、チャネルが異なっている場合データグラムごとに、ChannelLocalに状態を保存することができます。

+0

残念ながら、問題はチャネルがデータグラムごとに区別されていないことです、達成しようとしていることです。 –

関連する問題