Skip to content

Commit

Permalink
Add a no_context ServerContext (#1613)
Browse files Browse the repository at this point in the history
* Improve typehinting

Signed-off-by: paul.profizi <[email protected]>

* Add BaseService.initialize()

Signed-off-by: paul.profizi <[email protected]>

* Initialize servers with no context

Signed-off-by: paul.profizi <[email protected]>

* Add a no_context ServerContext

Signed-off-by: paul.profizi <[email protected]>

* Add a test

Signed-off-by: paul.profizi <[email protected]>

* Fix retro and case where SERVER_CONTEXT is indeed None (Docker)

Signed-off-by: paul.profizi <[email protected]>

* Try fixing LicensingContextType.same_licensing_context for LicensingContextType.none

Signed-off-by: paul.profizi <[email protected]>

* Fix test_server_without_context

Signed-off-by: paul.profizi <[email protected]>

* Fix test_server_without_context

Signed-off-by: paul.profizi <[email protected]>

* Initialize on client if server has client

Signed-off-by: paul.profizi <[email protected]>

* Skip test <232

Signed-off-by: paul.profizi <[email protected]>

* Skip test <232

Signed-off-by: paul.profizi <[email protected]>

---------

Signed-off-by: paul.profizi <[email protected]>
  • Loading branch information
PProfizi committed Jun 14, 2024
1 parent 5f4db80 commit 08d6203
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 50 deletions.
9 changes: 9 additions & 0 deletions src/ansys/dpf/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ def initialize_with_context(self, context):
int(context.licensing_context_type), context.xml_path
)

def initialize(self):
"""Initialize a DPF server without a context."""
if self._server().has_client():
self._api.data_processing_initialization_on_client(
self._server().client
)
else:
self._api.data_processing_initialization()

@version_requires("6.0")
def release_dpf(self):
"""Clears the available Operators and Releases licenses when necessary.
Expand Down
5 changes: 5 additions & 0 deletions src/ansys/dpf/core/server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


class LicensingContextType(Enum):
none = 5
premium = 1
"""Checks if at least one license increment exists
and allows operators to block an increment."""
Expand All @@ -33,6 +34,9 @@ def __int__(self):

@staticmethod
def same_licensing_context(first, second):
if ((first == LicensingContextType.none and second != LicensingContextType.none)
or (first != LicensingContextType.none and second == LicensingContextType.none)):
return False
if int(first) == int(LicensingContextType.entry) and int(second) != int(
LicensingContextType.entry
):
Expand Down Expand Up @@ -207,6 +211,7 @@ def __ne__(self, other):
class AvailableServerContexts:
"""Defines available server contexts."""

no_context = ServerContext(LicensingContextType.none, "")
pre_defined_environment = ServerContext(0)
"""DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will
be taken"""
Expand Down
120 changes: 70 additions & 50 deletions src/ansys/dpf/core/server_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Contains the different kinds of
servers available for the factory.
"""
from __future__ import annotations
import abc
import io
import os
Expand All @@ -15,7 +16,8 @@
import traceback
from threading import Thread, Lock
from abc import ABC
from ctypes import *
import ctypes
from typing import TYPE_CHECKING, Union

import psutil

Expand All @@ -26,6 +28,9 @@
from ansys.dpf.core import server_context
from ansys.dpf.gate import load_api, data_processing_grpcapi

if TYPE_CHECKING:
from ansys.dpf.core.server_factory import DockerConfig

import logging

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -671,17 +676,17 @@ class GrpcServer(CServer):

def __init__(
self,
ansys_path=None,
ip=LOCALHOST,
port=DPF_DEFAULT_PORT,
timeout=10,
as_global=True,
load_operators=True,
launch_server=True,
docker_config=RUNNING_DOCKER,
use_pypim=True,
num_connection_tryouts=3,
context=server_context.SERVER_CONTEXT,
ansys_path: Union[str, None] = None,
ip: str = LOCALHOST,
port: str = DPF_DEFAULT_PORT,
timeout: float = 10.,
as_global: bool = True,
load_operators: bool = True,
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
num_connection_tryouts: int = 3,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
# Load DPFClientAPI
from ansys.dpf.core.misc import is_pypim_configured
Expand Down Expand Up @@ -733,11 +738,16 @@ def __init__(
self.live = True
self._create_shutdown_funcs()
self._check_first_call(num_connection_tryouts)
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
if context:
if context == core.AvailableServerContexts.no_context:
self._base_service.initialize()
self._context = context
else:
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
self.set_as_global(as_global=as_global)

def _check_first_call(self, num_connection_tryouts):
Expand Down Expand Up @@ -892,11 +902,11 @@ class InProcessServer(CServer):

def __init__(
self,
ansys_path=None,
as_global=True,
load_operators=True,
timeout=None,
context=server_context.SERVER_CONTEXT,
ansys_path: Union[str, None] = None,
as_global: bool = True,
load_operators: bool = True,
timeout: None = None,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
# Load DPFClientAPI
super().__init__(ansys_path=ansys_path, load_operators=load_operators)
Expand All @@ -914,14 +924,19 @@ def __init__(
f"Unable to locate the following file: {path}"
)
raise e
try:
self.apply_context(context)
except errors.DpfVersionNotSupported:
self._base_service.initialize_with_context(
server_context.AvailableServerContexts.premium
)
self._context = server_context.AvailableServerContexts.premium
pass
if context:
if context == core.AvailableServerContexts.no_context:
self._base_service.initialize()
self._context = context
else:
try:
self.apply_context(context)
except errors.DpfVersionNotSupported:
self._base_service.initialize_with_context(
server_context.AvailableServerContexts.premium
)
self._context = server_context.AvailableServerContexts.premium
pass
self.set_as_global(as_global=as_global)
# Update the python os.environment
if not os.name == "posix":
Expand Down Expand Up @@ -974,19 +989,20 @@ def config(self):
def get_system_path() -> str:
"""Return the current PATH environment variable value of the system."""
if not os.name == "posix":
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
windll.kernel32.GetEnvironmentVariableA.restype = c_int
ctypes.windll.kernel32.GetEnvironmentVariableA.argtypes = (
ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int
)
ctypes.windll.kernel32.GetEnvironmentVariableA.restype = ctypes.c_int
name = "PATH"
b_name = name.encode("utf-8")
size = 32767
buffer = create_string_buffer(b"", size)
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
buffer = ctypes.create_string_buffer(b"", size)
_ = ctypes.windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
return buffer.value.decode("utf-8")
else:
return sys.path



class LegacyGrpcServer(BaseServer):
"""Provides an instance of the DPF server using InProcess gRPC.
Kept for backward-compatibility with dpf servers <0.5.0.
Expand Down Expand Up @@ -1023,16 +1039,16 @@ class LegacyGrpcServer(BaseServer):

def __init__(
self,
ansys_path=None,
ip=LOCALHOST,
port=DPF_DEFAULT_PORT,
timeout=5,
as_global=True,
load_operators=True,
launch_server=True,
docker_config=RUNNING_DOCKER,
use_pypim=True,
context=server_context.SERVER_CONTEXT,
ansys_path: Union[str, None] = None,
ip: str = LOCALHOST,
port: str = DPF_DEFAULT_PORT,
timeout: float = 5.,
as_global: bool = True,
load_operators: bool = True,
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
"""Start the DPF server."""
# Use ansys.grpc.dpf
Expand Down Expand Up @@ -1097,11 +1113,15 @@ def __init__(
self._create_shutdown_funcs()

check_ansys_grpc_dpf_version(self, timeout)
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
if context:
if context == core.AvailableServerContexts.no_context:
self._context = context
else:
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
self.set_as_global(as_global=as_global)

def _create_shutdown_funcs(self):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,21 @@ def test_context_environment_variable(reset_context_environment_variable):
continue


@pytest.mark.skipif(
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0,
reason="Failures on Windows 231"
)
def test_server_without_context(remote_config_server_type):
"""Tests starting a server without a no_context given."""
server = dpf.core.start_local_server(
as_global=False,
config=remote_config_server_type,
context=dpf.core.AvailableServerContexts.no_context
)
none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type
assert server.context.licensing_context_type == none_type


@pytest.mark.order("last")
@pytest.mark.skipif(
running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
Expand Down

0 comments on commit 08d6203

Please sign in to comment.