Skip to content

Commit

Permalink
Merge pull request #65 from janusvr/multiprocessing
Browse files Browse the repository at this point in the history
Method pipelines
  • Loading branch information
bioid authored Jul 24, 2017
2 parents 9cbdb66 + f8f9166 commit b80cc65
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 233 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: node_js
node_js:
- "6"
- "7"
- "8"
services:
- mysql
- redis-server
Expand Down
15 changes: 15 additions & 0 deletions src/MethodMiddleware/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ## user chat ##
function chat(message, next) {

var data = {
roomId: this.currentRoom.id,
userId: this.id,
message: message
};

this.currentRoom.emit('user_chat', data);
return next();
};

module.exports = [chat];

44 changes: 44 additions & 0 deletions src/MethodMiddleware/enter_room.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function enterRoom(data, next) {
if(data.roomId === undefined) {
this.clientError('Missing roomId in data packet');
return;
}

var oldRoomId = null;
if(this.currentRoom) {
oldRoomId = this.currentRoom.id;
this.currentRoom.emit('user_leave', {
userId: this.id,
roomId: this.currentRoom.id,
newRoomId: data.roomId
});
}
//this._server._plugins.call("enter_room", data);
// TODO
this._server._userList[this.id].oldRoomId = oldRoomId;
this._server._userList[this.id].roomId = data.roomId;
if ((data.partyMode == true) || (data.partyMode == "true")) {
if (this._server._partyList[this.id] === undefined) {
this._server._partyList[this.id] = {};

}
if ((data.roomUrl !== undefined) && (data.roomUrl.match('^https?://'))){
this._server._partyList[this.id].roomId = data.roomId;
this._server._partyList[this.id].roomUrl = data.roomUrl;
this._server._partyList[this.id].roomName = (data.roomName === undefined) ? "" : data.roomName;
this._server._partyList[this.id].client_version = this.client_version;
}
} else {
delete this._server._partyList[this.id];
}
this._server.savePartyList();
this.currentRoom = this._server.getRoom(data.roomId);
this.currentRoom.emit('user_enter', {
userId: this.id,
roomId: data.roomId,
oldRoomId: oldRoomId
});
return next();
}

module.exports = [enterRoom];
30 changes: 30 additions & 0 deletions src/MethodMiddleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var norm = require('path').join(__dirname);
var fs = require('fs');
var middleware = require('../Middleware.js');

var imported = {};

var files = fs.readdirSync(norm);
let jsfiles = files.filter(x => {
let split = x.split('.');
return (split[split.length - 1] === 'js' && split[0] !== 'index')
});

var modules = jsfiles.map(x => x.split('.')[0]);

modules.forEach(method => {
imported[method] = require("./"+method);
});

function getMiddleware(context) {
var keys = Object.keys(imported);
var methods = {};
keys.forEach((key) => {
methods[key] = (data) => {
var tasks = imported[key].map(x => x.bind(context));
return middleware(data, tasks, () => {});
};
});
return methods;
}
module.exports = getMiddleware;
52 changes: 52 additions & 0 deletions src/MethodMiddleware/logon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function validateLogon(data, next) {
if(typeof data.userId !== "string" || data.userId === '')
return this.clientError('Missing userId in data packet');

if (!data.userId.match('^[a-zA-Z0-9_]+$'))
return this.clientError('illegal character in user name, only use alphanumeric and underscore');;

if(data.roomId === undefined)
return this.clientError('Missing roomId in data packet');;
return next();
}

function checkNameFree(data, next) {
this._server.isNameFree(data.userId, (err, free) => {
if (!free) return this.clientError('User name is already in use');;
return next();
});
}

function setLogonData(data, next) {
this.id = data.userId;
this._authed = true;
this.client_version =
(data.version === undefined)?"undefined":data.version;

// TODO
this._server._userList[data.userId] = {
roomId: data.roomId,
}
return next();
}

function setRoomData(data, next) {
this.currentRoom = this._server.getRoom(data.roomId);
log.info('User: ' + this.id + ' logged on');
return next();
}

function subscribe(data, next) {
if(data.roomId === undefined)
return this.clientError('Missing roomId in data packet');

var room = this._server.getRoom(data.roomId);

if(this._rooms.indexOf(room) === -1) {
room.addSession(this);
this._rooms.push(room);
}
this.clientOkay();
return next();
}
module.exports = [validateLogon, checkNameFree, setLogonData, setRoomData, subscribe];
14 changes: 14 additions & 0 deletions src/MethodMiddleware/move.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ## user move ##
function move(position, next) {

var data = {
roomId: this.currentRoom.id,
userId: this.id,
position: position
};

this.currentRoom.emit('user_moved', data);
return next();
};

module.exports = [move];
18 changes: 18 additions & 0 deletions src/MethodMiddleware/portal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function portal(portal, next) {

//TODO: Persist portals

var data = {
roomId: this.currentRoom.id,
userId: this.id,
url: portal.url,
pos: portal.pos,
fwd: portal.fwd
};

this.currentRoom.emit('user_portal', data);
this.clientOkay();
return next();
};

module.exports = [portal];
18 changes: 18 additions & 0 deletions src/MethodMiddleware/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function subscribe(data, next) {

if(data.roomId === undefined)
return this.clientError('Missing roomId in data packet');

var room = this._server.getRoom(data.roomId);

if(this._rooms.indexOf(room) === -1) {
room.addSession(this);
this._rooms.push(room);
}

this.clientOkay();
return next();
};

module.exports = [subscribe];

21 changes: 21 additions & 0 deletions src/MethodMiddleware/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

function unsubscribe(data, next) {

if(data.roomId === undefined)
return this.clientError('Missing roomId in data packet');

var room = this._server.getRoom(data.roomId);
var i = this._rooms.indexOf(room);
if(i !== -1) {
room.removeSession(this);
this._rooms.splice(i,1);
}
if (room.isEmpty())
delete this._server._rooms[data.roomId];

this.clientOkay();
return next();
};

module.exports = [unsubscribe];

32 changes: 32 additions & 0 deletions src/MethodMiddleware/users_online.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function usersOnline(data, next) {
var maxResults = config.maxUserResults;
var count = 0;
var results = Array();

if(data.maxResults !== undefined && data.maxResults < maxResults) maxResults = data.maxResults;

if(data.roomId === undefined) {
// TODO
for(k in this._server._userList) {
results.push(k);
count++;
if(count >= maxResults) break;
}
}
else {
for(k in this._server._userList) {
if(this._server._userList[k].roomId == data.roomId) {
results.push([k]);
count++;
if(count >= maxResults) break;
}
}
}

json = { "results": count, "roomId": data.roomId, "users": results };
this.send(this.makeMessage('users_online', json));
return next();
}

module.exports = [usersOnline];

13 changes: 13 additions & 0 deletions src/Middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function middleware(data, tasks, cb) {
function iterate(idx) {
if (idx === tasks.length && typeof cb === "function")
return cb();
const task = tasks[idx];
task(data, () => {
iterate(idx + 1)
});
}
return iterate(0);
}

module.exports = middleware;
Loading

0 comments on commit b80cc65

Please sign in to comment.