<s> """<STR_LIT>""" <EOL> import eventlet <EOL> from oslo_config import cfg <EOL> import oslo_messaging as messaging <EOL> from oslo_serialization import jsonutils <EOL> import solum . common . context <EOL> from solum import objects <EOL> eventlet . monkey_patch ( ) <EOL> TRANSPORT_ALIASES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class JsonPayloadSerializer ( messaging . NoOpSerializer ) : <EOL> @ staticmethod <EOL> def serialize_entity ( context , entity ) : <EOL> return jsonutils . to_primitive ( entity , convert_instances = True ) <EOL> class RequestContextSerializer ( messaging . Serializer ) : <EOL> def __init__ ( self , base = None ) : <EOL> self . _base = base or messaging . NoOpSerializer ( ) <EOL> def serialize_entity ( self , context , entity ) : <EOL> if not self . _base : <EOL> return entity <EOL> return self . _base . serialize_entity ( context , entity ) <EOL> def deserialize_entity ( self , context , entity ) : <EOL> if not self . _base : <EOL> return entity <EOL> return self . _base . deserialize_entity ( context , entity ) <EOL> def serialize_context ( self , context ) : <EOL> return context . to_dict ( ) <EOL> def deserialize_context ( self , context ) : <EOL> return solum . common . context . RequestContext . from_dict ( context ) <EOL> class Service ( object ) : <EOL> _server = None <EOL> def __init__ ( self , topic , server , handlers ) : <EOL> serializer = RequestContextSerializer ( JsonPayloadSerializer ( ) ) <EOL> transport = messaging . get_transport ( cfg . CONF , <EOL> aliases = TRANSPORT_ALIASES ) <EOL> target = messaging . Target ( topic = topic , server = server ) <EOL> self . _server = messaging . get_rpc_server ( transport , target , handlers , <EOL> serializer = serializer ) <EOL> def serve ( self ) : <EOL> objects . load ( ) <EOL> self . _server . start ( ) <EOL> self . _server . wait ( ) <EOL> class API ( object ) : <EOL> def __init__ ( self , transport = None , context = None , topic = None ) : <EOL> serializer = RequestContextSerializer ( JsonPayloadSerializer ( ) ) <EOL> if transport is None : <EOL> transport = messaging . get_transport ( cfg . CONF , <EOL> aliases = TRANSPORT_ALIASES ) <EOL> self . _context = context <EOL> if topic is None : <EOL> topic = '<STR_LIT>' <EOL> target = messaging . Target ( topic = topic ) <EOL> self . _client = messaging . RPCClient ( transport , target , <EOL> serializer = serializer ) <EOL> def _call ( self , method , * args , ** kwargs ) : <EOL> return self . _client . call ( self . _context , method , * args , ** kwargs ) <EOL> def _cast ( self , method , * args , ** kwargs ) : <EOL> self . _client . cast ( self . _context , method , * args , ** kwargs ) <EOL> def echo ( self , message ) : <EOL> self . _cast ( '<STR_LIT>' , message = message ) </s>
