Skip to content

Commit

Permalink
Use CoapResponse to keep transport context in SeparateResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
akolosov-n committed Aug 19, 2024
1 parent dbe7323 commit 3403185
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private CoapResponse(Code code, Opaque payload, HeaderOptions options, Transport
this.code = code;
this.payload = Objects.requireNonNull(payload);
this.options = Objects.requireNonNull(options);
this.transContext = transContext;
this.transContext = Objects.requireNonNull(transContext);
}

// --- STATIC CONSTRUCTORS ---
Expand Down Expand Up @@ -101,8 +101,9 @@ public String getPayloadString() {
return payload.toUtf8String();
}

@Deprecated
public SeparateResponse toSeparate(Opaque token, InetSocketAddress peerAddress, TransportContext transContext) {
return new SeparateResponse(this, token, peerAddress, transContext);
return new SeparateResponse(this.withContext(transContext), token, peerAddress);
}

public SeparateResponse toSeparate(Opaque token, InetSocketAddress peerAddress) {
Expand Down
23 changes: 13 additions & 10 deletions coap-core/src/main/java/com/mbed/coap/packet/SeparateResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ public class SeparateResponse {
private final CoapResponse response;
private final Opaque token;
private final InetSocketAddress peerAddress;
private final TransportContext transContext;

public SeparateResponse(CoapResponse response, Opaque token, InetSocketAddress peerAddress, TransportContext transContext) {
public SeparateResponse(CoapResponse response, Opaque token, InetSocketAddress peerAddress) {
this.response = Objects.requireNonNull(response);
this.token = Objects.requireNonNull(token);
this.peerAddress = peerAddress;
this.transContext = Objects.requireNonNull(transContext);
}

@Deprecated
public SeparateResponse(CoapResponse response, Opaque token, InetSocketAddress peerAddress, TransportContext transContext) {
this(response.withContext(transContext), token, peerAddress);
}

@Override
Expand All @@ -46,15 +49,15 @@ public InetSocketAddress getPeerAddress() {
}

public TransportContext getTransContext() {
return transContext;
return response.getTransContext();
}

public <T> T getTransContext(TransportContext.Key<T> key) {
return transContext.get(key);
return response.getTransContext().get(key);
}

public <T> T getTransContext(TransportContext.Key<T> key, T defaultValue) {
return transContext.getOrDefault(key, defaultValue);
return response.getTransContext().getOrDefault(key, defaultValue);
}

public Code getCode() {
Expand Down Expand Up @@ -82,19 +85,19 @@ public boolean equals(Object o) {
return false;
}
SeparateResponse that = (SeparateResponse) o;
return Objects.equals(response, that.response) && Objects.equals(token, that.token) && Objects.equals(peerAddress, that.peerAddress) && Objects.equals(transContext, that.transContext);
return Objects.equals(response, that.response) && Objects.equals(token, that.token) && Objects.equals(peerAddress, that.peerAddress);
}

@Override
public int hashCode() {
return Objects.hash(response, token, peerAddress, transContext);
return Objects.hash(response, token, peerAddress);
}

public SeparateResponse withPayload(Opaque newPayload) {
return new SeparateResponse(response.withPayload(newPayload), token, peerAddress, transContext);
return new SeparateResponse(response.withPayload(newPayload), token, peerAddress);
}

public SeparateResponse duplicate() {
return new SeparateResponse(CoapResponse.of(response.getCode(), response.getPayload(), response.options().duplicate()), token, peerAddress, transContext);
return new SeparateResponse(CoapResponse.of(response.getCode(), response.getPayload(), response.options().duplicate()).withContext(response.getTransContext()), token, peerAddress);
}
}

0 comments on commit 3403185

Please sign in to comment.