diff options
author | Matthew Wild <mwild1@gmail.com> | 2015-11-26 00:19:31 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2015-11-26 00:19:31 +0000 |
commit | 630248084188d2bbc01da354ec022db6b18aebc7 (patch) | |
tree | 1584719c9dc1939806c4e9f35c3cc4bd7e368320 /util | |
parent | c829b49476995a24a512c6aa1d7626df5acfc9d3 (diff) | |
parent | 0ad40f774a7fd8334289f9ef256e4ff082ce762d (diff) | |
download | prosody-630248084188d2bbc01da354ec022db6b18aebc7.tar.gz prosody-630248084188d2bbc01da354ec022db6b18aebc7.zip |
Merge 0.10->trunk
Diffstat (limited to 'util')
-rw-r--r-- | util/cache.lua | 73 | ||||
-rw-r--r-- | util/session.lua | 65 |
2 files changed, 108 insertions, 30 deletions
diff --git a/util/cache.lua b/util/cache.lua index 9453697c..72b74351 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -1,11 +1,3 @@ -local cache_methods = {}; -local cache_mt = { __index = cache_methods }; - -local function new(size) - assert(size > 0, "cache size must be greater than zero"); - local data = {}; - return setmetatable({ data = data, count = 0, size = size, head = nil, tail = nil }, cache_mt); -end local function _remove(list, m) if m.prev then @@ -14,29 +6,32 @@ local function _remove(list, m) if m.next then m.next.prev = m.prev; end - if list.tail == m then - list.tail = m.prev; + if list._tail == m then + list._tail = m.prev; end - if list.head == m then - list.head = m.next; + if list._head == m then + list._head = m.next; end - list.count = list.count - 1; + list._count = list._count - 1; end local function _insert(list, m) - if list.head then - list.head.prev = m; + if list._head then + list._head.prev = m; end - m.prev, m.next = nil, list.head; - list.head = m; - if not list.tail then - list.tail = m; + m.prev, m.next = nil, list._head; + list._head = m; + if not list._tail then + list._tail = m; end - list.count = list.count + 1; + list._count = list._count + 1; end +local cache_methods = {}; +local cache_mt = { __index = cache_methods }; + function cache_methods:set(k, v) - local m = self.data[k]; + local m = self._data[k]; if m then -- Key already exists if v ~= nil then @@ -47,27 +42,33 @@ function cache_methods:set(k, v) else -- Remove from list _remove(self, m); - self.data[k] = nil; + self._data[k] = nil; end - return; + return true; end -- New key if v == nil then - return; + return true; end -- Check whether we need to remove oldest k/v - if self.count == self.size then - self.data[self.tail.key] = nil; - _remove(self, self.tail); + if self._count == self.size then + local tail = self._tail; + local on_evict = self._on_evict; + if on_evict then + on_evict(tail.key, tail.value); + end + _remove(self, tail); + self._data[tail.key] = nil; end m = { key = k, value = v, prev = nil, next = nil }; - self.data[k] = m; + self._data[k] = m; _insert(self, m); + return true; end function cache_methods:get(k) - local m = self.data[k]; + local m = self._data[k]; if m then return m.value; end @@ -75,7 +76,7 @@ function cache_methods:get(k) end function cache_methods:items() - local m = self.head; + local m = self._head; return function () if not m then return; @@ -86,6 +87,18 @@ function cache_methods:items() end end +function cache_methods:count() + return self._count; +end + +local function new(size, on_evict) + size = assert(tonumber(size), "cache size must be a number"); + size = math.floor(size); + assert(size > 0, "cache size must be greater than zero"); + local data = {}; + return setmetatable({ _data = data, _count = 0, size = size, _head = nil, _tail = nil, _on_evict = on_evict }, cache_mt); +end + return { new = new; } diff --git a/util/session.lua b/util/session.lua new file mode 100644 index 00000000..fda7fccd --- /dev/null +++ b/util/session.lua @@ -0,0 +1,65 @@ +local initialize_filters = require "util.filters".initialize; +local logger = require "util.logger"; + +local function new_session(typ) + local session = { + type = typ .. "_unauthed"; + }; + return session; +end + +local function set_id(session) + local id = typ .. tostring(session):match("%x+$"):lower(); + session.id = id; + return session; +end + +local function set_logger(session) + local log = logger.init(id); + session.log = log; + return session; +end + +local function set_conn(session, conn) + session.conn = conn; + session.ip = conn:ip(); + return session; +end + +local function set_send(session) + local conn = session.conn; + if not conn then + function session.send(data) + log("debug", "Discarding data sent to unconnected session: %s", tostring(data)); + return false; + end + return session; + end + local filter = initialize_filters(session); + local w = conn.write; + session.send = function (t) + if t.name then + t = filter("stanzas/out", t); + end + if t then + t = filter("bytes/out", tostring(t)); + if t then + local ret, err = w(conn, t); + if not ret then + session.log("debug", "Error writing to connection: %s", tostring(err)); + return false, err; + end + end + end + return true; + end + return session; +end + +return { + new = new_session; + set_id = set_id; + set_logger = set_logger; + set_conn = set_conn; + set_send = set_send; +} |