xref: /aosp_15_r20/external/autotest/server/hosts/tls_client/connection.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2021 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Abstract Client for Autotest side communications to the TLS Server."""
6
7import grpc
8
9import common
10
11from autotest_lib.server.hosts.tls_client import autotest_common_pb2_grpc
12
13TLS_PORT = 7152
14TLS_IP = '10.254.254.254'
15
16
17class TLSConnection(object):
18    """The client side connection to Common-TLS service running in a drone."""
19
20    def __init__(self):
21        """Configure the grpc channel."""
22        self.channel = grpc.insecure_channel('{}:{}'.format(TLS_IP, TLS_PORT))
23        self.stub = autotest_common_pb2_grpc.CommonStub(self.channel)
24        self.alive = True
25
26    def __enter__(self):
27        return self
28
29    def __exit__(self, *exc):
30        self.close()
31
32    def close(self):
33        """Close the grpc channel."""
34        self.channel.close()
35        self.alive = False
36