Class: Minecraft
- Inherits:
-
Object
show all
- Defined in:
- lib/minecraft.rb
Defined Under Namespace
Classes: HistoryQueue, MessageQueue, ServerNotRunningError, StartupFailedError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(cmd, options = {}) ⇒ Minecraft
Returns a new instance of Minecraft.
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/minecraft.rb', line 105
def initialize(cmd, options = {})
@cmd = cmd
@in_queue = Queue.new
@message_queue = MessageQueue.new
@startup_timeout = (options[:startup_timeout] or 120)
@command_timeout = (options[:command_timeout] or 40)
@server_pid = nil
@collector = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
242
243
244
|
# File 'lib/minecraft.rb', line 242
def method_missing(m, *args)
command(([m.to_s.tr('_', '-')] + args).join(' '))
end
|
Instance Attribute Details
#server_pid ⇒ Object
Returns the value of attribute server_pid.
118
119
120
|
# File 'lib/minecraft.rb', line 118
def server_pid
@server_pid
end
|
Instance Method Details
#command(cmd) ⇒ Object
246
247
248
249
250
251
252
253
254
|
# File 'lib/minecraft.rb', line 246
def command(cmd)
raise ServerNotRunningError unless running?
@in_queue << "#{cmd}\n"
if block_given?
yield
else
active_wait
end
end
|
#error(msg) ⇒ Object
143
144
145
|
# File 'lib/minecraft.rb', line 143
def error(msg)
@message_queue.error(msg)
end
|
#history ⇒ Object
256
257
258
|
# File 'lib/minecraft.rb', line 256
def history
@message_queue.history
end
|
#list ⇒ Object
236
237
238
239
240
|
# File 'lib/minecraft.rb', line 236
def list
command('list') do
wait_msg{|m| m.msg =~ /Connected players:/}
end
end
|
#log(msg) ⇒ Object
139
140
141
|
# File 'lib/minecraft.rb', line 139
def log(msg)
@message_queue.log(msg)
end
|
#running? ⇒ Boolean
135
136
137
|
# File 'lib/minecraft.rb', line 135
def running?
@out_reader and @in_writter.alive?
end
|
#save_all ⇒ Object
228
229
230
231
232
233
234
|
# File 'lib/minecraft.rb', line 228
def save_all
command('save-all') do
time_operation("Save") do
wait_msg{|m| m.msg =~ /Save complete/}
end
end
end
|
#start ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/minecraft.rb', line 147
def start
if running?
log "Server already running"
else
time_operation("Server start") do
begin
log "Starting minecraft: #{@cmd}"
pid, @stdin, @stdout, @stderr = Spawn::spawn(@cmd)
@server_pid = pid
log "Started server process with pid: #{@server_pid}"
@in_writter = Thread.new do
begin
loop do
msg = @in_queue.pop
@stdin.write(msg)
end
rescue IOError, Errno::EPIPE
end
end
@err_reader = Thread.new do
begin
@stderr.each do |line|
@message_queue.err(line.strip)
end
rescue IOError
end
end
@out_reader = Thread.new do
begin
@stdout.each do |line|
@message_queue.out(line.strip)
end
rescue IOError
ensure
@in_writter.kill
@err_reader.kill
@stdin.close unless @stdin.closed?
@stderr.close unless @stderr.closed?
@stdout.close unless @stdout.closed?
log "Minecraft exits"
end
end
wait_msg(false, @startup_timeout) do |m|
m.msg =~ /Done \(([^n]*)ns\)!/ or m.msg =~ /Minecraft exits/
end
unless running?
Process.wait(@server_pid)
@server_pid = nil
raise StartupFailedError, @cmd
end
rescue Errno::ENOENT
raise StartupFailedError, @cmd
end
end
end
end
|
#stop ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/minecraft.rb', line 213
def stop
unless running?
log "Server already stopped"
else
command('stop') do
time_operation("Server stop") do
wait_msg{|m| m.msg == "Minecraft exits"}
Process.wait(@server_pid)
@server_pid = nil
log "Server stopped"
end
end
end
end
|
#with_message_collector(collector, &operations) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/minecraft.rb', line 120
def with_message_collector(collector, &operations)
@message_queue.flush
@collector = collector
begin
instance_eval &operations
rescue Timeout::Error
error "Command timed out"
ensure
@message_queue.flush do |msg|
collect(msg)
end
@collector = nil
end
end
|