def run(self): start = time.time() while time.time() - start < 30: sd = linda.connect(self.serverport) if sd: sd = linda.getSD() break time.sleep(1) if time.time() - start >= 30: print "Unable to connect to server. The Monitor is exiting." return linda.send(sd, ("MONITOR", )) linda.recv(sd) while True: try: self.realrun(sd) break except: print "Caught exception" raise print "Monitor shutting down..." linda.disconnect()
def doCommand(self, sd, command): global keyboard_interrupt if command is None: return elif command[0] == "quit": return True elif command[0] == "list": linda.send(sd, ("LIST_TS", )) r = linda.recv(sd) print "TupleSpaces: " + " ".join(eval(r[2])) elif command[0] == "inspect": ts = self.eval(command[1]) if not isinstance(ts, linda.TupleSpace): print "'%s' is not a tuplespace." % (ts, ) return linda.send(sd, ("INSPECT", ts._id)) r = linda.recv(sd) if r[1] == "DONT_KNOW": print "The local server does not have a partition of %s" % (ts._id, ) return elif r[1] != "RESULT_TUPLE" or not isinstance(r[2], tuple) or len(r[2]) != 5: print "Invalid reply: '%s'" % (r, ) return r = r[2] print "References:", [x for x in eval(r[0].string) if x != linda.process_id] + ["Monitor" for x in eval(r[0].string) if x == linda.process_id] print "Threads Blocked:", eval(r[1].string) print "Servers Blocked:", eval(r[4].string) print "Partitions", eval(r[3].string) print "Tuples:", r[2].string print "" elif command[0] == "details": print "Node Id:", server.node_id elif command[0] == "route": linda.send(sd, ("GET_ROUTES", )) cons = linda.recv(sd) if len(cons[2]) == 0: print "No other servers connected" else: for c in cons[2]: if len(c) == 3: print "%s -> %s:%s" % (c[0], c[1], c[2]) else: print "%s -> %s" % (c[0], c[1]) elif command[0] == "watch": if command[1] is None: delay = 10 else: delay = min(0.1, command[1]) while True: self.doCommand(command[2]) if keyboard_interrupt: keyboard_interrupt = False return time.sleep(delay) elif command[0] == "out": ts = command[1] tup = command[2] if tup is None and ts[0] != "funccall": raise SystemError, "Expected 'out TS TUPLE'." elif tup is None: tup = ts[2] ts = ts[1] ts = self.eval(ts) tup = self.eval(tup) if not isinstance(ts, linda.TupleSpace): raise SystemError, "'%s' is not a tuplespace" % (ts, ) if not isinstance(tup, tuple): raise SystemError, "'%s' is not a tuple" % (tup, ) ts._out(tup) elif command[0] == "expr": print self.eval(command[1]) elif command[0] == "help": print "Possible commands..." print "list - Print list of all tuplespaces on the server" print "inspect <tsid> - List the tuples in the given tuplespace" print "watch <delay> <command> - Repeat the given command every <delay> seconds.\n If left out <delay> defaults to 10." print "quit - Shut down the server." else: raise SystemError, "Unknown command %s" % (command, )