Skip to content

Commit

Permalink
Merge pull request #1082 from JKRhb/coap-port
Browse files Browse the repository at this point in the history
feat!(coap-server): adjust constructor to HTTP server
  • Loading branch information
relu91 authored Sep 14, 2023
2 parents c10fba2 + 0cbecdc commit 46f2d5a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 35 deletions.
7 changes: 4 additions & 3 deletions packages/binding-coap/src/coap-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import slugify from "slugify";
import { Readable } from "stream";
import { MdnsIntroducer } from "./mdns-introducer";
import { PropertyElement, DataSchema } from "wot-thing-description-types";
import { CoapServerConfig } from "./coap";

const { debug, warn, info, error } = createLoggers("binding-coap", "coap-server");

Expand Down Expand Up @@ -76,9 +77,9 @@ export default class CoapServer implements ProtocolServer {

private readonly coreResources = new Map<string, CoreLinkFormatResource>();

constructor(port?: number, address?: string) {
this.port = port ?? 5683;
this.address = address;
constructor(config?: CoapServerConfig) {
this.port = config?.port ?? 5683;
this.address = config?.address;

// WoT-specific content formats
registerFormat(ContentSerdes.JSON_LD, 2100);
Expand Down
5 changes: 5 additions & 0 deletions packages/binding-coap/src/coap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export * from "./coap-client";
export * from "./coaps-client-factory";
export * from "./coaps-client";

export interface CoapServerConfig {
port?: number;
address?: string;
}

export type CoapMethodName = "GET" | "POST" | "PUT" | "DELETE" | "FETCH" | "PATCH" | "iPATCH";

export type BlockSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024;
Expand Down
6 changes: 3 additions & 3 deletions packages/binding-coap/test/coap-client-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class CoapClientTest {
// testThing.extendInteractions();
// await testThing.writeProperty("test", "UNSET");

const coapServer = new CoapServer(port1);
const coapServer = new CoapServer({ port: port1 });

await coapServer.start(new Servient());
expect(coapServer.getPort()).to.equal(port1);
Expand Down Expand Up @@ -102,7 +102,7 @@ class CoapClientTest {
}

@test async "should re-use port"() {
const coapServer = new CoapServer(port2, "localhost");
const coapServer = new CoapServer({ port: port2, address: "localhost" });
await coapServer.start(new Servient());
const coapClient = new CoapClient(coapServer);
await coapClient.readResource({
Expand All @@ -113,7 +113,7 @@ class CoapClientTest {
}

@test(timeout(5000)) async "subscribe test"() {
const coapServer = new CoapServer(port2, "localhost");
const coapServer = new CoapServer({ port: port2, address: "localhost" });
await coapServer.start(new Servient());
const coapClient = new CoapClient(coapServer);
const form: CoapForm = {
Expand Down
44 changes: 22 additions & 22 deletions packages/binding-coap/test/coap-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const PORT = 31831;
@suite("CoAP server implementation")
class CoapServerTest {
@test async "should start and stop a server"() {
const coapServer = new CoapServer(PORT);
const coapServer = new CoapServer({ port: PORT });

await coapServer.start(new Servient());
expect(coapServer.getPort()).to.eq(PORT); // from test
Expand All @@ -44,7 +44,7 @@ class CoapServerTest {
}

@test async "should read property"() {
const coapServer = new CoapServer(PORT);
const coapServer = new CoapServer({ port: PORT });

await coapServer.start(new Servient());

Expand Down Expand Up @@ -75,7 +75,7 @@ class CoapServerTest {
}

@test async "should write property"() {
const coapServer = new CoapServer(PORT);
const coapServer = new CoapServer({ port: PORT });

await coapServer.start(new Servient());

Expand Down Expand Up @@ -114,7 +114,7 @@ class CoapServerTest {
}

@test async "should perform an action"() {
const coapServer = new CoapServer(PORT);
const coapServer = new CoapServer({ port: PORT });

await coapServer.start(new Servient());

Expand Down Expand Up @@ -151,7 +151,7 @@ class CoapServerTest {
}

@test async "should subscribe to event"() {
const coapServer = new CoapServer(PORT);
const coapServer = new CoapServer({ port: PORT });

await coapServer.start(new Servient());

Expand Down Expand Up @@ -185,25 +185,25 @@ class CoapServerTest {
}

@test async "should cause EADDRINUSE error when already running"() {
const portNumber = 9000;
const coapServer1 = new CoapServer(portNumber);
const port = 9000;
const coapServer1 = new CoapServer({ port });
await coapServer1.start(new Servient());

expect(coapServer1.getPort()).to.eq(portNumber);
expect(coapServer1.getPort()).to.eq(port);

const coapServer2 = new CoapServer(coapServer1.getPort());
const coapServer2 = new CoapServer({ port: coapServer1.getPort() });

try {
await coapServer2.start(new Servient());
} catch (err) {
expect((err as Error).message).to.eql(`bind EADDRINUSE 0.0.0.0:${portNumber}`);
expect((err as Error).message).to.eql(`bind EADDRINUSE 0.0.0.0:${port}`);
}

await coapServer1.stop();
}

@test async "should support IPv6"() {
const coapServer = new CoapServer(PORT, "::");
const coapServer = new CoapServer({ port: PORT, address: "::" });
await coapServer.start(new Servient());

const testThing = new ExposedThing(new Servient(), {
Expand Down Expand Up @@ -232,8 +232,8 @@ class CoapServerTest {
}

@test async "should take in account global uriVariables"() {
const portNumber = 9001;
const coapServer = new CoapServer(portNumber);
const port = 9001;
const coapServer = new CoapServer({ port });

await coapServer.start(new Servient());

Expand Down Expand Up @@ -284,8 +284,8 @@ class CoapServerTest {
}

@test async "should support /.well-known/core"() {
const portNumber = 9001;
const coapServer = new CoapServer(portNumber);
const port = 9001;
const coapServer = new CoapServer({ port });

await coapServer.start(new Servient());

Expand All @@ -311,8 +311,8 @@ class CoapServerTest {
}

@test async "should support TD Content-Format negotiation"() {
const portNumber = 5683;
const coapServer = new CoapServer(portNumber);
const port = 5683;
const coapServer = new CoapServer({ port });

await coapServer.start(new Servient());

Expand Down Expand Up @@ -365,8 +365,8 @@ class CoapServerTest {
}

@test async "should supply Size2 option when fetching a TD"() {
const portNumber = 9002;
const coapServer = new CoapServer(portNumber);
const port = 9002;
const coapServer = new CoapServer({ port });

await coapServer.start(new Servient());

Expand All @@ -391,11 +391,11 @@ class CoapServerTest {
}

@test async "should check uriVariables consistency"() {
const portNumber = 9003;
const coapServer = new CoapServer(portNumber);
const port = 9003;
const coapServer = new CoapServer({ port });
const servient = new Servient();

const baseUri = `coap://localhost:${portNumber}/test`;
const baseUri = `coap://localhost:${port}/test`;

await coapServer.start(servient);

Expand Down
10 changes: 4 additions & 6 deletions packages/cli/src/cli-default-servient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class DefaultServient extends Servient {
},
http: {
port: 8080,
selfSigned: false,
allowSelfSigned: false,
},
coap: {
port: 5683,
Expand Down Expand Up @@ -131,11 +131,9 @@ export default class DefaultServient extends Servient {
// re-use httpServer (same port)
// this.addServer(new WebSocketServer(httpServer));
}
if (this.config.coap) {
coapServer =
typeof this.config.coap.port === "number"
? new CoapServer(this.config.coap.port)
: new CoapServer();
const coapConfig = this.config.coap;
if (coapConfig != null) {
coapServer = new CoapServer(coapConfig);
this.addServer(coapServer);
}
if (this.config.mqtt) {
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/src/quickstart/smart-clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CoapServer } from "@node-wot/binding-coap";

// create Servient add CoAP binding with port configuration
const servient = new Servient();
servient.addServer(new CoapServer(5686));
servient.addServer(new CoapServer({ port: 5686 }));

Helpers.setStaticAddress("plugfest.thingweb.io"); // comment this out if you are testing locally

Expand Down

0 comments on commit 46f2d5a

Please sign in to comment.