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.



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

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

Class Attribute Details

.max_countObject (readonly)

Returns the value of attribute max_count.



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

def max_count
  @max_count
end

.storesObject (readonly)

Agent ID => TourStore



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

def stores
  @stores
end

Instance Attribute Details

#active_tour_mapObject (readonly)

Returns the value of attribute active_tour_map.



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

def active_tour_map
  @active_tour_map
end

#free_toursObject (readonly)

Returns the value of attribute free_tours.



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

def free_tours
  @free_tours
end

Class Method Details

.get_store(agent_id) ⇒ Object



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

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

.init(max_tours) ⇒ Object

class methods



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

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

Instance Method Details

#get(key) ⇒ Object



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

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

print memory usage



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

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



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

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



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

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