backend.when_discovered   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 7

3 Functions

Rating   Name   Duplication   Size   Complexity  
A when_discovered() 0 11 2
A dodiscovered() 0 18 3
A main() 0 9 2
1
'''#discovered something that responds to cgminer api'''
2
from helpers.queuehelper import QueueName, QueueEntries
3
from backend.fcmapp import Component
4
5
COMPONENTDISCOVERED = Component(componentname='discover', option='')
6
7
def when_discovered(channel, method, properties, body):
8
    '''when new miner is discovered on the network'''
9
    try:
10
        print("[{0}] Received discovered notice".format(COMPONENTDISCOVERED.app.now()))
11
        entries = dodiscovered(COMPONENTDISCOVERED.app.messagedecodeminer(body))
12
        COMPONENTDISCOVERED.app.enqueue(entries)
13
        COMPONENTDISCOVERED.app.bus.acknowledge(COMPONENTDISCOVERED.listeningqueue, method.delivery_tag)
14
15
    except Exception as ex:
16
        COMPONENTDISCOVERED.app.logexception(ex)
17
        COMPONENTDISCOVERED.app.bus.reject(COMPONENTDISCOVERED.listeningqueue, method.delivery_tag)
18
19
def dodiscovered(miner):
20
    '''then provision it'''
21
    entries = QueueEntries()
22
    entries.add(QueueName.Q_PROVISION, COMPONENTDISCOVERED.app.messageencode(miner))
23
    cachedminer = COMPONENTDISCOVERED.app.getminer(miner)
24
    #knownminer should be None
25
    if cachedminer is not None:
26
        cachedminer.updatefrom(miner)
27
    COMPONENTDISCOVERED.app.putminer(cachedminer)
28
    knownminer = COMPONENTDISCOVERED.app.getknownminer(miner)
29
    if knownminer is None:
30
        COMPONENTDISCOVERED.app.addknownminer(miner)
31
    else:
32
        COMPONENTDISCOVERED.app.updateknownminer(miner)
33
34
    entries.addbroadcast(QueueName.Q_ALERT, 'discovered miner {0}'.format(miner.name))
35
    print("Discovered {0}".format(miner.name))
36
    return entries
37
38
def main():
39
    if COMPONENTDISCOVERED.app.isrunnow:
40
        miner = COMPONENTDISCOVERED.app.getknownminerbyname('S9102')
41
        dodiscovered(miner)
42
        COMPONENTDISCOVERED.app.shutdown()
43
    else:
44
45
        COMPONENTDISCOVERED.listeningqueue = COMPONENTDISCOVERED.app.subscribe(QueueName.Q_DISCOVERED, when_discovered, no_acknowledge=False)
46
        COMPONENTDISCOVERED.app.listen(COMPONENTDISCOVERED.listeningqueue)
47
48
if __name__ == "__main__":
49
    main()
50