From 27c3147c7efe37657563fadcb835b50d00423475 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Mon, 5 Apr 2021 09:45:49 -0400 Subject: Implemented titan handling on the server, hopefully. Added sending the captured file for a task to failed jobs as well. --- captain/api.f90 | 87 ++++++++++++++++++++++++++++++++-------------------- captain/gemini.f90 | 9 ++++-- captain/response.f90 | 44 ++++++++++++++++++++++++-- captain/special.f90 | 12 +++++++- 4 files changed, 114 insertions(+), 38 deletions(-) (limited to 'captain') diff --git a/captain/api.f90 b/captain/api.f90 index 8b6890d..9e92185 100644 --- a/captain/api.f90 +++ b/captain/api.f90 @@ -6,6 +6,34 @@ implicit none contains + subroutine handle_task_request(req) + use server_response + use captain_db + implicit none + + class(request)::req + character(8)::job_text, task_text + integer::job_i, task_i, ierr + + call req%path_component(5, job_text) + read(job_text, *, iostat=ierr) job_i + + if(ierr == 0) then + call req%path_component(7, task_text) + read(task_text, *, iostat=ierr) task_i + if(ierr == 0) then + if(req%query_string == "starting" .or. req%query_string == "inprogress") then + call update_task_status(job_i, task_i, JOB_STATUS_WORKING) + else if(req%query_string == "complete") then + call update_task_status(job_i, task_i, JOB_STATUS_SUCCESS) + else if(req%query_string == "failed") then + call update_task_status(job_i, task_i, JOB_STATUS_FAILURE) + end if + end if + end if + + end subroutine handle_task_request + function api_request_gemini(req) result(resp) use server_response use captain_db @@ -15,9 +43,9 @@ contains type(request), intent(in)::req type(response)::resp - character(8)::job_text, task_text + character(8)::job_text character(PLAYER_NAME_LENGTH)::player - integer::job_i, task_i, player_i, instruction_i, ierr + integer::job_i, player_i, instruction_i, ierr ! Complete - "/api/player/{name}/job/{jobid}/complete" ! Failed - "/api/player/{name}/job/{jobid}/failed" @@ -35,17 +63,7 @@ contains end if if(ierr == 0 .and. trim(req%component(6)) == "task") then - call req%path_component(7, task_text) - read(task_text, *, iostat=ierr) task_i - if(ierr == 0) then - if(req%query_string == "starting" .or. req%query_string == "inprogress") then - call update_task_status(job_i, task_i, JOB_STATUS_WORKING) - else if(req%query_string == "complete") then - call update_task_status(job_i, task_i, JOB_STATUS_SUCCESS) - else if(req%query_string == "failed") then - call update_task_status(job_i, task_i, JOB_STATUS_FAILURE) - end if - end if + call handle_task_request(req) end if resp%code = GEMINI_CODE_SUCCESS @@ -75,12 +93,15 @@ contains function api_request_titan(req) result(resp) use server_response + use special_filenames implicit none type(titan_request), intent(in)::req type(response)::resp character(len=:), pointer::fullpath + character(12)::job_text, task_text + integer::job_id, task_num, ierr fullpath => null() @@ -89,12 +110,31 @@ contains trim(req%component(4)) == "job" .and. & trim(req%component(6)) == "task") then - fullpath => + call req%path_component(5, job_text) + read(job_text, *, iostat=ierr) job_id + if(ierr /= 0) then + resp%code = GEMINI_CODE_PERMFAIL + return + end if + + call req%path_component(5, task_text) + read(task_text, *, iostat=ierr) task_num + if(ierr /= 0) then + resp%code = GEMINI_CODE_PERMFAIL + return + end if + + call handle_task_request(req) + + fullpath => get_task_result_static_filename(job_id, task_num) end if if(associated(fullpath)) then + ! Write the file + call req%write_to(fullpath) + resp%code = GEMINI_CODE_SUCCESS call resp%set_body_contents(RESPONSE_JSON_OKAY) resp%body_mimetype = "application/json" @@ -105,25 +145,6 @@ contains end if - - end function api_request_titan - function api_request(req) result(resp) - use server_response - implicit none - - type(request)::req - type(titan_request)::titan_req - type(response)::resp - - if(req%protocol == "gemini") then - resp = api_request_gemini(req) - else if(req%protocol == "titan") then - call titan_req%init_from_request(req) - resp = api_request_titan(titan_req) - end if - - end function api_request - end module api_handling \ No newline at end of file diff --git a/captain/gemini.f90 b/captain/gemini.f90 index 8822ed5..8453db9 100644 --- a/captain/gemini.f90 +++ b/captain/gemini.f90 @@ -178,7 +178,7 @@ contains use config use iso_fortran_env use external_handling, only: external_request_gemini - use api_handling, only: api_request + use api_handling use logging, only: write_log use server_response implicit none @@ -190,6 +190,7 @@ contains integer(kind=c_long)::res type(request)::req + type(titan_request)::treq type(response)::resp ! Requested file @@ -264,7 +265,11 @@ contains call req%path_component(1, first) if(trim(first) == 'api') then - resp = api_request(request) + if(req%protocol == "gemini") then + resp = api_request_gemini(req) + else if(req%protocol == "titan") then + call treq%init_from_request(req, ssl) + end if else resp = external_request_gemini(req) end if diff --git a/captain/response.f90 b/captain/response.f90 index cd1895a..3526766 100644 --- a/captain/response.f90 +++ b/captain/response.f90 @@ -1,4 +1,6 @@ module server_response +use iso_c_binding +implicit none integer, parameter::GEMINI_CODE_INPUT = 10 integer, parameter::GEMINI_CODE_SUCCESS = 20 @@ -48,15 +50,18 @@ module server_response end type request type, extends(request) :: titan_request - + integer(kind=8)::size character(len=:), pointer::mimetype character(len=:), pointer::token + type(c_ptr)::ssl_connection + contains procedure :: init_from_request => titan_request_init procedure :: destroy => titan_request_destroy + procedure :: write_to => titan_write_to_filename end type titan_request @@ -338,11 +343,12 @@ contains end function titan_get_request_value - subroutine titan_request_init(self, regular_request) + subroutine titan_request_init(self, regular_request, ssl_connection) implicit none class(titan_request)::self class(request), intent(inout)::regular_request + type(c_ptr)::ssl_connection character(len=:), pointer::size_text integer::i, ierr @@ -372,6 +378,8 @@ contains self%mimetype => titan_get_request_value(regular_request%location, "mime") self%token => titan_get_request_value(regular_request%location, "token") + self%ssl_connection = ssl_connection + end subroutine titan_request_init subroutine titan_request_destroy(self) @@ -390,4 +398,36 @@ contains end subroutine titan_request_destroy + subroutine titan_write_to_filename(self, filename) + use jessl, only: ssl_read + implicit none + + class(titan_request)::self + character(*), intent(in)::filename + integer::unum + + character, dimension(64)::buf + integer::bufread + + integer(kind=8)::bytes_to_go + integer::i + + open(newunit=unum, file=filename, status="unknown", action="write", access="stream") + + bytes_to_go = self%size + + do while(bytes_to_go > 0) + bufread = ssl_read(self%ssl_connection, buf) + bytes_to_go = bytes_to_go - bufread + + do i = 1, bufread + write(unum, '(A1)', advance='no') buf(i:i) + end do + + end do + + close(unum) + + end subroutine titan_write_to_filename + end module server_response diff --git a/captain/special.f90 b/captain/special.f90 index 3850d3f..26f31e3 100644 --- a/captain/special.f90 +++ b/captain/special.f90 @@ -51,11 +51,21 @@ contains end function get_instructions_static_filename function get_task_result_static_filename(job_id, task_num) result(res) + use config implicit none + integer, intent(in)::job_id, task_num + character(len=:), pointer::res + character(64)::filename + character(12)::job_text, task_text + + write(job_text, '(I8)') job_id + write(task_text, '(I8)') task_num + filename = "results-job"//trim(adjustl(job_text))//"-task"//trim(adjustl(task_text))//".txt" - + res => get_special_full_filename("results", filename) + end function get_task_result_static_filename end module special_filenames -- cgit v1.2.3