Ejemplo n.º 1
0
    def initEngine(self, engine, color, lowPriority):
        name = engine['name']
        protocol = engine["protocol"]
        protover = 2 if engine.get("protover") is None else engine.get(
            "protover")
        path = engine['command']
        args = [] if engine.get('args') is None else [
            a for a in engine['args']
        ]
        if engine.get('vm_command') is not None:
            vmpath = engine['vm_command']
            vmargs = [] if engine.get('vm_args') is None else [
                a for a in engine['vm_args']
            ]
            args = vmargs + [path] + args
            path = vmpath
        md5_engine = engine['md5']

        working_directory = engine.get("workingDirectory")
        if working_directory:
            workdir = working_directory
        else:
            workdir = getEngineDataPrefix()
        warnwords = ("illegal", "error", "exception")
        try:
            subprocess = SubProcess(path,
                                    args=args,
                                    warnwords=warnwords,
                                    cwd=workdir,
                                    lowPriority=lowPriority)
            yield from subprocess.start()
        except OSError:
            raise PlayerIsDead
        except asyncio.TimeoutError:
            raise PlayerIsDead
        except GLib.GError:
            raise PlayerIsDead
        except Exception:
            raise PlayerIsDead

        engine_proc = attrToProtocol[protocol](subprocess, color, protover,
                                               md5_engine)
        engine_proc.setName(name)

        # If the user has configured special options for this engine, here is
        # where they should be set.

        def optionsCallback(set_option):
            if engine.get("options"):
                for option in engine["options"]:
                    key = option["name"]
                    value = option.get("value")
                    if (value is not None) and option["default"] != value:
                        if protocol == "xboard" and option["type"] == "check":
                            value = int(bool(value))
                        set_option.setOption(key, value)

        engine_proc.connect("readyForOptions", optionsCallback)

        return engine_proc
Ejemplo n.º 2
0
 def start(self):
     assert not self.subproc
     if sys.platform == "win32":
         args = ["-t", self.host]
     else:
         args = ["-i10", self.host]
     self.subproc = SubProcess(searchPath("ping"), args, env={"LANG": "en"})
     self.conid1 = self.subproc.connect("line", self.__handleLines)
     self.conid2 = self.subproc.connect("died", self.__handleDead)
Ejemplo n.º 3
0
    def initEngine(self, engine, color, lowPriority):
        name = engine['name']
        protocol = engine["protocol"]
        protover = 2 if engine.get("protover") is None else engine.get(
            "protover")
        path = engine['command']
        args = [] if engine.get('args') is None else [a
                                                      for a in engine['args']]
        if engine.get('vm_command') is not None:
            vmpath = engine['vm_command']
            vmargs = [] if engine.get('vm_args') is None else [
                a for a in engine['vm_args']
            ]
            args = vmargs + [path] + args
            path = vmpath
        md5_engine = engine['md5']

        working_directory = engine.get("workingDirectory")
        if working_directory:
            workdir = working_directory
        else:
            workdir = getEngineDataPrefix()
        warnwords = ("illegal", "error", "exception")
        try:
            subprocess = SubProcess(path, args=args, warnwords=warnwords, cwd=workdir, lowPriority=lowPriority)
            yield from subprocess.start()
        except OSError:
            raise PlayerIsDead
        except asyncio.TimeoutError:
            raise PlayerIsDead
        except GLib.GError:
            raise PlayerIsDead
        except Exception:
            raise PlayerIsDead

        engine_proc = attrToProtocol[protocol](subprocess, color, protover,
                                               md5_engine)
        engine_proc.setName(name)

        # If the user has configured special options for this engine, here is
        # where they should be set.

        def optionsCallback(set_option):
            if engine.get("options"):
                for option in engine["options"]:
                    key = option["name"]
                    value = option.get("value")
                    if (value is not None) and option["default"] != value:
                        if protocol == "xboard" and option["type"] == "check":
                            value = int(bool(value))
                        set_option.setOption(key, value)

        engine_proc.connect("readyForOptions", optionsCallback)

        return engine_proc
Ejemplo n.º 4
0
    def initEngine(self, xmlengine, color):
        protover = int(xmlengine.get("protover"))
        protocol = xmlengine.get("protocol")

        path = xmlengine.find('path').text.strip()
        args = [a.get('value') for a in xmlengine.findall('args/arg')]
        if xmlengine.find('vm') is not None:
            vmpath = xmlengine.find('vm/path').text.strip()
            vmargs = [a.get('value') for a in xmlengine.findall('vm/args/arg')]
            args = vmargs + [path] + args
            path = vmpath

        warnwords = ("illegal", "error", "exception")
        subprocess = SubProcess(path, args, warnwords, SUBPROCESS_SUBPROCESS)
        engine = attrToProtocol[protocol](subprocess, color, protover)

        if protocol == "uci":
            # If the user has configured special options for this engine, here is
            # where they should be set.
            def optionsCallback(engine):
                if engine.hasOption("OwnBook"):
                    engine.setOption("OwnBook", True)

            engine.connect("readyForOptions", optionsCallback)

        return engine
Ejemplo n.º 5
0
    def initEngine(self, engine, color):
        name = engine['name']
        protocol = engine["protocol"]
        protover = 2 if engine.get("protover") is None else engine.get(
            "protover")
        path = engine['command']
        args = [] if engine.get('args') is None else [
            a for a in engine['args']
        ]
        if engine.get('vm_command') is not None:
            vmpath = engine['vm_command']
            vmargs = [] if engine.get('vm_args') is None else [
                a for a in engine['vm_args']
            ]
            args = vmargs + [path] + args
            path = vmpath
        md5 = engine['md5']

        working_directory = engine.get("workingDirectory")
        if working_directory:
            workdir = working_directory
        else:
            workdir = getEngineDataPrefix()
        warnwords = ("illegal", "error", "exception")
        subprocess = SubProcess(path, args, warnwords, SUBPROCESS_SUBPROCESS,
                                workdir)
        engine_proc = attrToProtocol[protocol](subprocess, color, protover,
                                               md5)

        engine_proc.setName(name)

        # If the user has configured special options for this engine, here is
        # where they should be set.
        def optionsCallback(e):
            if engine.get("options"):
                for option in engine["options"]:
                    key = option["name"]
                    value = option.get("value")
                    if (value is not None) and option["default"] != value:
                        if protocol == "xboard" and option["type"] == "check":
                            value = int(bool(value))
                        e.setOption(key, value)

        engine_proc.connect("readyForOptions", optionsCallback)

        return engine_proc
Ejemplo n.º 6
0
class Pinger (GObject.GObject):
    """ The recieved signal contains the time it took to get response from the
        server in millisecconds. -1 means that some error occurred """
    
    __gsignals__ = {
        "recieved": (GObject.SignalFlags.RUN_FIRST, None, (float,)),
        "error": (GObject.SignalFlags.RUN_FIRST, None, (str,))
    }
    
    def __init__ (self, host):
        GObject.GObject.__init__(self)
        self.host = host
        self.subproc = None
        
        self.expression = re.compile("time=([\d\.]+) (m?s)")

        # We need untranslated error messages in regexp search
        # below, so have to use deferred translation here
        def _(msg): return msg
        error = _("Destination Host Unreachable")
        self.errorExprs = (
            re.compile("(%s)" % error),
        )
        del _

        self.restartsOnDead = 3
        self.deadCount = 0
    
    def start (self):
        assert not self.subproc
        self.subproc = SubProcess(searchPath("ping"),
                                  ["-i10", self.host], env={"LANG":"en"})
        self.conid1 = self.subproc.connect("line", self.__handleLines)
        self.conid2 = self.subproc.connect("died", self.__handleDead)

    def __handleLines (self, subprocess, lines):
        for line in lines:
            self.__handleLine(line)
    
    def __handleLine (self, line):
        match = self.expression.search(line)
        if match:
            time, unit = match.groups()
            time = float(time)
            if unit == "s":
                time *= 1000
            self.emit("recieved", time)
        else:
            for expr in self.errorExprs:
                match = expr.search(line)
                if match:
                    msg = match.groups()[0]
                    self.emit("error", _(msg))
    
    def __handleDead (self, subprocess):
        if self.deadCount < self.restartsOnDead:
            log.warning("Pinger died and restarted (%d/%d)" %
                     (self.deadCount+1, self.restartsOnDead),
                     extra={"task": self.subproc.defname})
            self.stop()
            self.start()
            self.deadCount += 1
        else:
            self.emit("error", _("Died"))
            self.stop()
    
    def stop (self):
        if not self.subproc:
            return
        exitCode = self.subproc.gentleKill()
        self.subproc.disconnect(self.conid1)
        self.subproc.disconnect(self.conid2)
        self.subproc = None
Ejemplo n.º 7
0
 def start (self):
     assert not self.subproc
     self.subproc = SubProcess(searchPath("ping"),
                               ["-i10", self.host], env={"LANG":"en"})
     self.conid1 = self.subproc.connect("line", self.__handleLines)
     self.conid2 = self.subproc.connect("died", self.__handleDead)
Ejemplo n.º 8
0
class Pinger(GObject.GObject):
    """ The received signal contains the time it took to get response from the
        server in millisecconds. -1 means that some error occurred """

    __gsignals__ = {
        "received": (GObject.SignalFlags.RUN_FIRST, None, (float, )),
        "error": (GObject.SignalFlags.RUN_FIRST, None, (str, ))
    }

    def __init__(self, host):
        GObject.GObject.__init__(self)
        self.host = host
        self.subproc = None

        self.expression = re.compile("=([\d\.]+) (m?s)")

        # We need untranslated error messages in regexp search
        # below, so have to use deferred translation here
        def _(msg):
            return msg

        error = _("Destination Host Unreachable")
        self.errorExprs = (re.compile("(%s)" % error), )
        del _

        self.restartsOnDead = 3
        self.deadCount = 0

    def start(self):
        assert not self.subproc
        if sys.platform == "win32":
            args = ["-t", self.host]
        else:
            args = ["-i10", self.host]
        self.subproc = SubProcess(shutil.which("ping"),
                                  args,
                                  env={"LANG": "en"})
        create_task(self.subproc.start())
        self.conid1 = self.subproc.connect("line", self.__handleLines)
        self.conid2 = self.subproc.connect("died", self.__handleDead)

    def __handleLines(self, subprocess, line):
        match = self.expression.search(line)
        if match:
            time, unit = match.groups()
            time = float(time)
            if unit == "s":
                time *= 1000
            self.emit("received", time)
        else:
            for expr in self.errorExprs:
                match = expr.search(line)
                if match:
                    msg = match.groups()[0]
                    self.emit("error", _(msg))

    def __handleDead(self, subprocess):
        if self.deadCount < self.restartsOnDead:
            log.warning("Pinger died and restarted (%d/%d)" %
                        (self.deadCount + 1, self.restartsOnDead),
                        extra={"task": self.subproc.defname})
            self.stop()
            self.start()
            self.deadCount += 1
        else:
            self.emit("error", _("Died"))
            self.stop()

    def stop(self):
        if not self.subproc:
            return
        # exitCode = self.subproc.gentleKill()
        self.subproc.disconnect(self.conid1)
        self.subproc.disconnect(self.conid2)
        self.subproc.terminate()
        self.subproc = None
Ejemplo n.º 9
0
 def start(self):
     assert not self.subproc
     self.subproc = SubProcess(searchPath("ping"), [self.host],
                               env={"LANG": "en"})
     self.conid1 = self.subproc.connect("line", self.__handleLines)
     self.conid2 = self.subproc.connect("died", self.__handleDead)