Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _create_connection(self):
while True:
parameters = GatewayParameters(address=self.address,
port=self.port,
auto_close=self.auto_close,
eager_load=True)
connection = MuffledGatewayConnection(parameters, self.gateway_property)
connection_success = False
try:
connection.start()
connection_success = True
except Py4JNetworkError:
pass
except (KeyboardInterrupt, SystemExit):
break
if connection_success:
break
time.sleep(0.1)
return connection
def init(self, gateway=None):
"""
Sets up the Py4J Gateway, called automatically on import.
"""
if not self.is_initialized:
try:
# Set-up Main Gateway Connection to JVM
if gateway is None:
self.GATEWAY = JavaGateway(gateway_parameters=GatewayParameters(auto_field=True))
else:
self.GATEWAY = gateway
self.PKG = self.GATEWAY.jvm
self.GEOWAVE_PKG = self.GATEWAY.jvm.org.locationtech.geowave
### Reflection utility ###
self.reflection_util= self.GATEWAY.jvm.py4j.reflection.ReflectionUtil
self.is_initialized = True
except Py4JNetworkError as e:
raise GeoWaveConfiguration.PyGwJavaGatewayNotStartedError("The GeoWave Py4J Java Gateway must be running before you can use pygw.") from e
else:
logger.error(login.text)
raise ConnectionError(login.text)
try:
session = None
if(os.environ.get('SSL-Enabled') == "True"):
session = JavaGateway(gateway_parameters=GatewayParameters(address=self.__address,
port=self.__port,
auth_token=self.__token
),
callback_server_parameters=CallbackServerParameters(
port=(self.__port+1)))
else:
session = JavaGateway(
gateway_parameters=GatewayParameters(
address=self.__address, port=self.__port),
callback_server_parameters=CallbackServerParameters(
port=(self.__port+1))
)
self._java_gateway = session
self._java_gaffer_session = session.entry_point
logger.info("Connected to Gaffer Session")
return self
except:
logger.error("Issue connecting to Gaffer Session")
def _start_gateway(self, p2=Machete):
# auto select callback server port and reset it in java env
self.gateway = JavaGateway(gateway_parameters=GatewayParameters(
port=self.port), callback_server_parameters=CallbackServerParameters(port=0))
python_port = self.gateway.get_callback_server().get_listening_port()
self.gateway.java_gateway_server.resetCallbackClient(
self.gateway.java_gateway_server.getCallbackClient().getAddress(), python_port)
self.manager = self.gateway.entry_point
# create pipe between gym_env_api and python_ai for java env
server, client = Pipe()
self.pipe = server
self.p1 = GymAI(self.gateway, client, True)
self.manager.registerAI(self.p1.__class__.__name__, self.p1)
if isinstance(p2, str):
# p2 is a java class name
self.p2 = p2
self.game_to_start = self.manager.createGame(
cmd.append(os.path.dirname(self._path) + '/')
try:
# start the py4j.GatewayServer, cannot use subprocess.call() because it blocks
subprocess.Popen(cmd, stderr=sys.stderr, stdout=sys.stdout)
err = None
except IOError as e:
err = str(e) + '\nYou must have a Java Runtime Environment installed and available on PATH'
if err:
raise IOError(err)
utils.wait_for_server(address, port, 5.0)
self._gateway = JavaGateway(
gateway_parameters=GatewayParameters(address=address, port=port, **kwargs)
)
self._lib = self._gateway.jvm
elif libtype == 'net' or libtype == 'clr':
if not utils.is_pythonnet_installed():
raise IOError('Cannot load a .NET Assembly because pythonnet is not installed.\n'
'To install pythonnet run: pip install pythonnet')
import clr
try:
# By default, pythonnet can only load libraries that are for .NET 4.0+.
#
# In order to allow pythonnet to load a library from .NET <4.0 the
# useLegacyV2RuntimeActivationPolicy property needs to be enabled
# in a .config file. If the following statement
def init_py4j_stuff(self):
print("Connecting to Java Gateway on port " + str(self.myPort))
self.gateway = JavaGateway(gateway_parameters=GatewayParameters(auto_convert=True, port=self.myPort))
self.javaAgent = self.gateway.entry_point.getAgent()
print("Connection to Java successful!")
'{0}{1}KOMORAN-4e7b5ef.jar',
'{0}{1}KOMORANEntryPoint-0.1.6.jar',
# '{0}{1}*'
]
classpath = os.pathsep.join([lib.format(jar_path, os.sep) for lib in libraries])
py4j_path = "{0}{1}py4j0.10.8.1.jar".format(jar_path, os.sep)
port = launch_gateway(jarpath=py4j_path,
classpath=classpath,
javaopts=['-Dfile.encoding=UTF8', '-ea', '-Xmx{}m'.format(max_heap)],
die_on_exit=True)
logging.debug("initializing JVM... ")
try:
jvm_gateway = JavaGateway(gateway_parameters=GatewayParameters(port=port, auto_convert=True))
# # for debugging with Java-side
# jvm_gateway = JavaGateway(gateway_parameters=GatewayParameters(port=25335, auto_convert=True))
except Exception as e:
jvm_gateway = None
logging.debug("fail")
logging.debug("success")
return jvm_gateway.jvm
def _start_gateway(self, p2=Machete):
# auto select callback server port and reset it in java env
self.gateway = JavaGateway(gateway_parameters=GatewayParameters(
port=self.port), callback_server_parameters=CallbackServerParameters(port=0))
python_port = self.gateway.get_callback_server().get_listening_port()
self.gateway.java_gateway_server.resetCallbackClient(
self.gateway.java_gateway_server.getCallbackClient().getAddress(), python_port)
self.manager = self.gateway.entry_point
# create pipe between gym_env_api and python_ai for java env
server, client = Pipe()
self.pipe = server
self.p1 = GymAIDisplay(self.gateway, client, False)
self.manager.registerAI(self.p1.__class__.__name__, self.p1)
if isinstance(p2, str):
# p2 is a java class name
self.p2 = p2
self.game_to_start = self.manager.createGame(
def restoreContext():
names = list(sys.modules[__name__].__dict__.keys())
for n in names:
if n not in __saved_context__:
del sys.modules[__name__].__dict__[n]
# connect python side to Java side with Java dynamic port and start python
# callback server with a dynamic port
py4jInterpreter = Py4jInterpreter()
logger = logging.getLogger("py4j")
logger.setLevel(logging.ERROR)
#logger.addHandler(logging.StreamHandler())
gateway = JavaGateway(
gateway_parameters=GatewayParameters(address=sys.argv[1], port=int(sys.argv[2])),
callback_server_parameters=CallbackServerParameters(port=0),
python_server_entry_point=py4jInterpreter)
# retrieve the port on which the python callback server was bound to.
python_port = gateway.get_callback_server().get_listening_port()
# tell the Java side to connect to the python callback server with the new
# python port. Note that we use the java_gateway_server attribute that
# retrieves the GatewayServer instance.
gateway.java_gateway_server.resetCallbackClient(
gateway.java_gateway_server.getCallbackClient().getAddress(),
python_port)
saveContext()
gateway.entry_point.ready()