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-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(
|
|
|
|
universeCache: UniverseCache,
|
|
|
|
universeConnectionArg: UniverseConnection
|
|
|
|
) {
|
|
|
|
let universeConnection = universeConnectionArg;
|
|
|
|
universeConnection = await UniverseConnection.deduplicateUniverseConnection(
|
|
|
|
universeCache,
|
|
|
|
universeConnection
|
|
|
|
);
|
|
|
|
universeConnection = await UniverseConnection.authenticateAuthenticationRequests(
|
|
|
|
universeConnection
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public static authenticateAuthenticationRequests(
|
|
|
|
universeConnectionArg
|
|
|
|
): Promise<UniverseConnection> {
|
|
|
|
// TODO: authenticate connections
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
// TODO: merge connections
|
|
|
|
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-06-07 09:49:10 +00:00
|
|
|
public authenticationRequests = [];
|
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-06-10 15:46:06 +00:00
|
|
|
// TODO: check if this is correct
|
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
|
|
|
}
|