Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Basic reconnect #2

Merged
merged 2 commits into from

2 participants

@ArthurClune

Mark,

As per David's email, very simple minded indeed reconnect on fail.

Arthur

@rep
Owner

will merge without checking - i have trust in you guys :)

@rep rep merged commit 43be9c3 into rep:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 15, 2012
  1. @ArthurClune
Commits on Jun 11, 2012
  1. @ArthurClune
This page is out of date. Refresh to see the latest.
Showing with 37 additions and 13 deletions.
  1. +15 −7 cli/csv2file.py
  2. +22 −6 cli/hpfeeds.py
View
22 cli/csv2file.py
@@ -4,7 +4,7 @@
import json
import logging
logging.basicConfig(level=logging.CRITICAL)
-
+from time import sleep
import hpfeeds
HOST = 'hpfeeds.honeycloud.net'
@@ -20,9 +20,6 @@ def main():
print >>sys.stderr, 'could not open output file for message log.'
return 1
- hpc = hpfeeds.new(HOST, PORT, IDENT, SECRET)
- print >>sys.stderr, 'connected to', hpc.brokername
-
def on_message(identifier, channel, payload):
try: decoded = json.loads(str(payload))
except: decoded = {'raw': payload}
@@ -39,9 +36,20 @@ def on_error(payload):
print >>sys.stderr, ' -> errormessage from server: {0}'.format(payload)
hpc.stop()
- hpc.subscribe(CHANNELS)
- hpc.run(on_message, on_error)
- hpc.close()
+ while True:
+ try:
+ hpc = hpfeeds.new(HOST, PORT, IDENT, SECRET)
+ print >>sys.stderr, 'connected to', hpc.brokername
+ hpc.subscribe(CHANNELS)
+ except hpfeeds.FeedException:
+ # failed to connect
+ sleep(20)
+ break
+ hpc.run(on_message, on_error)
+ hpc.close()
+ # if we get here, connection has error'd out
+ # just wait and try again
+ sleep(20)
return 0
if __name__ == '__main__':
View
28 cli/hpfeeds.py
@@ -3,6 +3,7 @@
import socket
import hashlib
import logging
+from time import sleep
logger = logging.getLogger('pyhpfeeds')
@@ -52,10 +53,12 @@ class FeedException(Exception):
pass
class HPC(object):
- def __init__(self, host, port, ident, secret, timeout=3):
+ def __init__(self, host, port, ident, secret, timeout=3, reconnect=False, sleepwait=20):
self.host, self.port = host, port
self.ident, self.secret = ident, secret
self.timeout = timeout
+ self.reconnect = reconnect
+ self.sleepwait = sleepwait
self.brokername = 'unknown'
self.connected = False
self.stopped = False
@@ -91,7 +94,7 @@ def connect(self):
self.s.settimeout(None)
- def run(self, message_callback, error_callback):
+ def _run(self, message_callback, error_callback):
while not self.stopped:
while self.connected:
d = self.s.recv(BUFSIZ)
@@ -115,6 +118,21 @@ def run(self, message_callback, error_callback):
if self.stopped: break
self.connect()
+ def run(self, message_callback, error_callback):
+ if not self.reconnect:
+ self._run(message_callback, error_callback)
+ else:
+ while True:
+ self._run(message_callback, error_callback)
+ # reconnect now we've failed
+ sleep(self.sleepwait)
+ while True:
+ try:
+ self.connect()
+ break
+ except FeedException:
+ sleep(self.sleepwait)
+
def subscribe(self, chaninfo):
if type(chaninfo) == str:
chaninfo = [chaninfo,]
@@ -133,8 +151,6 @@ def close(self):
try: self.s.close()
except: logger.warn('Socket exception when closing.')
-def new(host=None, port=10000, ident=None, secret=None):
- if not host or not ident or not secret:
- raise Exception('host, ident and secret are mandatory!')
- return HPC(host, port, ident, secret)
+def new(host=None, port=10000, ident=None, secret=None, reconnect=True, sleepwait=20):
+ return HPC(host, port, ident, secret, reconnect)
Something went wrong with that request. Please try again.