Port Unification Overview(端口统一)
针对在一个端口上开放多种协议的服务,类似下图:
通过Port unification Filter去判断三种协议,然后针对不同协议传递到下一个Filter执行后续工作。
其中Finder的Grizzly官方给了一个简单用例,针对协议包头是"add"的Finder
-
/** -
* {@link ProtocolFinder}, responsible to determine if incoming byte buffer -
* represents ADD-service request. -
*/ -
public class AddProtocolFinder implements ProtocolFinder { -
-
private final static byte [] magic = { 'a' , 'd' , 'd' }; -
-
/** -
* {@inheritDoc} -
*/ -
@Override -
public Result find ( final PUContext puContext , final FilterChainContext ctx ) { -
// Get the input Buffer -
final Buffer inputBuffer = ctx . getMessage (); -
-
final int bytesToCompare = Math . min ( magic . length , inputBuffer . remaining ()); -
-
final int 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 -
return Result . NOT_FOUND ; -
} -
} -
-
// if we check entire magic - return FOUND, or NEED_MORE_DATA otherwise -
return bytesToCompare == magic . length ? -
Result . FOUND : Result . NEED_MORE_DATA ; -
} -
-
}
这个简单例子应该明白怎么判断了吧,然后就是转到对应的协议
-------------------------------
理论学习暂且到此为止,水平实在有限,本人仅使用了基础功能,所以大部分扩展都没有去学习,希望有高人可以补充

