私のプロトコルの一部として、新しい接続が確立されたときにクライアントにバージョン番号を送信してもらいたいです。これはパイプラインの別のハンドラーで実行したいので、これはかなり基本的な質問かもしれませんが、どうすればよいかわかりませんので、ご容赦ください。もう 1 つは、接続 (パイプライン) を介して POJO を前後に送信できるようにしたいということです。また、認証ハンドラーを追加したいと思います。とにかく、現在、何らかのエラーが発生しています。これは、バージョン チェックがパイプラインから適切に消化されていないためだと確信しています。
基本的に、以下のコードは、接続が確立されたときにバージョンがチェックされた後にサーバーが出力する「Hello World」を送信するように設定されています。少なくとも理論的には、実際にはこれはうまくいきません ;)
現在私は持っています:
クライアント.java
public static void main(String[] args)
{
...
// Set up the pipeline factory.
bootstrap.setPipelineFactory(new ChannelPipelineFactory()
{
@Override
public ChannelPipeline getPipeline() throws Exception
{
return Channels.pipeline(
new ObjectEncoder(),
new ObjectDecoder(),
new VersionClientHandler(),
new BusinessLogicClientHandler());
}
});
...
// The idea is that it will all be request and response. Much like http but with pojo's.
ChannelFuture lastWriteFuture = channel.write("Hello world".getBytes());
if (lastWriteFuture != null)
{
System.out.println("waiting for message to be sent");
lastWriteFuture.awaitUninterruptibly();
}
...
}
VersionClientHandler.java
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
{
ChannelBuffer versionBuffer = ChannelBuffers.buffer(VERSION_STRING_LENGTH);
versionBuffer.writeBytes("v123.45a".getBytes());
// If I understand correctly, the next line says use the rest of the stream to do what you need to the next Handler in the pipeline?
Channels.write(ctx, e.getFuture(), versionBuffer);
}
BusinessLogicClientHandler.java
Not really doing anything at this point. Should it?
サーバー.java
public static void main(String[] args)
{
...
public ChannelPipeline getPipeline() throws Exception
{
return Channels.pipeline(
new ObjectEncoder(),
new ObjectDecoder(),
new VersionServerHandler(),
new BusinessLogicServerHandler());
}
...
}
VersionServerHandler.java
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
{
ChannelBuffer versionBuffer = ChannelBuffers.buffer(VERSION_NUMBER_MAX_SIZE);
System.out.println("isReadable - messageReceived: " + versionBuffer.readable()); // returns false???
// Basically I want to read it and confirm the client and server versions match.
// And if the match fails either send a message or throw an exception
// How do I also pass on the stream to the next Handler?
}
BusinessLogicServerHandler.java
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
{
e.getMessage();
byte[] message = (byte[])e.getMessage(); // "Hello World" in byte[] from Client.java
}
基本的に私が望むのは、通信プロトコルの一部としてチャネルが接続されたときにバージョン番号が渡され、検証されることです。すべてが舞台裏で自動的に行われます。同様に、この方法で認証メカニズムを渡したいと思います。
セキュア チャットの例でやりたかったことと多少似ているコードを見たことがありますが、実際には理解できませんでした。このコードをセットアップする方法についてのヘルプは本当にありがたいです. 1 つの大規模なハンドラーですべてを実行できることはわかっていますが、それがパイプラインのポイントであり、論理的に意味のある単位に分割することです。