Reimplement scoring and create connection abstraction

Co-authored-by: Felix Auringer <felix.auringer@student.hpi.uni-potsdam.de>
This commit is contained in:
Konrad Hanff
2021-03-31 16:18:21 +02:00
committed by Sebastian Serth
parent 1546f70818
commit 92b249e7b3
5 changed files with 172 additions and 111 deletions

View File

@ -1,10 +1,10 @@
# frozen_string_literal: true
require 'container_connection'
class Container
BASE_URL = "http://192.168.178.53:5000"
attr_accessor :socket
def initialize(execution_environment, time_limit = nil)
url = "#{BASE_URL}/execution-environments/#{execution_environment.id}/containers/create"
body = {}
@ -39,7 +39,11 @@ class Container
def execute_interactively(command)
websocket_url = execute_command(command)[:websocket_url]
@socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1)
EventMachine.run do
#socket = Faye::WebSocket::Client.new(websocket_url, [], ping: 0.1)
socket = ContainerConnection.new(websocket_url)
yield(self, socket) if block_given?
end
end
def destroy

View File

@ -0,0 +1,58 @@
require 'faye/websocket/client'
class ContainerConnection
EVENTS = %i[start message exit stdout stderr].freeze
def initialize(url)
@socket = Faye::WebSocket::Client.new(url, [], ping: 0.1)
%i[open message error close].each do |event_type|
@socket.on event_type, &:"on_#{event_type}"
end
EVENTS.each { |event_type| instance_variable_set(:"@#{event_type}_callback", lambda {}) }
end
def on(event, &block)
return unless EVENTS.include? event
instance_variable_set(:"@#{event}_callback", block)
end
def send(data)
@socket.send(data)
end
private
def parse(event)
JSON.parse(event.data).deep_symbolize_keys
end
def on_message(event)
event = parse(event)
case event[:type]
when :exit_code
@exit_code = event[:data]
when :stderr
@stderr_callback.call event[:data]
@message_callback.call event[:data]
when :stdout
@stdout_callback.call event[:data]
@message_callback.call event[:data]
else
:error
end
end
def on_open(event)
@start_callback.call
end
def on_error(event)
end
def on_close(event)
@exit_callback.call @exit_code
end
end