require "al_worker_tcp" require "al_worker_timer" class Worker1 < AlWorker def initialize2() set_state( "EN" ) @count = 0 @tcp = Tcp.new() @tcp.run( self ) @timer = Timer.periodic( 10 ) @timer.run() { change_state() } end def tcp_say( sock, param ) trigger_event( "SAY", sock ) # イベント "SAY" を発生させる。 return true end def change_state() # 10秒ごとに、ステートを変更する。 case @state when "EN"; set_state( "JP" ) when "JP"; set_state( "DE" ) when "DE"; set_state( "EN" ) end end # ステートが"EN"の時に、"SAY"イベントが発生した時の処理。 def state_EN_event_SAY( sock ) a = ["one","two","three","four","five","six","seven","eight","nine","ten"] sock.puts a[@count] @count = 0 if (@count += 1) > 9 end # ステートが"JP"の時に、"SAY"イベントが発生した時の処理。 def state_JP_event_SAY( sock ) a = ["ichi","ni","san","shi","go","roku","shichi","hachi","kyu","jyu"] sock.puts a[@count] @count = 0 if (@count += 1) > 9 end # ステートが"DE"の時に、"SAY"イベントが発生した時の処理。 def state_DE_event_SAY( sock ) a = ["eins","zwei","drei","vier","fünf","sechs","sieben","acht","neun","zehn"] sock.puts a[@count] @count = 0 if (@count += 1) > 9 end end server = Worker1.new server.daemon