-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster_server.py
963 lines (822 loc) · 29.8 KB
/
cluster_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
import socket
import time
import configparser
import json
import requests
import threading
import struct
import select
import traceback
import logging
import types
# https://github.com/DoctorEenot/DuinoCoin_android_cluster
'''
For more details go to projects page:
https://github.com/DoctorEenot/DuinoCoin_android_cluster
'''
'''
GLOBALS:
'''
username = ''
requestedDiff= ''
rigIdentifier= ''
algorithm= ''
MIN_DIFFICULTY = 300000 #real difficulty to start dividing jobs
INC_COEF = 0
TIME_FOR_DEVICE = 90 #Time for device to update it's aliveness
DISABLE_LOGGING = True
PING_MASTER_SERVER = 45 # Seconds to ping master server
'''
LOGGER:
'''
logger = logging.getLogger('Cluster_Server')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Config loading section
def loadConfig():
global username
global requestedDiff
global rigIdentifier
global algorithm
global MIN_DIFFICULTY
global INC_COEF
global TIME_FOR_DEVICE
global DISABLE_LOGGING
logger.info('Loading config')
config.read('Cluster_Config.cfg')
username = config["cluster"]["username"]
requestedDiff = config["cluster"]["difficulty"] # LOW/MEDIUM/NET
algorithm = config["cluster"]["algorithm"] # XXHASH/DUCO-S1
rigIdentifier = config["cluster"]["identifier"]
MIN_DIFFICULTY = int(config['cluster']['MIN_DIFFICULTY'])
INC_COEF = int(config['cluster']['INC_COEF'])
TIME_FOR_DEVICE = int(config['cluster']['TIME_FOR_DEVICE'])
if config['cluster']['DISABLE_LOGGING']=='False':
DISABLE_LOGGING = False
config = configparser.ConfigParser()
loadConfig()
serveripfile = ("https://raw.githubusercontent.com/"
+ "revoxhere/"
+ "duino-coin/gh-pages/"
+ "serverip.txt") # Serverip file
masterServer_address = ''
masterServer_port = 0
'''
LOGGER FILE HANDLER:
'''
if not DISABLE_LOGGING:
fh = logging.FileHandler('Cluster_Server.log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch.setFormatter(formatter)
logger.addHandler(ch)
def get_master_server_info():
global masterServer_address
global masterServer_port
logger.info('Getting Master server info')
while True:
try:
res = requests.get(serveripfile, data=None)
break
except:
pass
logger.info('getting data again')
time.sleep(10)
if res.status_code == 200:
logger.info('Master server info accepted')
# Read content and split into lines
content = (res.content.decode().splitlines())
masterServer_address = content[0] # Line 1 = pool address
masterServer_port = content[1] # Line 2 = pool port
else:
raise Exception('CANT GET MASTER SERVER ADDRESS')
class Device:
def __init__(self,name,address):
self.name = name
self.last_updated = time.time()
self.busy = False
self.address = address
def is_alive(self):
return (time.time()-self.last_updated)<TIME_FOR_DEVICE
def update_time(self):
self.last_updated = time.time()
def isbusy(self):
return self.busy
def job_stopped(self):
self.busy = False
def job_started(self):
self.busy = True
def __str__(self):
return self.name+' '+str(self.address)
def __repr__(self):
return str(self)
devices = {}
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
server_socket.setblocking(False)
SERVER_ADDRESS = ('0.0.0.0',9090)
server_socket.bind(SERVER_ADDRESS)
master_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
master_server_socket.settimeout(0)
master_server_timeout = 30
master_server_is_connected = False
master_server_last_pinged = 0
AVAILABLE_PORTS = [2812,2813,2814,2815,2816]
def get_fastest_connection(server_ip:str):
connection_pool = []
available_connections = []
for i in range(len(AVAILABLE_PORTS)):
connection_pool.append(socket.socket())
connection_pool[i].setblocking(0)
try:
connection_pool[i].connect((server_ip,
AVAILABLE_PORTS[i]))
except BlockingIOError as e:
pass
ready_connections,_,__ = select.select(connection_pool,[],[])
while True:
for connection in ready_connections:
try:
server_version = connection.recv(100)
except:
continue
if server_version == b'':
continue
available_connections.append(connection)
connection.send(b'PING')
ready_connections,_,__ = select.select(available_connections,[],[])
ready_connections[0].recv(100)
# python is smart enough to close all other connections
# I hope
ready_connections[0].settimeout(10)
return ready_connections[0],server_version
def connect_to_master(dispatcher,event):
'''
event - {'t':'e',
'event':'connect_to_master'}
'''
logger.info('CONNECTING TO MASTER')
global master_server_socket
global masterServer_address
global masterServer_port
global master_server_timeout
global master_server_is_connected
try:
event.dict_representation['address']
return
except:
pass
master_server_is_connected = False
get_master_server_info()
while True:
master_server_socket,server_version = get_fastest_connection(masterServer_address)
master_server_socket.settimeout(0)
master_server_is_connected = True
if server_version != None\
and server_version != '':
break
def register(dispatcher,event):
'''
event = {'t':'e',
'event':'register',
'name':'Test',
'address':('127.0.0.1',1234),
'callback':socket}
'''
global devices
logger.info('Register')
device = devices.get(event.address,None)
if device != None:
event.callback.sendto(b'{"t":"a",\
"status":"ok",\
"message":"already exists"}',
event.address)
device.update_time()
return None
devices[event.address] = Device(event.name,event.address)
devices[event.address].update_time()
event.callback.sendto(b'{"t":"a",\
"status":"ok",\
"message":"device added"}',
event.address)
event_ = Event({'t':'e',
'event':'get_job',
'address':event.address,
'callback':event.callback})
dispatcher.add_to_queue(event_)
return None
def ping(dispatcher,event):
'''
event = {'t':'e',
'event':'ping',
'address':('127.0.0.1',1234),
'callback':socket}
'''
global devices
logger.debug('Ping')
device = devices.get(event.address,None)
if device == None:
event.callback.sendto(b'{"t":"e",\
"event":"register",\
"message":"You must register in cluster"}',
event.address)
return None
device.update_time()
data = b'{"t":"a","status":"ok","message":"server is running"}'
event.callback.sendto(data,event.address)
return None
JOB = None
JOB_START_SECRET = 'ejnejkfnhiuhwefiy87usdf'
JOBS_TO_PROCESS = {}
HASH_COUNTER = 0
JOB_STARTED_TIME = 0
class Job:
def __init__(self,devices = []):
self.devices = devices
self.done = False
def set_device(self,device):
self.devices.append(device)
def get_devices(self):
return self.devices
def is_done(self):
return self.done
def set_done(self):
self.done = True
def is_claimed(self):
return len(self.devices)>0
def unclaim(self):
self.devices = []
def number_of_devices(self):
return len(self.devices)
def job_start(dispatcher,event):
'''
event = {'t':'e',
'event':'job_start',
'secret':'',
'callback':socket}
'''
global JOB
global JOB_START_SECRET
global algorithm
global JOBS_TO_PROCESS
global JOB_STARTED_TIME
if event.secret != JOB_START_SECRET:
logger.warning('bad secret')
return
logger.info('Job is starting')
JOB_STARTED_TIME = time.time()
counter = 0
jobs = list(JOBS_TO_PROCESS.items())
sent = True
for addr,device in devices.items():
if device.isbusy():
continue
start_end,job = jobs[counter]
data = json.dumps({'t':'e',
'event':'start_job',
'lastBlockHash':JOB[0],
'expectedHash':JOB[1],
'start':start_end[0],
'end':start_end[1],
'algorithm':algorithm})
device.job_started()
event.callback.sendto(data.encode('ascii'),addr)
job.set_device(device)
counter += 1
if counter == len(jobs):
return
#yield
def send_results(dispatcher,result):
global algorithm
global minerVersion
global rigIdentifier
global HASH_COUNTER
global devices
global master_server_timeout
logger.info('Sending results')
logger.debug(str(result))
logger.info('Hashes were checked: '+str(HASH_COUNTER))
if HASH_COUNTER<result[0]:
HASH_COUNTER = result[0]
master_server_socket.settimeout(master_server_timeout)
while True:
try:
master_server_socket.send(bytes(
str(result[0])
+ ","
+ str(HASH_COUNTER)#HASHCOUNTER
+ ","
+ "YeahNot Cluster("
+ str(algorithm)
+ f") Devices:{len(devices)}"
+ ","
+ str(rigIdentifier),
encoding="utf8"))
feedback = master_server_socket.recv(8).decode().rstrip("\n")
except Exception as e:
event = {'t':'e',
'event':'connect_to_master'}
event = Event(event)
dispatcher.add_to_queue(event)
logger.warning('Giving up on that hash')
break
if feedback == 'GOOD':
logger.info('Hash accepted')
elif feedback == 'BLOCK':
logger.info('Hash blocked')
elif feedback == '':
logger.debug('Connection with master is lost')
#connect_to_master()
continue
else:
logger.info('Hash rejected')
break
HASH_COUNTER = 0
def get_job(dispatcher,event):
'''
event - {'t':'e',
'event':'get_job',
'address':'address',
'callback':'callback'}
'''
global JOB
global algorithm
global JOBS_TO_PROCESS
job_to_send = None
logger.info('Get job packet')
device = devices.get(event.address,None)
if device == None:
logger.warning('device is not registered')
event.callback.sendto(b'{"t":"e",\
"event":"register",\
"message":"You must register in cluster"}',
event.address)
return None
if not device.is_alive():
logger.warning('Device '+device.name+' '+str(event.address)+' is dead')
data = json.dumps({'t':'e',
'event':'ping'})
event.callback.sendto(data.encode('ascii'),event.address)
return None
# searching for unclaimed jobs
for start_end,job in JOBS_TO_PROCESS.items():
if not job.is_claimed() and not job.is_done():
job.set_device(device)
job_to_send = start_end
break
yield
# searching for claimed by 1 device undone jobs
if job_to_send == None:
for start_end,job in JOBS_TO_PROCESS.items():
if not job.is_done():
job_to_send = start_end
if not job.number_of_devices()<2:
job.set_device(device)
job_to_send = start_end
break
yield
if job_to_send == None:
device.job_stopped()
logger.warning('CANT FIND FREE JOB')
return None
data = json.dumps({'t':'e',
'event':'start_job',
'lastBlockHash':JOB[0],
'expectedHash':JOB[1],
'start':job_to_send[0],
'end':job_to_send[1],
'algorithm':algorithm})
logger.debug('Sending job: '+data)
device.job_started()
event.callback.sendto(data.encode('ascii'),event.address)
def job_done(dispatcher,event):
'''
event = {'t':'e',
'event':'job_done',
'result':[1,1] | ['None',1],
'start_end':[1,1],
'expected_hash':'',
'address':('127.0.0.1',1234),
'callback':socket}
'''
global JOB
global algorithm
global JOBS_TO_PROCESS
global HASH_COUNTER
global JOB_STARTED_TIME
logger.info('job done packet')
if (event.result[0] == 'None' \
or event.result[0] == None):
logger.info('Empty block')
device = devices.get(event.address,None)
if device == None:
logger.warning('device is not registered')
event.callback.sendto(b'{"t":"e",\
"event":"register",\
"message":"You must register in cluster"}',
event.address)
return None
if not device.is_alive():
logger.warning('Device '+device.name+' '+str(event.address)+' is dead')
data = json.dumps({'t':'e',
'event':'ping'})
event.callback.sendto(data.encode('ascii'),event.address)
return None
device.job_stopped()
if JOB == None:
logger.debug('Job is already over')
data = b'{"t":"a",\
"status":"ok",\
"message":"No job to send"}'
event.callback.sendto(data,event.address)
return
recieved_start_end = tuple(event.start_end)
if event.expected_hash == None:
logger.debug('redirect from register')
else:
if event.expected_hash == JOB[1]:
logger.debug('currently running job')
CURRENT_JOB = None
try:
CURRENT_JOB = JOBS_TO_PROCESS[recieved_start_end]
except:
logger.error('CANT FIND BLOCK: '+str(recieved_start_end))
if CURRENT_JOB != None:
HASH_COUNTER += event.result[1]
logger.debug('terminating linked devices')
data_dict = {'t':'e',
'event':'stop_job',
'expected_hash':JOB[1],
'start_end':event.start_end,
'message':'another device already solved hash'}
data = json.dumps(data_dict).encode('ascii')
CURRENT_JOB.set_done()
for device in CURRENT_JOB.get_devices():
if device.address != event.address\
and device.isbusy():
device.job_stopped()
event.callback.sendto(data,device.address)
yield
else:
logger.debug('Old packet')
else:
logger.info('accepted result')
recieved_start_end = tuple(event.start_end)
CURRENT_JOB = JOBS_TO_PROCESS.get(recieved_start_end,None)
if event.expected_hash != JOB[1]\
or CURRENT_JOB == None:
logger.warning('STOP JOB ON WRONG JOB')
return
HASH_COUNTER += event.result[1]
logger.info('HASHRATE: '+str(event.result[0]//(time.time()-JOB_STARTED_TIME))+' H/s')
send_results(dispatcher,event.result)
JOBS_TO_PROCESS = {}
data_dict = {'t':'e',
'event':'stop_job',
'expected_hash':JOB[1],
'start_end':event.start_end,
'message':'another device already solved hash'}
JOB = None
data = json.dumps(data_dict).encode('ascii')
logger.debug('stopping workers')
for addr,device in devices.items():
device.job_stopped()
if addr != event.address:
event.callback.sendto(data,addr)
yield
def request_job(dispatcher,event):
'''
event = {'t':'e',
'event':'requets_job',
'secret':'',
'parts':10}
'''
global JOB
global JOB_START_SECRET
global algorithm
global username
global requestedDiff
global master_server_socket
global JOBS_TO_PROCESS
global INC_COEF
global master_server_timeout
global master_server_is_connected
logger.info('requesting job')
if event.secret != JOB_START_SECRET:
logger.warning('bad secret')
return
job = None
while job == None or job == '':
try:
if algorithm == "XXHASH":
master_server_socket.send(bytes(
"JOBXX,"
+ str(username)
+ ","
+ str(requestedDiff),
encoding="utf8"))
else:
master_server_socket.send(bytes(
"JOB,"
+ str(username)
+ ","
+ str(requestedDiff),
encoding="utf8"))
except Exception as e:
master_server_is_connected = False
logger.error('asking for job error accured')
event = {'t':'e',
'event':'connect_to_master'}
event = Event(event)
dispatcher.add_to_queue(event)
break
# make socket non-blocking
master_server_socket.settimeout(0)
timeout_start = time.time()
master_server_is_connected = False
# pure implementation of timeout for socket, but with yielding back to main event loop
while time.time()-timeout_start<master_server_timeout:
try:
job = master_server_socket.recv(128).decode().rstrip("\n")
master_server_is_connected = True
break
except:
yield
continue
# server didn't respond in <master_server_timeout> seconds
if job == None:
master_server_is_connected = False
logger.warning('Couldnt recieve job from server')
event = {'t':'e',
'event':'connect_to_master'}
event = Event(event)
dispatcher.add_to_queue(event)
break
#continue
# if server sent job
job = job.split(",")
if job[0] == 'BAD':
logger.warning('GOT "BAD" PACKET IN RESPONSE')
return
elif job[0] == '':
logger.warning('CONNECTION WITH MASTER SERVER WAS BROKEN')
#connect_to_master()
continue
logger.info('job accepted')
logger.info('Difficulty: '+str(job[2]))
logger.debug(str(job))
event = Event({'t':'e',
'event':'job_start',
'secret':JOB_START_SECRET,
'callback':server_socket})
dispatcher.add_to_queue(event)
JOBS_TO_PROCESS = {}
parts = len(devices)+INC_COEF
JOB = job[:2]
real_difficulty = (100*int(job[2]))
if real_difficulty <= MIN_DIFFICULTY:
job_part = real_difficulty
else:
job_part = (real_difficulty//parts)
start = 0
if job_part == real_difficulty:
end = job_part+1
else:
end = job_part
while start<real_difficulty:
job_object = Job()
JOBS_TO_PROCESS[(start,end)] = job_object
start = end
if real_difficulty<=end+job_part:
end = real_difficulty+1
else:
end += job_part
logger.debug('JOB: '+str(JOBS_TO_PROCESS))
break
def clean_up_devices(dispatcher,event):
try:
# if event was recieved by server
event.dict_representation['address']
return None
except:
pass
counter = 0
items = list(devices.items())
while counter<len(devices):
address,device = items[counter]
if not device.is_alive():
del devices[address]
del items[counter]
continue
counter += 1
yield
class Event(object):
def __init__(self,input:dict):
self.dict_representation = input
def __dict__(self):
return super(Event, self).__getattribute__('dict_representation')
#def event_name(self) -> str:
# return self.dict_representation['event']
def __getattribute__(self, item):
# Calling the super class to avoid recursion
return super(Event, self).__getattribute__(item)
def __getattr__(self, item):
try:
return super(Event, self).__getattribute__('dict_representation')[item]
except:
logger.warning('NO SUCH ELEMENT AS '+str(item))
pass
def __str__(self):
return str(self.dict_representation)
class Dispatcher:
def __init__(self):
self.actions = {}
self.queue = []
self.active_loop = []
def register(self,event_name,action):
self.actions[event_name] = action
def add_to_queue(self,event:Event):
logger.debug('added event')
logger.debug(str(event.dict_representation))
self.queue.append(event)
def clear_queue(self):
self.queue = []
def iter_through_active_list(self):
counter = 0
while counter<len(self.active_loop):
try:
next(self.active_loop[counter])
except StopIteration:
self.active_loop.pop(counter)
continue
counter += 1
def dispatch_event(self,count=1):
for i in range(count):
try:
event = self.queue.pop(0)
except:
return None
logger.debug('dispatching event')
func = self.actions.get(event.event,None)
if func == None:
logger.warning('NO SUCH ACTION '+event.event)
return None
activity = self.actions[event.event](self,event)
if isinstance(activity,types.GeneratorType):
self.active_loop.append(activity)
def ping_master(dispatcher,event):
'''
event - {'t':'e',
'event':'ping_master'}
'''
global master_server_socket
global master_server_last_pinged
global master_server_is_connected
global master_server_timeout
global JOB
logger.debug('Pinging master server')
ping_packet = b'PING'
master_server_last_pinged = time.time()
if JOB == None:
return None
try:
master_server_socket.send(ping_packet)
except Exception as e:
master_server_is_connected = False
return False
#master_server_socket.settimeout(master_server_timeout)
started_pinging = time.time()
pinged = False
while time.time()-started_pinging<master_server_timeout:
if JOB == None:
return None
try:
data = master_server_socket.recv(5)
if data==b'Pong!':
logger.debug('Pong! packed received')
pinged = True
elif data == '':
logger.warning('connection with master was closed')
else:
logger.debug('Unexpected data received %s',data.hex())
break
except Exception as e:
yield
if not pinged:
master_server_is_connected = False
#new_event = Event({'t':'e',
# 'event':'connect_to_master'})
#dispatcher.add_to_queue(new_event)
#master_server_socket.settimeout(0)
def server():
global server_socket
global devices
global MIN_PARTS
global INC_COEF
global TIME_FOR_DEVICE
global master_server_last_pinged
global PING_MASTER_SERVER
logger.debug('Initializing dispatcher')
event_dispatcher = Dispatcher()
event_dispatcher.register('register',register)
event_dispatcher.register('ping',ping)
event_dispatcher.register('job_start',job_start)
event_dispatcher.register('job_done',job_done)
event_dispatcher.register('request_job',request_job)
event_dispatcher.register('clean_up_devices',clean_up_devices)
event_dispatcher.register('connect_to_master',connect_to_master)
event_dispatcher.register('get_job',get_job)
event_dispatcher.register('ping_master',ping_master)
logger.debug('Dispatcher initialized')
event = {'t':'e',
'event':'connect_to_master'}
event = Event(event)
event_dispatcher.add_to_queue(event)
event_dispatcher.dispatch_event()
event_dispatcher.iter_through_active_list()
last_devices_cleenup = time.time()
last_ping_master = time.time()
while True:
# recieving events
data = None
try:
data, address = server_socket.recvfrom(1024)
except:
pass
# parsing events and registering events
if data != None:
data_is_ok = False
try:
message = json.loads(data.decode('ascii'))
data_is_ok = True
except:
logger.warning("can't parse packet")
logger.debug(str(data))
if data_is_ok:
logger.debug('accepted packet')
logger.debug(str(message))
if message['t'] == 'e':
message['address'] = address
message['callback'] = server_socket
event = Event(message)
event_dispatcher.add_to_queue(event)
else:
device = devices.get(address,None)
if device == None:
server_socket.sendto(b'{"t":"e",\
"event":"register",\
"message":"You must register in cluster"}',
address)
else:
device.update_time()
# dispatching events
try:
event_dispatcher.dispatch_event(3)
except Exception as e:
logger.error('CANT DISPATCH EVENT')
logger.debug('Traceback',exc_info=e)
try:
event_dispatcher.iter_through_active_list()
except Exception as e:
logger.error('CANT EXECUTE')
logger.debug('Traceback',exc_info=e)
# request job and start it
if len(devices)>0 and master_server_is_connected:
# pinging master server
if JOB != None:
if time.time()-master_server_last_pinged>=PING_MASTER_SERVER:
event = Event({'t':'e',
'event':'ping_master'})
event_dispatcher.add_to_queue(event)
# requetsing job
if JOB == None:
event_dispatcher.clear_queue()
event = Event({'t':'e',
'event':'request_job',
'secret':JOB_START_SECRET,
'parts':20})
event_dispatcher.add_to_queue(event)
#elif not master_server_is_connected:
# event = Event({'t':'e',
# 'event':'connect_to_master'})
# event_dispatcher.add_to_queue(event)
# cleenup devices
if time.time()-last_devices_cleenup>TIME_FOR_DEVICE:
last_devices_cleenup = time.time()
logger.debug('Cleaning up devices')
event = {'t':'e',
'event':'clean_up_devices'}
event = Event(event)
event_dispatcher.add_to_queue(event)
time.sleep(0.1)
if __name__ == '__main__':
logger.info('STARTING SERVER')
#connect_to_master()
try:
server()
except Exception as e:
logger.error('ERROR ACCURED',exc_info=e)
input()