Class: LanGrove::Daemon::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/langrove/daemon_base.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_hash, daemon_name, logger) ⇒ Base

Returns a new instance of Base.



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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/langrove/daemon_base.rb', line 74

def initialize( config_hash, daemon_name, logger )
  
  @config      = config_hash
  @daemon_name = daemon_name
  @logger      = logger
  @server      = true
  
  #
  # A network connection.
  #
  @adaptor = nil
  
  #
  # A handler to direct the traffic.
  #
  @handler = nil
  
  
  begin
    
    @logger.info "Starting daemon: #{@daemon_name}"
    
  rescue
    
    #
    # you shall not pass
    #
    raise LanGrove::DaemonConfigException.new( "Requires a logger" )
    
  end

  begin

    @my_config = @config[ :daemons ][ @daemon_name ]
    
    @server    = @my_config[ :server ] if @my_config.has_key? :server
    
    adaptor    = @my_config[ :adaptor ][ :connection ]
    
    handler    = @my_config[ :handler ][ :collection ]
       
  rescue
    
    error = "Missing config item for daemon: #{@daemon_name}" 
    
    @logger.error "EXIT: #{error}"
    
    raise LanGrove::DaemonConfigException.new( 
    
      "Missing config item for daemon: #{@daemon_name}" 
      
    )
      
  end
  
  #
  # initialize the connection adaptor specified in config
  # 
  # daemons:
  #   name_of_daemon:
  #     adaptor:
  #       connection: TcpServer  <----------
  #     handler:
  #       collection: CollectionOfClients 
  #

  @logger.info "Initialize instance: Adaptor::#{adaptor}.new( @my_config[ :adaptor ], logger )"
  
  @adaptor = LanGrove::ClassLoader.create( {
    
    :module => 'Adaptor',
    :class  => adaptor
    
  } ).new( @my_config[ :adaptor ], logger )
  
  
  #
  # bind to the handler collection specified in config
  # 
  # daemons:
  #   name_of_daemon:
  #     adaptor:
  #       connection: TcpServer
  #     handler:
  #       collection: CollectionOfClients  <---------- 
  #    
  #  CollectionOfClients ---> is the handler passed into listen()                       
  #

  @logger.info "Initialize instance: Handler::#{handler}.new( config_hash, '#{daemon_name}', logger )"
  
  @handler = LanGrove::ClassLoader.create( {
    
    :module => 'Handler',
    :class  => handler
    
  } ).new( @config, @daemon_name, @logger )
  
 
end

Instance Method Details

#listenObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/langrove/daemon_base.rb', line 7

def listen
  
  #
  # Handler - The CollectionOfClients, is passed 
  #           through the adaptor to be assigned 
  #           to the attaching Clients.
  #
  @adaptor.listen( @handler, @handler.protocol )
  
end

#reload_daemonObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/langrove/daemon_base.rb', line 28

def reload_daemon
  
  #
  # TODO: Handle reloading daemon properly
  # 
  # [ !!complexities abound around reloading config!! ]
  #
  
  @logger.info "#{self} received reload_daemon"
  @handler.reload_daemon
  
end

#runObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/langrove/daemon_base.rb', line 58

def run
  
  EventMachine::run do
    
    # 
    # https://github.com/eventmachine/eventmachine/wiki/Protocol-Implementations
    # 
    
    schedule
    
    listen if @server
    
  end
  
end

#scheduleObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/langrove/daemon_base.rb', line 41

def schedule
  
  EM::add_periodic_timer( @my_config['periodic'] ) do
    
    @logger.info "#{self} Scheduling periodic ...TODO..."
    
    #
    # TODO: This can be a lot more usefull than
    #       then one ticker to one function call
    #
    
    @handler.periodic
    
  end if @my_config.has_key? 'periodic'
  
end

#stop_daemonObject



18
19
20
21
22
23
24
25
26
# File 'lib/langrove/daemon_base.rb', line 18

def stop_daemon
  
  #
  # Handle stopping daemon
  #
  @logger.info "#{self} received stop_daemon"
  @handler.stop_daemon
  
end