ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf ·...

19
Ruby入門 高宮 安仁 @yasuhito

Upload: others

Post on 25-Oct-2019

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

Ruby入門

高宮 安仁 @yasuhito

Page 2: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

品詞+文法

Page 3: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

登場する品詞• キーワード

• 名詞

• 動詞

Page 4: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

キーワード(予約語)alias and BEGIN begin break case class def defined do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield

Page 5: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

class HelloTrema < Trema::Controller

def start(_args)

logger.info ‘Trema started.’

end

end

• キーワードは構造を決める

Page 6: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

class HelloTrema < Trema::Controller

def start(_args)

logger.info ‘Trema started.’

end

end

• クラス定義 (class … end) •子クラス < 親クラス名

Page 7: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

固有名詞=定数Tsutenkaku

Rainbow Bridge

HelloTrema

•大文字で始まる

•内容を変更できない

Page 8: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

• 定数は大文字で始まる

class HelloTrema < Trema::Controller

def start(_args)

logger.info ‘Trema started.’

end

end

Page 9: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

• boy.run

• girl.write ‘abc’

• logger.info

メソッド=動詞

Page 10: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

• logger = ロガー • info = info ログの出力

class HelloTrema < Trema::Controller

def start(_args)

logger.info ‘Trema started.’

end

end

Page 11: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

• def … end = メソッド定義 • start はハンドラメソッド

class HelloTrema < Trema::Controller

def start(_args)

logger.info ‘Trema started.’

end

end

Page 12: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

コントローラ

メッセージ

タイマー

• ハンドラ1• ハンドラ2• ハンドラ3

コントローラの外界からのメッセージやイベントに応じ、コントローラの対応するハンドラメソッドが呼ばれる

シグナル

Page 13: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

コントローラ

start() コントローラが起動した

Page 14: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

コントローラ

packet_in()

Packet In

フローに無いパケットを送った

Page 15: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

コントローラ

flow_removed()

Flow Removed フローがexpire した

Page 16: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

start switch_ready switch_disconnected packet_in flow_removed port_status openflow_error features_reply stats_reply barrier_reply get_config_reply queue_get_config_reply vendor

ハンドラ一覧

Page 17: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

• スイッチを停止したら“Bye 0xabc”と表示せよ

課題

スイッチのDPID

Page 18: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

class FooBar < Trema::Controller

def switch_ready(dpid)

# ...

end

def switch_disconnected(dpid)

# ...

end

end

• 接続を捕捉

•切断を捕捉

Page 19: Ruby入門 - handai-trema.github.iohandai-trema.github.io/deck/week1/ruby_intro.pdf · キーワード(予約語) alias and BEGIN begin break case class def defined do else elsif

• trema stop/start コマンドで仮想スイッチを操作

vswitch { dpid 0x1 }

vswitch { dpid 0x2 }

vswitch { dpid 0x3 }

% trema run foobar.rb -c network.conf

% trema stop 0x1 # 仮想スイッチを殺す% trema start 0x1 # 仮想スイッチを復活