Skip to content

Commit

Permalink
#637: add callback on instance deletion at client side
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jan 17, 2019
1 parent e14092a commit 2dbc840
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
Expand Up @@ -150,6 +150,11 @@ public ObserveResponse observe(ServerIdentity identity, int resourceid) {
readResponse.getErrorMessage());
}

@Override
public void onDelete(ServerIdentity identity) {
// No default behavior
}

@Override
public void reset(int resourceid) {
// No default behavior
Expand Down
Expand Up @@ -166,6 +166,14 @@ public interface LwM2mInstanceEnabler {
*/
ObserveResponse observe(ServerIdentity identity, int resourceid);

/**
* A callback called when this instance is deleted
*
* @param identity the identity of the requester. This could be an internal call in this case
* <code> identity == ServerIdentity.SYSTEM</code>.
*/
void onDelete(ServerIdentity identity);

/**
* @param objectModel the model of this instance
* @return the list of the implemented resources of this instances mainly used for discover operation
Expand Down
Expand Up @@ -263,7 +263,9 @@ protected ExecuteResponse doExecute(ServerIdentity identity, ExecuteRequest requ

@Override
protected DeleteResponse doDelete(ServerIdentity identity, DeleteRequest request) {
if (null != instances.remove(request.getPath().getObjectInstanceId())) {
LwM2mInstanceEnabler deletedInstance = instances.remove(request.getPath().getObjectInstanceId());
if (deletedInstance != null) {
deletedInstance.onDelete(identity);
return DeleteResponse.success();
}
return DeleteResponse.notFound();
Expand Down
Expand Up @@ -72,6 +72,11 @@ public ExecuteResponse execute(ServerIdentity identity, int resourceid, String p
}
}

@Override
public void onDelete(ServerIdentity identity) {
LOG.info("Instance {} from object {} ({}) deleted.", getId(), getModel().name, getModel().id);
}

@Override
public void reset(int resourceid) {
resources.remove(resourceid);
Expand Down
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2015 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
* Achim Kraus (Bosch Software Innovations GmbH) - use ServerIdentity
* Achim Kraus (Bosch Software Innovations GmbH) - implement REPLACE/UPDATE
*******************************************************************************/
package org.eclipse.leshan.client.util;

import static org.junit.Assert.assertTrue;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.eclipse.leshan.LwM2mId;
import org.eclipse.leshan.client.request.ServerIdentity;
import org.eclipse.leshan.client.resource.BaseInstanceEnabler;
import org.eclipse.leshan.client.resource.LwM2mObjectEnabler;
import org.eclipse.leshan.client.resource.ObjectsInitializer;
import org.eclipse.leshan.core.request.DeleteRequest;
import org.junit.Test;

public class ObjectEnablerTest {

@Test
public void check_callback_on_delete() throws InterruptedException {
ObjectsInitializer initializer = new ObjectsInitializer();
TestInstanceEnabler instanceEnabler = new TestInstanceEnabler();
initializer.setInstancesForObject(LwM2mId.ACCESS_CONTROL, instanceEnabler);
LwM2mObjectEnabler objectEnabler = initializer.create(LwM2mId.ACCESS_CONTROL);

objectEnabler.delete(ServerIdentity.SYSTEM, new DeleteRequest(LwM2mId.ACCESS_CONTROL, instanceEnabler.getId()));
assertTrue("callback delete should have been called", instanceEnabler.waitForDelete(2, TimeUnit.SECONDS));
}

private class TestInstanceEnabler extends BaseInstanceEnabler {

CountDownLatch onDelete = new CountDownLatch(1);

@Override
public void onDelete(ServerIdentity identity) {
onDelete.countDown();
}

public boolean waitForDelete(long timeout, TimeUnit unit) throws InterruptedException {
return onDelete.await(timeout, unit);
}
}
}

0 comments on commit 2dbc840

Please sign in to comment.