Advertisement
Guest User

scratchrsc.rb

a guest
Jan 27th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.21 KB | None | 0 0
  1. require "socket"
  2. require "scanf"
  3.  
  4. class RSCWatcher
  5.     def initialize(host="127.0.0.1")
  6.         @socket = TCPSocket.open(host, 42001)
  7.         onConnect
  8.     end
  9.     def sendCommand(cmd)
  10.         i = cmd.length
  11.         @socket.write [i].pack("I").reverse + cmd
  12.     end
  13.     def socket
  14.         @socket
  15.     end
  16.     def handle_command
  17.         message = @socket.recv(@socket.recv(4).reverse.unpack("I")[0]) 
  18.         if message.include? "sensor-update"
  19.             split = message.split
  20.             a = ""
  21.             split.length.times do |i|
  22.                 unless i == 0
  23.                     if i % 2 == 1
  24.                         a = split[i]
  25.                     end
  26.                     if i % 2 == 0
  27.                         on_sensor_update a.gsub(/"/, ''), split[i].gsub(/"/, '')
  28.                     end
  29.                 end
  30.             end
  31.         end
  32.         if message.include? "broadcast"
  33.             split = message.split
  34.             if split.length == 2
  35.                 on_broadcast split[1].gsub(/"/, '')
  36.             end
  37.            
  38.         end
  39.     end
  40.     def __on_broadcast(name)
  41.         method = "broadcast_#{name}"
  42.         if self.respond_to? method
  43.             self.send method
  44.         else
  45.             on_broadcast(name)
  46.         end
  47.     end
  48.     def on_broadcast(name)
  49.     end
  50.     def on_sensor_update(name, value)
  51.     end
  52.     def broadcast(name)
  53.         sendCommand("broadcast \"#{name}\"")
  54.     end
  55.     def sensor_update(name,value)
  56.         sendCommand("sensor=update \"#{name}\" \"#{value}\"")
  57.     end
  58.  
  59.     private
  60.     def onConnect
  61.     end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement