/
engine_state.rb
105 lines (90 loc) · 2.37 KB
/
engine_state.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (C) 2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require "English"
require "coolio"
require "droonga/loggable"
require "droonga/event_loop"
require "droonga/forwarder"
require "droonga/replier"
module Droonga
class EngineState
include Loggable
attr_reader :loop
attr_reader :name
attr_reader :internal_name
attr_reader :forwarder
attr_reader :replier
attr_accessor :on_finish
def initialize(loop, name, internal_name)
@loop = loop
@name = name
@internal_name = internal_name
@sessions = {}
@current_id = 0
@forwarder = Forwarder.new(@loop)
@replier = Replier.new(@forwarder)
@on_finish = nil
end
def start
logger.trace("start start")
@forwarder.start
logger.trace("start done")
end
def shutdown
logger.trace("shutdown: start")
@forwarder.shutdown
logger.trace("shutdown: done")
end
def local_route?(route)
route.start_with?(@name) or route.start_with?(@internal_name)
end
def farm_path(route)
if /\A[^:]+:\d+\/[^.]+/ =~ route
name = $MATCH
if name == @internal_name
@name
else
name
end
else
route
end
end
def generate_id
id = @current_id
@current_id = id.succ
return [@internal_name, id].join(".#")
end
def find_session(id)
@sessions[id]
end
def register_session(id, session)
@sessions[id] = session
end
def unregister_session(id)
@sessions.delete(id)
unless have_session?
@on_finish.call if @on_finish
end
end
def have_session?
not @sessions.empty?
end
private
def log_tag
"engine_state"
end
end
end