Grizzly NIO框架 理论篇 【4】
Port Unification Overview(端口统一)
针对在一个端口上开放多种协议的服务,类似下图:
![""](http://images0.cnblogs.com/blog/619301/201409/040946036578184.png)
通过Port unification Filter去判断三种协议,然后针对不同协议传递到下一个Filter执行后续工作。
其中Finder的Grizzly官方给了一个简单用例,针对协议包头是"add"的Finder
/**
* {@link ProtocolFinder}, responsible to determine if incoming byte buffer
* represents ADD-service request.
*/
publicclassAddProtocolFinderimplementsProtocolFinder{
privatefinalstaticbyte[] magic ={'a','d','d'};
/**
* {@inheritDoc}
*/
@Override
publicResult find(finalPUContext puContext,finalFilterChainContext ctx){
// Get the input Buffer
finalBuffer inputBuffer = ctx.getMessage();
finalint bytesToCompare =Math.min(magic.length, inputBuffer.remaining());
finalint bufferStart = inputBuffer.position();
// Compare incoming bytes with ADD-service protocol magic
for(int i =0; i < bytesToCompare; i++){
if(magic[i]!= inputBuffer.get(bufferStart + i)){
// If at least one byte doesn't match - it's not ADD-service protocol
returnResult.NOT_FOUND;
}
}
// if we check entire magic - return FOUND, or NEED_MORE_DATA otherwise
return bytesToCompare == magic.length ?
Result.FOUND :Result.NEED_MORE_DATA;
}
}
这个简单例子应该明白怎么判断了吧,然后就是转到对应的协议
-------------------------------
理论学习暂且到此为止,水平实在有限,本人仅使用了基础功能,所以大部分扩展都没有去学习,希望有高人可以补充