11import datetime
2+ import json
23import logging
4+ import sys
35import time
46
57from .receptor import Receptor
@@ -17,20 +19,41 @@ def run_as_controller(config):
1719
1820def run_as_ping (config ):
1921 logger .info (f'Sending ping to { config .ping_recipient } .' )
20- now = datetime .datetime .utcnow ()
21- pings_sent = 0
22- while True :
23- controller .send_directive ('receptor:ping' , config .ping_recipient , now .isoformat (), config .ping_socket_path )
24- pings_sent += 1
25- if config .ping_count != 0 and pings_sent >= config .ping_count :
26- break
27- elif config .ping_delay > 0.0 :
28- time .sleep (config .ping_delay )
22+ sock = controller .connect_to_socket (config .ping_socket_path )
23+
24+ try :
25+ pings_sent = 0
26+ while True :
27+ now = datetime .datetime .utcnow ()
28+ response = controller .send_directive ('receptor:ping' , config .ping_recipient , now .isoformat (), sock )
29+ resp_json = json .loads (response )
30+ if 'code' in resp_json and resp_json ['code' ] != 0 :
31+ sys .stdout .buffer .write (b"Failed to ping node.\n " )
32+ else :
33+ sys .stdout .buffer .write (response + b"\n " )
34+ sys .stdout .flush ()
35+ pings_sent += 1
36+ if config .ping_count != 0 and pings_sent >= config .ping_count :
37+ break
38+ elif config .ping_delay > 0.0 :
39+ time .sleep (config .ping_delay )
40+ except KeyboardInterrupt :
41+ pass
42+ finally :
43+ sock .close ()
2944
3045
3146def run_as_send (config ):
3247 logger .info (f'Sending a { config .send_directive } directive to { config .send_recipient } .' )
33- controller .send_directive (config .send_directive , config .send_recipient , config .send_payload , config .send_socket_path )
48+ sock = controller .connect_to_socket (config .send_socket_path )
49+ try :
50+ response = controller .send_directive (config .send_directive , config .send_recipient , config .send_payload , sock )
51+ sys .stdout .buffer .write (response + b"\n " )
52+ sys .stdout .flush ()
53+ except KeyboardInterrupt :
54+ pass
55+ finally :
56+ sock .close ()
3457
3558
3659def run_as_node (config ):
0 commit comments