Californium : Coap for java
1. CoapEndpoint.java
成员变量有CoapStack:
/** The stack of layers that make up the CoAP protocol */
protected final CoapStack coapstack;
/**
* Standard coap-stack-factory.
*
* This will be also the default, if no other default gets provided with
* {@link #setDefaultCoapStackFactory(CoapStackFactory)}. If an other
* default factory is used, this one may be used to build a standard
* coap-stack on demand.
*/
public static final CoapStackFactory STANDARD_COAP_STACK_FACTORY = new CoapStackFactory() {
public CoapStack createCoapStack(String protocol, NetworkConfig config, Outbox outbox, Object customStackArgument) {
if (CoAP.isTcpProtocol(protocol)) {
return new CoapTcpStack(config, outbox);
} else {
return new CoapUdpStack(config, outbox);
}
}
};
/**
* Creates a new stack for UDP as the transport.
*
* @param config The configuration values to use.
* @param outbox The adapter for submitting outbound messages to the transport.
*/
public CoapUdpStack(final NetworkConfig config, final Outbox outbox) {
super(outbox);
Layer layers[] = new Layer[] {
createExchangeCleanupLayer(config),
createObserveLayer(config),
createBlockwiseLayer(config),
createReliabilityLayer(config)};
setLayers(layers);
}
2. Layer相关类、结构
AbstractLayer.java
通过成员变量upperLayer、lowerLayer,把coap协议栈上下层关系,串联起来(双向关系);
接收数据时,从coap协议栈,自下而上处理;
发送数据时,从coap协议栈,自上而下处理;
(其中,单向处理,有点像责任链模式,不过,责任链模式的单个Handler环节,只有单向关联关系;这里的协议栈单个layer,有双向关联关系)
3. 逻辑结构
coap协议栈的设计模式,挺巧妙的,把coap协议层的解析,很形象的进行分层拆分;
(后面在分析其他开源协议解析框架时,可以作为对比;)
TODO coap协议栈每层layer的功能
参考: