![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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/1.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/2.jpg)
品詞+文法
![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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/3.jpg)
登場する品詞• キーワード
• 名詞
• 動詞
![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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/4.jpg)
キーワード(予約語)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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/7.jpg)
固有名詞=定数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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/8.jpg)
• 定数は大文字で始まる
class HelloTrema < Trema::Controller
def start(_args)
logger.info ‘Trema started.’
end
end
![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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/10.jpg)
• 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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/11.jpg)
• 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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/12.jpg)
コントローラ
メッセージ
タイマー
• ハンドラ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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/13.jpg)
コントローラ
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/14.jpg)
コントローラ
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/15.jpg)
コントローラ
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/17.jpg)
• スイッチを停止したら“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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/18.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022040423/5e1921ed82c2cd356067d99c/html5/thumbnails/19.jpg)
• 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 # 仮想スイッチを復活