Skip to content

Commit

Permalink
Share CIDConverter code between demos.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Aug 16, 2021
1 parent d4f7259 commit eeb8907
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.californium.scandium.dtls.cipher.CipherSuite;
import org.eclipse.leshan.core.CertificateUsage;
import org.eclipse.leshan.core.demo.cli.InvalidOptionsException;
import org.eclipse.leshan.core.demo.cli.converters.CIDConverter;
import org.eclipse.leshan.core.demo.cli.converters.PortConverter;
import org.eclipse.leshan.core.demo.cli.converters.StrictlyPositiveIntegerConverter;

Expand Down Expand Up @@ -224,20 +225,12 @@ public static class DTLSSection {
"- Positive value define the size in byte of CID generated.", //
"- 0 value means we accept to use CID but will not generated one for foreign peer.", //
"Default: off" },
converter = CIDConverter.class)
converter = ClientCIDConverter.class)
public Integer cid;

private static class CIDConverter implements ITypeConverter<Integer> {
@Override
public Integer convert(String cid) {
if ("off".equals(cid)) {
return null;
} else if ("on".equals(cid)) {
return 0;
} else {
Integer res = Integer.parseInt(cid);
return res < 0 ? null : res;
}
private static class ClientCIDConverter extends CIDConverter {
public ClientCIDConverter() {
super(0);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2021 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.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.core.demo.cli.converters;

import picocli.CommandLine.ITypeConverter;

public class CIDConverter implements ITypeConverter<Integer> {

private Integer onValue;

public CIDConverter(Integer onValue) {
this.onValue = onValue;
}

@Override
public Integer convert(String cid) {
if ("off".equals(cid)) {
return null;
} else if ("on".equals(cid)) {
return onValue;
} else {
Integer res = Integer.parseInt(cid);
return res < 0 ? null : res;
}
}
};
4 changes: 4 additions & 0 deletions leshan-server-core-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Contributors:
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-server-cf</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.leshan</groupId>
<artifactId>leshan-core-demo</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.eclipse.leshan.server.core.demo.cli.converters;

import org.eclipse.leshan.core.demo.cli.converters.CIDConverter;

public class ServerCIDConverter extends CIDConverter {
public ServerCIDConverter() {
super(6);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.eclipse.leshan.core.LwM2m;
import org.eclipse.leshan.core.demo.cli.converters.PortConverter;
import org.eclipse.leshan.server.core.demo.cli.converters.ServerCIDConverter;

import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
Expand Down Expand Up @@ -139,23 +140,9 @@ public static class DTLSSection {
"- Positive value define the size in byte of CID generated.", //
"- 0 value means we accept to use CID but will not generated one for foreign peer.", //
"Default: on" },
converter = CIDConverter.class)
converter = ServerCIDConverter.class)
public Integer cid;

private static class CIDConverter implements ITypeConverter<Integer> {
@Override
public Integer convert(String cid) {
if ("off".equals(cid)) {
return null;
} else if ("on".equals(cid)) {
return 6;
} else {
Integer res = Integer.parseInt(cid);
return res < 0 ? null : res;
}
}
};

@Option(names = { "-oc", "--support-deprecated-ciphers" },
description = { //
"Activate support of old/deprecated cipher suites." })
Expand Down

0 comments on commit eeb8907

Please sign in to comment.