Class: NSCA::Server

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nsca/server.rb

Defined Under Namespace

Classes: Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Server

Returns a new instance of Server.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nsca/server.rb', line 6

def initialize *args
	opts = {}
	opts = args.pop.dup  if args.last.is_a? Hash
	opts[:host] ||= opts[:hostname]
	opts[:sock] ||= opts[:socket]
	opts[:pass] ||= opts[:password]

	case args[0]
	when Integer
		opts[:port] = args[0]
		opts[:host] ||= args[1]
	when IO
		opts[:sock] = args[0]
	end

	@packet_version = opts[:packet_version] || PacketV3
	@iv_key = (opts[:iv_key] || SecureRandom.random_bytes( 128)).to_s
	raise ArgumentError, "Key must be 128 bytes long"  unless 128 == @iv_key.length
	@password = opts[:pass].to_s
	@server = if opts[:serv].is_a?( TCPServer) or opts[:serv].is_a?( UNIXServer)
			opts[:serv]
		elsif opts[:port].is_a? Integer
			TCPServer.new *[opts[:port], opts[:host]].compact
		else
			raise ArgumentError, "Server or port-number expected"
		end
end

Instance Attribute Details

#iv_keyObject (readonly)

Returns the value of attribute iv_key.



5
6
7
# File 'lib/nsca/server.rb', line 5

def iv_key
  @iv_key
end

#packet_versionObject (readonly)

Returns the value of attribute packet_version.



5
6
7
# File 'lib/nsca/server.rb', line 5

def packet_version
  @packet_version
end

#passwordObject (readonly)

Returns the value of attribute password.



5
6
7
# File 'lib/nsca/server.rb', line 5

def password
  @password
end

#serverObject (readonly)

Returns the value of attribute server.



5
6
7
# File 'lib/nsca/server.rb', line 5

def server
  @server
end

Instance Method Details

#acceptObject



34
# File 'lib/nsca/server.rb', line 34

def accept() Connection.new @server.accept, self end

#closeObject



35
# File 'lib/nsca/server.rb', line 35

def close() @server.close end

#each(&block) ⇒ Object



37
38
39
40
41
42
# File 'lib/nsca/server.rb', line 37

def each &block
	return Enumerator.new( self)  unless block_given?
	while conn = accept
		yield conn
	end
end