4848 pass
4949
5050
51- def _create_geometry_channel (target : str ) -> grpc .Channel :
51+ def _create_geometry_channel (
52+ target : str ,
53+ transport_mode : str | None = None ,
54+ uds_dir : Path | str | None = None ,
55+ uds_id : str | None = None ,
56+ certs_dir : Path | str | None = None ,
57+ ) -> grpc .Channel :
5258 """Create a Geometry service gRPC channel.
5359
5460 Parameters
5561 ----------
5662 target : str
5763 Target of the channel. This is usually a string in the form of
5864 ``host:port``.
65+ transport_mode : str | None
66+ Transport mode selected, by default `None` and thus it will be selected
67+ for you based on the connection criteria. Options are: "insecure", "uds", "wnua", "mtls"
68+ uds_dir : Path | str | None
69+ Directory to use for Unix Domain Sockets (UDS) transport mode.
70+ By default `None` and thus it will use the "~/.conn" folder.
71+ uds_id : str | None
72+ Optional ID to use for the UDS socket filename.
73+ By default `None` and thus it will use "aposdas_socket.sock".
74+ Otherwise, the socket filename will be "aposdas_socket-<uds_id>.sock".
75+ certs_dir : Path | str | None
76+ Directory to use for TLS certificates.
77+ By default `None` and thus search for the "ANSYS_GRPC_CERTIFICATES" environment variable.
78+ If not found, it will use the "certs" folder assuming it is in the current working
79+ directory.
5980
6081 Returns
6182 -------
@@ -66,16 +87,38 @@ def _create_geometry_channel(target: str) -> grpc.Channel:
6687 -----
6788 Contains specific options for the Geometry service.
6889 """
69- return grpc .insecure_channel (
70- target ,
71- options = [
72- ("grpc.max_receive_message_length" , pygeom_defaults .MAX_MESSAGE_LENGTH ),
73- ("grpc.max_send_message_length" , pygeom_defaults .MAX_MESSAGE_LENGTH ),
74- ],
90+ from ansys .tools .common .cyberchannel import create_channel
91+
92+ # Split target into host and port
93+ host , port = target .split (":" )
94+
95+ # Add specific gRPC options for the Geometry service
96+ grpc_options = [
97+ ("grpc.max_receive_message_length" , pygeom_defaults .MAX_MESSAGE_LENGTH ),
98+ ("grpc.max_send_message_length" , pygeom_defaults .MAX_MESSAGE_LENGTH ),
99+ ]
100+
101+ # Create the channel accordingly
102+ return create_channel (
103+ host = host ,
104+ port = port ,
105+ transport_mode = transport_mode ,
106+ uds_service = "aposdas_socket" ,
107+ uds_dir = uds_dir ,
108+ uds_id = uds_id ,
109+ certs_dir = certs_dir ,
110+ grpc_options = grpc_options ,
75111 )
76112
77113
78- def wait_until_healthy (channel : grpc .Channel | str , timeout : float ) -> grpc .Channel :
114+ def wait_until_healthy (
115+ channel : grpc .Channel | str ,
116+ timeout : float ,
117+ transport_mode : str | None = None ,
118+ uds_dir : Path | str | None = None ,
119+ uds_id : str | None = None ,
120+ certs_dir : Path | str | None = None ,
121+ ) -> grpc .Channel :
79122 """Wait until a channel is healthy before returning.
80123
81124 Parameters
@@ -93,6 +136,21 @@ def wait_until_healthy(channel: grpc.Channel | str, timeout: float) -> grpc.Chan
93136 is made with the remaining time.
94137 * If the total elapsed time exceeds the value for the ``timeout`` parameter,
95138 a ``TimeoutError`` is raised.
139+ transport_mode : str | None
140+ Transport mode selected, by default `None` and thus it will be selected
141+ for you based on the connection criteria. Options are: "insecure", "uds", "wnua", "mtls"
142+ uds_dir : Path | str | None
143+ Directory to use for Unix Domain Sockets (UDS) transport mode.
144+ By default `None` and thus it will use the "~/.conn" folder.
145+ uds_id : str | None
146+ Optional ID to use for the UDS socket filename.
147+ By default `None` and thus it will use "aposdas_socket.sock".
148+ Otherwise, the socket filename will be "aposdas_socket-<uds_id>.sock".
149+ certs_dir : Path | str | None
150+ Directory to use for TLS certificates.
151+ By default `None` and thus search for the "ANSYS_GRPC_CERTIFICATES" environment variable.
152+ If not found, it will use the "certs" folder assuming it is in the current working
153+ directory.
96154
97155 Returns
98156 -------
@@ -115,7 +173,15 @@ def wait_until_healthy(channel: grpc.Channel | str, timeout: float) -> grpc.Chan
115173 while time .time () < t_max :
116174 try :
117175 tmp_channel = (
118- _create_geometry_channel (channel ) if channel_creation_required else channel
176+ _create_geometry_channel (
177+ channel ,
178+ transport_mode = transport_mode ,
179+ uds_dir = uds_dir ,
180+ uds_id = uds_id ,
181+ certs_dir = certs_dir ,
182+ )
183+ if channel_creation_required
184+ else channel
119185 )
120186 health_stub = health_pb2_grpc .HealthStub (tmp_channel )
121187 request = health_pb2 .HealthCheckRequest (service = "" )
@@ -179,6 +245,21 @@ class GrpcClient:
179245 proto_version : str | None, default: None
180246 Protocol version to use for communication with the server. If None, v0 is used.
181247 Available versions are "v0", "v1", etc.
248+ transport_mode : str | None
249+ Transport mode selected, by default `None` and thus it will be selected
250+ for you based on the connection criteria. Options are: "insecure", "uds", "wnua", "mtls"
251+ uds_dir : Path | str | None
252+ Directory to use for Unix Domain Sockets (UDS) transport mode.
253+ By default `None` and thus it will use the "~/.conn" folder.
254+ uds_id : str | None
255+ Optional ID to use for the UDS socket filename.
256+ By default `None` and thus it will use "aposdas_socket.sock".
257+ Otherwise, the socket filename will be "aposdas_socket-<uds_id>.sock".
258+ certs_dir : Path | str | None
259+ Directory to use for TLS certificates.
260+ By default `None` and thus search for the "ANSYS_GRPC_CERTIFICATES" environment variable.
261+ If not found, it will use the "certs" folder assuming it is in the current working
262+ directory.
182263 """
183264
184265 @check_input_types
@@ -194,6 +275,10 @@ def __init__(
194275 logging_level : int = logging .INFO ,
195276 logging_file : Path | str | None = None ,
196277 proto_version : str | None = None ,
278+ transport_mode : str | None = None ,
279+ uds_dir : Path | str | None = None ,
280+ uds_id : str | None = None ,
281+ certs_dir : Path | str | None = None ,
197282 ):
198283 """Initialize the ``GrpcClient`` object."""
199284 self ._closed = False
@@ -208,7 +293,19 @@ def __init__(
208293 self ._channel = wait_until_healthy (channel , self ._grpc_health_timeout )
209294 else :
210295 self ._target = f"{ host } :{ port } "
211- self ._channel = wait_until_healthy (self ._target , self ._grpc_health_timeout )
296+ self ._channel = wait_until_healthy (
297+ self ._target ,
298+ self ._grpc_health_timeout ,
299+ transport_mode = transport_mode ,
300+ uds_dir = uds_dir ,
301+ uds_id = uds_id ,
302+ certs_dir = certs_dir ,
303+ )
304+
305+ # HACK: If we are using UDS, the target needs to be updated to reflect
306+ # the actual socket file being used.
307+ if transport_mode == "uds" :
308+ self ._target = self ._channel ._channel .target ().decode ()
212309
213310 # Initialize the gRPC services
214311 self ._services = _GRPCServices (self ._channel , version = proto_version )
0 commit comments