Skip to content

Commit

Permalink
Added ClientAwakeTimeInformation to change the default 93 sec.
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Gonzalo <carlosgp143@gmail.com>
  • Loading branch information
carlosgp143 authored and sbernard31 committed Mar 1, 2018
1 parent 99dbc67 commit 1c322bf
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import org.eclipse.leshan.server.impl.InMemorySecurityStore;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.model.StandardModelProvider;
import org.eclipse.leshan.server.queue.ClientAwakeTimeProvider;
import org.eclipse.leshan.server.queue.StaticClientAwakeTimeProvider;
import org.eclipse.leshan.server.registration.Registration;
import org.eclipse.leshan.server.registration.RegistrationStore;
import org.eclipse.leshan.server.security.Authorizer;
Expand All @@ -69,6 +71,7 @@ public class LeshanServerBuilder {
private SecurityStore securityStore;
private LwM2mModelProvider modelProvider;
private Authorizer authorizer;
private ClientAwakeTimeProvider awakeTimeProvider;

private InetSocketAddress localAddress;
private InetSocketAddress localSecureAddress;
Expand Down Expand Up @@ -319,6 +322,16 @@ public LeshanServerBuilder disableQueueModeSupport() {
return this;
}

/**
* Sets a new {@link ClientAwakeTimeProvider} object different from the default one (93 seconds).
*
* @param awakeTimeProvider the {@link ClientAwakeTimeProvider} to set.
*/
public LeshanServerBuilder setClientAwakeTimeProvider(ClientAwakeTimeProvider awakeTimeProvider) {
this.awakeTimeProvider = awakeTimeProvider;
return this;
}

/**
* The default Californium/CoAP {@link NetworkConfig} used by the builder.
*/
Expand All @@ -341,9 +354,10 @@ public LeshanServer build() {
encoder = new DefaultLwM2mNodeEncoder();
if (decoder == null)
decoder = new DefaultLwM2mNodeDecoder();
if (coapConfig == null) {
if (coapConfig == null)
coapConfig = createDefaultNetworkConfig();
}
if (awakeTimeProvider == null)
awakeTimeProvider = new StaticClientAwakeTimeProvider();

// handle dtlsConfig
DtlsConnectorConfig dtlsConfig = null;
Expand Down Expand Up @@ -464,6 +478,6 @@ public LeshanServer build() {
}

return new LeshanServer(unsecuredEndpoint, securedEndpoint, registrationStore, securityStore, authorizer,
modelProvider, encoder, decoder, coapConfig, noQueueMode);
modelProvider, encoder, decoder, coapConfig, noQueueMode, awakeTimeProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.eclipse.leshan.server.impl.RegistrationServiceImpl;
import org.eclipse.leshan.server.model.LwM2mModelProvider;
import org.eclipse.leshan.server.observation.ObservationService;
import org.eclipse.leshan.server.queue.ClientAwakeTimeProvider;
import org.eclipse.leshan.server.queue.PresenceService;
import org.eclipse.leshan.server.queue.PresenceServiceImpl;
import org.eclipse.leshan.server.queue.PresenceStateListener;
Expand Down Expand Up @@ -117,11 +118,12 @@ public class LeshanServer implements LwM2mServer {
* @param encoder encode used to encode request payload.
* @param coapConfig the CoAP {@link NetworkConfig}.
* @param noQueueMode true to disable presenceService.
* @param awakeTimeProvider to set the client awake time if queue mode is used
*/
public LeshanServer(CoapEndpoint unsecuredEndpoint, CoapEndpoint securedEndpoint,
CaliforniumRegistrationStore registrationStore, SecurityStore securityStore, Authorizer authorizer,
LwM2mModelProvider modelProvider, LwM2mNodeEncoder encoder, LwM2mNodeDecoder decoder,
NetworkConfig coapConfig, boolean noQueueMode) {
NetworkConfig coapConfig, boolean noQueueMode, ClientAwakeTimeProvider awakeTimeProvider) {

Validate.notNull(registrationStore, "registration store cannot be null");
Validate.notNull(authorizer, "authorizer cannot be null");
Expand Down Expand Up @@ -197,7 +199,7 @@ protected Resource createRoot() {
decoder);
presenceService = null;
} else {
presenceService = new PresenceServiceImpl();
presenceService = new PresenceServiceImpl(awakeTimeProvider);
registrationService.addListener(new PresenceStateListener(presenceService));
requestSender = new QueueModeLwM2mRequestSender(presenceService,
new CaliforniumLwM2mRequestSender(endpoints, observationService, modelProvider, encoder, decoder));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*******************************************************************************
* Copyright (c) 2018 RISE SICS AB.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* RISE SICS AB - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.queue;

import org.eclipse.leshan.server.registration.Registration;

public interface ClientAwakeTimeProvider {

/**
* Returns the client awake time for the corresponding client, identified by the {@link Registration} object.
*
* @param reg the client's registration object
* @return the client awake time in milliseconds
*/
int getClientAwakeTime(Registration reg);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*
*/
public interface PresenceListener {


/**
* This method is invoked when the LWM2M client with the given endpoint state changes to awake.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ public final class PresenceServiceImpl implements PresenceService {

private final ConcurrentMap<String, PresenceStatus> clientStatusList = new ConcurrentHashMap<>();
private final List<PresenceListener> listeners = new CopyOnWriteArrayList<>();
ScheduledExecutorService clientTimersExecutor = Executors.newSingleThreadScheduledExecutor();
private final ClientAwakeTimeProvider awakeTimeProvider;
private final ScheduledExecutorService clientTimersExecutor = Executors.newSingleThreadScheduledExecutor();

public PresenceServiceImpl(ClientAwakeTimeProvider awakeTimeProvider) {
this.awakeTimeProvider = awakeTimeProvider;
}

@Override
public void addListener(PresenceListener listener) {
Expand Down Expand Up @@ -76,8 +81,10 @@ public void setAwake(Registration reg) {

boolean stateChanged = false;
synchronized (status) {

// Every time we set the clientAwakeTime, in case it changes dynamically
stateChanged = status.setAwake();
startClientAwakeTimer(reg, status);
startClientAwakeTimer(reg, status, awakeTimeProvider.getClientAwakeTime(reg));
}

if (stateChanged) {
Expand Down Expand Up @@ -137,9 +144,9 @@ private PresenceStatus getPresenceStatusObject(Registration reg) {
*
* @param status
*/
public void startClientAwakeTimer(final Registration reg, PresenceStatus clientPresenceStatus) {
public void startClientAwakeTimer(final Registration reg, PresenceStatus clientPresenceStatus,
int clientAwakeTime) {

int clientAwakeTime = clientPresenceStatus.getClientAwakeTime();
ScheduledFuture<?> clientScheduledFuture = clientPresenceStatus.getClientScheduledFuture();

if (clientAwakeTime != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ public class PresenceStatus {
/* The state of the client: Awake or Sleeping */
private Presence state;

ScheduledFuture<?> clientScheduledFuture;

private int clientAwakeTime;
private ScheduledFuture<?> clientScheduledFuture;

public PresenceStatus() {
this.state = Presence.SLEEPING;
this.clientAwakeTime = 93000; /* ms, default CoAP value */
}

public PresenceStatus(int clientAwakeTime) {
this.state = Presence.SLEEPING;
this.clientAwakeTime = clientAwakeTime; /* ms */
}

/* Client State Control */
Expand Down Expand Up @@ -88,23 +80,6 @@ public boolean isClientAwake() {
}

/* Control of the time the client waits before going to sleep */
/**
* Get the time that the client stays awake after an update message or the last received request.
*
* @return The client awake time.
*/
public int getClientAwakeTime() {
return clientAwakeTime;
}

/**
* Sets the client awake time, in case it wants to be modified during run time.
*
* @param clientAwakeTime
*/
public void setClientAwakeTime(int clientAwakeTime) {
this.clientAwakeTime = clientAwakeTime;
}

/**
* Sets the client scheduled task future, in order to cancel it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2018 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.server.queue;

import org.eclipse.leshan.server.registration.Registration;

public class StaticClientAwakeTimeProvider implements ClientAwakeTimeProvider {

private final int clientAwakeTime;

/**
* Create a {@link ClientAwakeTimeProvider} which always return 9300ms which is the default CoAP MAX_TRANSMIT_WAIT
* value.
*/
public StaticClientAwakeTimeProvider() {
this.clientAwakeTime = 93000;
}

public StaticClientAwakeTimeProvider(int defaultClientAwakeTime) {
this.clientAwakeTime = defaultClientAwakeTime;
}

@Override
public int getClientAwakeTime(Registration reg) {
return clientAwakeTime;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
*
*/
public class PresenceServiceTest {

private PresenceServiceImpl presenceService = new PresenceServiceImpl();
private ClientAwakeTimeProvider awakeTimeProvider = new StaticClientAwakeTimeProvider();
private PresenceServiceImpl presenceService = new PresenceServiceImpl(awakeTimeProvider);

@Test
public void testSetOnlineForNonQueueMode() throws Exception {
Expand Down Expand Up @@ -63,7 +63,6 @@ public void testIsOnline() throws Exception {

private Registration givenASimpleClient() throws UnknownHostException {
InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 5683);

Registration.Builder builder = new Registration.Builder("ID", "urn:client",
Identity.unsecure(Inet4Address.getLoopbackAddress(), 12354), address);

Expand All @@ -82,4 +81,4 @@ private Registration givenASimpleClientWithQueueMode() throws UnknownHostExcepti
presenceService.setAwake(reg);
return reg;
}
}
}

0 comments on commit 1c322bf

Please sign in to comment.