def __init__(self, symb, name, cmd, start_signal, size, tk, log): print cmd proc=subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) self.proc = proc self.name = name self.symb = symb self.type = "AI" self.logger = log self.state = 0 self.size = size tkinter.createfilehandler(proc.stderr, tkinter.READABLE, self.on_err_ready) self.kick_start(start_signal) self.tk = tk self.clean = False
class Controller: def __init__(self, path=_filename): # register a destruction handler with the Grail Application object app = get_grailapp() self._app = app app.register_on_exit(self._close) # calculate the socket's filename self._path = path self._fileno = None self._socket = None self._enabled = None # Don't create the socket now, because we want to allow # clients of this class to register callbacks for commands # first. self._cbdict = {} self._cmdre = re.compile('([^ \\t]+)(.*)') def start(self): """Begin listening for remote control commands.""" # initialize the socket if self._fileno is None: # for security, create the file structure head, self._filename = os.path.split(self._path) dirhier = [] while head and not os.path.isdir(head): head, t = os.path.split(head) dirhier.insert(0, t) for dir in dirhier: head = os.path.join(head, dir) os.mkdir(head, 0700) self._filename = self._path # TBD: What do we do with multiple Grail processes? Which # one do we remote control? if os.path.exists(self._filename): # first make sure that the socket is connected to a # live Grail process. E.g. if Python core dumped, the # exit handler won't run so you'd be dead in the # water. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.connect(self._filename) s.send('PING NOACK') s.close() raise ClashError except socket.error, (errno, msg): os.unlink(self._filename) s.close() # create the FIFO object s = self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind(self._filename) s.listen(1) # register with Tk self._fileno = s.fileno() if self._fileno < 0: self._fileno = None raise InitError if not self._enabled: self._enabled = 1 tkinter.createfilehandler(self._fileno, tkinter.READABLE, self._dispatch) self.register('PING', self.ping_cmd)
def get_move(self, timeout, todo): self.todo = todo self.timeout_id = self.tk.after(timeout, self.on_timeout) tkinter.createfilehandler(self.proc.stdout, tkinter.READABLE, self.on_read_ready)