Class: Baykit::BayServer::Tours::TourStore

Inherits:
Object
  • Object
show all
Includes:
Agent, Util
Defined in:
lib/baykit/bayserver/tours/tour_store.rb

Overview

TourStore

You must lock object before call methods because all the methods may be called by different threads. (agent, tours agent)

Defined Under Namespace

Classes: AgentListener

Constant Summary collapse

MAX_TOURS =
1024

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTourStore

Returns a new instance of TourStore.



46
47
48
49
50
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 46

def initialize()
  @free_tours = []
  @active_tour_map = {}
  @lock = ::Monitor.new
end

Class Attribute Details

.max_countObject (readonly)

Returns the value of attribute max_count.



38
39
40
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 38

def max_count
  @max_count
end

.storesObject (readonly)

Agent ID => TourStore



41
42
43
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 41

def stores
  @stores
end

Instance Attribute Details

#active_tour_mapObject (readonly)

Returns the value of attribute active_tour_map.



34
35
36
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 34

def active_tour_map
  @active_tour_map
end

#free_toursObject (readonly)

Returns the value of attribute free_tours.



33
34
35
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 33

def free_tours
  @free_tours
end

Class Method Details

.get_store(agent_id) ⇒ Object



116
117
118
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 116

def self.get_store(agent_id)
  return stores[agent_id]
end

.init(max_tours) ⇒ Object

class methods



111
112
113
114
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 111

def self.init(max_tours)
  @max_count = max_tours
  GrandAgent.add_lifecycle_listener(AgentListener.new())
end

Instance Method Details

#get(key) ⇒ Object



52
53
54
55
56
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 52

def get(key)
  @lock.synchronize do
    return @active_tour_map[key]
  end
end

print memory usage



96
97
98
99
100
101
102
103
104
105
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 96

def print_usage(indent)
  BayLog.info("%sTour store usage:", StringUtil.indent(indent))
  BayLog.info("%sfreeList: %d", StringUtil.indent(indent+1), @free_tours.length())
  BayLog.info("%sactiveList: %d", StringUtil.indent(indent+1), @active_tour_map.length())
  if BayLog.debug_mode?
    @active_tour_map.values.each do |obj|
      BayLog.debug("%s%s", StringUtil.indent(indent+1), obj)
    end
  end
end

#rent(key, ship, force = false) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 58

def rent(key, ship, force = false)
  @lock.synchronize do
    tur = get(key)
    if tur != nil
      raise Sink.new("#{ship} Tour already exists")
    end

    if !@free_tours.empty?
      tur = @free_tours.delete_at(@free_tours.length - 1)
    else
      if !force && (@active_tour_map.length >= TourStore.max_count)
        BayLog.warn("Max tour count reached")
        return nil
      else
        tur = Tour.new()
      end
    end

    @active_tour_map[key] = tur
    return tur
  end
end

#Return(key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/baykit/bayserver/tours/tour_store.rb', line 81

def Return(key)
  @lock.synchronize do
    if !@active_tour_map.key?(key)
      raise Sink.new("Tour is not active: key=#{key}")
    end

    tur = @active_tour_map.delete(key)
    tur.reset()
    @free_tours.append(tur)
  end
end