Class: BMF::ThreadStatus

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bmf/lib/thread_status.rb

Constant Summary collapse

STASH_FILE =
BMF::Settings.fully_qualified_filename("thread_status_stash")

Instance Method Summary collapse

Constructor Details

#initializeThreadStatus

Returns a new instance of ThreadStatus.



11
12
13
14
# File 'lib/bmf/lib/thread_status.rb', line 11

def initialize
  @thread_last_visited = {}
  load_stash
end

Instance Method Details

#deserialize_stash(serialized_stash) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/bmf/lib/thread_status.rb', line 27

def deserialize_stash serialized_stash
  serialized_stash.split(";").each do |stash_line|
    address, thread, update_time = stash_line.split(':')
    thread = Base64.decode64(thread.gsub("\\n","\n"))
    update_time = update_time.to_i
    thread_visited(address,thread,update_time)
  end
end

#load_stashObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bmf/lib/thread_status.rb', line 36

def load_stash
  if File.exists? STASH_FILE
    stash = File.open(STASH_FILE).read
    deserialize_stash stash
  end
rescue Exception => ex # Failure is not an option!
  puts "@" * 80
  puts "Error loading ThreadStatus stash."
  puts "Ignoring so that the app is usable."
  puts "Please report the following information to the project maintainers"
  puts
  puts "Exception: #{ex.message}"
  puts ex.backtrace.join("\n")
end

#new_messages?(address, thread) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bmf/lib/thread_status.rb', line 77

def new_messages?(address, thread)
  if @thread_last_visited[address] && @thread_last_visited[address][thread]
    last_visited_time = @thread_last_visited[address][thread]
  else
    last_visited_time = 0
  end
  
  last_message_time = BMF::MessageStore.instance.thread_last_updates[address][thread]

  raise thread.inspect if last_message_time.nil?

  last_visited_time < last_message_time
end

#new_messages_for_address?(address, threads) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/bmf/lib/thread_status.rb', line 91

def new_messages_for_address?(address, threads)
  return true if !@thread_last_visited[address] # never been updated

  not threads.detect{ |thread| new_messages?(address, thread)}.nil?

end

#persistObject



51
52
53
54
55
# File 'lib/bmf/lib/thread_status.rb', line 51

def persist
  File.open(STASH_FILE,"w",0600) do |f|
    f.write(serialize_stash)
  end
end

#serialize_stashObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/bmf/lib/thread_status.rb', line 16

def serialize_stash
  updates = []
  @thread_last_visited.each_pair do |address, threads|
    threads.each_pair do |thread_name, update_time|
      updates << "#{address}:#{Base64.encode64(thread_name).gsub("\n","\\n")}:#{update_time}"
    end
  end
  
  updates.join(";")
end

#thread_last_visited(address, thread) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/bmf/lib/thread_status.rb', line 58

def thread_last_visited(address, thread)
  if @thread_last_visited[address] && @thread_last_visited[address][thread]
    @thread_last_visited[address][thread]
  else
    0
  end
end

#thread_visited(address, thread, time) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/bmf/lib/thread_status.rb', line 66

def thread_visited(address, thread, time)
  @thread_last_visited[address] ||= {}
  @thread_last_visited[address][thread] ||= 0

  if time > @thread_last_visited[address][thread]
    @thread_last_visited[address][thread] = time
  end

  persist
end