| 1 | -- flaw, a Lua OO management framework for Awesome WM widgets. |
| 2 | -- Copyright (C) 2009,2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net> |
| 3 | |
| 4 | -- This program is free software: you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation, either version 3 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | |
| 14 | -- You should have received a copy of the GNU General Public License |
| 15 | -- along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | |
| 18 | -- Grab environment. |
| 19 | local pairs = pairs |
| 20 | local os = os |
| 21 | local setmetatable = setmetatable |
| 22 | |
| 23 | local capi = { |
| 24 | timer = timer, |
| 25 | } |
| 26 | |
| 27 | local flaw = { |
| 28 | helper = require('flaw.helper'), |
| 29 | } |
| 30 | |
| 31 | |
| 32 | --- Providers core mechanisms. |
| 33 | -- |
| 34 | -- <p>In <b>flaw</b>, all data is gathered by provider objects which, |
| 35 | -- in an attempt to minimize resources usage, can be shared among |
| 36 | -- gadgets. The providers can poll disks or memory at a given rate for |
| 37 | -- their information, or can be updated by scripts or direct |
| 38 | -- <b>Lua</b> invocation, like <b>awful</b> callbacks. When updated, |
| 39 | -- providers notify the gadgets that have subscribed to them.</p> |
| 40 | -- |
| 41 | -- <p>Providers are normally handled automatically when a gadget is |
| 42 | -- created. You only have to take care of them when you are writing |
| 43 | -- your own gadget, or if you want to create a new provider, or extend |
| 44 | -- an existing one. <b>flaw</b> already implements many providers for |
| 45 | -- common system information. The existing providers are usually |
| 46 | -- defined in the module of the widget type they serve.</p> |
| 47 | -- |
| 48 | -- <p>A provider is identified by its type and an identifier, which |
| 49 | -- must remain unique for one type. The provider type usually |
| 50 | -- represents the module of this provider and can be composed of any |
| 51 | -- character. Thus, it is common to create a new provider prototype |
| 52 | -- this way:</p> |
| 53 | -- |
| 54 | -- <div class='example'> |
| 55 | -- flaw.provider.Provider:new{ type = _NAME } |
| 56 | -- </div> |
| 57 | -- |
| 58 | -- <p>All created providers are kept in a <a |
| 59 | -- href='#_providers_cache'>global store</a> from which they can be |
| 60 | -- retrieved anytime. Note that this store has normally no use for the |
| 61 | -- user, but allows gadgets to share providers.</p> |
| 62 | -- |
| 63 | -- @author David Soulayrol <david.soulayrol AT gmail DOT com> |
| 64 | -- @copyright 2009,2010,2011 David Soulayrol |
| 65 | module('flaw.provider') |
| 66 | |
| 67 | |
| 68 | --- Global providers store. |
| 69 | -- |
| 70 | -- <p>This table stores all the registered provider |
| 71 | -- instances. Providers are sorted by their type first, and then by |
| 72 | -- their ID. The store is private to the <code>provider</code> |
| 73 | -- module. It can be accessed using its <a |
| 74 | -- href='#get'><code>get</code></a>, and <a |
| 75 | -- href='#add'><code>add</code></a> functions.</p> |
| 76 | -- |
| 77 | -- @class table |
| 78 | -- @name _providers_cache |
| 79 | local _providers_cache = {} |
| 80 | |
| 81 | --- Store a provider instance. |
| 82 | -- |
| 83 | -- <p>This function stores the given provider in the global providers |
| 84 | -- store. It fails if the instance is invalid, that is if it is nil or |
| 85 | -- if its type is nil.</p> |
| 86 | -- |
| 87 | -- <p>You normally do not have to call this function since it is |
| 88 | -- silently invoked each time a gadget instantiates its provider.</p> |
| 89 | -- |
| 90 | -- @param p the provider prototype to store. |
| 91 | function add(p) |
| 92 | if p == nil or p.id == nil then |
| 93 | flaw.helper.debug.error('flaw.provider.provider_add: invalid provider.') |
| 94 | else |
| 95 | if _providers_cache[p.type] == nil then |
| 96 | _providers_cache[p.type] = {} |
| 97 | end |
| 98 | _providers_cache[p.type][p.id] = p |
| 99 | end |
| 100 | end |
| 101 | |
| 102 | --- Retrieve a provider instance. |
| 103 | -- |
| 104 | -- <p>This function returns the provider matching the given |
| 105 | -- information. It immediately fails if the given type or identifier |
| 106 | -- is nil. It also fails if no instance in the store matches the given |
| 107 | -- parameters.</p> |
| 108 | -- |
| 109 | -- @param type the type of the provider to retrieve. |
| 110 | -- @param id the uniquer identifier of the provider to retrieve. |
| 111 | -- @return The matching provider instance, or nil if information was |
| 112 | -- incomplete or if there is no such provider. |
| 113 | function get(type, id) |
| 114 | if type == nil or id == nil then |
| 115 | flaw.helper.debug.error('flaw.provider.provider_get: invalid information.') |
| 116 | else |
| 117 | return _providers_cache[type] ~= nil |
| 118 | and _providers_cache[type][id] or nil |
| 119 | end |
| 120 | end |
| 121 | |
| 122 | |
| 123 | --- The Provider prototype. |
| 124 | -- |
| 125 | -- <p>This is the root prototype of all providers. It defines nothing |
| 126 | -- but the prototype's type and a method for gadgets subscription.</p> |
| 127 | -- |
| 128 | -- @class table |
| 129 | -- @name Provider |
| 130 | Provider = { type = 'unknown' } |
| 131 | |
| 132 | --- Provider constructor. |
| 133 | -- |
| 134 | -- <p>Remember that providers are normally handled automatically when |
| 135 | -- a gadget is created. This constructor is only used internally, or |
| 136 | -- to create new gadget prototypes.</p> |
| 137 | -- |
| 138 | -- @param o a table with default values. |
| 139 | -- @return The brand new provider. |
| 140 | function Provider:new(o) |
| 141 | o = o or {} |
| 142 | o.data = o.data or {} |
| 143 | o.subscribers = o.subscribers or {} |
| 144 | setmetatable(o, self) |
| 145 | self.__index = self |
| 146 | return o |
| 147 | end |
| 148 | |
| 149 | --- Subscribe a gadget to this provider. |
| 150 | -- |
| 151 | -- <p>This is the method a gadget automatically uses, when created by |
| 152 | -- the gadget factory, to register itself to its provider. The root |
| 153 | -- provider does strictly nothing with this subscription, but any |
| 154 | -- active provider uses the subscribers list to update its |
| 155 | -- gadgets.</p> |
| 156 | -- |
| 157 | -- <p>This method immediately fails if the given gadget is nil. On |
| 158 | -- success, the gadget is stored in the provider internal subscribers |
| 159 | -- list.</p> |
| 160 | -- |
| 161 | -- @param g the gadget subscriber. |
| 162 | -- @return True if the subscriber was correctly stored, False otherwise. |
| 163 | function Provider:subscribe(g) |
| 164 | if g == nil then |
| 165 | flaw.helper.debug.error('flaw.provider.Provider:subscribe: invalid gadget') |
| 166 | return false |
| 167 | end |
| 168 | self.subscribers[g] = { timestamp = os.time() } |
| 169 | g:update() |
| 170 | return true |
| 171 | end |
| 172 | |
| 173 | --- Refresh gadgets subscribed to this provider. |
| 174 | -- |
| 175 | -- <p>This method is a facility for providers to invoke all the |
| 176 | -- gadgets refresh when they are done with data gathering.</p> |
| 177 | function Provider:refresh_gadgets() |
| 178 | for g, props in pairs(self.subscribers) do |
| 179 | props['timestamp'] = self.timestamp |
| 180 | g:update() |
| 181 | end |
| 182 | end |
| 183 | |
| 184 | |
| 185 | --- The Cyclic Provider prototype. |
| 186 | -- |
| 187 | -- <p>This specialized provider is the root of all the providers |
| 188 | -- needing to provide cyclic information, based on files, network and |
| 189 | -- so on, rather than on events. Cyclic providers poll resources |
| 190 | -- status from system only when necessary (ie. when the gadget with |
| 191 | -- the shortest refresh rate demands it).</p> |
| 192 | -- |
| 193 | -- <p>The CyclicProvider relies on a C API timer. The timer timeout |
| 194 | -- value defaults to 60 seconds but is updated each time a gadget |
| 195 | -- starts to use the provider, if its own rate is shorter. Note that |
| 196 | -- the higher rate demand only will be served in time, all others will |
| 197 | -- be refreshed using a comparison between their timestamp and the |
| 198 | -- refresh date. For example, if a provider is asked to refresh every |
| 199 | -- 10 seconds and then every 3 seconds, its timer will be set to 3 |
| 200 | -- seconds. The second gadget will be refreshed exactly in time, |
| 201 | -- whereas the first will be refreshed at 12 seconds, 21 seconds, 30 |
| 202 | -- seconds, and so on.</p> |
| 203 | -- |
| 204 | -- <p>All CyclicProvider also define a <code>timestamp</code> value |
| 205 | -- which can be used in specific treatments to know the last refresh |
| 206 | -- date.</p> |
| 207 | -- |
| 208 | -- @class table |
| 209 | -- @name CyclicProvider |
| 210 | CyclicProvider = Provider:new{ type = 'unknown.cyclic', timestamp = 0, timer = nil } |
| 211 | |
| 212 | --- Subscribe a gadget to this provider. |
| 213 | -- |
| 214 | -- <p>The CyclicProvider specialised method adds the poll timer |
| 215 | -- handling to the parent method. It also requires the gadget's rate |
| 216 | -- as second argument (which defaults to 60s).</p> |
| 217 | -- |
| 218 | -- <p>This method immediately fails if the given gadget is nil. On |
| 219 | -- success, the gadget is stored in the provider internal subscribers |
| 220 | -- list.</p> |
| 221 | -- |
| 222 | -- @param g the gadget subscriber. |
| 223 | -- @param rate the new poll interval asked by the gadget. |
| 224 | -- @return True if the subscriber was correctly stored and the timer |
| 225 | -- correctly started or reconfigured (if necessary), False otherwise. |
| 226 | function CyclicProvider:subscribe(g, rate) |
| 227 | rate = rate or 60 |
| 228 | if Provider.subscribe(self, g) then |
| 229 | self.subscribers[g].rate = rate |
| 230 | if self.timer == nil then |
| 231 | self.timer = capi.timer{ timeout = rate } |
| 232 | self.timer:add_signal('timeout', function() self:refresh() end, true) |
| 233 | self.timer:start() |
| 234 | else |
| 235 | if rate < self.timer.timeout then |
| 236 | self.timer.timeout = rate |
| 237 | end |
| 238 | end |
| 239 | |
| 240 | -- Initial refresh. |
| 241 | self:refresh(g) |
| 242 | |
| 243 | return true |
| 244 | end |
| 245 | return false |
| 246 | end |
| 247 | |
| 248 | --- Refresh the provider status. |
| 249 | -- |
| 250 | -- <p>This callback is invoked by the provider timer, which timeouts |
| 251 | -- at the highest asked rate. The actual refresh process is dedicated |
| 252 | -- to the <code>do_refresh</code> method, which is called with no |
| 253 | -- argument. This definition depends on the provider role, and should |
| 254 | -- be defined in all derived prototypes.</p> |
| 255 | -- |
| 256 | -- <p>After refresh is done, the gadgets are redrawn.</p> |
| 257 | -- |
| 258 | -- @param g if provided, this gadget only is refreshed. |
| 259 | -- @return True is the provider did refresh its data set since the |
| 260 | -- given gadget last asked for the refresh. |
| 261 | function CyclicProvider:refresh(g) |
| 262 | if self.do_refresh then |
| 263 | self:do_refresh() |
| 264 | if g ~= nil then |
| 265 | self.subscribers[g].timestamp = self.timestamp |
| 266 | g:update() |
| 267 | else |
| 268 | self:refresh_gadgets() |
| 269 | end |
| 270 | else |
| 271 | flaw.helper.debug.warn( |
| 272 | 'CyclicProvider ' .. self.type .. '.' .. self.id .. ' misses do_refresh()') |
| 273 | end |
| 274 | end |
| 275 | |
| 276 | --- Refresh gadgets subscribed to this provider. |
| 277 | -- |
| 278 | -- <p>Redefined here to handle the rate required by the different |
| 279 | -- gadgets. All child providers should rely on the |
| 280 | -- <code>refresh</code> method which calls directly this one.</p> |
| 281 | function CyclicProvider:refresh_gadgets(force) |
| 282 | local force = force or false |
| 283 | self.timestamp = os.time() |
| 284 | for g, props in pairs(self.subscribers) do |
| 285 | if force or props['timestamp'] + props['rate'] <= self.timestamp then |
| 286 | props['timestamp'] = self.timestamp |
| 287 | g:update() |
| 288 | end |
| 289 | end |
| 290 | end |