Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement PluginInterface.

  • Loading branch information...
commit 6d14dda5c1fa8ed858e40fba23b81d9f3c6e9957 1 parent 24c46d4
Sam Edwards CFSworks authored
3  phoenix2/core/PhoenixCore.py
@@ -36,6 +36,7 @@
36 36 from .KernelInterface import KernelInterface
37 37 from .PhoenixConfig import PhoenixConfig
38 38 from .PhoenixRPC import PhoenixRPC
  39 +from .PluginInterface import PluginInterface
39 40
40 41 class PhoenixCore(object):
41 42 """The root-level object of a Phoenix mining instance."""
@@ -64,7 +65,7 @@ def __init__(self, cfgFilename='phoenix.cfg'):
64 65
65 66 self.pluginModules = {}
66 67
67   - self.pluginIntf = None # TODO
  68 + self.pluginIntf = PluginInterface(self)
68 69 self.plugins = {}
69 70
70 71 self.kernels = {}
174 phoenix2/core/PhoenixRPC.py
@@ -101,8 +101,8 @@ def render_POST(self, request):
101 101 except (KeyError, TypeError):
102 102 return rpcError(-32600, 'Invalid request.')
103 103
104   - func = getattr(self, 'rpc_' + method, None)
105   - if func is None:
  104 + func = getattr(self.core.pluginIntf, method, None)
  105 + if func is None or getattr(func, 'rpc_forbidden', False):
106 106 return rpcError(-32601, 'Method not found.')
107 107
108 108 d = defer.maybeDeferred(func, *params)
@@ -121,173 +121,3 @@ def errback(failure):
121 121 d.addErrback(errback)
122 122
123 123 return server.NOT_DONE_YET
124   -
125   - def rpc_getstatus(self):
126   - return {'uptime': int(time.time() - self.core.startTime),
127   - 'connection': {'type': self.core.connectionType,
128   - 'connected': self.core.connected,
129   - 'url': self.core.connectionURL},
130   - 'results': {'accepted': self.core.logger.accepted,
131   - 'rejected': self.core.logger.rejected}}
132   -
133   - def rpc_getrawconfig(self):
134   - return self.core.config.text
135   -
136   - def rpc_setrawconfig(self, text):
137   - text = str(text)
138   - self.core.config.setraw(text)
139   - self.core.config.save()
140   - self.core.configChanged()
141   -
142   - def _checkSection(self, section):
143   - if ':' in section and section not in self.core.config.listsections():
144   - # Make sure autoconfiguration is preserved.
145   - self.core.config.set(section, 'autoconfigure', True)
146   - self.core.config.set(section, 'start_undetected',
147   - False)
148   -
149   - def rpc_getconfig(self, section, var):
150   - section = str(section)
151   - var = str(var)
152   - return self.core.config.get(section, var, str, None)
153   -
154   - def rpc_setconfig(self, section, var, value):
155   - section = str(section)
156   - var = str(var)
157   - # value doesn't get converted to str - set does that (unless it's None)
158   - self._checkSection(section)
159   - self.core.config.set(section, var, value)
160   - self.core.config.save()
161   - self.core.configChanged()
162   -
163   - def rpc_redetect(self, terminate=False):
164   - self.core.redetect(terminate)
165   -
166   - def rpc_switchto(self, backend=None):
167   - if backend is None:
168   - backend = self.core.config.get('general', 'backend', str)
169   - else:
170   - backend = str(backend)
171   - self.core.switchURL(backend)
172   -
173   - def _getminers(self):
174   - miners = [section for section in self.core.config.listsections()
175   - if ':' in section]
176   - miners.extend([miner for miner in self.core.kernels
177   - if miner is not None and miner not in miners])
178   - return miners
179   -
180   - def rpc_listdevices(self):
181   - devices = []
182   - for miner in self._getminers():
183   - device = {'id': miner}
184   -
185   - config = self.core.getKernelConfig(miner)
186   -
187   - if self.core.kernels.get(miner) is not None:
188   - kernel = self.core.kernels[miner]
189   - interface = self.core.interfaces[kernel]
190   -
191   - device['status'] = 'running'
192   - device['name'] = interface.getName()
193   - device['rate'] = interface.getRate()
194   - device['config'] = config
195   - device['meta'] = interface.meta
196   - device['uptime'] = int(time.time() - interface.started)
197   - device['results'] = interface.results
198   - device['accepted'] = interface.accepted
199   - device['rejected'] = interface.rejected
200   - else:
201   - disabled = self.core.config.get(miner, 'disabled', bool, False)
202   -
203   - device['status'] = ('disabled' if disabled else 'suspended')
204   - device['name'] = config.get('name', miner)
205   - device['rate'] = 0
206   - device['config'] = config
207   - for key, value in self.core.config.getsection(miner).items():
208   - device['config'][key.lower()] = value
209   - device['meta'] = {}
210   - device['uptime'] = 0
211   - device['results'] = 0
212   - device['accepted'] = 0
213   - device['rejected'] = 0
214   -
215   - devices.append(device)
216   -
217   - return devices
218   -
219   - def rpc_getlogs(self, skip, limit=0):
220   - skip = int(skip)
221   - limit = int(limit)
222   -
223   - total = len(self.core.logger.rpcLogs) + self.core.logger.rpcIndex
224   - if skip < 0:
225   - skip %= total
226   -
227   - buf = [{'id': None, 'timestamp': None, 'msg': None, 'type': 'purged',
228   - 'details': {}}] * (self.core.logger.rpcIndex - skip)
229   - skip = max(0, skip - self.core.logger.rpcIndex)
230   -
231   - if limit == 0:
232   - limit = None
233   -
234   - return (buf + [log.formatRPC(self.core.logger) for log in
235   - self.core.logger.rpcLogs[skip:]])[:limit]
236   -
237   - def _manage(self, minerID, action):
238   - # Just a quick helper function to be used for the next 4...
239   - if minerID is not None:
240   - minerID = str(minerID)
241   -
242   - saveConfig = False
243   - managed = False
244   - for miner in self._getminers():
245   - running = self.core.kernels.get(miner) is not None
246   - disabled = self.core.config.get(miner, 'disabled', bool, False)
247   - if minerID is None or miner == minerID.lower():
248   - if action == 'suspend':
249   - if running:
250   - self.core.stopKernel(miner)
251   - managed = True
252   - elif action == 'restart':
253   - if running:
254   - self.core.stopKernel(miner)
255   - self.core.startKernel(miner)
256   - managed = True
257   - elif action == 'disable':
258   - if running:
259   - self.core.stopKernel(miner)
260   - if not disabled:
261   - self._checkSection(miner)
262   - self.core.config.set(miner, 'disabled', True)
263   - saveConfig = True
264   - managed = True
265   - elif action == 'start':
266   - if disabled:
267   - continue # Can't use start(null) for disabled.
268   - if self.core.startKernel(miner):
269   - managed = True
270   -
271   - if saveConfig:
272   - self.core.config.save()
273   - return managed
274   -
275   - def rpc_restart(self, minerID=None):
276   - return self._manage(minerID, 'restart')
277   -
278   - def rpc_suspend(self, minerID=None):
279   - return self._manage(minerID, 'suspend')
280   -
281   - def rpc_disable(self, minerID):
282   - return self._manage(minerID, 'disable')
283   -
284   - def rpc_start(self, minerID=None):
285   - if minerID is None:
286   - return self._manage(None, 'start')
287   - else:
288   - self.core.config.set(minerID, 'disabled', None)
289   - self.core.config.save()
290   - return self.core.startKernel(minerID) is not None
291   -
292   - def rpc_shutdown(self):
293   - reactor.callLater(0.01, reactor.stop)
193 phoenix2/core/PluginInterface.py
... ... @@ -0,0 +1,193 @@
  1 +import time
  2 +
  3 +from twisted.internet import reactor
  4 +
  5 +def norpc(func):
  6 + """This is a quick decorator to mark a function as FORBIDDEN to the RPC
  7 + server. It's intended for useful plugin functions that could pose security
  8 + risks if inadvertently exposed over the network.
  9 + """
  10 +
  11 + func.rpc_forbidden = True
  12 + return func
  13 +
  14 +class PluginInterface(object):
  15 + """All of the functions that do not start with a _ are acceptable to use in
  16 + third-party plugins.
  17 + """
  18 + @norpc
  19 + def __init__(self, core):
  20 + self.core = core
  21 +
  22 + def getstatus(self):
  23 + return {'uptime': int(time.time() - self.core.startTime),
  24 + 'connection': {'type': self.core.connectionType,
  25 + 'connected': self.core.connected,
  26 + 'url': self.core.connectionURL},
  27 + 'results': {'accepted': self.core.logger.accepted,
  28 + 'rejected': self.core.logger.rejected}}
  29 +
  30 + def getrawconfig(self):
  31 + return self.core.config.text
  32 +
  33 + def setrawconfig(self, text):
  34 + text = str(text)
  35 + self.core.config.setraw(text)
  36 + self.core.config.save()
  37 + self.core.configChanged()
  38 +
  39 + @norpc
  40 + def _checkSection(self, section):
  41 + if ':' in section and section not in self.core.config.listsections():
  42 + # Make sure autoconfiguration is preserved.
  43 + self.core.config.set(section, 'autoconfigure', True)
  44 + self.core.config.set(section, 'start_undetected',
  45 + False)
  46 +
  47 + def getconfig(self, section, var):
  48 + section = str(section)
  49 + var = str(var)
  50 + return self.core.config.get(section, var, str, None)
  51 +
  52 + def setconfig(self, section, var, value):
  53 + section = str(section)
  54 + var = str(var)
  55 + # value doesn't get converted to str - set does that (unless it's None)
  56 + self._checkSection(section)
  57 + self.core.config.set(section, var, value)
  58 + self.core.config.save()
  59 + self.core.configChanged()
  60 +
  61 + def redetect(self, terminate=False):
  62 + self.core.redetect(terminate)
  63 +
  64 + def switchto(self, backend=None):
  65 + if backend is None:
  66 + backend = self.core.config.get('general', 'backend', str)
  67 + else:
  68 + backend = str(backend)
  69 + self.core.switchURL(backend)
  70 +
  71 + @norpc
  72 + def _getminers(self):
  73 + miners = [section for section in self.core.config.listsections()
  74 + if ':' in section]
  75 + miners.extend([miner for miner in self.core.kernels
  76 + if miner is not None and miner not in miners])
  77 + return miners
  78 +
  79 + def listdevices(self):
  80 + devices = []
  81 + for miner in self._getminers():
  82 + device = {'id': miner}
  83 +
  84 + config = self.core.getKernelConfig(miner)
  85 +
  86 + if self.core.kernels.get(miner) is not None:
  87 + kernel = self.core.kernels[miner]
  88 + interface = self.core.interfaces[kernel]
  89 +
  90 + device['status'] = 'running'
  91 + device['name'] = interface.getName()
  92 + device['rate'] = interface.getRate()
  93 + device['config'] = config
  94 + device['meta'] = interface.meta
  95 + device['uptime'] = int(time.time() - interface.started)
  96 + device['results'] = interface.results
  97 + device['accepted'] = interface.accepted
  98 + device['rejected'] = interface.rejected
  99 + else:
  100 + disabled = self.core.config.get(miner, 'disabled', bool, False)
  101 +
  102 + device['status'] = ('disabled' if disabled else 'suspended')
  103 + device['name'] = config.get('name', miner)
  104 + device['rate'] = 0
  105 + device['config'] = config
  106 + for key, value in self.core.config.getsection(miner).items():
  107 + device['config'][key.lower()] = value
  108 + device['meta'] = {}
  109 + device['uptime'] = 0
  110 + device['results'] = 0
  111 + device['accepted'] = 0
  112 + device['rejected'] = 0
  113 +
  114 + devices.append(device)
  115 +
  116 + return devices
  117 +
  118 + def getlogs(self, skip, limit=0):
  119 + skip = int(skip)
  120 + limit = int(limit)
  121 +
  122 + total = len(self.core.logger.rpcLogs) + self.core.logger.rpcIndex
  123 + if skip < 0:
  124 + skip %= total
  125 +
  126 + buf = [{'id': None, 'timestamp': None, 'msg': None, 'type': 'purged',
  127 + 'details': {}}] * (self.core.logger.rpcIndex - skip)
  128 + skip = max(0, skip - self.core.logger.rpcIndex)
  129 +
  130 + if limit == 0:
  131 + limit = None
  132 +
  133 + return (buf + [log.formatRPC(self.core.logger) for log in
  134 + self.core.logger.rpcLogs[skip:]])[:limit]
  135 +
  136 + @norpc
  137 + def _manage(self, minerID, action):
  138 + # Just a quick helper function to be used for the next 4...
  139 + if minerID is not None:
  140 + minerID = str(minerID)
  141 +
  142 + saveConfig = False
  143 + managed = False
  144 + for miner in self._getminers():
  145 + running = self.core.kernels.get(miner) is not None
  146 + disabled = self.core.config.get(miner, 'disabled', bool, False)
  147 + if minerID is None or miner == minerID.lower():
  148 + if action == 'suspend':
  149 + if running:
  150 + self.core.stopKernel(miner)
  151 + managed = True
  152 + elif action == 'restart':
  153 + if running:
  154 + self.core.stopKernel(miner)
  155 + self.core.startKernel(miner)
  156 + managed = True
  157 + elif action == 'disable':
  158 + if running:
  159 + self.core.stopKernel(miner)
  160 + if not disabled:
  161 + self._checkSection(miner)
  162 + self.core.config.set(miner, 'disabled', True)
  163 + saveConfig = True
  164 + managed = True
  165 + elif action == 'start':
  166 + if disabled:
  167 + continue # Can't use start(null) for disabled.
  168 + if self.core.startKernel(miner):
  169 + managed = True
  170 +
  171 + if saveConfig:
  172 + self.core.config.save()
  173 + return managed
  174 +
  175 + def restart(self, minerID=None):
  176 + return self._manage(minerID, 'restart')
  177 +
  178 + def suspend(self, minerID=None):
  179 + return self._manage(minerID, 'suspend')
  180 +
  181 + def disable(self, minerID):
  182 + return self._manage(minerID, 'disable')
  183 +
  184 + def start(self, minerID=None):
  185 + if minerID is None:
  186 + return self._manage(None, 'start')
  187 + else:
  188 + self.core.config.set(minerID, 'disabled', None)
  189 + self.core.config.save()
  190 + return self.core.startKernel(minerID) is not None
  191 +
  192 + def shutdown(self):
  193 + reactor.callLater(0.01, reactor.stop)

0 comments on commit 6d14dda

Please sign in to comment.
Something went wrong with that request. Please try again.