<s> from calvin . actor . actor import Actor , ActionResult , manage , condition , guard <EOL> from calvin . utilities . calvinlogger import get_logger <EOL> _log = get_logger ( __name__ ) <EOL> class UDPListener ( Actor ) : <EOL> """<STR_LIT>""" <EOL> @ manage ( [ '<STR_LIT:host>' , '<STR_LIT:port>' ] ) <EOL> def init ( self ) : <EOL> self . host = None <EOL> self . port = None <EOL> self . listener = None <EOL> self . setup ( ) <EOL> def listen ( self ) : <EOL> self . listener = self [ '<STR_LIT>' ] . start ( self . host , self . port , "<STR_LIT>" ) <EOL> def did_migrate ( self ) : <EOL> self . setup ( ) <EOL> if self . port is not None : <EOL> self . listen ( ) <EOL> def setup ( self ) : <EOL> self . use ( '<STR_LIT>' , shorthand = '<STR_LIT>' ) <EOL> self . use ( '<STR_LIT>' , shorthand = '<STR_LIT>' ) <EOL> @ condition ( action_output = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . listener and self . listener . have_data ( ) ) <EOL> def receive ( self ) : <EOL> data = self . listener . data_get ( ) <EOL> return ActionResult ( production = ( data , ) ) <EOL> URI_REGEXP = r'<STR_LIT>' <EOL> def parse_uri ( self , uri ) : <EOL> status = False <EOL> try : <EOL> parsed_uri = self [ '<STR_LIT>' ] . findall ( self . URI_REGEXP , uri ) [ <NUM_LIT:0> ] <EOL> protocol = parsed_uri [ <NUM_LIT:0> ] <EOL> if protocol != '<STR_LIT>' : <EOL> _log . warn ( "<STR_LIT>" % ( protocol , ) ) <EOL> self . host = parsed_uri [ <NUM_LIT:1> ] <EOL> self . port = int ( parsed_uri [ <NUM_LIT:2> ] ) <EOL> status = True <EOL> except : <EOL> _log . warn ( "<STR_LIT>" % ( uri , ) ) <EOL> self . host = None <EOL> self . port = None <EOL> return status <EOL> @ condition ( action_input = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self , control : control . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' and not self . listener ) <EOL> def new_port ( self , control ) : <EOL> if self . parse_uri ( control . get ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . listen ( ) <EOL> return ActionResult ( ) <EOL> @ condition ( action_input = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self , control : control . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' and self . listener ) <EOL> def close_port ( self , control ) : <EOL> self . listener . stop ( ) <EOL> del self . listener <EOL> self . listener = None <EOL> return ActionResult ( production = ( ) ) <EOL> action_priority = ( new_port , close_port , receive ) <EOL> requires = [ '<STR_LIT>' , '<STR_LIT>' ] </s>
