|  | 
| 15 | 15 |     SetValueRequest, | 
| 16 | 16 | ) | 
| 17 | 17 | from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub | 
| 18 |  | -from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient | 
|  | 18 | +from ni_measurement_plugin_sdk_service.discovery import DiscoveryClient, ServiceLocation | 
| 19 | 19 | from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool | 
| 20 | 20 | from typing_extensions import ParamSpec | 
| 21 | 21 | 
 | 
| @@ -57,8 +57,14 @@ def __init__( | 
| 57 | 57 |         self._discovery_client = discovery_client | 
| 58 | 58 |         self._grpc_channel_pool = grpc_channel_pool | 
| 59 | 59 |         self._grpc_channel = grpc_channel | 
|  | 60 | +        self._proxy_location: ServiceLocation | None = None | 
| 60 | 61 |         self._stub: PythonPanelServiceStub | None = None | 
| 61 | 62 | 
 | 
|  | 63 | +    @property | 
|  | 64 | +    def proxy_location(self) -> ServiceLocation: | 
|  | 65 | +        """Return the ServiceLocation for the panel's proxy.""" | 
|  | 66 | +        return self._get_proxy_location() | 
|  | 67 | + | 
| 62 | 68 |     def start_panel(self, panel_id: str, panel_script_path: str, python_path: str) -> str: | 
| 63 | 69 |         """Start the panel. | 
| 64 | 70 | 
 | 
| @@ -134,29 +140,61 @@ def get_value(self, panel_id: str, value_id: str) -> tuple[bool, object]: | 
| 134 | 140 |         else: | 
| 135 | 141 |             return False, None | 
| 136 | 142 | 
 | 
|  | 143 | +    def _get_proxy_location(self) -> ServiceLocation: | 
|  | 144 | +        if self._proxy_location is None: | 
|  | 145 | +            self._proxy_location = self._resolve_discovery_service() | 
|  | 146 | +        return self._proxy_location | 
|  | 147 | + | 
| 137 | 148 |     def _get_stub(self) -> PythonPanelServiceStub: | 
| 138 | 149 |         if self._stub is None: | 
| 139 | 150 |             if self._grpc_channel is not None: | 
| 140 | 151 |                 self._stub = PythonPanelServiceStub(self._grpc_channel) | 
| 141 | 152 |             else: | 
| 142 |  | -                with self._initialization_lock: | 
| 143 |  | -                    if self._grpc_channel_pool is None: | 
| 144 |  | -                        _logger.debug("Creating unshared GrpcChannelPool.") | 
| 145 |  | -                        self._grpc_channel_pool = GrpcChannelPool() | 
| 146 |  | -                    if self._discovery_client is None: | 
| 147 |  | -                        _logger.debug("Creating unshared DiscoveryClient.") | 
| 148 |  | -                        self._discovery_client = DiscoveryClient( | 
| 149 |  | -                            grpc_channel_pool=self._grpc_channel_pool | 
| 150 |  | -                        ) | 
| 151 |  | - | 
| 152 |  | -                    service_location = self._discovery_client.resolve_service( | 
| 153 |  | -                        provided_interface=self._provided_interface, | 
| 154 |  | -                        service_class=self._service_class, | 
| 155 |  | -                    ) | 
| 156 |  | -                    channel = self._grpc_channel_pool.get_channel(service_location.insecure_address) | 
| 157 |  | -                    self._stub = PythonPanelServiceStub(channel) | 
|  | 153 | +                channel = self._get_panel_service_channel() | 
|  | 154 | +                self._stub = PythonPanelServiceStub(channel) | 
| 158 | 155 |         return self._stub | 
| 159 | 156 | 
 | 
|  | 157 | +    def _get_grpc_channel_pool(self) -> GrpcChannelPool: | 
|  | 158 | +        if self._grpc_channel_pool is None: | 
|  | 159 | +            _logger.debug("Creating unshared GrpcChannelPool.") | 
|  | 160 | +            self._grpc_channel_pool = GrpcChannelPool() | 
|  | 161 | +        return self._grpc_channel_pool | 
|  | 162 | + | 
|  | 163 | +    def _get_discovery_client(self, channel_pool: GrpcChannelPool) -> DiscoveryClient: | 
|  | 164 | +        if self._discovery_client is None: | 
|  | 165 | +            _logger.debug("Creating unshared DiscoveryClient.") | 
|  | 166 | +            self._discovery_client = DiscoveryClient(grpc_channel_pool=channel_pool) | 
|  | 167 | +        return self._discovery_client | 
|  | 168 | + | 
|  | 169 | +    def _resolve_service( | 
|  | 170 | +        self, discovery_client: DiscoveryClient, provided_interface: str, service_class: str = "" | 
|  | 171 | +    ) -> ServiceLocation: | 
|  | 172 | +        _logger.debug("Resolving '%s'.", provided_interface) | 
|  | 173 | +        service_location = discovery_client.resolve_service( | 
|  | 174 | +            provided_interface=provided_interface, | 
|  | 175 | +            service_class=service_class, | 
|  | 176 | +        ) | 
|  | 177 | +        return service_location | 
|  | 178 | + | 
|  | 179 | +    def _get_panel_service_channel(self) -> grpc.Channel: | 
|  | 180 | +        with self._initialization_lock: | 
|  | 181 | +            grpc_channel_pool = self._get_grpc_channel_pool() | 
|  | 182 | +            discovery_client = self._get_discovery_client(grpc_channel_pool) | 
|  | 183 | +            panel_location = self._resolve_service( | 
|  | 184 | +                discovery_client, self._provided_interface, self._service_class | 
|  | 185 | +            ) | 
|  | 186 | +            channel = grpc_channel_pool.get_channel(panel_location.insecure_address) | 
|  | 187 | +            return channel | 
|  | 188 | + | 
|  | 189 | +    def _resolve_discovery_service(self) -> ServiceLocation: | 
|  | 190 | +        with self._initialization_lock: | 
|  | 191 | +            grpc_channel_pool = self._get_grpc_channel_pool() | 
|  | 192 | +            discovery_client = self._get_discovery_client(grpc_channel_pool) | 
|  | 193 | +            discovery_location = self._resolve_service( | 
|  | 194 | +                discovery_client, provided_interface="ni.http1.proxy" | 
|  | 195 | +            ) | 
|  | 196 | +            return discovery_location | 
|  | 197 | + | 
| 160 | 198 |     def _invoke_with_retry( | 
| 161 | 199 |         self, method: Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs | 
| 162 | 200 |     ) -> _T: | 
|  | 
0 commit comments