Module: Frypan::Node

Defined in:
lib/frypan/node.rb

Class Method Summary collapse

Class Method Details

.apply_operations(list, ops) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/frypan/node.rb', line 8

def self.apply_operations(list, ops)
  case
  when ops == []
    [list, ""]
  when ops.first[:kind] == :add
    added_list = list + [ops.first[:target]]
    if overlaped?(added_list.map{|a| a[:start_time]..a[:end_time]})
      [nil, "cannot add reservation: #{ops.first[:target]}"]
    else
      apply_operations(added_list, ops.drop(1))
    end
  when ops.first[:kind] == :remove
    if list.find{|a| a[:start_time] == ops.first[:target][:start_time]}
      apply_operations(list.reject{|a| a[:start_time] == ops.first[:target][:start_time]}, ops.drop(1))
    else
      [nil, "unexist remove reservation id: #{ops.first[:target][:id]}"]
    end
  else
    [nil, "unknown operation '#{ops.first[:kind]}'"]
  end
end

.EpgExtractAsyncFoldp(epg_parser) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/frypan/node.rb', line 98

def self.EpgExtractAsyncFoldp(epg_parser)
  TinyFRP.foldp(busy: false, parser_process: nil, waiting: [], extracted: []) do |acc, log|
    case
    when log[:epg_order]
      {waiting: acc[:waiting] + [log[:epg_order]], extracted: []}
    when acc[:busy]
      {busy: !acc[:parser_process].finished?, extracted: acc[:parser_process].get_parsed}
    when acc[:waiting] != []
      {
        busy:           true,
        parser_process: epg_parser.parse(acc[:waiting].first[:file_path]), 
        waiting:        acc[:waiting].drop(1),
        extracted:      []
      }
    else
      {extracted: []}
    end
  end
end

.overlaped?(list_of_range) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/frypan/node.rb', line 4

def self.overlaped?(list_of_range)
  !list_of_range.sort{|a, b| a.begin <=> b.begin}.each_cons(2).all?{|a, b| a.end <= b.begin}
end

.PersistantReserveListFoldp(tuner_id, dbsync_reserve) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/frypan/node.rb', line 53

def self.PersistantReserveListFoldp(tuner_id, dbsync_reserve)
  TinyFRP.foldp(last_list: nil, list: dbsync_reserve) do |acc, rlist|
    if !acc[:last_list] || acc[:last_list] != rlist[:list]
      {last_list: rlist[:list], list: acc[:list].update(rlist[:list])}
    else
      {}
    end
  end
end

.ProgramListFoldp(initial_list = []) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/frypan/node.rb', line 118

def self.ProgramListFoldp(initial_list=[])
  TinyFRP.foldp(list: initial_list) do |acc, epg|
    if epg[:extracted] != []
      # epg[:extracted] := list of {program_id: [service_id, event_id], ...}
      {list: acc[:list].update_elements(epg[:extracted], :program_id)}
    else
      {}
    end
  end
end

.RecordedListFoldp(initial_list = []) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/frypan/node.rb', line 129

def self.RecordedListFoldp(initial_list=[])
  TinyFRP.foldp(list: initial_list) do |acc, log|
    if log[:recorded] != []
      {list: acc[:list] + log[:recorded]}
    else
      {}
    end
  end
end

.RecordingAsyncFoldp(tuner) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/frypan/node.rb', line 63

def self.RecordingAsyncFoldp(tuner)
  TinyFRP.foldp(busy: false, rec_process: nil, waiting: [], log: []) do |acc, rlist|
    case
    when rlist[:launch]
      {waiting: acc[:waiting] + [rlist[:launch]], log: []}
    when acc[:busy]
      {busy: !acc[:rec_process].finished?, log: acc[:rec_process].get_log}
    when acc[:waiting] != []
      {
        busy:        true,
        rec_process: tuner.rec(acc[:waiting].first),
        waiting:     acc[:waiting].drop(1),
        log:         []
      }
    else
      {log: []}
    end
  end
end

.RecordingLogFoldp(initial_log = []) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/frypan/node.rb', line 83

def self.RecordingLogFoldp(initial_log=[])
  TinyFRP.foldp(log: initial_log, epg_order: [], recorded: []) do |acc, *log_inputs|
    if log_inputs.any?{|a| a != []}
      new_logs = log_inputs.select{|a| a != []}.flatten
      {
        log:       acc[:log] + new_logs,
        epg_order: Fnc.find_epg_order(new_logs),
        recorded:  Fnc.find_recorded(new_logs)
      }
    else
      {epg_order: [], recorded: []}
    end
  end
end

.ReserveListFoldp(tuner_id, initial_list = []) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/frypan/node.rb', line 30

def self.ReserveListFoldp(tuner_id, initial_list=[])
  TinyFRP.foldp(launch: nil, list: initial_list, ack: nil) do |acc, command, current_time|
    case
    when command && command[:target_tuner] == tuner_id
      new_list, status = *apply_operations(acc[:list], command[:operations])
      ack = command.merge({
         timestamp: current_time,
         succeeded: !!new_list,
         status: status
       })
      if new_list
        {launch: nil, list: new_list.sort{|a, b| a[:start_time] <=> b[:start_time]}, ack: ack}
      else
        {launch: nil, ack: ack}
      end
    when acc[:list] != [] && acc[:list].first[:start_time] <= current_time
      {launch: acc[:list].first, list: acc[:list].drop(1), ack: nil}
    else
      {launch: nil, ack: nil}
    end
  end
end

.UIresponseFoldp(initial_list = []) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/frypan/node.rb', line 139

def self.UIresponseFoldp(initial_list=[])
  TinyFRP.foldp(list: initial_list) do |acc, current_time, *acks|
    if acks.any?{|a| a}
      {list: acc[:list] + acks.select{|a| a}}
    else
      {}
    end
  end
end