class Sentinel(threading.Thread):
    def __init__(self, nwsHost, nwsPort, nwsName, gotShutdown):
        threading.Thread.__init__(self, name='Sentinel:' + nwsName)
        self.nwsHost = nwsHost
        self.nwsPort = nwsPort
        self.nwsName = nwsName
        self.gotShutdown = gotShutdown
        self.ws = NetWorkSpace(self.nwsName, self.nwsHost, self.nwsPort,
                useUse=True, create=False)

    def __str__(self):
        return "%s@%s:%d" % (self.nwsName, self.nwsHost, self.nwsPort)

    def run(self):
        try:
            try:
                self.ws.find('Sleigh ride over')
                self.ws.store('bye', 'Sleigh ride over')
            except Exception, e:
                try: self.ws.store('bye', str(sys.exc_info()[1]))
                except: pass
        finally:
            try: self.gotShutdown(self)
            except: pass
            try: self.ws.server.close()
            except: pass
try:
    ws = NetWorkSpace('snake pit')
except ImportError, e:
    print >> sys.stderr, "make sure you're running the NWS server on this machine"
    print >> sys.stderr, str(e)
    sys.exit(1)

print 'connected, listing contents of netWorkSpace (should be nothing there).'
print ws.listVars()

ws.store('x', 1)
print 'should now see x.'
print ws.listVars()

print 'find (but don\'t consume) x.'
print ws.find('x')
print 'check that it is still there.'
print ws.listVars()

print 'associate another value with x.'
ws.store('x', 2)
print ws.listVars()

print 'consume values for x, should see them in order saved.'
print ws.fetch('x')
print ws.fetch('x')
print 'no more values for x... .'
print ws.listVars()

print 'so try to fetch and see what happens... .'
print ws.fetchTry('x', 'no go')