+ if use_port:
+ print "Check if port can be used: %d" % use_port,
+ if port == use_port and port != -1:
+ print "- OK"
+ __setup_config(use_port, args, save_config)
+ return
+ else:
+ print "- KO: port is busy"
+ pass
+ #
+ print "Searching for a free port for naming service:",
+ if port == -1: # try again
+ port = getPort(use_port)
+
+ if port != -1:
+ print "%s - OK"%(port)
+ __setup_config(port, args, save_config)
+ else:
+ print "Unable to obtain port"
+
+ queue.put([os.environ['OMNIORB_CONFIG'],
+ os.environ['NSPORT'],
+ os.environ['NSHOST']])
+#
+
+def __savePortToFile(args):
+ # Save Naming service port name into
+ # the file args["ns_port_log_file"]
+ if args.has_key('ns_port_log_file'):
+ omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
+ file_name = os.path.join(omniorbUserPath, args["ns_port_log_file"])
+ with open(file_name, "w") as f:
+ f.write(os.environ['NSPORT'])
+#
+
+def searchFreePort(args={}, save_config=1, use_port=None):
+ """
+ Search free port for SALOME session.
+ Returns first found free port number.
+ """
+ try:
+ import PortManager # mandatory
+ from multiprocessing import Process, Queue
+ queue = Queue()
+ p = Process(target = searchFreePort_withPortManager, args=(queue, args, save_config, use_port,))
+ p.start()
+ info = queue.get()
+
+ os.environ['OMNIORB_CONFIG'] = info[0]
+ os.environ['NSPORT'] = info[1]
+ args['port'] = os.environ['NSPORT']
+ os.environ['NSHOST'] = info[2]
+ __savePortToFile(args)
+
+ p.join() # this blocks until the process terminates
+ except ImportError:
+ searchFreePort_withoutPortManager(args, save_config, use_port)
+ __savePortToFile(args)
+#