From 64313c3293df7fea20f5efcfc7e1145bebd03bff Mon Sep 17 00:00:00 2001 From: Ivano Picco Date: Fri, 20 Feb 2015 17:16:57 +0000 Subject: [PATCH 1/2] Explicit gethostname support on Windows --- index.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.lua b/index.lua index 078ac12..79f4afb 100644 --- a/index.lua +++ b/index.lua @@ -11,18 +11,27 @@ local timer = require('timer') local ffi = require ('ffi') local fs = require('fs') local json = require('json') +local os = require ('os') + local success, boundary = pcall(require,'boundary') if (not success) then boundary = nil end +local isWindows = os.type() == 'win32' + -- portable gethostname syscall ffi.cdef [[ int gethostname (char *, int); ]] function gethostname() local buf = ffi.new("uint8_t[?]", 256) - ffi.C.gethostname(buf,256); + if ( not isWindows ) then + ffi.C.gethostname(buf,256) + else + local clib = ffi.load('ws2_32') + clib.gethostname(buf,256) + end return ffi.string(buf) end From 5c53360d01c58c3db485751f315f3786ad023ef7 Mon Sep 17 00:00:00 2001 From: Ivano Picco Date: Sat, 7 Mar 2015 12:03:20 +0000 Subject: [PATCH 2/2] From ffi syscalls to os, tools library and small refactories --- index.lua | 32 ++++--------------- modules/split/package.lua | 7 ---- modules/split/src/init.lua | 15 --------- modules/tools.lua | 65 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 47 deletions(-) delete mode 100644 modules/split/package.lua delete mode 100644 modules/split/src/init.lua create mode 100644 modules/tools.lua diff --git a/index.lua b/index.lua index 79f4afb..aa834e8 100644 --- a/index.lua +++ b/index.lua @@ -1,39 +1,21 @@ -- [boundary.com] Redis Lua Plugin -- [author] Ivano Picco --- Requires. +-- Common requires. local utils = require('utils') -local uv_native = require ('uv_native') -local string = require('string') -local split = require('split') -local redis = require('luvit-redis') local timer = require('timer') -local ffi = require ('ffi') local fs = require('fs') local json = require('json') local os = require ('os') +local tools = require ('tools') local success, boundary = pcall(require,'boundary') if (not success) then boundary = nil end -local isWindows = os.type() == 'win32' - --- portable gethostname syscall -ffi.cdef [[ - int gethostname (char *, int); -]] -function gethostname() - local buf = ffi.new("uint8_t[?]", 256) - if ( not isWindows ) then - ffi.C.gethostname(buf,256) - else - local clib = ffi.load('ws2_32') - clib.gethostname(buf,256) - end - return ffi.string(buf) -end +-- Business requires. +local redis = require('luvit-redis') -- Default parameters. local pollInterval = 10000 @@ -50,7 +32,7 @@ _parameters.pollInterval = _parameters.source = (type(_parameters.source) == 'string' and _parameters.source:gsub('%s+', '') ~= '' and _parameters.source ~= nil and _parameters.source) or - gethostname() + os.hostname() _parameters.host = (type(_parameters.host) == 'string' and _parameters.host:gsub('%s+', '') ~= '' and _parameters.host) or @@ -85,7 +67,7 @@ end -- Parse line (i.e. line: "connected_clients : "). function parseEachLine(line) - local t = split(line,':') + local t = tools.split(line,':') if (#t == 2) then currentValues[t[1]]=t[2]; end @@ -100,7 +82,7 @@ function outputs() utils.print('REDIS_KEY_EVICTIONS', diffvalues('evicted_keys'), _parameters.source) utils.print('REDIS_COMMANDS_PROCESSED', diffvalues('total_commands_processed'), _parameters.source) utils.print('REDIS_CONNECTIONS_RECEIVED', diffvalues('total_connections_received'), _parameters.source) - utils.print('REDIS_USED_MEMORY', currentValues.used_memory_rss / uv_native.getTotalMemory(), _parameters.source) + utils.print('REDIS_USED_MEMORY', currentValues.used_memory_rss / os.totalmem(), _parameters.source) end -- Get current values. diff --git a/modules/split/package.lua b/modules/split/package.lua deleted file mode 100644 index 119a318..0000000 --- a/modules/split/package.lua +++ /dev/null @@ -1,7 +0,0 @@ -return { - name = 'split', - version = '0.0.1', - description = 'split string to a table', - author = 'Ivano Picco ', - main = 'src/init.lua' -} diff --git a/modules/split/src/init.lua b/modules/split/src/init.lua deleted file mode 100644 index af52e92..0000000 --- a/modules/split/src/init.lua +++ /dev/null @@ -1,15 +0,0 @@ -local string=require('string'); - -exports = function (inputstr, sep) - if sep == nil then - sep = "%s" - end - local t={} ; local i=1 - for str in string.gmatch(inputstr, "([^"..sep.."]+)") do - t[i] = str - i = i + 1 - end - return t -end - -return exports \ No newline at end of file diff --git a/modules/tools.lua b/modules/tools.lua new file mode 100644 index 0000000..6430598 --- /dev/null +++ b/modules/tools.lua @@ -0,0 +1,65 @@ +-- +-- Module. +-- +local tools = {} + + +-- Requires. +local string = require('string') + +-- +-- Limit a given number x between two boundaries. +-- Either min or max can be nil, to fence on one side only. +-- +tools.fence = function(x, min, max) + return (min and x < min and min) or (max and x > max and max) or x +end + +-- +-- Encode data in Base64 format. +-- +tools.base64 = function(data) + local _lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + return ((data:gsub('.', function(x) + local r, b = '', x:byte() + for i = 8, 1, -1 do + r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0') + end + return r + end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x) + if #x < 6 then + return '' + end + local c = 0 + for i = 1, 6 do + c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0) + end + return _lookup:sub(c + 1, c + 1) + end) .. ({ + '', + '==', + '=' + })[#data % 3 + 1]) +end + + +-- +-- Split a string into a table +-- + +tools.split = function (inputstr, sep) + if sep == nil then + sep = "%s" + end + local t={} ; local i=1 + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + t[i] = str + i = i + 1 + end + return t +end + +-- +-- Export. +-- +return tools \ No newline at end of file