Class: BilibiliSunday::Server

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

Constant Summary collapse

DEFAULT_PORT =
10753

Instance Method Summary collapse

Constructor Details

#initialize(port = DEFAULT_PORT, working_dir = nil) ⇒ Server

Returns a new instance of Server.



109
110
111
112
# File 'lib/bilibili_sunday/server.rb', line 109

def initialize(port = DEFAULT_PORT, working_dir = nil)
	@port = port
	@downloader = Downloader.new(working_dir || File.expand_path("~/.bilibili_sunday"))
end

Instance Method Details

#startObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bilibili_sunday/server.rb', line 114

def start
	@running = true

	@downloader_thread = Thread.new do
		while true
			@downloader.routine_work
			sleep 1
		end
	end

	@rpc_server_thread = Thread.new do
		begin
			server = WEBrick::HTTPServer.new(:Port => @port)
			server.mount "/jsonrpc", Servlet, @downloader
			server.start
		rescue
		ensure
			server.shutdown
		end
	end

	while true
		unless @running
			@downloader_thread.terminate
			@rpc_server_thread.terminate
			break
		end
		sleep 1
	end
end

#stopObject



145
146
147
# File 'lib/bilibili_sunday/server.rb', line 145

def stop
	@running = false
end