Skip to content

Commit

Permalink
Fix eclipse-leshan#166: Use Atomic long instead of hashcode
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 authored and danielhqv committed Mar 9, 2018
1 parent d26e0b6 commit aa50dd5
Showing 1 changed file with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.SortedMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicLong;

import org.eclipse.californium.core.coap.MessageObserver;
import org.eclipse.californium.core.coap.MessageObserverAdapter;
Expand Down Expand Up @@ -77,8 +78,8 @@ public <T extends LwM2mResponse> T send(final Registration destination, final Do
final LwM2mModel model = modelProvider.getObjectModel(destination);

// Create the CoAP request from LwM2m request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(),
destination.getId(), destination.getEndpoint(), model, encoder);
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(),
destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
request.accept(coapRequestBuilder);
final Request coapRequest = coapRequestBuilder.getRequest();

Expand Down Expand Up @@ -113,9 +114,8 @@ public <T extends LwM2mResponse> void send(final Registration destination, final
final LwM2mModel model = modelProvider.getObjectModel(destination);

// Create the CoAP request from LwM2m request
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(
destination.getIdentity(), destination.getRootPath(),
destination.getId(), destination.getEndpoint(), model, encoder);
CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(),
destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
request.accept(coapRequestBuilder);
final Request coapRequest = coapRequestBuilder.getRequest();

Expand Down Expand Up @@ -152,40 +152,44 @@ public void cancelPendingRequests(Registration registration) {
requests.clear();
}

private String getFloorKey(String registrationId) {
// The key format is regid#int, So we need a key which is always before this pattern (in natural order).
private static String getFloorKey(String registrationId) {
// The key format is regid#long, So we need a key which is always before this pattern (in natural order).
return registrationId + '#';
}

private String getCeilingKey(String registrationId) {
// The key format is regid#int, So we need a key which is always after this pattern (in natural order).
private static String getCeilingKey(String registrationId) {
// The key format is regid#long, So we need a key which is always after this pattern (in natural order).
return registrationId + "#A";
}

private String getKey(String registrationId, int requestId) {
private static String getKey(String registrationId, long requestId) {
return registrationId + '#' + requestId;
}

private void addPendingRequest(String registrationId, Request coapRequest) {
Validate.notNull(registrationId);
CleanerMessageObserver observer = new CleanerMessageObserver(registrationId, coapRequest);
coapRequest.addMessageObserver(observer);
pendingRequests.put(observer.getRequestKey(), coapRequest);
if (coapRequest.isConfirmable()) {
CleanerMessageObserver observer = new CleanerMessageObserver(registrationId, coapRequest);
coapRequest.addMessageObserver(observer);
pendingRequests.put(observer.getRequestKey(), coapRequest);
}
}

private void removePendingRequest(String key, Request coapRequest) {
Validate.notNull(key);
pendingRequests.remove(key, coapRequest);
}

private AtomicLong idGenerator = new AtomicLong(0l);

private class CleanerMessageObserver extends MessageObserverAdapter {

private final String requestKey;
private final Request coapRequest;

public CleanerMessageObserver(String registrationId, Request coapRequest) {
super();
requestKey = getKey(registrationId, hashCode());
requestKey = getKey(registrationId, idGenerator.incrementAndGet());
this.coapRequest = coapRequest;
}

Expand Down

0 comments on commit aa50dd5

Please sign in to comment.