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-08-12 12:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deduplicates UniverseConnections
|
|
|
|
*/
|
|
|
|
public static async deduplicateUniverseConnection(
|
|
|
|
universeCache: UniverseCache,
|
|
|
|
universeConnectionArg: UniverseConnection
|
|
|
|
): Promise<UniverseConnection> {
|
|
|
|
let connectionToReturn: UniverseConnection;
|
|
|
|
universeCache.connectionMap.forEach(async existingConnection => {
|
|
|
|
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 {
|
|
|
|
const universeConnection = universeCache.connectionMap.find(universeConnectionArg => {
|
|
|
|
return universeConnectionArg.socketConnection === socketConnectionArg;
|
|
|
|
});
|
|
|
|
return universeConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-13 13:48:20 +00:00
|
|
|
public authenticationRequests: interfaces.IServerCallSubscribeActionPayload[] = [];
|
2019-06-06 21:23:37 +00:00
|
|
|
public subscribedChannels: UniverseChannel[] = [];
|
|
|
|
public authenticatedChannels: UniverseChannel[] = [];
|
|
|
|
public failedToJoinChannels: UniverseChannel[] = [];
|
2019-04-24 16:20:31 +00:00
|
|
|
|
2019-06-07 09:49:10 +00:00
|
|
|
/**
|
|
|
|
* terminates the connection
|
|
|
|
*/
|
2019-08-12 12:59:37 +00:00
|
|
|
public terminateConnection() {
|
2019-06-07 09:49:10 +00:00
|
|
|
this.socketConnection.socket.disconnect();
|
|
|
|
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: {
|
|
|
|
socketConnection: plugins.smartsocket.SocketConnection;
|
2019-08-12 12:59:37 +00:00
|
|
|
authenticationRequests: interfaces.IServerCallSubscribeActionPayload[];
|
2019-06-06 21:23:37 +00:00
|
|
|
}) {
|
2019-08-13 13:48:20 +00:00
|
|
|
this.authenticationRequests = optionsArg.authenticationRequests;
|
2019-08-12 12:59:37 +00:00
|
|
|
this.socketConnection = optionsArg.socketConnection;
|
2019-04-24 16:20:31 +00:00
|
|
|
}
|
2019-06-06 21:23:37 +00:00
|
|
|
}
|