Added clsn's ping-pong code

master
Sean B. Palmer 2012-01-12 14:21:49 +00:00
parent e3633957a9
commit 2575536078
1 changed files with 45 additions and 0 deletions

View File

@ -7,6 +7,51 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/ http://inamidst.com/phenny/
""" """
import threading, time
def setup(phenny):
# by clsn
phenny.data = {}
refresh_delay = 300.0
if hasattr(phenny.config, 'refresh_delay'):
try: refresh_delay = float(phenny.config.refresh_delay)
except: pass
def close():
print "Nobody PONGed our PING, restarting"
phenny.handle_close()
def pingloop():
timer = threading.Timer(refresh_delay, close, ())
phenny.data['startup.setup.timer'] = timer
phenny.data['startup.setup.timer'].start()
# print "PING!"
phenny.write(('PING', phenny.config.host))
phenny.data['startup.setup.pingloop'] = pingloop
def pong(phenny, input):
try:
# print "PONG!"
phenny.data['startup.setup.timer'].cancel()
time.sleep(refresh_delay + 60.0)
pingloop()
except: pass
pong.event = 'PONG'
pong.thread = True
pong.rule = r'.*'
phenny.variables['pong'] = pong
# Need to wrap handle_connect to start the loop.
inner_handle_connect = phenny.handle_connect
def outer_handle_connect():
inner_handle_connect()
if phenny.data.get('startup.setup.pingloop'):
phenny.data['startup.setup.pingloop']()
phenny.handle_connect = outer_handle_connect
def startup(phenny, input): def startup(phenny, input):
if hasattr(phenny.config, 'serverpass'): if hasattr(phenny.config, 'serverpass'):
phenny.write(('PASS', phenny.config.serverpass)) phenny.write(('PASS', phenny.config.serverpass))