2019-04-24 16:20:31 +00:00
|
|
|
import * as plugins from './smartuniverse.plugins';
|
2019-08-12 12:59:37 +00:00
|
|
|
import * as interfaces from './interfaces';
|
2019-04-24 16:20:31 +00:00
|
|
|
import { UniverseChannel } from './smartuniverse.classes.universechannel';
|
2019-08-12 12:59:37 +00:00
|
|
|
import { UniverseCache } from './smartuniverse.classes.universecache';
|
2019-08-13 13:48:20 +00:00
|
|
|
import { Universe } from './smartuniverse.classes.universe';
|
2019-04-24 16:20:31 +00:00
|
|
|
|
|
|
|
/**
|
2019-04-30 17:16:03 +00:00
|
|
|
* represents a connection to the universe
|
2019-04-24 16:20:31 +00:00
|
|
|
*/
|
|
|
|
export class UniverseConnection {
|
2019-08-12 12:59:37 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param universeConnectionArg
|
|
|
|
*/
|
|
|
|
public static async addConnectionToCache(
|
2019-08-13 13:48:20 +00:00
|
|
|
universeRef: Universe,
|
2019-08-12 12:59:37 +00:00
|
|
|
universeConnectionArg: UniverseConnection
|
|
|
|
) {
|
|
|
|
let universeConnection = universeConnectionArg;
|
|
|
|
universeConnection = await UniverseConnection.deduplicateUniverseConnection(
|
2019-08-13 13:48:20 +00:00
|
|
|
universeRef.universeCache,
|
2019-08-12 12:59:37 +00:00
|
|
|
universeConnection
|
|
|
|
);
|
|
|
|
universeConnection = await UniverseConnection.authenticateAuthenticationRequests(
|
2019-08-13 13:48:20 +00:00
|
|
|
universeRef,
|
2019-08-12 12:59:37 +00:00
|
|
|
universeConnection
|
|
|
|
);
|
2019-08-13 13:48:20 +00:00
|
|
|
universeRef.universeCache.connectionMap.add(universeConnection);
|
2019-11-03 19:23:22 +00:00
|
|
|
console.log('hi');
|
2019-08-12 12:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deduplicates UniverseConnections
|
|
|
|
*/
|
|
|
|
public static async deduplicateUniverseConnection(
|
|
|
|
universeCache: UniverseCache,
|
|
|
|
universeConnectionArg: UniverseConnection
|
|
|
|
): Promise<UniverseConnection> {
|
|
|
|
let connectionToReturn: UniverseConnection;
|
2020-09-24 18:17:52 +00:00
|
|
|
universeCache.connectionMap.forEach(async (existingConnection) => {
|
2019-08-12 12:59:37 +00:00
|
|
|
if (existingConnection.socketConnection === universeConnectionArg.socketConnection) {
|
|
|
|
connectionToReturn = await this.mergeUniverseConnections(
|
|
|
|
existingConnection,
|
|
|
|
universeConnectionArg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (!connectionToReturn) {
|
|
|
|
connectionToReturn = universeConnectionArg;
|
|
|
|
}
|
|
|
|
return connectionToReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* authenticate AuthenticationRequests
|
|
|
|
*/
|
2019-08-13 13:48:20 +00:00
|
|
|
public static async authenticateAuthenticationRequests(
|
|
|
|
universeRef: Universe,
|
|
|
|
universeConnectionArg: UniverseConnection
|
2019-08-12 12:59:37 +00:00
|
|
|
): Promise<UniverseConnection> {
|
2019-08-13 13:48:20 +00:00
|
|
|
for (const authenticationRequest of universeConnectionArg.authenticationRequests) {
|
2019-09-01 15:04:25 +00:00
|
|
|
const universeChannelToAuthenticateAgainst = UniverseChannel.getUniverseChannelByName(
|
|
|
|
universeRef,
|
|
|
|
authenticationRequest.name
|
|
|
|
);
|
2019-08-13 13:48:20 +00:00
|
|
|
if (universeChannelToAuthenticateAgainst.passphrase === authenticationRequest.passphrase) {
|
|
|
|
universeConnectionArg.authenticatedChannels.push(universeChannelToAuthenticateAgainst);
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 12:59:37 +00:00
|
|
|
return universeConnectionArg;
|
|
|
|
}
|
2019-06-07 09:49:10 +00:00
|
|
|
|
2019-08-12 12:59:37 +00:00
|
|
|
/**
|
|
|
|
* merges two UniverseConnections
|
|
|
|
*/
|
|
|
|
public static mergeUniverseConnections(
|
|
|
|
connectionArg1: UniverseConnection,
|
|
|
|
connectionArg2: UniverseConnection
|
|
|
|
) {
|
|
|
|
return connectionArg1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* finds a UniverseConnection by providing a socket connection
|
|
|
|
*/
|
|
|
|
public static findUniverseConnectionBySocketConnection(
|
|
|
|
universeCache: UniverseCache,
|
|
|
|
socketConnectionArg: plugins.smartsocket.SocketConnection
|
|
|
|
): UniverseConnection {
|
2020-09-24 18:17:52 +00:00
|
|
|
const universeConnection = universeCache.connectionMap.find((universeConnectionArg) => {
|
2019-08-12 12:59:37 +00:00
|
|
|
return universeConnectionArg.socketConnection === socketConnectionArg;
|
|
|
|
});
|
|
|
|
return universeConnection;
|
|
|
|
}
|
|
|
|
|
2019-11-06 23:59:45 +00:00
|
|
|
// INSTANCE
|
|
|
|
public universeRef: Universe;
|
2019-08-12 12:59:37 +00:00
|
|
|
public terminatedDeferred = plugins.smartpromise.defer();
|
2019-06-07 09:49:10 +00:00
|
|
|
|
2019-04-24 16:20:31 +00:00
|
|
|
/**
|
|
|
|
* the socketClient to ping
|
|
|
|
*/
|
2019-06-06 21:23:37 +00:00
|
|
|
public socketConnection: plugins.smartsocket.SocketConnection;
|
2019-09-09 22:29:08 +00:00
|
|
|
public authenticationRequests: Array<interfaces.ISocketRequest_SubscribeChannel['request']> = [];
|
2019-06-06 21:23:37 +00:00
|
|
|
public authenticatedChannels: UniverseChannel[] = [];
|
|
|
|
public failedToJoinChannels: UniverseChannel[] = [];
|
2019-04-24 16:20:31 +00:00
|
|
|
|
2019-06-07 09:49:10 +00:00
|
|
|
/**
|
2019-11-09 11:23:33 +00:00
|
|
|
* disconnect the connection
|
2019-06-07 09:49:10 +00:00
|
|
|
*/
|
2019-11-09 11:23:33 +00:00
|
|
|
public async disconnect(reason: 'upstreamevent' | 'triggered' = 'triggered') {
|
|
|
|
if (reason === 'triggered') {
|
|
|
|
await this.socketConnection.disconnect();
|
|
|
|
}
|
2019-11-06 23:59:45 +00:00
|
|
|
this.universeRef.universeCache.connectionMap.remove(this);
|
2019-06-07 09:49:10 +00:00
|
|
|
this.terminatedDeferred.resolve();
|
2019-06-06 21:23:37 +00:00
|
|
|
}
|
2019-04-24 16:20:31 +00:00
|
|
|
|
2019-06-06 21:23:37 +00:00
|
|
|
constructor(optionsArg: {
|
2019-11-09 11:23:33 +00:00
|
|
|
universe: Universe;
|
2019-06-06 21:23:37 +00:00
|
|
|
socketConnection: plugins.smartsocket.SocketConnection;
|
2019-09-09 22:29:08 +00:00
|
|
|
authenticationRequests: Array<interfaces.ISocketRequest_SubscribeChannel['request']>;
|
2019-06-06 21:23:37 +00:00
|
|
|
}) {
|
2019-11-09 11:59:51 +00:00
|
|
|
this.universeRef = optionsArg.universe;
|
2019-08-13 13:48:20 +00:00
|
|
|
this.authenticationRequests = optionsArg.authenticationRequests;
|
2019-08-12 12:59:37 +00:00
|
|
|
this.socketConnection = optionsArg.socketConnection;
|
2020-09-24 18:17:52 +00:00
|
|
|
this.socketConnection.eventSubject.subscribe(async (eventArg) => {
|
2019-11-09 11:23:33 +00:00
|
|
|
switch (eventArg) {
|
|
|
|
case 'disconnected':
|
|
|
|
await this.disconnect('upstreamevent');
|
|
|
|
break;
|
|
|
|
}
|
2019-11-06 23:59:45 +00:00
|
|
|
});
|
2019-04-24 16:20:31 +00:00
|
|
|
}
|
2019-06-06 21:23:37 +00:00
|
|
|
}
|