@hla4ts/session 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +377 -377
- package/package.json +3 -3
- package/src/errors.ts +98 -98
- package/src/index.ts +147 -147
- package/src/messages.ts +329 -329
- package/src/sequence-number.ts +200 -200
- package/src/session.ts +976 -976
- package/src/timeout-timer.ts +204 -204
- package/src/types.ts +235 -235
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hla4ts/session",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "HLA 4 Federate Protocol Session Layer - State machine, heartbeats, reconnection",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"clean": "rm -rf dist"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@hla4ts/transport": "^0.1.
|
|
28
|
-
"@hla4ts/proto": "^0.1.
|
|
27
|
+
"@hla4ts/transport": "^0.1.1",
|
|
28
|
+
"@hla4ts/proto": "^0.1.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/bun": "latest",
|
package/src/errors.ts
CHANGED
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session Exceptions
|
|
3
|
-
*
|
|
4
|
-
* Custom error types for session-related failures.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { SessionState } from "./types.ts";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Base class for session-related errors
|
|
11
|
-
*/
|
|
12
|
-
export class SessionError extends Error {
|
|
13
|
-
constructor(message: string) {
|
|
14
|
-
super(message);
|
|
15
|
-
this.name = "SessionError";
|
|
16
|
-
// Maintains proper stack trace for where the error was thrown
|
|
17
|
-
if (Error.captureStackTrace) {
|
|
18
|
-
Error.captureStackTrace(this, this.constructor);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Thrown when the session is lost unexpectedly.
|
|
25
|
-
* This can happen due to:
|
|
26
|
-
* - Network disconnection
|
|
27
|
-
* - Server timeout
|
|
28
|
-
* - Protocol errors
|
|
29
|
-
*/
|
|
30
|
-
export class SessionLostError extends SessionError {
|
|
31
|
-
constructor(message: string, public readonly cause?: Error) {
|
|
32
|
-
super(message);
|
|
33
|
-
this.name = "SessionLostError";
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Thrown when an operation is attempted on a session that is already terminated.
|
|
39
|
-
*/
|
|
40
|
-
export class SessionAlreadyTerminatedError extends SessionError {
|
|
41
|
-
constructor(message: string = "Session has already terminated") {
|
|
42
|
-
super(message);
|
|
43
|
-
this.name = "SessionAlreadyTerminatedError";
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Thrown when an operation is attempted in an invalid session state.
|
|
49
|
-
*/
|
|
50
|
-
export class SessionIllegalStateError extends SessionError {
|
|
51
|
-
constructor(
|
|
52
|
-
public readonly currentState: SessionState,
|
|
53
|
-
public readonly operation: string
|
|
54
|
-
) {
|
|
55
|
-
super(`Cannot ${operation} in state ${currentState}`);
|
|
56
|
-
this.name = "SessionIllegalStateError";
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Thrown when a bad message is received from the server.
|
|
62
|
-
*/
|
|
63
|
-
export class BadMessageError extends SessionError {
|
|
64
|
-
constructor(message: string) {
|
|
65
|
-
super(message);
|
|
66
|
-
this.name = "BadMessageError";
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Thrown when the message queue is full and cannot accept more messages.
|
|
72
|
-
*/
|
|
73
|
-
export class MessageQueueFullError extends SessionError {
|
|
74
|
-
constructor() {
|
|
75
|
-
super("Message queue is full");
|
|
76
|
-
this.name = "MessageQueueFullError";
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Thrown when a connection attempt times out.
|
|
82
|
-
*/
|
|
83
|
-
export class ConnectionTimeoutError extends SessionError {
|
|
84
|
-
constructor(timeoutMs: number) {
|
|
85
|
-
super(`Connection timed out after ${timeoutMs}ms`);
|
|
86
|
-
this.name = "ConnectionTimeoutError";
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Thrown when a response is not received within the expected timeout.
|
|
92
|
-
*/
|
|
93
|
-
export class ResponseTimeoutError extends SessionError {
|
|
94
|
-
constructor(timeoutMs: number) {
|
|
95
|
-
super(`Response timed out after ${timeoutMs}ms`);
|
|
96
|
-
this.name = "ResponseTimeoutError";
|
|
97
|
-
}
|
|
98
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Session Exceptions
|
|
3
|
+
*
|
|
4
|
+
* Custom error types for session-related failures.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { SessionState } from "./types.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base class for session-related errors
|
|
11
|
+
*/
|
|
12
|
+
export class SessionError extends Error {
|
|
13
|
+
constructor(message: string) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "SessionError";
|
|
16
|
+
// Maintains proper stack trace for where the error was thrown
|
|
17
|
+
if (Error.captureStackTrace) {
|
|
18
|
+
Error.captureStackTrace(this, this.constructor);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Thrown when the session is lost unexpectedly.
|
|
25
|
+
* This can happen due to:
|
|
26
|
+
* - Network disconnection
|
|
27
|
+
* - Server timeout
|
|
28
|
+
* - Protocol errors
|
|
29
|
+
*/
|
|
30
|
+
export class SessionLostError extends SessionError {
|
|
31
|
+
constructor(message: string, public readonly cause?: Error) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = "SessionLostError";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Thrown when an operation is attempted on a session that is already terminated.
|
|
39
|
+
*/
|
|
40
|
+
export class SessionAlreadyTerminatedError extends SessionError {
|
|
41
|
+
constructor(message: string = "Session has already terminated") {
|
|
42
|
+
super(message);
|
|
43
|
+
this.name = "SessionAlreadyTerminatedError";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Thrown when an operation is attempted in an invalid session state.
|
|
49
|
+
*/
|
|
50
|
+
export class SessionIllegalStateError extends SessionError {
|
|
51
|
+
constructor(
|
|
52
|
+
public readonly currentState: SessionState,
|
|
53
|
+
public readonly operation: string
|
|
54
|
+
) {
|
|
55
|
+
super(`Cannot ${operation} in state ${currentState}`);
|
|
56
|
+
this.name = "SessionIllegalStateError";
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Thrown when a bad message is received from the server.
|
|
62
|
+
*/
|
|
63
|
+
export class BadMessageError extends SessionError {
|
|
64
|
+
constructor(message: string) {
|
|
65
|
+
super(message);
|
|
66
|
+
this.name = "BadMessageError";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Thrown when the message queue is full and cannot accept more messages.
|
|
72
|
+
*/
|
|
73
|
+
export class MessageQueueFullError extends SessionError {
|
|
74
|
+
constructor() {
|
|
75
|
+
super("Message queue is full");
|
|
76
|
+
this.name = "MessageQueueFullError";
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Thrown when a connection attempt times out.
|
|
82
|
+
*/
|
|
83
|
+
export class ConnectionTimeoutError extends SessionError {
|
|
84
|
+
constructor(timeoutMs: number) {
|
|
85
|
+
super(`Connection timed out after ${timeoutMs}ms`);
|
|
86
|
+
this.name = "ConnectionTimeoutError";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Thrown when a response is not received within the expected timeout.
|
|
92
|
+
*/
|
|
93
|
+
export class ResponseTimeoutError extends SessionError {
|
|
94
|
+
constructor(timeoutMs: number) {
|
|
95
|
+
super(`Response timed out after ${timeoutMs}ms`);
|
|
96
|
+
this.name = "ResponseTimeoutError";
|
|
97
|
+
}
|
|
98
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @hla4ts/session - HLA 4 Federate Protocol Session Layer
|
|
3
|
-
*
|
|
4
|
-
* This package provides session management for the HLA 4 Federate Protocol,
|
|
5
|
-
* including connection establishment, state machine, heartbeats, and reconnection.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* import { Session, SessionState } from '@hla4ts/session';
|
|
10
|
-
* import { TlsTransport } from '@hla4ts/transport';
|
|
11
|
-
*
|
|
12
|
-
* // Create transport and session
|
|
13
|
-
* const transport = new TlsTransport({
|
|
14
|
-
* host: 'rti.example.com',
|
|
15
|
-
* port: 15165,
|
|
16
|
-
* });
|
|
17
|
-
*
|
|
18
|
-
* const session = new Session(transport, {
|
|
19
|
-
* connectionTimeout: 30000,
|
|
20
|
-
* responseTimeout: 180000,
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* // Listen for state changes
|
|
24
|
-
* session.addStateListener({
|
|
25
|
-
* onStateTransition: (oldState, newState, reason) => {
|
|
26
|
-
* console.log(`Session: ${oldState} -> ${newState} (${reason})`);
|
|
27
|
-
* if (newState === SessionState.DROPPED) {
|
|
28
|
-
* // Attempt to resume
|
|
29
|
-
* session.resume().catch(console.error);
|
|
30
|
-
* }
|
|
31
|
-
* }
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* // Start the session
|
|
35
|
-
* await session.start({
|
|
36
|
-
* onHlaCallbackRequest: (seqNum, callbackData) => {
|
|
37
|
-
* // Handle RTI callback
|
|
38
|
-
* const response = processCallback(callbackData);
|
|
39
|
-
* session.sendHlaCallbackResponse(seqNum, response);
|
|
40
|
-
* }
|
|
41
|
-
* });
|
|
42
|
-
*
|
|
43
|
-
* // Send HLA calls
|
|
44
|
-
* const response = await session.sendHlaCallRequest(encodedCall);
|
|
45
|
-
*
|
|
46
|
-
* // Clean up
|
|
47
|
-
* await session.terminate();
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @packageDocumentation
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
// =============================================================================
|
|
54
|
-
// Session
|
|
55
|
-
// =============================================================================
|
|
56
|
-
export { Session } from "./session.ts";
|
|
57
|
-
|
|
58
|
-
// =============================================================================
|
|
59
|
-
// Types
|
|
60
|
-
// =============================================================================
|
|
61
|
-
export {
|
|
62
|
-
// State machine
|
|
63
|
-
SessionState,
|
|
64
|
-
VALID_TRANSITIONS,
|
|
65
|
-
isValidTransition,
|
|
66
|
-
isOperationalState,
|
|
67
|
-
|
|
68
|
-
// Options
|
|
69
|
-
type SessionOptions,
|
|
70
|
-
DEFAULT_SESSION_OPTIONS,
|
|
71
|
-
|
|
72
|
-
// Callbacks
|
|
73
|
-
type HlaCallbackRequestListener,
|
|
74
|
-
type SessionStateListener,
|
|
75
|
-
type MessageSentListener,
|
|
76
|
-
|
|
77
|
-
// Stats
|
|
78
|
-
type SessionStats,
|
|
79
|
-
} from "./types.ts";
|
|
80
|
-
|
|
81
|
-
// =============================================================================
|
|
82
|
-
// Errors
|
|
83
|
-
// =============================================================================
|
|
84
|
-
export {
|
|
85
|
-
SessionError,
|
|
86
|
-
SessionLostError,
|
|
87
|
-
SessionAlreadyTerminatedError,
|
|
88
|
-
SessionIllegalStateError,
|
|
89
|
-
BadMessageError,
|
|
90
|
-
MessageQueueFullError,
|
|
91
|
-
ConnectionTimeoutError,
|
|
92
|
-
ResponseTimeoutError,
|
|
93
|
-
} from "./errors.ts";
|
|
94
|
-
|
|
95
|
-
// =============================================================================
|
|
96
|
-
// Sequence Numbers
|
|
97
|
-
// =============================================================================
|
|
98
|
-
export {
|
|
99
|
-
// Constants
|
|
100
|
-
NO_SEQUENCE_NUMBER,
|
|
101
|
-
INITIAL_SEQUENCE_NUMBER,
|
|
102
|
-
MAX_SEQUENCE_NUMBER,
|
|
103
|
-
|
|
104
|
-
// Functions
|
|
105
|
-
isValidSequenceNumber,
|
|
106
|
-
nextSequenceNumber,
|
|
107
|
-
addSequenceNumbers,
|
|
108
|
-
isInInterval,
|
|
109
|
-
|
|
110
|
-
// Classes
|
|
111
|
-
SequenceNumber,
|
|
112
|
-
AtomicSequenceNumber,
|
|
113
|
-
} from "./sequence-number.ts";
|
|
114
|
-
|
|
115
|
-
// =============================================================================
|
|
116
|
-
// Timers
|
|
117
|
-
// =============================================================================
|
|
118
|
-
export { TimeoutTimer, OneShotTimer } from "./timeout-timer.ts";
|
|
119
|
-
|
|
120
|
-
// =============================================================================
|
|
121
|
-
// Messages (for advanced use)
|
|
122
|
-
// =============================================================================
|
|
123
|
-
export {
|
|
124
|
-
// Session control messages
|
|
125
|
-
createNewSessionMessage,
|
|
126
|
-
decodeNewSessionStatus,
|
|
127
|
-
createResumeRequestMessage,
|
|
128
|
-
decodeResumeStatus,
|
|
129
|
-
ResumeStatusCode,
|
|
130
|
-
createHeartbeatMessage,
|
|
131
|
-
decodeHeartbeatResponse,
|
|
132
|
-
createTerminateSessionMessage,
|
|
133
|
-
|
|
134
|
-
// HLA messages
|
|
135
|
-
createHlaCallRequestMessage,
|
|
136
|
-
decodeHlaCallResponse,
|
|
137
|
-
decodeHlaCallbackRequest,
|
|
138
|
-
createHlaCallbackResponseMessage,
|
|
139
|
-
|
|
140
|
-
// Types
|
|
141
|
-
type ResumeStatusResult,
|
|
142
|
-
type HlaCallResponseResult,
|
|
143
|
-
type HlaCallbackRequestResult,
|
|
144
|
-
|
|
145
|
-
// Utilities
|
|
146
|
-
describeHeader,
|
|
147
|
-
} from "./messages.ts";
|
|
1
|
+
/**
|
|
2
|
+
* @hla4ts/session - HLA 4 Federate Protocol Session Layer
|
|
3
|
+
*
|
|
4
|
+
* This package provides session management for the HLA 4 Federate Protocol,
|
|
5
|
+
* including connection establishment, state machine, heartbeats, and reconnection.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { Session, SessionState } from '@hla4ts/session';
|
|
10
|
+
* import { TlsTransport } from '@hla4ts/transport';
|
|
11
|
+
*
|
|
12
|
+
* // Create transport and session
|
|
13
|
+
* const transport = new TlsTransport({
|
|
14
|
+
* host: 'rti.example.com',
|
|
15
|
+
* port: 15165,
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* const session = new Session(transport, {
|
|
19
|
+
* connectionTimeout: 30000,
|
|
20
|
+
* responseTimeout: 180000,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Listen for state changes
|
|
24
|
+
* session.addStateListener({
|
|
25
|
+
* onStateTransition: (oldState, newState, reason) => {
|
|
26
|
+
* console.log(`Session: ${oldState} -> ${newState} (${reason})`);
|
|
27
|
+
* if (newState === SessionState.DROPPED) {
|
|
28
|
+
* // Attempt to resume
|
|
29
|
+
* session.resume().catch(console.error);
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Start the session
|
|
35
|
+
* await session.start({
|
|
36
|
+
* onHlaCallbackRequest: (seqNum, callbackData) => {
|
|
37
|
+
* // Handle RTI callback
|
|
38
|
+
* const response = processCallback(callbackData);
|
|
39
|
+
* session.sendHlaCallbackResponse(seqNum, response);
|
|
40
|
+
* }
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Send HLA calls
|
|
44
|
+
* const response = await session.sendHlaCallRequest(encodedCall);
|
|
45
|
+
*
|
|
46
|
+
* // Clean up
|
|
47
|
+
* await session.terminate();
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @packageDocumentation
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
// =============================================================================
|
|
54
|
+
// Session
|
|
55
|
+
// =============================================================================
|
|
56
|
+
export { Session } from "./session.ts";
|
|
57
|
+
|
|
58
|
+
// =============================================================================
|
|
59
|
+
// Types
|
|
60
|
+
// =============================================================================
|
|
61
|
+
export {
|
|
62
|
+
// State machine
|
|
63
|
+
SessionState,
|
|
64
|
+
VALID_TRANSITIONS,
|
|
65
|
+
isValidTransition,
|
|
66
|
+
isOperationalState,
|
|
67
|
+
|
|
68
|
+
// Options
|
|
69
|
+
type SessionOptions,
|
|
70
|
+
DEFAULT_SESSION_OPTIONS,
|
|
71
|
+
|
|
72
|
+
// Callbacks
|
|
73
|
+
type HlaCallbackRequestListener,
|
|
74
|
+
type SessionStateListener,
|
|
75
|
+
type MessageSentListener,
|
|
76
|
+
|
|
77
|
+
// Stats
|
|
78
|
+
type SessionStats,
|
|
79
|
+
} from "./types.ts";
|
|
80
|
+
|
|
81
|
+
// =============================================================================
|
|
82
|
+
// Errors
|
|
83
|
+
// =============================================================================
|
|
84
|
+
export {
|
|
85
|
+
SessionError,
|
|
86
|
+
SessionLostError,
|
|
87
|
+
SessionAlreadyTerminatedError,
|
|
88
|
+
SessionIllegalStateError,
|
|
89
|
+
BadMessageError,
|
|
90
|
+
MessageQueueFullError,
|
|
91
|
+
ConnectionTimeoutError,
|
|
92
|
+
ResponseTimeoutError,
|
|
93
|
+
} from "./errors.ts";
|
|
94
|
+
|
|
95
|
+
// =============================================================================
|
|
96
|
+
// Sequence Numbers
|
|
97
|
+
// =============================================================================
|
|
98
|
+
export {
|
|
99
|
+
// Constants
|
|
100
|
+
NO_SEQUENCE_NUMBER,
|
|
101
|
+
INITIAL_SEQUENCE_NUMBER,
|
|
102
|
+
MAX_SEQUENCE_NUMBER,
|
|
103
|
+
|
|
104
|
+
// Functions
|
|
105
|
+
isValidSequenceNumber,
|
|
106
|
+
nextSequenceNumber,
|
|
107
|
+
addSequenceNumbers,
|
|
108
|
+
isInInterval,
|
|
109
|
+
|
|
110
|
+
// Classes
|
|
111
|
+
SequenceNumber,
|
|
112
|
+
AtomicSequenceNumber,
|
|
113
|
+
} from "./sequence-number.ts";
|
|
114
|
+
|
|
115
|
+
// =============================================================================
|
|
116
|
+
// Timers
|
|
117
|
+
// =============================================================================
|
|
118
|
+
export { TimeoutTimer, OneShotTimer } from "./timeout-timer.ts";
|
|
119
|
+
|
|
120
|
+
// =============================================================================
|
|
121
|
+
// Messages (for advanced use)
|
|
122
|
+
// =============================================================================
|
|
123
|
+
export {
|
|
124
|
+
// Session control messages
|
|
125
|
+
createNewSessionMessage,
|
|
126
|
+
decodeNewSessionStatus,
|
|
127
|
+
createResumeRequestMessage,
|
|
128
|
+
decodeResumeStatus,
|
|
129
|
+
ResumeStatusCode,
|
|
130
|
+
createHeartbeatMessage,
|
|
131
|
+
decodeHeartbeatResponse,
|
|
132
|
+
createTerminateSessionMessage,
|
|
133
|
+
|
|
134
|
+
// HLA messages
|
|
135
|
+
createHlaCallRequestMessage,
|
|
136
|
+
decodeHlaCallResponse,
|
|
137
|
+
decodeHlaCallbackRequest,
|
|
138
|
+
createHlaCallbackResponseMessage,
|
|
139
|
+
|
|
140
|
+
// Types
|
|
141
|
+
type ResumeStatusResult,
|
|
142
|
+
type HlaCallResponseResult,
|
|
143
|
+
type HlaCallbackRequestResult,
|
|
144
|
+
|
|
145
|
+
// Utilities
|
|
146
|
+
describeHeader,
|
|
147
|
+
} from "./messages.ts";
|