diff options
-rw-r--r-- | 01-hub2.rb | 28 | ||||
-rw-r--r-- | forward-please.rb | 67 | ||||
-rw-r--r-- | frame.rb | 14 | ||||
-rw-r--r-- | level.rb | 23 |
4 files changed, 132 insertions, 0 deletions
diff --git a/01-hub2.rb b/01-hub2.rb new file mode 100644 index 0000000..4ed0f6b --- /dev/null +++ b/01-hub2.rb @@ -0,0 +1,28 @@ +require './level' +require './frame' + +class L1_Hub2 < Level + interfaces %w(1 2) + description <<~EOD + You are a two-port hub. Your job is to forward frames from one interface to the other. + EOD + + def self.target(frame) + case frame.iface + when 1 + [frame.to(2)] + when 2 + [frame.to(1)] + end + end + + def self.generate + frames = {} + @@interfaces.each do |iface| + next if rand < 0.4 + frames[@@count.to_s] = Frame.new(iface, "Frame #{@@count}") + @@count += 1 + end + frames + end +end diff --git a/forward-please.rb b/forward-please.rb new file mode 100644 index 0000000..fc36a6e --- /dev/null +++ b/forward-please.rb @@ -0,0 +1,67 @@ +require './01-hub2.rb' + +$level = L1_Hub2 + +puts $level.description + +def help + puts <<~EOF + Commands: + f {frame} {iface}: forward a frame to iface + h: this help + c: click on + d: show the level description + i: show the interfaces + a: show the click frames and actions + q: quit + EOF +end + +$instructions = [] + +def forward(frame, iface) + $instructions << [frame, $clickframes[frame].to(iface)] +end + +def clickactions + puts "Frames:" + $clickframes.each do |i, frame| + puts " #{i}: Interface #{frame.iface} -> #{frame.description}" + end + + l = 1 + puts "Actions:" + $instructions.each do |instr| + puts " #{l}: Forward frame #{instr[0]} to interface #{instr[1].iface}" + end +end + +def click +end + +def handle(cmd) + case cmd[0] + when ?h + help + when ?f + forward(*cmd.split[1..]) + when ?d + puts $level.description + when ?i + puts $level.interfaces + when ?a + clickactions + when ?c + click + when :q + exit 0 + end +end + +$clickframes = $level.generate +pp $clickframes + +loop do + print ">: " + handle(gets.strip) +end diff --git a/frame.rb b/frame.rb new file mode 100644 index 0000000..70216c8 --- /dev/null +++ b/frame.rb @@ -0,0 +1,14 @@ +class Frame + def initialize(iface, desc) + @iface = iface + @description = desc + end + + attr_accessor :iface, :description + + def to(iface) + t = dup + t.iface = iface + t + end +end diff --git a/level.rb b/level.rb new file mode 100644 index 0000000..21ebc61 --- /dev/null +++ b/level.rb @@ -0,0 +1,23 @@ +class Level + @@interfaces = [] + @@description = "" + @@count = 0 + + attr_accessor :description + + def self.interfaces(arr = nil) + if arr.nil? + (["Interfaces:"] + @@interfaces).join("\n ") + else + @@interfaces = arr + end + end + + def self.description(desc = nil) + if desc.nil? + @@description + else + @@description = desc + end + end +end |