Skip to content

Instantly share code, notes, and snippets.

@psyomn
Created April 30, 2012 21:39
Show Gist options
  • Save psyomn/2562884 to your computer and use it in GitHub Desktop.
Save psyomn/2562884 to your computer and use it in GitHub Desktop.
Just a simple udp server that can be used as a registry.
require 'socket'
# This is the main registry.
# When an individual node is started, it first looks
# for the registry and provides its information so that
# other nodes can query this entity for the location of
# said nodes.
class Registry
# Default initializement w/ setting the port
# to 20000.
def initialize
@udp_listener_handle = UDPSocket.new
@default_port = 20000
@default_host = 'localhost'
@registry = Hash.new
end
# Listen at standard port number
def listen
listen_backend(@default_port)
end
private
#== Variables
# The udp handle for the service
attr_reader :udp_listener_handle
# The default port to listen to
attr_reader :default_port
# The default host to listen on
attr_reader :default_host
# The registry is stored as a hash.
# The keys are the port numbers, and the
# values are a short description. For the sake
# of this example, let's not worry about foreign hosts
attr_reader :registry
#== Functions
# Register a node to the registry!
def register(portnum,description)
@registry[portnum] = description unless @registry.contains? portnum
end
# TODO
def destroy(portnum)
end
# Listen to a specified port number.
def listen_backend(portnum)
@udp_listener_handle.bind(@default_host, @default_port)
puts "Registry started, listening at port #{@default_port}, host #{@default_host}"
while true do
packet = @udp_listener_handle.recvfrom(1024)
puts "[#{packet[1]}]"
puts " #{packet[0]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment