Class: Baykit::BayServer::Protocol::PacketStore

Inherits:
Object
  • Object
show all
Includes:
Agent, Util, Util::Reusable
Defined in:
lib/baykit/bayserver/protocol/packet_store.rb

Defined Under Namespace

Classes: AgentListener, ProtocolInfo

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proto, factory) ⇒ PacketStore

Returns a new instance of PacketStore.



64
65
66
67
68
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 64

def initialize(proto, factory)
  @protocol = proto
  @factory = factory
  @store_map = {}
end

Class Attribute Details

.proto_mapObject (readonly)

Returns the value of attribute proto_map.



56
57
58
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 56

def proto_map
  @proto_map
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



62
63
64
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 62

def factory
  @factory
end

#protocolObject (readonly)

Returns the value of attribute protocol.



60
61
62
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 60

def protocol
  @protocol
end

#store_mapObject (readonly)

Returns the value of attribute store_map.



61
62
63
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 61

def store_map
  @store_map
end

Class Method Details

.get_store(protocol, agent_id) ⇒ Object



118
119
120
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 118

def self.get_store(protocol, agent_id)
  return @proto_map[protocol].stores[agent_id]
end

.get_stores(agent_id) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 128

def self.get_stores(agent_id)
  store_list = []
  @proto_map.values.each do |ifo|
    store_list.append(ifo.stores[agent_id])
  end
  return store_list
end

.initObject

class methods



114
115
116
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 114

def self.init()
  GrandAgent.add_lifecycle_listener(AgentListener.new())
end

.register_protocol(protocol, factory) ⇒ Object



122
123
124
125
126
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 122

def self.register_protocol(protocol, factory)
  if !@proto_map.include?(protocol)
    @proto_map[protocol] = PacketStore::ProtocolInfo.new(protocol, factory)
  end
end

Instance Method Details



103
104
105
106
107
108
109
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 103

def print_usage(indent)
  BayLog.info("%sPacketStore(%s) usage nTypes=%d", StringUtil.indent(indent), @protocol, @store_map.keys().size)
  @store_map.keys.each do |type|
    BayLog.info("%sType: %s", StringUtil.indent(indent+1), type)
    @store_map[type].print_usage(indent+2)
  end
end

#rent(type) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 76

def rent(type)
  if type == nil
    raise RuntimeError.new("Nil argument")
  end

  store = @store_map[type]
  if store == nil
    store = ObjectStore.new(lambda do
      if @factory.kind_of?(PacketFactory)
        return @factory.create_packet(type)
      else
        # lambda
        return @factory.call(type)
      end
    end)
    @store_map[type] = store
  end
  return store.rent
end

#resetObject



70
71
72
73
74
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 70

def reset
  @store_map.values.each do |store|
    store.reset
  end
end

#Return(pkt) ⇒ Object



96
97
98
99
100
# File 'lib/baykit/bayserver/protocol/packet_store.rb', line 96

def Return(pkt)
  store = @store_map[pkt.type]
  #puts "Return packet #{pkt}"
  store.Return(pkt)
end