Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (29 sloc) 1.27 KB
'''Some concept of neighbourhood is useful to a distributed system. Whether
it's for gossipping, or pinging nearby nodes to ensure they're down, or peer to
peer communication.
This module provides a place to store a persistent, generic neighbourhood. No
bias towards application is made - this neighbourhood is probably just the
entire node list. The only guarantees provided are that it gets updated once
in a while, and that any two neighbourhoods with the same time stamp will be
identical in content and ordering even if they're on two different nodes.
'''
import cPickle
import conf
CACHE_FILE=conf.CACHE_FILE # persistent storage
stamp=0 #4-byte unique timestamp
nodes=[] #string list of host names, might be empty
def set(timestamp, neighbours):
'''Sets a new neighbourhood and time stamp.'''
global stamp,nodes
stamp,nodes = timestamp,neighbours
print 'Saving neighbours to file:',CACHE_FILE
cPickle.dump((stamp,nodes), open(CACHE_FILE, 'wb'))
def load(file=CACHE_FILE):
try:
global stamp,nodes
stamp,nodes = cPickle.load(open(file, 'rb'))
print 'Neighbours loaded. Timestamp:',stamp
except:
print 'Couldn''t load neighbour data from cache: keeping empty.'
# file probably doesn't exist - that's absolutely fine
load()