merge with master

This commit is contained in:
yqbk
2016-08-03 12:09:58 +02:00
77 changed files with 770 additions and 515 deletions

View File

@@ -6,9 +6,9 @@ class SubmissionsController < ApplicationController
include SubmissionScoring
include Tubesock::Hijack
before_action :set_submission, only: [:download_file, :render_file, :run, :score, :show, :statistics, :stop, :test]
before_action :set_submission, only: [:download, :download_file, :render_file, :run, :score, :show, :statistics, :stop, :test]
before_action :set_docker_client, only: [:run, :test]
before_action :set_files, only: [:download_file, :render_file, :show]
before_action :set_files, only: [:download, :download_file, :render_file, :show]
before_action :set_file, only: [:download_file, :render_file]
before_action :set_mime_type, only: [:download_file, :render_file]
skip_before_action :verify_authenticity_token, only: [:download_file, :render_file]
@@ -53,6 +53,20 @@ class SubmissionsController < ApplicationController
end
end
def download
# files = @submission.files.map{ }
# zipline( files, 'submission.zip')
# send_data(@file.content, filename: @file.name_with_extension)
require 'zip'
stringio = Zip::OutputStream.write_buffer do |zio|
@files.each do |file|
zio.put_next_entry(file.name_with_extension)
zio.write(file.content)
end
end
send_data(stringio.string, filename: @submission.exercise.title.tr(" ", "_") + ".zip")
end
def download_file
if @file.native_file?
send_file(@file.native_file.path)
@@ -174,8 +188,14 @@ class SubmissionsController < ApplicationController
def parse_message(message, output_stream, socket, recursive = true)
begin
parsed = JSON.parse(message)
socket.send_data message
Rails.logger.info('parse_message sent: ' + message)
if(parsed.class == Hash && parsed.key?('cmd'))
socket.send_data message
Rails.logger.info('parse_message sent: ' + message)
else
parsed = {'cmd'=>'write','stream'=>output_stream,'data'=>message}
socket.send_data JSON.dump(parsed)
Rails.logger.info('parse_message sent: ' + JSON.dump(parsed))
end
rescue JSON::ParserError => e
# Check wether the message contains multiple lines, if true try to parse each line
if ((recursive == true) && (message.include? "\n"))
@@ -208,7 +228,11 @@ class SubmissionsController < ApplicationController
end
def score
render(json: score_submission(@submission))
hijack do |tubesock|
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
# tubesock is the socket to the client
tubesock.send_data JSON.dump(score_submission(@submission))
end
end
def set_docker_client
@@ -260,8 +284,14 @@ class SubmissionsController < ApplicationController
private :store_error
def test
output = @docker_client.execute_test_command(@submission, params[:filename])
render(json: [output])
hijack do |tubesock|
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
output = @docker_client.execute_test_command(@submission, params[:filename])
# tubesock is the socket to the client
tubesock.send_data JSON.dump(output)
end
end
def with_server_sent_events