Skip to content

Commit

Permalink
Add ID and cancel facility to bootstrap session.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Nov 14, 2019
1 parent d990528 commit 1205159
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
public interface BootstrapSession {

/**
* @Return the identifier for this session
*/
String getId();

/**
* @return the endpoint of the LwM2M client.
*/
Expand All @@ -37,7 +42,7 @@ public interface BootstrapSession {
Identity getIdentity();

/**
* @return true if the LwM2M client is authorized to start a bootstrap session.
* @return <code>true</code> if the LwM2M client is authorized to start a bootstrap session.
*/
boolean isAuthorized();

Expand All @@ -52,4 +57,13 @@ public interface BootstrapSession {
*/
long getCreationTime();

/**
* Cancel the current session
*/
void cancel();

/**
* @return True if this session was cancellled
*/
boolean isCancelled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@

import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.util.RandomStringUtils;

/**
* A default implementation for {@link BootstrapSession}
*/
public class DefaultBootstrapSession implements BootstrapSession {

private final String id;
private final String endpoint;
private final Identity identity;
private final boolean authorized;
private final ContentFormat contentFormat;
private final long creationTime;

private volatile boolean cancelled = false;

/**
* Create a {@link DefaultBootstrapSession} using default {@link ContentFormat#TLV} content format and using
* <code>System.currentTimeMillis()</code> to set the creation time.
Expand Down Expand Up @@ -65,13 +69,19 @@ public DefaultBootstrapSession(String endpoint, Identity identity, boolean autho
*/
public DefaultBootstrapSession(String endpoint, Identity identity, boolean authorized, ContentFormat contentFormat,
long creationTime) {
this.id = RandomStringUtils.random(10, true, true);
this.endpoint = endpoint;
this.identity = identity;
this.authorized = authorized;
this.contentFormat = contentFormat;
this.creationTime = creationTime;
}

@Override
public String getId() {
return id;
}

@Override
public String getEndpoint() {
return endpoint;
Expand All @@ -97,11 +107,20 @@ public long getCreationTime() {
return creationTime;
}

@Override
public void cancel() {
cancelled = true;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public String toString() {
return String.format(
"BootstrapSession [endpoint=%s, identity=%s, authorized=%s, contentFormat=%s, creationTime=%dms]",
endpoint, identity, authorized, contentFormat, creationTime);
"BootstrapSession [id=%s, endpoint=%s, identity=%s, authorized=%s, contentFormat=%s, creationTime=%dms, cancelled=%s]",
id, endpoint, identity, authorized, contentFormat, creationTime, cancelled);
}

}
}

0 comments on commit 1205159

Please sign in to comment.