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