Exemplo n.º 1
0
class Server():
    def __init__(self, servicename, ip_addr, port,
                 workdir, argv, autostart=False,
                 statuschange=None, evaluator=default_evaluator,
                 pyruntimevars=None):
        self.continueloop = True
        self.daemon = None
        self.servicename = servicename
        self.ip_addr = ip_addr
        self.port = port
        self.workdir = workdir
        self.argv = argv
        self.plcobj = None
        self.servicepublisher = None
        self.autostart = autostart
        self.statuschange = statuschange
        self.evaluator = evaluator
        self.pyruntimevars = pyruntimevars

    def Loop(self):
        while self.continueloop:
            self.Start()

    def Restart(self):
        self.Stop()

    def Quit(self):
        self.continueloop = False
        if self.plcobj is not None:
            self.plcobj.StopPLC()
            self.plcobj.UnLoadPLC()
        self.Stop()

    def Start(self):
        pyro.initServer()
        self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port)
        self.plcobj = PLCObject(self.workdir, self.daemon, self.argv,
                                self.statuschange, self.evaluator,
                                self.pyruntimevars)
        uri = self.daemon.connect(self.plcobj, "PLCObject")

        print _("Pyro port :"), self.port
        print _("Pyro object's uri :"), uri

        # Beremiz IDE detects daemon start by looking
        # for self.workdir in the daemon's stdout.
        # Therefore don't delete the following line
        print _("Current working directory :"), self.workdir

        # Configure and publish service
        # Not publish service if localhost in address params
        if self.servicename is not None and \
           self.ip_addr is not None and \
           self.ip_addr != "localhost" and \
           self.ip_addr != "127.0.0.1":
            print _("Publishing service on local network")
            self.servicepublisher = ServicePublisher.ServicePublisher()
            self.servicepublisher.RegisterService(self.servicename, self.ip_addr, self.port)

        self.plcobj.AutoLoad()
        if self.plcobj.GetPLCstatus()[0] != "Empty":
            if self.autostart:
                self.plcobj.StartPLC()
        self.plcobj.StatusChange()

        sys.stdout.flush()

        self.daemon.requestLoop()
        self.daemon.sock.close()

    def Stop(self):
        if self.plcobj is not None:
            self.plcobj.StopPLC()
        if self.servicepublisher is not None:
            self.servicepublisher.UnRegisterService()
            self.servicepublisher = None
        self.daemon.shutdown(True)
Exemplo n.º 2
0
class Server(object):
    def __init__(self,
                 servicename,
                 ip_addr,
                 port,
                 workdir,
                 argv,
                 statuschange=None,
                 evaluator=default_evaluator,
                 pyruntimevars=None):
        self.continueloop = True
        self.daemon = None
        self.servicename = servicename
        self.ip_addr = ip_addr
        self.port = port
        self.workdir = workdir
        self.argv = argv
        self.servicepublisher = None
        self.statuschange = statuschange
        self.evaluator = evaluator
        self.pyruntimevars = pyruntimevars
        self.plcobj = PLCObject(self)

    def _to_be_published(self):
        return self.servicename is not None and \
               self.ip_addr is not None and \
               self.ip_addr != "localhost" and \
               self.ip_addr != "127.0.0.1"

    def PrintServerInfo(self):
        print(_("Pyro port :"), self.port)

        # Beremiz IDE detects LOCAL:// runtime is ready by looking
        # for self.workdir in the daemon's stdout.
        print(_("Current working directory :"), self.workdir)

        if self._to_be_published():
            print(_("Publishing service on local network"))

        sys.stdout.flush()

    def PyroLoop(self, when_ready):
        while self.continueloop:
            Pyro.config.PYRO_MULTITHREADED = 0
            pyro.initServer()
            self.daemon = pyro.Daemon(host=self.ip_addr, port=self.port)

            # pyro never frees memory after connection close if no timeout set
            # taking too small timeout value may cause
            # unwanted diconnection when IDE is kept busy for long periods
            self.daemon.setTimeout(60)

            self.daemon.connect(self.plcobj, "PLCObject")

            if self._to_be_published():
                self.servicepublisher = ServicePublisher.ServicePublisher()
                self.servicepublisher.RegisterService(self.servicename,
                                                      self.ip_addr, self.port)

            when_ready()
            self.daemon.requestLoop()
            self.daemon.sock.close()

    def Restart(self):
        self._stop()

    def Quit(self):
        self.continueloop = False
        if self.plcobj is not None:
            self.plcobj.StopPLC()
            self.plcobj.UnLoadPLC()
        self._stop()

    def _stop(self):
        if self.plcobj is not None:
            self.plcobj.StopPLC()
        if self.servicepublisher is not None:
            self.servicepublisher.UnRegisterService()
            self.servicepublisher = None
        self.daemon.shutdown(True)

    def AutoLoad(self):
        self.plcobj.AutoLoad()
        if self.plcobj.GetPLCstatus()[0] == "Stopped":
            if autostart:
                self.plcobj.StartPLC()
        self.plcobj.StatusChange()