aboutsummaryrefslogtreecommitdiff
path: root/player/player.F90
blob: 2233affbc2ce05ed29d8c6da0683f376f22f40f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
! Copyright (c) 2021 Approximatrix, LLC <support@approximatrix.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

program player
use config
use instructions
use player_endpoints
use json_module
use talking

#ifdef WINDOWS
use wsa_network, only: windows_network_startup => startup
#endif

implicit none

    character(len=1024)::url
    type(json_file)::j_checkin, j_instructions
    logical::work_to_do, checkin_json_available, instr_json_available
    
    integer::i_task
    
    integer::job_id
    character(128)::instruction_name

#ifdef WINDOWS
    call windows_network_startup()
#endif

    call parse_options()
    
    ! Change directory to the working directory now
    call chdir(working_directory)
    
    do while(.true.)
        
        ! Check in for work
        call get_check_in_url(url)
        checkin_json_available = request_json(url, j_checkin)
        if(checkin_json_available) then
            work_to_do = work_available(j_checkin)
        else
            Print *, "Checkin failed: "//trim(url)
            work_to_do = .false.
        end if
        
        if(work_to_do) then
    
            ! GNU Extension... :(
            Print *, fdate()//" Work Available"
            job_id = get_job_id_from_checkin(j_checkin)
            call get_instruction_name_from_checkin(j_checkin, instruction_name)
            
            call get_instruction_url(instruction_name, url)
            
            instr_json_available = request_json(url, j_instructions)
            
            if(instr_json_available) then
                ! Task loop
                call perform_tasks(j_instructions, job_id)
                
                call destroy_instructions(j_instructions)
            end if
        else
        
#ifdef GNU
            call sleep(20)
#endif

        end if
        
        ! Sleep a bit regardless
#ifdef GNU
        call sleep(10)
#endif

        ! Destroy any existing json
        if(checkin_json_available) then
            call j_checkin%destroy()
        end if
    
    end do
    
contains

    subroutine usage()
    implicit none
    
        character(len=256)::pname
        
        call get_command_argument(0, pname)
    
        Print *, "Usage: "//trim(pname)//" <options> <captain>"
        Print *, " "
        Print *, "captain is the build control server"
        Print *, " "
        
        Print *, "Options:"
        Print *, "    -h            Display this help"
        Print *, "    -w <dir>      Use dir as the working directory"
        Print *, "    -l <log>      Use log as the logfile"
        Print *, "    -i <identity> This player's identity"
        Print *, "    -t <token>    This player's security token"
        
    end subroutine usage
    
    subroutine parse_options
    use config
    implicit none
    
        character(len=1024)::option
        integer::slen
        integer::i
        
        identity = " "
        token = "None"
        
        i = 1
        do while(i <= command_argument_count())
            call get_command_argument(i, option)
        
            if(option(1:1) /= "-") then
                captain = option
        
            else if(trim(option) == "-h") then
                call usage()
                stop
            
            else if(trim(option) == "-w") then
                i = i + 1
                call get_command_argument(i, length=slen)
                allocate(character(len=slen) :: working_directory)
                call get_command_argument(i, working_directory)
                
            else if(trim(option) == "-l") then
                i = i + 1
                call get_command_argument(i, length=slen)
                allocate(character(len=slen) :: logfile)
                call get_command_argument(i, logfile)
                
            else if(trim(option) == "-i") then
                i = i + 1
                call get_command_argument(i, identity)
            
            else if(trim(option) == "-t") then
                i = i + 1
                call get_command_argument(i, token)
                
            end if
            
            i = i + 1
        end do
        
        ! Assign working directory from command if not specified
        if(.not. associated(working_directory)) then
            call get_command_argument(0, length=slen)
            allocate(character(len=slen) :: working_directory)
            call get_command_argument(i, working_directory)
            i = index(working_directory, "/", back=.true.)
            if(i == 0) then
                i = index(working_directory, "/", back=.true.)
            endif
            if(i == 0) then
                Print *, "Could not determine working_directory"
                stop
            else
                working_directory(i:slen) = ' '
            end if
        end if
        
        ! Assign a temporary directory and file for a log file
        ! NOTE: will fail on Windows
        if(.not. associated(logfile)) then
            allocate(character(len=256) :: logfile)
            logfile = "/tmp/levitating-player.log"
        end if
        
        ! Assign this computer an identity if not explicitly specified
        if(len_trim(identity) == 0) then
#ifdef GNU
            call hostnm(identity)
#else
            Print *, "Could not determine host identity"
            stop
#endif
        end if
        
    end subroutine parse_options
    
    function request_json(url, j)
    use json_module
    use gemini_protocol
    use instructions, only: parse_instructions
    use talking, only: request_to_temporary_file
    use utilities, only: delete_file
    implicit none
    
        logical::request_json
        character(*), intent(in)::url
        type(json_file), intent(out)::j
        
        character(:), pointer::filename
        integer::status_code
        
        status_code = request_to_temporary_file(url, filename)
        if(status_code == STATUS_SUCCESS) then
            j = parse_instructions(filename)
            request_json = .true.
        else
            request_json = .false.
        end if
        
        if(associated(filename)) then
            call delete_file(filename)
            deallocate(filename)
        end if
        
    end function request_json
    
end program player