Skip to content

Commit b7b85ac

Browse files
committed
wip: handling connection events
Still need to hook stream events to the RPC handlers. [ci skip]
1 parent c86e164 commit b7b85ac

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/nodes/NodeConnectionManager.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import type {
1717
} from './types';
1818
import type KeyRing from '../keys/KeyRing';
1919
import type { Key, CertificatePEM } from '../keys/types';
20-
import type { Host, Hostname, Port } from '../network/types';
21-
import type { RPCStream } from '../rpc/types';
20+
import type { ConnectionData, Host, Hostname, Port } from '../network/types';
2221
import type { TLSConfig } from '../network/types';
2322
import type { HolePunchRelayMessage } from '../agent/handlers/types';
2423
import Logger from '@matrixai/logger';
@@ -849,7 +848,8 @@ class NodeConnectionManager {
849848
// The connection event should only contain connection metadata and not the connection itself otherwise we circumvent the locking.
850849
// Setting up events
851850
const nodeConnectionEventsHandler = (e) => {
852-
console.log(e);
851+
// Propagate all events upwards
852+
this.dispatchEvent(e.clone());
853853
};
854854
nodeConnection.addEventListener(nodeConnectionEventsHandler);
855855
nodeConnection.addEventListener(
@@ -883,6 +883,16 @@ class NodeConnectionManager {
883883
usageCount: 0,
884884
};
885885
this.connections.set(nodeIdString, newConnAndTimer);
886+
const connectionData: ConnectionData = {
887+
remoteNodeId: nodeConnection.nodeId,
888+
remoteHost: nodeConnection.host,
889+
remotePort: nodeConnection.port,
890+
};
891+
this.dispatchEvent(
892+
new nodesEvents.EventNodeConnectionManagerConnection({
893+
detail: connectionData,
894+
}),
895+
);
886896
return newConnAndTimer;
887897
}
888898

src/nodes/NodeManager.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { IdInternal } from '@matrixai/id';
3737
import { timedCancellable, context } from '@matrixai/contexts/dist/decorators';
3838
import * as nodesErrors from './errors';
3939
import * as nodesUtils from './utils';
40+
import * as nodesEvents from './events';
4041
import * as claimsUtils from '../claims/utils';
4142
import * as tasksErrors from '../tasks/errors';
4243
import * as claimsErrors from '../claims/errors';
@@ -190,6 +191,15 @@ class NodeManager {
190191
public readonly checkSeedConnectionsHandlerId: TaskHandlerId =
191192
`${this.basePath}.${this.checkSeedConnectionsHandler.name}.checkSeedConnectionsHandler` as TaskHandlerId;
192193

194+
handleNodeConnectionEvent = (
195+
e: nodesEvents.EventNodeConnectionManagerConnection,
196+
) => {
197+
this.setNode(e.detail.remoteNodeId, {
198+
host: e.detail.remoteHost,
199+
port: e.detail.remotePort,
200+
});
201+
};
202+
193203
constructor({
194204
db,
195205
keyRing,
@@ -259,11 +269,21 @@ class NodeManager {
259269
lazy: true,
260270
path: [this.basePath, this.checkSeedConnectionsHandlerId],
261271
});
272+
// Add handling for connections
273+
this.nodeConnectionManager.addEventListener(
274+
nodesEvents.EventNodeConnectionManagerConnection.name,
275+
this.handleNodeConnectionEvent,
276+
);
262277
this.logger.info(`Started ${this.constructor.name}`);
263278
}
264279

265280
public async stop() {
266281
this.logger.info(`Stopping ${this.constructor.name}`);
282+
// Remove handling for connections
283+
this.nodeConnectionManager.removeEventListener(
284+
nodesEvents.EventNodeConnectionManagerConnection.name,
285+
this.handleNodeConnectionEvent,
286+
);
267287
this.logger.info('Cancelling ephemeral tasks');
268288
if (this.taskManager.isProcessing()) {
269289
throw new tasksErrors.ErrorTaskManagerProcessing();

src/nodes/events.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { QUICStream } from '@matrixai/quic';
2+
import type { ConnectionData } from '@/network/types';
23

34
type OptDetail<T> = T extends undefined ? {} : { detail: T };
45

@@ -36,4 +37,18 @@ class EventNodeStream extends EventsNode<QUICStream> {
3637
}
3738
}
3839

39-
export { EventNodeConnectionDestroy, EventNodeStream };
40+
abstract class EventNodeConnectionManager<
41+
T = undefined,
42+
> extends EventsNode<T> {}
43+
44+
class EventNodeConnectionManagerConnection extends EventNodeConnectionManager<ConnectionData> {
45+
constructor(options?: EventInit & { detail: ConnectionData }) {
46+
super(EventNodeConnectionManagerConnection.name, options);
47+
}
48+
}
49+
50+
export {
51+
EventNodeConnectionDestroy,
52+
EventNodeStream,
53+
EventNodeConnectionManagerConnection,
54+
};

0 commit comments

Comments
 (0)