From 1545914afff13e37bfcfee1b04828942e430a819 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Thu, 25 Mar 2021 10:11:12 -0400 Subject: Added endpoints and convenience functions for talking to the server. Completed task loop and run loop. --- player/endpoints.f90 | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 player/endpoints.f90 (limited to 'player/endpoints.f90') diff --git a/player/endpoints.f90 b/player/endpoints.f90 new file mode 100644 index 0000000..1a8ca73 --- /dev/null +++ b/player/endpoints.f90 @@ -0,0 +1,150 @@ +module player_endpoints +implicit none + + character(*), parameter::LOCATION_CHECK_IN = "/player/{name}/checkin.json" + character(*), parameter::LOCATION_STATUS = "/player/{name}/job/{jobid}/task/{step}" + character(*), parameter::LOCATION_JOB_COMPLETE = "/player/{name}/job/{jobid}/complete" + character(*), parameter::LOCATION_JOB_FAILED = "/player/{name}/job/{jobid}/failed" + + integer, parameter::STATUS_STARTING=1 + integer, parameter::STATUS_COMPLETED=2 + integer, parameter::STATUS_FAILED=3 + integer, parameter::STATUS_IN_PROGRESS=4 + + character(len=10), dimension(4)::status_text = (/ "starting ", & + "complete ", & + "failed ", & + "inprogress" /) + + interface replace_field + module procedure replace_field_text + module procedure replace_field_int + end interface + +contains + + subroutine base_url(server, location, post, res) + implicit none + + character(*), intent(in)::server + character(*), intent(in)::location + logical, intent(in)::post + character(*), intent(out)::res + + if(post) then + res = "titan://"//trim(server)//trim(location) + else + res = "gemini://"//trim(server)//trim(location) + end if + + end subroutine base_url + + subroutine replace_field_text(str, field, val) + implicit none + + character(*), intent(inout)::str + character(*), intent(in)::field + character(*), intent(in)::val + + character(len=:), allocatable::holding + integer::length_estimate + integer::field_location, i, j + + ! This is too big, but close enough + length_estimate = len_trim(str) + len_trim(val) + allocate(character(len=length_estimate) :: holding) + holding = " " + + ! Find the field + field_location = index(str, "{"//trim(field)//"}") + if(field_location > 0) then + + holding(1:field_location-1) = str(1:field_location-1) + + j = field_location + holding(j:j+len_trim(val)) = trim(val) + + i = field_location + len_trim(field) + 2 + j = len_trim(holding)+1 + holding(j:j+(len_trim(str)-i)) = str(i:len_trim(str)) + + ! Put the results back now + str = holding + + end if + + deallocate(holding) + + end subroutine replace_field_text + + subroutine replace_field_int(str, field, val) + implicit none + + character(*), intent(inout)::str + character(*), intent(in)::field + integer, intent(in)::val + + character(16)::int_text + + write(int_text, *) val + + call replace_field_text(str, field, trim(adjustl(int_text))) + + end subroutine replace_field_int + + subroutine get_check_in_url(res) + use config + implicit none + + character(*), intent(out)::res + + call base_url(captain, LOCATION_CHECK_IN, .false., res) + call replace_field(res, "name", identity) + + end subroutine get_check_in_url + + subroutine get_status_url(job, step, url, posting, status) + use config + implicit none + + integer, intent(in)::job + integer, intent(in)::step + logical, intent(in), optional::posting + integer, intent(in), optional::status + character(*), intent(out)::url + + character(32)::int_text + + if(present(posting)) then + call base_url(captain, LOCATION_STATUS, posting, url) + else + call base_url(captain, LOCATION_STATUS, .false., url) + end if + call replace_field(url, "name", identity) + call replace_field(url, "jobid", job) + call replace_field(url, "step", step) + + if(present(status)) then + url = trim(url)//"?"//trim(status_text(status)) + end if + + end subroutine get_status_url + + subroutine get_job_report_url(job, success, res) + use config + implicit none + + integer, intent(in)::job + logical, intent(in)::success + character(*), intent(out)::res + + if(success) then + call base_url(captain, LOCATION_JOB_COMPLETE, .false., res) + else + call base_url(captain, LOCATION_JOB_FAILED, .false., res) + end if + call replace_field(res, "jobid", job) + + end subroutine get_job_report_url + +end module player_endpoints -- cgit v1.2.3