UNPKG

@iprokit/service

Version:

Powering distributed systems with simplicity and speed.

133 lines (132 loc) 3.86 kB
/** * @iProKit/Service * Copyright (c) 2019-2025 Rutvik Katuri / iProTechs * SPDX-License-Identifier: Apache-2.0 */ /// <reference types="node" /> /// <reference types="node" /> import { EventEmitter } from 'events'; import { Attributes } from './pod'; declare const socket: unique symbol; /** * `Server` binds to a multicast address and port number, listening for incoming SDP client connections. * Tracks pods' availability and emits events when their states change. * * @emits `listening` when the server is bound after calling `listen()`. * @emits `available` when a pod becomes available. * @emits `unavailable` when a pod becomes unavailable. * @emits `error` when an error occurs. * @emits `close` when the server is fully closed. */ export default class Server extends EventEmitter { #private; /** * Pods discovered. */ readonly pods: Map<string, IPod>; /** * Underlying UDP Socket. */ private readonly [socket]; /** * Creates an instance of SDP `Server`. * * @param identifier unique identifier of the server. */ constructor(identifier: string); /** * Unique identifier of the server. */ get identifier(): string; /** * Attributes of the server. */ get attributes(): Attributes; /** * Multicast group that have been joined. */ get membership(): string | null; /** * Local port of the server. */ get localPort(): number | null; /** * Local address of the server. */ get localAddress(): string | null; /** * `true` when the server is listening for connections, `false` otherwise. */ get listening(): boolean; /** * Retrieves the bound address, family, and port of the server as reported by operating system. */ address(): import("net").AddressInfo | null; /** * @emits `available` when a pod is available. * @emits `unavailable` when a pod is unavailable. */ private onMessage; /** * Encodes and multicasts `this.#pod` on the network. * * @param callback called once the pod is multicast. */ private send; /** * Encodes and multicasts `this.#pod` on the network, then waits for an echo. * * @param callback called once the echo is received. */ private echo; /** * Starts listening for pods on the network and emits `listening` event. * * @param port local port. * @param address address of the multicast group. * @param callback optional callback added as a one-time listener for the `listening` event. */ listen(port: number, address: string, callback?: () => void): this; /** * Closes the underlying UDP socket and stops listening for pods, emitting the `close` event. * * @param callback optional callback added as a one-time listener for the `close` event. */ close(callback?: () => void): this; /** * References the socket, preventing it from closing automatically. * Calling `ref` again has no effect if already referenced. */ ref(): this; /** * Unreferences the socket, allowing it to close automatically when no other event loop activity is present. * Calling `unref` again has no effect if already unreferenced. */ unref(): this; /** * Returns a new session token for an available pod. */ private static createToken; /** * Session token representing an unavailable pod. */ static readonly UNAVAILABLE_TOKEN = "00000"; } /** * Interface of `Pod`. */ export interface IPod { /** * Session token of the pod. */ session: string; /** * Attributes of the Pod. */ attributes: Attributes | null; /** * Host address of the Pod. */ host: string | null; } export {};