Created
May 2, 2010 19:42
-
-
Save Mon-Ouie/387383 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module IRB | |
# Simple hack, showing the evaluated object | |
# (we don't want to send the whole object through | |
# our socket) | |
class Evaluation | |
def initialize(evaluation) | |
@eval = evaluation | |
end | |
def inspect | |
@eval | |
end | |
end | |
class WorkSpace | |
def evaluate(context, statements, file = __FILE__, line = __LINE__) | |
unless @@remote_ip # Local evaluation | |
return eval(statements, @binding, file, line) | |
else # Remote evaluation | |
unless @socket # Create a socket if needed | |
@socket = TCPSocket.new(@@remote_ip, 9000) | |
end | |
begin | |
@socket.puts statements | |
@socket.puts "\001EVAL\001" # Ending message (invalid ruby code ;) | |
rescue | |
@@remote_ip = nil | |
return IRB::Evaluation.new("Disconnected from server") | |
end | |
evaluation = "" | |
loop do | |
line = @socket.gets | |
if line == "\001DONE\001\n" | |
return IRB::Evaluation.new(evaluation.chomp) | |
else | |
evaluation << line | |
end | |
end | |
end | |
end | |
def self.remote_ip=(val) | |
@@remote_ip = val | |
end | |
@@remote_ip = nil | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'joyau' | |
Joyau.init(:gfx, :lib, :wlan) | |
Joyau::Wlan.connect(2, 60) | |
Joyau::Debug.init | |
Joyau::Debug.puts "Starting server" | |
server = TCPServer.new("0.0.0.0", 9000) | |
Joyau::Debug.puts "Started server at #{Joyau::Wlan.ip}" | |
Joyau::Debug.puts "Waiting for client." | |
socket = server.accept | |
Joyau::Debug.puts "Client found!" | |
def clear_bind | |
binding | |
end | |
bind = clear_bind | |
command = "" | |
loop do | |
line = socket.gets | |
next unless line | |
if line == "\001EVAL\001\n" | |
begin | |
socket.puts(eval(command, bind).inspect + "\n\001DONE\001") | |
rescue Exception | |
socket.puts("#{$!.class}: #{$!.message}\n\001DONE\001") | |
end | |
command = "" | |
else | |
command << line | |
end | |
end | |
Joyau::Wlan.disconnect | |
Joyau.stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment