Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
isssue #1303 refactor: Add 'host' parameter to OPCUAServer for specif…
…ic interface binding
- Loading branch information
1 parent
9e5a34a
commit 73a423a
Showing
3 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import net from "net"; | ||
import os from "os"; | ||
import should from "should"; | ||
import { OPCUAServer } from ".."; | ||
|
||
async function findAvailablePort(): Promise<number> { | ||
return new Promise((resolve) => { | ||
const server = net.createServer(); | ||
server.listen(0); | ||
server.on("listening", function () { | ||
const port = (server.address() as net.AddressInfo).port; | ||
console.log(`INFO: Found available port ${port}`); | ||
server.close(() => resolve(port)); | ||
}); | ||
}); | ||
} | ||
|
||
function findLanIp(): string | undefined { | ||
const networkInterfaces = os.networkInterfaces(); | ||
|
||
for (const iface of Object.values(networkInterfaces)) { | ||
if (!iface) { | ||
continue; | ||
} | ||
|
||
for (const alias of iface) { | ||
if (alias.family === "IPv4" && !alias.internal) { | ||
return alias.address; | ||
} | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function checkServer(host: string | undefined, port: number): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
const socket = net.createConnection(port, host, () => { | ||
socket.end(); | ||
resolve(true); | ||
}); | ||
|
||
socket.on("error", () => { | ||
resolve(false); | ||
}); | ||
}); | ||
} | ||
|
||
async function testServerStartupAndShutdown(host: string | undefined, port: number) { | ||
try { | ||
const serverOptions = host === undefined ? { port } : { host, port }; | ||
console.log("INFO: running with serverOptions: ", serverOptions); | ||
|
||
const server = new OPCUAServer(serverOptions); | ||
await server.start(); | ||
|
||
const isServerListening = await checkServer(host, port); | ||
isServerListening.should.be.true; | ||
|
||
await server.shutdown(); | ||
server.dispose(); | ||
} catch (err) { | ||
should.fail(err, undefined, `Error in server startup/shutdown: ${err.message}`); | ||
} | ||
} | ||
|
||
describe("OPCUAServer - issue#1303", () => { | ||
it("should start a net.Server on loopback address", async () => { | ||
const port = await findAvailablePort(); | ||
const host = "localhost"; | ||
await testServerStartupAndShutdown(host, port); | ||
}); | ||
|
||
it("should start a net.Server on LAN IP", async () => { | ||
const port = await findAvailablePort(); | ||
const host = findLanIp(); | ||
console.log(host ? `INFO: LAN IP found: ${host}` : "WARNING: No LAN IP available, skiping test..."); | ||
if (host) { | ||
await testServerStartupAndShutdown(host, port); | ||
} | ||
}); | ||
|
||
it("should start a net.Server on default host (ensures backward compatibility)", async () => { | ||
const port = await findAvailablePort(); | ||
await testServerStartupAndShutdown(undefined, port); | ||
}); | ||
}); |