Commit 8b9e2a85 by Chavee Issariyapat

rewrite some functions

parent f78abcd2
......@@ -26,8 +26,6 @@ module.exports = function (options = {}) {
var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey);
console.log(req.body)
// checkRole
var checkRealDB = require('./checkClientRole').checkRealDB
checkRealDB(req.body.username, (err, decoded) => {
......@@ -35,34 +33,41 @@ module.exports = function (options = {}) {
// Set Response
var isShadow = require('./utils/getTopic').isShadow
if (isShadow(topic)) {
var _ftopic = require('./utils/getTopic').getTopic(topic, req.body.client_id) // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id) // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
}
}
}
console.log(response)
console.log(response)
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
res.send(response);
});
}
else {
res.send(response);
});
}
else {
res.send(response);
}
next();
}
next();
})
}
else {
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub',_id, req.body.client_id) // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
......
......@@ -31,8 +31,6 @@ module.exports = function (options = {}) {
var cachekey = 'sub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey);
console.log(req.body)
// checkRole
var checkRealDB = require('./checkClientRole').checkRealDB
checkRealDB(req.body.username, (err, decoded) => {
......@@ -40,34 +38,40 @@ module.exports = function (options = {}) {
// Set Response
var isShadow = require('./utils/getTopic').isShadow
if (isShadow(topic)) {
var _ftopic = require('./utils/getTopic').getTopic(topic, req.body.client_id) // get topic where concat with groupID
var _topic = [{ // setTopic for response
'topic': _ftopic,
'qos': 0
}]
var response = {
'result': 'ok',
'topics': _topic
}
console.log(response)
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, req.body.client_id) // get topic where concat with groupID
var _topic = [{ // setTopic for response
'topic': _ftopic,
'qos': 0
}]
var response = {
'result': 'ok',
'topics': _topic
}
console.log(response)
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
res.send(response);
});
}
else {
res.send(response);
});
}
else {
res.send(response);
}
next();
}
next();
})
}
else {
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, req.body.client_id) // get topic where concat with groupID
var _topic = [{ // setTopic for response
'topic': _ftopic,
'qos': 0
......@@ -93,7 +97,9 @@ module.exports = function (options = {}) {
}
}
else {
console.log('>>>>>>>>>>>>>>>>>>>')
console.log(decoded)
console.log('>>>>>>>>>>>>>>>>>>>')
if (decoded.role === 'realtimedb') {
var response = {
'result': 'ok'
......
......@@ -2,9 +2,9 @@ process.env["NODE_ENV"] = "development";
process.env["NODE_CONFIG_DIR"] = __dirname + "/config/";
var auth_on_register_debug = process.env["AUTH_ON_REGISTER_DEBUG"] || false;
var auth_on_publish_debug = process.env["AUTH_ON_PUBLISH_DEBUG"] || false;
var auth_on_subscribe_debug = process.env["AUTH_ON_SUBSCRIBE_DEBUG"] || false;
var on_publish_debug = process.env["ON_PUBLISH_DEBUG"] || false;
var auth_on_publish_debug = process.env["AUTH_ON_PUBLISH_DEBUG"] || true;
var auth_on_subscribe_debug = process.env["AUTH_ON_SUBSCRIBE_DEBUG"] || true;
var on_publish_debug = process.env["ON_PUBLISH_DEBUG"] || true;
var restify = require('restify');
var auth_on_register = require('./auth_on_register')({ debug: auth_on_register_debug });
......
var modify = require('./utils/modify');
function on_deliver(req, res, next) {
var topics = beDesireTopic(req.body.topic)
console.log('----on_deliver-----')
console.log(req.body)
console.log(topics)
var topics = modify.removeTempWordInTopic(req.body.topic)
console.log(req.body)
// console.log(topics)
var response = {
'result': 'ok',
'modifiers': {
"topic": topics
}
}
console.log(response);
res.send(response)
}
module.exports.on_deliver = on_deliver
......@@ -21,13 +24,12 @@ function hasGroup(topics) {
}
module.exports.hasGroup = hasGroup
function beDesireTopic(topics) {
if (hasGroup(topics)) {
return joinTopic(topics)
}
else return topics
}
module.exports.beDesireTopic = beDesireTopic
// function beDesireTopic(topics) {
// if (hasGroup(topics)) {
// return joinTopic(topics)
// }
// else return topics
// }
function joinTopic(topics) {
var topic = topics.split('/!')[1]
......
......@@ -10,7 +10,7 @@ function on_unsubscribe(req, res, next) {
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'unsub', _id, req.body.client_id) // get topic where concat with groupID
var response = {
"result": "ok",
"topics": [_ftopic]
......
function getTopic(topic, id) {
var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1]
return _topic
}
module.exports.getTopic = getTopic
function classifiedTopic(topic) {
if (topic.indexOf('@msg') !== -1) {
return '@msg'
} else if (topic.indexOf('@shadow/updated') !== -1) {
return '@shadow/updated'
} else if (topic.indexOf('@shadow/update') !== -1) {
return '@shadow/update'
} else if (topic.indexOf('@shadow/get') !== -1) {
return '@shadow/get'
} else if (topic.indexOf('@client') !== -1) {
return '@client'
// function getTopic(topic, id) {
// //var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1]
// var _topic = topicPrefix(topic) + id + topic.split(topicPrefix(topic))[1]
// return _topic
// }
// module.exports.getTopic = getTopic
// function topicPrefix(topic) {
// if (topic.indexOf('@msg') !== -1) {
// return '@msg/!'
// } else if (topic.indexOf('@shadow/updated') !== -1) {
// return '@shadow/updated/!'
// } else if (topic.indexOf('@shadow/update') !== -1) {
// return '@shadow/update/'
// } else if (topic.indexOf('@shadow/get') !== -1) {
// return '@shadow/get/!'
// } else if (topic.indexOf('@client') !== -1) {
// return '@client/'
// }
// }
var modify = require('./modify');
function rewriteTopic(topic, op, groupid, clientid) {
var chunk = topic.split('/', 2);
switch (chunk[0]) {
case '@msg' :
return modify.insertWordIntoTopic(topic, '!'+clientid, 1);
case '@shadow' :
if (chunk[1] == 'write')
return modify.insertWordIntoTopic(topic, ''+clientid, 2);
else if (chunk[1] == 'set')
return modify.insertWordIntoTopic(topic, ''+groupid, 2);
else if (chunk[1] == 'changed') {
if (op == 'pub') {
if ( chunk.length > 2 )
return modify.insertWordIntoTopic(topic, '!'+groupid, 2);
else if ( chunk.length == 2 )
return modify.insertWordIntoTopic(topic, '!'+clientid, 2);
else return "";
}
else {
if ( chunk.length > 2 )
return modify.insertWordIntoTopic(topic, '!'+groupid, 2);
else if ( chunk.length == 2 )
return modify.insertWordIntoTopic(topic, '!'+clientid, 2);
else return "";
}
}
else return "";
case '@private' :
return modify.insertWordIntoTopic(topic, '!'+clientid, 1);
}
}
module.exports.rewriteTopic = rewriteTopic
function isShadow(topic) {
if (topic.indexOf('@shadow') !== -1) {
return true
......
module.exports.insertWordIntoTopic = insertWordIntoTopic
module.exports.removeTempWordInTopic = removeTempWordInTopic
/*
Ex: insertWordIntoTopic('@shadow/home/temp', '!GROUP', 1)
expected return : "@shadow/!GROUP/home/temp"
*/
function insertWordIntoTopic(topic, word, pos) {
var a = topic.split('/');
a.splice(pos, 0, word);
// console.log('>>> insertWordIntoTopic('+topic +','+ word +','+ pos +');');
// console.log(a.join('/'));
return a.join('/');
}
/*
Ex : removeTempWordInTopic('@shadow/!GROUP/home/temp')
expected return : "@shadowhome/temp"
*/
function removeTempWordInTopic(topic) {
var a = topic.split('/');
var k=0, l=a.length;
while (k<l) {
if (a[k].substr(0,1)=='!') {
a.splice(k,1);
l--;
}
else k++;
}
return a.join('/');
}
// Sample
// var str = insertWordIntoTopic('@shadow/home/temp', '!GROUP', 0);
// console.log(str);
// console.log(removeTempWordInTopic(str));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment