Last active
September 10, 2016 15:34
-
-
Save marceloboeira/0deaaf800a126aac42a5bb3c868838e7 to your computer and use it in GitHub Desktop.
This file contains 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" | |
class Server | |
def self.start | |
server = TCPServer.new("localhost", 5000) | |
server.recv_buffer_size = 4096 | |
data = Hash(String, String).new | |
loop do | |
socket = server.accept | |
if socket | |
spawn do | |
loop do | |
if request = socket.gets | |
request = request.split(" ").map{|item| item.strip } | |
command = request[0] | |
key = request[1] | |
if command == "set" | |
value = request[2] | |
data[key] = value | |
socket.puts(value) | |
elsif command == "get" | |
value = data[key] | |
socket.puts(value) | |
else | |
socket.puts("error: #{command} is not a valid command") | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment