github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/sandboxes/shared/greeter/greeter.py (about)

     1  # Copyright 2023 Google Inc.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     https://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  import binascii
    15  import logging
    16  
    17  from absl import app
    18  from absl import flags
    19  from fleetspeak.server_connector.connector import InsecureGRPCServiceClient
    20  from fleetspeak.src.common.proto.fleetspeak.common_pb2 import Message
    21  from google.protobuf.wrappers_pb2 import StringValue
    22  
    23  
    24  FLAGS = flags.FLAGS
    25  
    26  flags.DEFINE_string(
    27      name="client_id",
    28      default="",
    29      help="An id of the client to send the messages to.",
    30  )
    31  
    32  
    33  def listener(message, context):
    34    del context  # Unused
    35  
    36    data = StringValue()
    37    message.data.Unpack(data)
    38    logging.info(f"RESPONSE: {data.value}")
    39  
    40  
    41  def main(argv=None):
    42    del argv  # Unused.
    43  
    44    service_client = InsecureGRPCServiceClient("greeter")
    45    service_client.Listen(listener)
    46  
    47    while True:
    48      data = StringValue()
    49      data.value = input("Enter your name: ")
    50  
    51      request = Message()
    52      request.destination.client_id = binascii.unhexlify(FLAGS.client_id)
    53      request.destination.service_name = "hello"
    54      request.data.Pack(data)
    55  
    56      service_client.Send(request)
    57  
    58  
    59  if __name__ == "__main__":
    60    app.run(main)