Class: LanGrove::Server::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/langrove/server/server_base.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#logger

Instance Method Summary collapse

Methods inherited from Base

#config_exception, #type

Constructor Details

#initialize(root, config_hash, daemon_name) ⇒ Base

Returns a new instance of Base.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/langrove/server/server_base.rb', line 229

def initialize( root, config_hash, daemon_name )
  
  @type = :server
  
  super( root )
  
  @config        = config_hash
  @daemon_name   = daemon_name
  
  @handlers = {}
  @behaviours = {}
  
  
  begin
    
    @my_config = @config[ :daemons ][ @daemon_name ][ :server ]

  rescue
    
    error = "Missing config item for daemon: #{@daemon_name}" 
    
    @logger.error "EXIT: #{error}"
    
    raise_exception( 
    
      "Missing config item for daemon: #{@daemon_name}" 
      
    )
    
  end
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Expose behaviours by their action name



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/langrove/server/server_base.rb', line 265

def method_missing( symbol, *args, &block )
  
  @behaviours.each do |name, behaviour|
    
    if behaviour.requires.has_key?( symbol )
      
      return behaviour
      
    end
    
  end
  
  super
  
end

Instance Attribute Details

#handlersObject

One Server exists in the daemon with the primary purpose of housing through an addressable Hash the set of connected clients (Handlers),

In this:



15
16
17
# File 'lib/langrove/server/server_base.rb', line 15

def handlers
  @handlers
end

Instance Method Details

#connect(handler) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/langrove/server/server_base.rb', line 137

def connect( handler )
  
  #
  # A client has connected at the Adaptor
  #
  # It's Handler is passed in here.
  # 
  # It should be inserted into the collection
  # of handlers maintained here. 
  #
  @logger.debug "#{handler} registers with #{self.class}"

  I.notify( @logger, "TODO - move unique_key onto protocol, use first message, assuming 'HELO, i am XXX' style protocol" )
  
  if handler.respond_to?( :unique_key ) then

    @handlers[ handler.unique_key ] = handler
    return
    
  end
  
  @handlers[ handler ] = 1
  
end

#disconnect(handler) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/langrove/server/server_base.rb', line 162

def disconnect( handler )
  
  #
  # A client has disconnected
  # 
  # It's Handler should be removed from the collection.
  #
  
  @logger.debug "#{handler} un-registers with #{self.class}"
  
  handler.stop_handler
  
  if handler.respond_to?( :unique_key ) then
    
    @handlers.delete handler.unique_key
    return
    
  end
  
  @handlers.delete handler
  
end

#each_handler(&block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/langrove/server/server_base.rb', line 114

def each_handler( &block )
  
  #
  # Call periodic on all handlers
  # in the collection
  #
  
  @handlers.each do |key, value|
    
    if value == 1 then
      
      yield key if block
      
      next
      
    end
    
    yield value if block
    
  end
  
end

#reloadObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/langrove/server/server_base.rb', line 45

def reload
  
  #
  # OVERRIDE
  #
  # To perform any necessties on HUP,
  #
  # The daemon has received a signal to reload.
  #
  
end

#reload_serverObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/langrove/server/server_base.rb', line 97

def reload_server

  @logger.info "Server reloading."

  each_handler do |handler|
    
    handler.reload_handler
    
  end
  
  @root.trigger( self, :server, :reload )
  
  reload
  
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/langrove/server/server_base.rb', line 281

def respond_to?( symbol, include_private = false )
  
  @behaviours.each do |name, behaviour|
    
    if behaviour.requires.has_key?( symbol )
      
      return true
      
    end
    
  end
  
  super
  
end

#schedule(&block) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/langrove/server/server_base.rb', line 186

def schedule( &block )
  
  if @my_config.nil? or 
     @my_config[:behaviours].nil? or
     @my_config[:behaviours].size == 0
  
    @logger.warn "No :behaviours: listed in the :server:"
    
    return
    
  end
  
  @my_config[:behaviours].each do |behaviour, config|
    
    config = {} if config.nil?
    
    classname = behaviour.to_s.camelize
    
    at_event = nil
    at_event = config[:at] if config.has_key?( :at )
  
    behaviour_instance = LanGrove::ClassLoader.create( {
  
      :module => 'Behaviour',
      :class  => classname
  
    } ).new( @root, config, nil )
    
    yield behaviour_instance, at_event if block
  
  
    #
    # Keep local collection of behaviours
    #  
    #  - for inject() into Handlers on connect.  
    #
    @behaviours[behaviour] = behaviour_instance
  
  end unless @my_config[:behaviours].nil?
  
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/langrove/server/server_base.rb', line 18

def start

  #
  # OVERRIDE 
  #
  # To prepare the Server in any necessary 
  # ways before the thread is handed over to 
  # the socket listener, (or thing),
  #
  # The daemon has just started.
  #

end

#start_serverObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/langrove/server/server_base.rb', line 58

def start_server
  
  @logger.info "Server starting."

  #
  # Post call to whichever Behaviours
  # and associated plugins are configured
  # to action upon the Server(self) on 
  # this event.
  #
  
  @root.trigger( self, :server, :start )
  
  #
  # Call to implementor overridable 
  #
  
  start
  
end

#stopObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/langrove/server/server_base.rb', line 32

def stop
  
  #
  # OVERRIDE 
  # 
  # To perfom any necessties before process
  # termination,
  #
  # The daemon has received a signal to stop.
  #
  
end

#stop_serverObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/langrove/server/server_base.rb', line 80

def stop_server

  @logger.info "Server stopping."

  each_handler do |handler|
    
    handler.stop_handler
    
  end
  
  @root.trigger( self, :server, :stop )
  
  stop
  
end