summaryrefslogtreecommitdiff
path: root/lib/qra/q65/q65_callers.F90
blob: 224ca60f23daddf81461d868c0fee2b883da0424 (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
#include "q65_limits.h"
module q65_callers
  use types, only: q3list
  use, intrinsic :: iso_fortran_env, only: int64
  implicit none
  private
  integer, parameter, public :: Q65_MAX_CALLERS=Q65_CALLER_CAPACITY
  integer, parameter, public :: Q65_ROW_WIDTH=Q65_CALLER_ROW_WIDTH
  integer, parameter, public :: Q65_MAX_CODEWORDS=Q65_AP_LIST_CAPACITY
  public :: q65_load_callers,q65_save_callers,q65_expire_callers
  public :: q65_record_caller,q65_remove_caller

contains

  subroutine q65_load_callers(filename,callers,count,status)
    character(len=*), intent(in) :: filename
    type(q3list), intent(out) :: callers(Q65_MAX_CALLERS)
    integer, intent(out) :: count,status
    type(q3list) :: loaded(Q65_MAX_CALLERS)
    integer :: unit,n,close_status,file_size
    logical :: exists

    callers=q3list('','',0,0,0)
    count=0
    exists=.false.
    file_size=-1
    inquire(file=filename,exist=exists,size=file_size,iostat=status)
    if(status.ne.0) return
    if(.not.exists .or. file_size.eq.0) return
    open(newunit=unit,file=filename,status='old',form='unformatted',action='read',iostat=status)
    if(status.ne.0) return
    loaded=q3list('','',0,0,0)
    read(unit,iostat=status) n
    if(status.eq.0) then
       if(n.lt.0 .or. n.gt.Q65_MAX_CALLERS) then
          status=1
       else
          read(unit,iostat=status) loaded(1:n)
       endif
    endif
    close(unit,iostat=close_status)
    if(status.eq.0) status=close_status
    if(status.ne.0) return
    callers=loaded
    count=n
  end subroutine q65_load_callers

  subroutine q65_save_callers(filename,callers,count,status)
    character(len=*), intent(in) :: filename
    type(q3list), intent(in) :: callers(Q65_MAX_CALLERS)
    integer, intent(in) :: count
    integer, intent(out) :: status
    integer :: unit,close_status

    status=1
    if(count.lt.0 .or. count.gt.Q65_MAX_CALLERS) return
    open(newunit=unit,file=filename,status='replace',form='unformatted',action='write',iostat=status)
    if(status.ne.0) return
    write(unit,iostat=status) count
    if(status.eq.0) write(unit,iostat=status) callers(1:count)
    close(unit,iostat=close_status)
    if(status.eq.0) status=close_status
  end subroutine q65_save_callers

  subroutine q65_expire_callers(callers,count,now)
    type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
    integer, intent(inout) :: count
    integer, intent(in) :: now
    integer :: i,kept

    kept=0
    do i=1,count
       if(int(now,int64)-int(callers(i)%nsec,int64).gt.86400_int64) cycle
       kept=kept+1
       callers(kept)=callers(i)
    enddo
    count=kept
    callers(count+1:)=q3list('','',0,0,0)
  end subroutine q65_expire_callers

  subroutine q65_record_caller(callers,count,freq,msg,now)
    type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
    integer, intent(inout) :: count
    integer, intent(in) :: freq,now
    character(len=*), intent(in) :: msg
    character(len=37) :: text
    character(len=6) :: call
    character(len=4) :: grid
    integer :: i,i0,i1,i2,oldest

    call q65_expire_callers(callers,count,now)
    text=msg
    if(index(text,'/').gt.0) return
    i0=index(text,' R ')
    if(i0.ge.7) text=text(1:i0)//text(i0+3:)
    i1=index(text,' ')
    if(i1.lt.4 .or. i1.gt.13) return
    i2=index(text(i1+1:),' ')+i1
    if(i2.le.i1+1 .or. i2+4.gt.len(text)) return
    call=text(i1+1:i2-1)
    grid=text(i2+1:i2+4)
    do i=1,count
       if(callers(i)%call.ne.call) cycle
       callers(i)%nsec=now
       callers(i)%nfreq=freq
       return
    enddo
    if(.not.valid_grid(grid)) return
    if(count.eq.Q65_MAX_CALLERS) then
       oldest=minloc(callers(1:count)%nsec,dim=1)
       callers(oldest:count-1)=callers(oldest+1:count)
       count=count-1
    endif
    count=count+1
    callers(count)=q3list(call,grid,now,freq,0)
  end subroutine q65_record_caller

  subroutine q65_remove_caller(callers,count,call)
    type(q3list), intent(inout) :: callers(Q65_MAX_CALLERS)
    integer, intent(inout) :: count
    character(len=*), intent(in) :: call
    character(len=6) :: target
    integer :: i

    target=call
    do i=1,count
       if(callers(i)%call.ne.target) cycle
       callers(i:count-1)=callers(i+1:count)
       callers(count)=q3list('','',0,0,0)
       count=count-1
       return
    enddo
  end subroutine q65_remove_caller

  logical function valid_grid(grid)
    character(len=4), intent(in) :: grid
    valid_grid=grid(1:1).ge.'A' .and. grid(1:1).le.'R' .and. &
         grid(2:2).ge.'A' .and. grid(2:2).le.'R' .and. &
         grid(3:3).ge.'0' .and. grid(3:3).le.'9' .and. &
         grid(4:4).ge.'0' .and. grid(4:4).le.'9' .and. grid.ne.'RR73'
  end function valid_grid
end module q65_callers