<s> from calvin . actor . actor import Actor , ActionResult , manage , condition , guard <EOL> from calvin . runtime . north . calvin_token import EOSToken <EOL> from calvin . utilities . calvinlogger import get_logger <EOL> _log = get_logger ( __name__ ) <EOL> class SocketClient ( Actor ) : <EOL> """<STR_LIT>""" <EOL> @ manage ( [ '<STR_LIT:address>' , '<STR_LIT:port>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def init ( self , address = None , port = None , protocol = '<STR_LIT>' , type_ = "<STR_LIT>" , delimiter = '<STR_LIT:\r\n>' ) : <EOL> self . address = address <EOL> self . port = port <EOL> self . protocol = protocol <EOL> self . type_ = type_ <EOL> self . delimiter = delimiter <EOL> self . EOST_token_received = False <EOL> self . cc = None <EOL> self . use ( '<STR_LIT>' , shorthand = '<STR_LIT>' ) <EOL> if address is not None : <EOL> self . connect ( ) <EOL> def connect ( self ) : <EOL> self . cc = self [ '<STR_LIT>' ] . connect ( self . address , self . port , self . protocol , self . type_ , self . delimiter ) <EOL> def will_migrate ( self ) : <EOL> self . cc . disconnect ( ) <EOL> def did_migrate ( self ) : <EOL> self . connect ( ) <EOL> @ condition ( action_input = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self , token : self . cc and self . cc . is_connected ( ) ) <EOL> def send ( self , token ) : <EOL> self . cc . send ( token ) <EOL> return ActionResult ( production = ( ) ) <EOL> @ condition ( action_output = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . cc and self . cc . is_connected ( ) and self . cc . have_data ( ) ) <EOL> def receive ( self ) : <EOL> data = self . cc . get_data ( ) <EOL> return ActionResult ( production = ( data , ) ) <EOL> @ condition ( action_input = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self , token : True ) <EOL> def receive_control ( self , token ) : <EOL> if token [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if not self . cc : <EOL> self . address = token [ <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> self . port = token [ <NUM_LIT:0> ] [ '<STR_LIT:port>' ] <EOL> self . connect ( ) <EOL> if token [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if self . cc : <EOL> self . cc . disconnect ( ) <EOL> self . cc = None <EOL> return ActionResult ( production = ( ) ) <EOL> @ condition ( action_output = [ '<STR_LIT>' ] ) <EOL> @ guard ( lambda self : self . cc and self . cc . have_control ( ) ) <EOL> def send_control ( self ) : <EOL> data = self . cc . get_control ( ) <EOL> return ActionResult ( production = ( data , ) ) <EOL> def exception_handler ( self , action , args , context ) : <EOL> """<STR_LIT>""" <EOL> self . EOST_token_received = True <EOL> return ActionResult ( production = ( ) ) <EOL> action_priority = ( receive_control , receive , send , send_control ) <EOL> requires = [ '<STR_LIT>' ] </s>
