From 7673c19b81066f86baf0c4948326707e9173c8e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20S=C3=A1nchez?= Date: Sun, 24 Apr 2011 17:28:40 +0200 Subject: [PATCH] Added concurrent server in ruby 0.1 --- ServidorConcurrente/servidor.rb | 45 +++++++++++++++++++++++++++++++++ servidor.rb | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 ServidorConcurrente/servidor.rb create mode 100644 servidor.rb diff --git a/ServidorConcurrente/servidor.rb b/ServidorConcurrente/servidor.rb new file mode 100644 index 0000000..9cfade3 --- /dev/null +++ b/ServidorConcurrente/servidor.rb @@ -0,0 +1,45 @@ +require 'socket' + +numCon = 0 +maxCon = 10 + +def respond(socket, code, body) # Genera prespuestas + socket.puts "HTTP/1.0 #{ code }" + socket.puts "Content-type: text/html" + socket.puts "Content-length: #{ body.length }" + socket.puts + socket.puts body + socket.close +end + +server = TCPServer.open(8080) +puts "web server created at port 8080" +loop do + if(numCon < maxCon) + sock = server.accept # Acepta conexión del cliente + t = Thread.new(sock) do |socket| + numCon=numCon+1 + request = socket.gets # Lee la primera línea + o = socket.gets until ( o != "") # cabecera acaba con lí­nea vacía + + unless request =~ /^GET/ # Si el comando no esta soportado + respond socket, "400 Bad Request", "

Comando desconocido

" + next # Acaba el ciclo + end + request =~ /^GET \/(.*) .*/ # GET /(fichero) HTTP/1.x + path = $1 + if path == "" || path =~ /\/$/ then path += "index.html" end # Inserta index.html + if File.exists?(path) + file = open(path) # Lee fichero y prepara respuesta + puts "OK #{ path }" + respond socket, "200 OK", file.read + else + puts "NotFound #{ path }" # Si el fichero no existe + respond socket, "404 Not Found", "

Recurso no encontrado

" + end + numCon=numCon-1; + end + end +end + + diff --git a/servidor.rb b/servidor.rb new file mode 100644 index 0000000..9cfade3 --- /dev/null +++ b/servidor.rb @@ -0,0 +1,45 @@ +require 'socket' + +numCon = 0 +maxCon = 10 + +def respond(socket, code, body) # Genera prespuestas + socket.puts "HTTP/1.0 #{ code }" + socket.puts "Content-type: text/html" + socket.puts "Content-length: #{ body.length }" + socket.puts + socket.puts body + socket.close +end + +server = TCPServer.open(8080) +puts "web server created at port 8080" +loop do + if(numCon < maxCon) + sock = server.accept # Acepta conexión del cliente + t = Thread.new(sock) do |socket| + numCon=numCon+1 + request = socket.gets # Lee la primera línea + o = socket.gets until ( o != "") # cabecera acaba con lí­nea vacía + + unless request =~ /^GET/ # Si el comando no esta soportado + respond socket, "400 Bad Request", "

Comando desconocido

" + next # Acaba el ciclo + end + request =~ /^GET \/(.*) .*/ # GET /(fichero) HTTP/1.x + path = $1 + if path == "" || path =~ /\/$/ then path += "index.html" end # Inserta index.html + if File.exists?(path) + file = open(path) # Lee fichero y prepara respuesta + puts "OK #{ path }" + respond socket, "200 OK", file.read + else + puts "NotFound #{ path }" # Si el fichero no existe + respond socket, "404 Not Found", "

Recurso no encontrado

" + end + numCon=numCon-1; + end + end +end + +