2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
5 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
28 import cPickle as pickle #@UnusedImport
30 import pickle #@Reimport
34 logger = logging.getLogger(__name__)
35 # logger.setLevel(logging.DEBUG)
36 logger.setLevel(logging.INFO)
37 ch = logging.StreamHandler()
38 ch.setLevel(logging.DEBUG)
39 formatter = logging.Formatter("%(levelname)s:%(threadName)s:%(message)s")
40 ch.setFormatter(formatter)
44 logger = createLogger()
46 #------------------------------------
47 # A file locker (Linux only)
48 def __acquire_lock(lock):
49 if sys.platform == "win32":
51 # lock 1 byte: file is supposed to be zero-byte long
52 msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1)
55 fcntl.flock(lock, fcntl.LOCK_EX)
57 def __release_lock(lock):
58 if sys.platform == "win32":
60 msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1)
63 fcntl.flock(lock, fcntl.LOCK_UN)
66 def _getConfigurationFilename():
67 omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
69 from salome_utils import generateFileName
70 portmanager_config = generateFileName(omniorbUserPath,
76 temp = tempfile.NamedTemporaryFile()
77 lock_file = os.path.join(os.path.dirname(temp.name), ".omniORB_PortManager.lock")
80 return (portmanager_config, lock_file)
83 def __isPortUsed(port, busy_ports):
84 return (port in busy_ports) or __isNetworkConnectionActiveOnPort(port)
87 def __isNetworkConnectionActiveOnPort(port):
88 # :NOTE: Under windows:
89 # netstat options -l and -t are unavailable
90 # grep command is unavailable
91 from subprocess import Popen, PIPE
92 stdout, _ = Popen(['netstat','-an'], stdout=PIPE).communicate()
94 buf = StringIO.StringIO(stdout)
95 ports = buf.readlines()
96 # search for TCP - LISTEN connections
98 regObj = re.compile( ".*tcp.*:([0-9]+).*:.*listen", re.IGNORECASE );
101 p = int(regObj.match(item).group(1))
102 if p == port: return True
107 def getPort(preferedPort=None):
108 logger.debug("GET PORT")
110 config_file, lock_file = _getConfigurationFilename()
111 oldmask = os.umask(0)
112 with open(lock_file, 'w') as lock:
117 config = {'busy_ports':[]}
118 logger.debug("read configuration file")
120 with open(config_file, 'r') as f:
121 config = pickle.load(f)
123 logger.info("Problem loading PortManager file: %s"%config_file)
124 # In this case config dictionary is reset
126 logger.debug("load busy_ports: %s"%str(config["busy_ports"]))
129 busy_ports = config["busy_ports"]
131 if not port or __isPortUsed(port, busy_ports):
133 while __isPortUsed(port, busy_ports):
136 msg += "Can't find a free port to launch omniNames\n"
137 msg += "Try to kill the running servers and then launch SALOME again.\n"
138 raise RuntimeError, msg
139 logger.debug("Port %s seems to be busy"%str(port))
140 if not port in config["busy_ports"]:
141 config["busy_ports"].append(port)
143 logger.debug("found free port: %s"%str(port))
144 config["busy_ports"].append(port)
147 logger.debug("write busy_ports: %s"%str(config["busy_ports"]))
149 with open(config_file, 'w') as f:
150 pickle.dump(config, f)
159 logger.debug("get port: %s"%str(port))
163 def releasePort(port):
165 logger.debug("RELEASE PORT (%s)"%port)
167 config_file, lock_file = _getConfigurationFilename()
168 with open(lock_file, 'w') as lock:
173 config = {'busy_ports':[]}
174 logger.debug("read configuration file")
176 with open(config_file, 'r') as f:
177 config = pickle.load(f)
178 except IOError: # empty file
181 logger.debug("load busy_ports: %s"%str(config["busy_ports"]))
183 # remove port from list
184 busy_ports = config["busy_ports"]
186 if port in busy_ports:
187 busy_ports.remove(port)
188 config["busy_ports"] = busy_ports
191 logger.debug("write busy_ports: %s"%str(config["busy_ports"]))
193 with open(config_file, 'w') as f:
194 pickle.dump(config, f)
201 logger.debug("released port port: %s"%str(port))
205 config_file, lock_file = _getConfigurationFilename()
206 with open(lock_file, 'w') as lock:
211 config = {'busy_ports':[]}
212 logger.debug("read configuration file")
214 with open(config_file, 'r') as f:
215 config = pickle.load(f)
216 except IOError: # empty file
219 logger.debug("load busy_ports: %s"%str(config["busy_ports"]))
221 busy_ports = config["busy_ports"]