Class: Net::IRC::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/net/irc/server.rb

Defined Under Namespace

Classes: Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, session_class, opts = {}) ⇒ Server

Returns a new instance of Server.



6
7
8
9
10
11
12
13
# File 'lib/net/irc/server.rb', line 6

def initialize(host, port, session_class, opts={})
	@host          = host
	@port          = port
	@session_class = session_class
	@opts          = OpenStruct.new(opts)
	@sessions      = []
	@state         = {}
end

Instance Attribute Details

#sessionsObject

Returns the value of attribute sessions.



4
5
6
# File 'lib/net/irc/server.rb', line 4

def sessions
  @sessions
end

#stateObject

Server global state for accessing Server::Session



3
4
5
# File 'lib/net/irc/server.rb', line 3

def state
  @state
end

Instance Method Details

#finishObject

Close all sessions.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/net/irc/server.rb', line 41

def finish
	Thread.exclusive do
		@accept.kill
		begin
			@serv.close
		rescue
		end
		@sessions.each do |s|
			s.finish
		end
	end
end

#startObject

Start server loop.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/net/irc/server.rb', line 16

def start
	@serv = TCPServer.new(@host, @port)
	@log  = @opts.logger || Logger.new($stdout)
	@log.info "Host: #{@host} Port:#{@port}"
	@accept = Thread.start do
		loop do
			Thread.start(@serv.accept) do |s|
				begin
					@log.info "Client connected, new session starting..."
					s = @session_class.new(self, s, @log, @opts)
					@sessions << s
					s.start
				rescue Exception => e
					puts e
					puts e.backtrace
				ensure
					@sessions.delete(s)
				end
			end
		end
	end
	@accept.join
end