code/superant.com.rwdtinkerbackwindow/network.rb in rubyslippers-1.01 vs code/superant.com.rwdtinkerbackwindow/network.rb in rubyslippers-1.02

- old
+ new

@@ -1,87 +1,87 @@ -#!ruby +#network require 'socket' # Network stuff require 'timeout' # Timeout functions def network_start - if RUBY_PLATFORM != "i386-mswin32" - processone = fork { - network_demon } - Process.detach(processone) + if RUBY_PLATFORM != "i386-mswin32" + processone = fork { + network_demon } + Process.detach(processone) - else - puts "fork not tried winXP?" - end - @viewremotecommandresult = "starting controller on next available port" -end + else + puts "fork not tried winXP?" + end + @viewremotecommandresult = "starting controller on next available port" + end - def network_demon - # Find configuration variables - host = "127.0.0.1" - port = $rwdcontrolports[0].to_i - -openportfound = false -until openportfound - - # Create a socket to listen on and bind it to the host and port - begin - @socket = UDPSocket::new() - @socket.bind(host, port) - # Rescue the "Address in use" error - openportfound = true - - rescue Errno::EADDRINUSE - puts "controller startup: Port #{port} on host #{host} is already in use." - port=port+1 - # Rescue the "Address not available' error - rescue Errno::EADDRNOTAVAIL - puts "Network plugin: Address #{host} is not available to bind." - port=port+1 - # Rescue "permission denied errors - rescue Errno::EACCES - puts "controller startup: Access denied when binding interface addresses. Did you try to bind a port under 1024 as a regular user?" - port=port+1 - # Rescue all other errors - rescue - puts "Network plugin: An error occured." - port = port+1 - # Rescue all other errors - end - - end - + def network_demon + # Find configuration variables + host = "127.0.0.1" + port = $rwdcontrolports[0].to_i - # Get all acceptable hosts - hosts = ["localhost","vaio-sag"] - # Do some nice errorchecking - if(hosts.type != Array) - puts "Network plugin: No hosts found in networkConfig/acceptHosts. Shutting down plugin." - return -1 # Return the errorcode for plugin failure - end + openportfound = false + until openportfound - # Main loop - while(true) - # Get data and client data from the client - data, info = @socket.recvfrom(256) + # Create a socket to listen on and bind it to the host and port + begin + @socket = UDPSocket::new() + @socket.bind(host, port) + # Rescue the "Address in use" error + openportfound = true - # Is this an acceptable IP address? - if(hosts.include?(info[2]) || hosts.include?(info[3])) - command, parameter = data.split(';') - - # Command actions - command.downcase!() - case command - - when 'stop' - @socket.send("hello world",0,info[3],info[1].to_i) - - when 'query' - @socket.send("#{$rwdapplicationidentity} on port #{$port}",0,info[3],info[1].to_i) - - - end # case command + rescue Errno::EADDRINUSE + puts "controller startup: Port #{port} on host #{host} is already in use." + port=port+1 + # Rescue the "Address not available' error + rescue Errno::EADDRNOTAVAIL + puts "Network plugin: Address #{host} is not available to bind." + port=port+1 + # Rescue "permission denied errors + rescue Errno::EACCES + puts "controller startup: Access denied when binding interface addresses. Did you try to bind a port under 1024 as a regular user?" + port=port+1 + # Rescue all other errors + rescue + puts "Network plugin: An error occured." + port = port+1 + # Rescue all other errors + end - end # Access control if - end # while(true) + end - end # Network::start() + + # Get all acceptable hosts + hosts = ["localhost","vaio-sag"] + # Do some nice errorchecking + if(hosts.type != Array) + puts "Network plugin: No hosts found in networkConfig/acceptHosts. Shutting down plugin." + return -1 # Return the errorcode for plugin failure + end + + # Main loop + while(true) + # Get data and client data from the client + data, info = @socket.recvfrom(256) + + # Is this an acceptable IP address? + if(hosts.include?(info[2]) || hosts.include?(info[3])) + command, parameter = data.split(';') + + # Command actions + command.downcase!() + case command + + when 'stop' + @socket.send("hello world",0,info[3],info[1].to_i) + + when 'query' + @socket.send("#{$rwdapplicationidentity} on port #{$port}",0,info[3],info[1].to_i) + + + end # case command + + end # Access control if + end # while(true) + + end # Network::start()