Skip to content

Commit

Permalink
Merge pull request #24 from gelic-idealab/dev
Browse files Browse the repository at this point in the history
v1.1.0: Private and message-based state catch-up, Sync namespace, Session.js, Connection Info, Socket Repair Center
  • Loading branch information
Barasakar authored Nov 30, 2021
2 parents 1b91f13 + 6d1db98 commit a5f4c7d
Show file tree
Hide file tree
Showing 13 changed files with 3,710 additions and 1,935 deletions.
28 changes: 14 additions & 14 deletions admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,32 @@ function JSONStringifyCircular(obj) {
const seen = new WeakSet();
return JSON.stringify (obj, (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
if (seen.has(value)) {
return;
}

seen.add(value);
}

return value;
});
}

module.exports = {
init: function (io, syncServer, chatServer) {
init: function (io, logger, syncServer, chatServer) {
var admin = io.of('/admin');

admin.use((socket, next) => {
//TODO(Brandon) - ADD AUTHENTICATION HERE!!! (should we use https://www.npmjs.com/package/socketio-auth ? )
next();
});

admin.on('connection', function(socket) { //TODO finish or remove.
admin.on('connection', function(socket) {
//TODO finish or remove.
// TODO(Brandon): log connection here
socket.emit("adminInfo", socket.id);

socket.on('getAllSessions0', function() {

this.sessions = syncServer.sessions;

socket.emit('receiveAllSessions0', JSONStringifyCircular(Array.from(sessions.entries())));
Expand Down Expand Up @@ -94,7 +97,6 @@ module.exports = {
});

socket.on('sessionsWithDetails', function () {

this.sessions = syncServer.sessions;

var result = fromEntries(this.sessions);
Expand All @@ -103,17 +105,14 @@ module.exports = {
});

socket.on('stateClientsSockets', function () {

this.sessions = syncServer.sessions;

var stateClientsSockets = {};

this.sessions.forEach((session, session_id, map) => {

stateClientsSockets[session_id] = {};

stateClientsSockets[session_id].state = {

clients: session.clients,
entities: session.entities,
scene: session.scene,
Expand All @@ -123,7 +122,6 @@ module.exports = {
stateClientsSockets[session_id].clientsAndSockets = [];

for (var socket_id in session.sockets) {

var client_id = session.sockets[socket_id].client_id;

stateClientsSockets[session_id].clientsAndSockets.push(`${client_id} - ${socket_id}`);
Expand All @@ -149,13 +147,11 @@ module.exports = {
var sockets = io.of("/").sockets;

for (var socket_id in sockets) {

let curSocketObj = sockets[socket_id];

socketsAndRooms[socket_id] = [];

for (var room_id in curSocketObj.rooms) {

socketsAndRooms[socket_id].push(room_id);
}
}
Expand All @@ -179,5 +175,9 @@ module.exports = {
socket.emit('clients', JSON.stringify(sessionToClient));
});
});

logger.info(`Admin namespace is waiting for connections...`);

return admin;
}
};
6 changes: 5 additions & 1 deletion chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
var chat = io.of('/chat');

chat.on('connection', function(socket) {
if (logger) logger.info(`Chat connection: ${socket.id}`);
// TODO(Brandon): log connection here

// setup text chat relay
socket.on('micText', function(data) {
Expand Down Expand Up @@ -142,5 +142,9 @@ module.exports = {
}
});
});

logger.info(`Chat namespace is waiting for connections...`);

return chat;
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Relay server for Komodo VR client application",
"main": "serve.js",
"scripts": {
"test": "nyc mocha --exit"
"test": "nyc mocha --debug-brk --exit"
},
"author": "Grainger IDEA Lab",
"license": "NCSA",
Expand Down
12 changes: 6 additions & 6 deletions serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ if (config.db.host && config.db.host != "") {

testQuery = pool.query(`SHOW TABLES;`, (err, res) => {
if (err) {
if (logger) logger.error(err);
if (logger) logger.error(`Tried to connect to database: ${err}`);

process.exit();
} else {
if (logger) logger.info(`Database initialized with ${res.length} tables.`);
}

else { if (logger) logger.info(`Database initialized with ${res.length} tables.`); }
});

if (logger) logger.info(`Database pool created: host: ${config.db.host}, database: ${config.db.database}.`);
Expand All @@ -97,8 +97,8 @@ io.listen(PORT, {

if (logger) logger.info(`Komodo relay is running on :${PORT}`);

syncServer.init(io, pool, logger);
var chatNamespace = chatServer.init(io, logger);

chatServer.init(io, logger);
var adminNamespace = adminServer.init(io, logger, syncServer, chatServer);

adminServer.init(io, syncServer, chatServer);
syncServer.init(io, pool, logger, chatNamespace, adminNamespace);
231 changes: 231 additions & 0 deletions session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
class Session {
constructor(id) {
this.id = id;
this.sockets = {}; // socket.id -> client_id
this.clients = [];
this.entities = [];
this.scene = null;
this.isRecording = false;
this.start = Date.now();
this.recordingStart = 0;
this.seq = 0;
// NOTE(rob) = DEPRECATED; use message_buffer. 8/3/2021
// writers = {
// pos = {
// buffer = Buffer.alloc(this.positionWriteBufferSize());
// cursor = 0
// };
// int = {
// buffer = Buffer.alloc(this.interactionWriteBufferSize());
// cursor = 0
// }
// };
this.message_buffer = [];
}

getId() {
return this.id;
}

getSockets() {
return this.sockets;
}

addSocket(socket, client_id) {
this.sockets[socket.id] = { client_id: client_id, socket: socket };
}

// Returns success = true if operation succeeded or false if socket or session with id were null.
// Returns isInSession if socket is in this.sockets.
hasSocket(socket) {
return socket.id in this.sockets;
}

removeSocket(socket) {
let isInSession = this.hasSocket(socket);

if (!isInSession) {
return false;
}

delete this.sockets[socket.id];

return true;
}

getSocketsFromClientId(client_id, excluded_socket_id) {
if (this.sockets == null) {
this.logErrorSessionClientSocketAction(
this.id,
client_id,
null,
`tried to get session sockets from client ID, but this.sockets was null`
);

return null;
}

var result = [];

for (var candidate_socket_id in this.sockets) {
let isCorrectId =
this.sockets[candidate_socket_id].client_id == client_id;

let doExclude =
this.sockets[candidate_socket_id].socket.id == excluded_socket_id;

if (isCorrectId && !doExclude) {
result.push(this.sockets[candidate_socket_id].socket);
}
}

return result;
}

getClients() {
return this.clients;
}

addClient(client_id) {
if (
this.clients == null ||
typeof this.clients === "undefined" ||
this.clients.length == 0
) {
this.clients = [client_id];

return true;
}

this.clients.push(client_id);

return true;
}

removeClient(client_id) {
if (this.clients == null) {
return false;
}

let index = this.clients.indexOf(client_id);

if (this.clients.length == 0 || this.clients.indexOf(client_id) == -1) {
//client_id is not in the array, so we don't need to remove it.
return false;
}

this.clients.splice(index, 1);
}

removeDuplicateClients(client_id) {
if (this.clients == null) {
this.logErrorSessionClientSocketAction(
this.id,
client_id,
null,
`tried to remove duplicate client from session, but this.clients was null`
);

return;
}

if (this.clients.length == 0) {
return;
}

const first_instance = this.clients.indexOf(client_id);

for (let i = 0; i < this.clients.length; i += 1) {
if (i != first_instance && this.clients[i] == client_id) {
this.clients.splice(i, 1);
}
}
}

hasClient (client_id) {
const numInstances = this.getNumClientInstances(client_id);

if (numInstances >= 1) {
return true;
}

return false;
}

getNumClientInstances(client_id) {
if (this.clients == null) {
this.logErrorSessionClientSocketAction(
this.id,
client_id,
null,
`Could not get number of client instances -- session was null or session.clients was null.`
);

return -1;
}

var count = 0;

this.clients.forEach((value) => {
if (value == client_id) {
count += 1;
}
});

return count;
}

getTotalNumInstancesForAllClients () {
if (this.clients == null) {
this.logWarningSessionClientSocketAction(
this.id,
null,
null,
`the session's session.clients was null.`
);

return -1;
}

return session.clients.length;
}

getClientIdFromSocket(socket) {
if (this.sockets == null) {
this.logErrorSessionClientSocketAction(
this.id,
client_id,
null,
`tried to get client ID from session socket, but this.sockets was null`
);

return null;
}

if (socket.id == null) {
this.logErrorSessionClientSocketAction(
this.id,
client_id,
null,
`tried to get client ID from session socket, but socket.id was null`
);

return null;
}

if (this.sockets[socket.id] == null) {
this.logErrorSessionClientSocketAction(
this.id,
client_id,
null,
`tried to get client ID from session socket, but this.sockets[socket.id] was null`
);

return null;
}

return this.sockets[socket.id].client_id;
}
}

module.exports = Session;
Loading

0 comments on commit a5f4c7d

Please sign in to comment.