import { EventEmitter } from "events" ; remarks export enum CommunicationState { /** Disconnected from the Coaty communication infrastructure. */ Offline , /** Connected to the Coaty communication infrastructure. */ Online } /**
 * Predefined event types for Coaty communication event patterns.
 */ export enum CommunicationEventType { External = 0 , Raw , OneWay , Advertise , Deadvertise , Channel , Associate , IoValue , TwoWay , Discover , Resolve , Query , Retrieve , Update , Complete , Call , Return } /**
 * Protocol for exchanging communication event properties for both publications and
 * subscriptions between a binding and the communication manager.
 */ export interface CommunicationEventLike { /** Event type. */ eventType : CommunicationEventType ; remarks remarks eventTypeFilter ? : string ; /**
     * Object ID of the event source object (except for Raw and external IoValue
     * events).
     */ sourceId ? : string ; /**
     * Correlation ID for inbound/outbound two-way request-response events.
     * Correlated subscription topic for inbound Raw and IoValue events.
     */ correlationId ? : string ; /** Event data (for publication events only). */ data ? : any ; /**
     * Indicates whether data property contains raw data (string or Uint8Array
     * or Buffer) or a JSON object (used for Raw and IO value publication and
     * subscription events only).
     *
     * If not specified, the value defaults to false.
     */ isDataRaw ? : boolean ; /**
     * Indicates whether the route of an IO value subscription event is external
     * or not (used for IO value subscription events only).
     *
     * If not specified, the value defaults to false.
     */ isExternalIoRoute ? : boolean ; /**
     * Binding-specific options for publication, subscription, or unsubscription.
     * (optional)
     */ options ? : { [ key ] : any ; } ; } /**
 * Defines static constructor signature for communication binding types that
 * extend and implement the abstract `CommunicationBinding` class.
 */ export type CommunicationBindingStatic < T extends CommunicationBinding < O > , O extends CommunicationBindingOptions > = new ( options ) => T ; /**
 * A utility type used to specify a value for the `CommunicationOptions.binding`
 * property in the agent container configuration.
 */ export type CommunicationBindingWithOptions < T extends CommunicationBinding < O > , O extends CommunicationBindingOptions > = { type : CommunicationBindingStatic < T , O > ; options : O ; } ; /**
 * Defines log levels for logging within a communication binding.
 */ export enum CommunicationBindingLogLevel { /** Log debug messages. */ debug = 0 , /** Log informational messages. */ info = 1 , /** Log error messages. */ error = 2 } /**
 * Defines common options of all communication binding types.
 */ export interface CommunicationBindingOptions { /**
     * Namespace used to isolate different Coaty applications running with the
     * same binding type in the same networking infrastructure (optional).
     *
     * Communication events are only routed between agents within a common
     * communication namespace: an agent publishes and observes communication
     * events with respect to the given namespace.
     *
     * If not specified (i.e. undefined), a default namespace named "-" is used.
     */ namespace ? : string ; /**
     * Determines whether to enable cross-namespace communication between agents
     * in special use cases (optional).
     *
     * If `true`, an agent receives communication events published by *any*
     * agent running with the same binding type in the same networking
     * infrastructure, regardless of namespace.
     *
     * If not specified or `false`, this option is not enabled.
     */ shouldEnableCrossNamespacing ? : boolean ; /**
     * The level used to log errors, informational, as well as debug messages
     * generated by the binding (optional).
     *
     * Messages corresponding to the specified log level are emitted on the
     * "error", "info", or "debug" event, respectively. If level is "error",
     * only errors are emitted; if level is "info", only "error" and "info"
     * messages are emitted; if level is "debug", all messages are emitted.
     *
     * If not specified, the log level defaults to "error".
     */ logLevel ? : CommunicationBindingLogLevel ; } /**
 * Defines the three lifecycle states of a communication binding.
 *
 * The lifecycle of a binding transitions from "Initialized" to "Joined" to
 * "Unjoined". An unjoined binding cannot be joined or initialized again.
 */ export enum CommunicationBindingState { /** Binding is initialized. */ Initialized , /** Binding has joined the Coaty communication infrastructure. */ Joined , /** Binding has left the Coaty communication infrastructure. */ Unjoined } /**
 * Defines options for joining the Coaty communication infrastructure.
 */ export interface CommunicationBindingJoinOptions { /**
     * An id that uniquely identifies the joining agent.
     *
     * A binding might use this id e.g. as a client or session id for the
     * underlying messaging transport.
     */ agentId : string ; remarks joinEvents : CommunicationEventLike [ ] ; remarks unjoinEvent : CommunicationEventLike ; } /**
 * Defines a generic public API for transmitting Coaty communication events via
 * specific broker-based or brokerless publish-subscribe messaging protocols.
 */ export interface CommunicationBindingProtocol { /**
     * Gets the current lifecycle state of this communication binding.
     */ readonly state : CommunicationBindingState ; /**
     * Defines the name of this binding used for identification and display
     * purposes.
     */ readonly apiName : string ; /**
     * Defines the protocol version number of this binding, a positive integer.
     *
     * Increment this version whenever you change or add new communication
     * events or make breaking changes to the binding's messaging protocol.
     *
     * Note that each specific binding type provides its own API versioning. API
     * versions are not synchronized or shared across different binding types.
     */ readonly apiVersion : number ; remarks on ( event , listener ) : void ; remarks on ( event , listener ) : void ; /**
     * Emitted whenever an inbound communication event has arrived.
     *
     * For Raw events, the `eventTypeFilter` property contains the published
     * topic name and the `correlationId` property contains the correlated
     * subscription topic.
     *
     * For IoValue events, the `eventTypeFilter` property contains the IO route.
     */ on ( event , listener ) : void ; param joinOptions join ( joinOptions ) : void ; returns unjoin ( ) : Promise < void > ; remarks remarks param eventLike publish ( eventLike ) : void ; remarks remarks param eventLike subscribe ( eventLike ) : void ; remarks param eventLike unsubscribe ( eventLike ) : void ; param ioSourceId returns createIoRoute ( ioSourceId ) : string ; } remarks export abstract class CommunicationBinding < O extends CommunicationBindingOptions > extends EventEmitter implements CommunicationBindingProtocol { protected static readonly DEFAULT_NAMESPACE = "-" ; private _state : CommunicationBindingState ; remarks get options ( ) : Readonly < O > { return this . _options ; } get state ( ) { return this . _state ; } abstract get apiName ( ) : string ; abstract get apiVersion ( ) : number ; internal param _options constructor ( private _options ) { super ( ) ; this . _state = CommunicationBindingState . Initialized ; if ( this . _options . logLevel === undefined ) { this . _options . logLevel = CommunicationBindingLogLevel . error ; } if ( this . _options . namespace === undefined ) { this . _options . namespace = CommunicationBinding . DEFAULT_NAMESPACE ; } if ( this . _options . shouldEnableCrossNamespacing === undefined ) { this . _options . shouldEnableCrossNamespacing = false ; } this . onInit ( ) ; } join ( joinOptions ) { if ( this . state === CommunicationBindingState . Joined ) { return ; } if ( this . state === CommunicationBindingState . Unjoined ) { this . log ( CommunicationBindingLogLevel . error , "Cannot join binding which is in Unjoined state" ) ; return ; } this . _state = CommunicationBindingState . Joined ; this . onJoin ( joinOptions ) ; } unjoin ( ) { if ( this . state === CommunicationBindingState . Unjoined ) { return Promise . resolve ( ) ; } this . _state = CommunicationBindingState . Unjoined ; return this . onUnjoin ( ) . then ( ( ) => { this . removeAllListeners ( ) ; } ) ; } publish ( eventLike ) { if ( this . state === CommunicationBindingState . Unjoined ) { return ; } this . onPublish ( eventLike ) ; } subscribe ( eventLike ) { if ( this . state === CommunicationBindingState . Unjoined ) { return ; } this . onSubscribe ( eventLike ) ; } unsubscribe ( eventLike ) { if ( this . state === CommunicationBindingState . Unjoined ) { return ; } this . onUnsubscribe ( eventLike ) ; } abstract createIoRoute ( ioSourceId ) ; /**
     * Called by base binding constructor once to initialize subclass members.
     */ protected abstract onInit ( ) ; /**
     * Called by base binding class once to join.
     */ protected abstract onJoin ( joinOptions ) ; remarks protected abstract onUnjoin ( ) ; /**
     * Called by base binding class to publish the given event like.
     */ protected abstract onPublish ( eventLike ) ; /**
     * Called by base binding class to subscribe to the given event like.
     */ protected abstract onSubscribe ( eventLike ) ; /**
     * Called by base binding class to unsubscribe to the given event like.
     */ protected abstract onUnsubscribe ( eventLike ) ; param logLevel param arg1 param arg2 param arg3 param arg4 param arg5 protected log ( logLevel , arg1 , arg2 ? , arg3 ? , arg4 ? , arg5 ? ) { const targetLogLevel = this . options . logLevel ?? CommunicationBindingLogLevel . error ; if ( targetLogLevel <= logLevel ) { const event = CommunicationBindingLogLevel [ logLevel ] ; this . emit ( event , ( arg1 ?? "" ) . concat ( arg2 ?? "" , arg3 ?? "" , arg4 ?? "" , arg5 ?? "" ) ) ; } } }