runtime/PLCObject.py
changeset 209 08dc3d064cb5
parent 203 cb9901076a21
child 216 11124e129a28
equal deleted inserted replaced
208:dd630979f628 209:08dc3d064cb5
    22 #License along with this library; if not, write to the Free Software
    22 #License along with this library; if not, write to the Free Software
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import Pyro.core as pyro
    25 import Pyro.core as pyro
    26 from threading import Timer
    26 from threading import Timer
    27 import ctypes, os, dl, commands
    27 import ctypes, os, commands
    28 #, sys
       
    29 #sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)
       
    30 
    28 
    31 if os.name == ("nt", "ce"):
    29 if os.name == ("nt", "ce"):
    32     from _ctypes import LoadLibrary as dlopen
    30     from _ctypes import LoadLibrary as dlopen
    33     from _ctypes import FreeLibrary as dlclose
    31     from _ctypes import FreeLibrary as dlclose
    34 elif os.name == "posix":
    32 elif os.name == "posix":
    40      "linux2":".so",
    38      "linux2":".so",
    41      "win32":".dll",
    39      "win32":".dll",
    42      }.get(sys.platform, "")
    40      }.get(sys.platform, "")
    43 
    41 
    44 class PLCObject(pyro.ObjBase):
    42 class PLCObject(pyro.ObjBase):
    45     def __init__(self, workingdir, daemon):
    43     def __init__(self, workingdir, daemon, argv):
    46         pyro.ObjBase.__init__(self)
    44         pyro.ObjBase.__init__(self)
       
    45         self.argv=argv
    47         self.workingdir = workingdir
    46         self.workingdir = workingdir
    48         self.PLCStatus = "Stopped"
    47         self.PLCStatus = "Stopped"
    49         self.PLClibraryHandle = None
    48         self.PLClibraryHandle = None
    50         # Creates fake C funcs proxies
    49         # Creates fake C funcs proxies
    51         self._FreePLC()
    50         self._FreePLC()
   140                                   "libgdk",
   139                                   "libgdk",
   141                                   "libatk",
   140                                   "libatk",
   142                                   "libpan",
   141                                   "libpan",
   143                                   "libX11",
   142                                   "libX11",
   144                                   ]:
   143                                   ]:
   145                     badhandle = dlopen(badlib, dl.RTLD_NOLOAD)
   144                     #badhandle = dlopen(badlib, dl.RTLD_NOLOAD)
   146                     print "Dirty lib detected :" + badlib
   145                     print "Dirty lib detected :" + badlib
   147                     #dlclose(badhandle)
   146                     #dlclose(badhandle)
   148                     return True
   147                     return True
   149         return False
   148         return False
   150 
   149 
   151     
   150     
   152     def StartPLC(self):
   151     def StartPLC(self):
   153         print "StartPLC"
   152         print "StartPLC"
   154         if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
   153         if self.CurrentPLCFilename is not None and self.PLCStatus == "Stopped":
   155             c_argv = ctypes.c_char_p * len(sys.argv)
   154             c_argv = ctypes.c_char_p * len(sys.argv)
   156             if self._LoadNewPLC() and self._startPLC(len(sys.argv),c_argv(*sys.argv)) == 0:
   155             if self._LoadNewPLC() and self._startPLC(len(self.argv),c_argv(*self.argv)) == 0:
   157                 self.PLCStatus = "Started"
   156                 self.PLCStatus = "Started"
   158                 return True
   157                 return True
   159             else:
   158             else:
   160                 print "_StartPLC did not return 0 !"
   159                 print "_StartPLC did not return 0 !"
   161         return False
   160                 self._DoStopPLC()
       
   161         return False
       
   162 
       
   163     def _DoStopPLC(self):
       
   164         self._stopPLC()
       
   165         self.PLCStatus = "Stopped"
       
   166         if self._FreePLC():
       
   167             self.PLCStatus = "Dirty"
       
   168         return True
   162 
   169 
   163     def StopPLC(self):
   170     def StopPLC(self):
   164         if self.PLCStatus == "Started":
   171         if self.PLCStatus == "Started":
   165             self._stopPLC()
   172             self._DoStopPLC()
   166             self.PLCStatus = "Stopped"
       
   167             if self._FreePLC():
       
   168                 self.PLCStatus = "Dirty"
       
   169             return True
       
   170         return False
   173         return False
   171 
   174 
   172     def _Reload(self):
   175     def _Reload(self):
   173         self.daemon.shutdown(True)
   176         self.daemon.shutdown(True)
   174         self.daemon.sock.close()
   177         self.daemon.sock.close()