Class: LanGrove::Daemon::Base
- Inherits:
-
Object
- Object
- LanGrove::Daemon::Base
- Defined in:
- lib/langrove/daemon_base.rb
Instance Method Summary collapse
-
#initialize(config_hash, daemon_name, logger) ⇒ Base
constructor
A new instance of Base.
- #listen ⇒ Object
- #reload_daemon ⇒ Object
- #run ⇒ Object
- #schedule ⇒ Object
- #stop_daemon ⇒ Object
Constructor Details
#initialize(config_hash, daemon_name, logger) ⇒ Base
Returns a new instance of Base.
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 174 175 176 177 178 179 180 181 182 183 184 185 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 227 228 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/langrove/daemon_base.rb', line 86 def initialize( config_hash, daemon_name, logger ) @config = config_hash @daemon_name = daemon_name @logger = logger @server = true # # A network connection. # @adaptor = nil # # The client collection class. # @handler = nil # # The client type # @client = nil # # The protocol type # @protocol = 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 if @my_config[ :adaptor ].has_key? :class adaptor = @my_config[ :adaptor ][ :class ] else @logger.warn( "Defaulting to Adaptor::Base" ) adaptor = 'Base' end @logger.info "TODO: fall back to default Handler::Base" handler = @my_config[ :handler ][ :collection ] client = @my_config[ :client ][ :class ] protocol = @my_config[ :protocol ][ :class ] @logger.info "TODO: Warn instead of raise and allow nil into ClassLoader -> load default" 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: # class: MySocket <----------( optional ) # transport: tcp|udp # iface: N.N.n.n # port: nNn # handler: # collection: CollectionOfClients # @logger.info "CREATE: 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 "CREATE: Handler::#{handler}.new( config_hash, '#{daemon_name}', logger )" @handler = LanGrove::ClassLoader.create( { :module => 'Handler', :class => handler } ).new( @config, @daemon_name, @logger ) # # initialize the client specified in config # # daemons: # name_of_daemon: # client: # class: Client <--------- # adaptor: # connection: TcpServer # handler: # collection: CollectionOfClients # # @logger.info "DEFINE: Client::#{client}" @client = LanGrove::ClassLoader.create( { :module => 'Client', :class => client } ) # # initialize the client specified in config # # daemons: # name_of_daemon: # protocol: # class: Protocol <--------- # more: config # client: # class: Client # adaptor: # connection: TcpServer # handler: # collection: CollectionOfClients # # @logger.info "DEFINE: Protocol::#{protocol}" @protocol = LanGrove::ClassLoader.create( { :module => 'Protocol', :class => protocol } ) # # Notifiy the handler # # About to go to listen... # @handler.start_daemon end |
Instance Method Details
#listen ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/langrove/daemon_base.rb', line 7 def listen # # <Client> and <Protocol> are passed into # the Adaptor.listen for binding onto the # sockets at connect. # @adaptor.listen( # # pass Client, Protocol and Handler to 'listener' # { :class => @client, :config => @my_config[ :client ] }, { :class => @protocol, :config => @my_config[ :protocol ] }, @handler ) end |
#reload_daemon ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/langrove/daemon_base.rb', line 40 def reload_daemon # # TODO: Handle reloading daemon properly # # [ !!complexities abound around reloading config!! ] # @logger.info "#{self} received reload_daemon" @handler.reload_daemon end |
#run ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/langrove/daemon_base.rb', line 70 def run EventMachine::run do # # https://github.com/eventmachine/eventmachine/wiki/Protocol-Implementations # schedule listen if @server end end |
#schedule ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/langrove/daemon_base.rb', line 53 def schedule @logger.info "#{self} Scheduling periodic ...TODO..." EM::add_periodic_timer( @my_config[ :periodic ] ) do # # 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_daemon ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/langrove/daemon_base.rb', line 30 def stop_daemon # # Handle stopping daemon # @logger.info "#{self} received stop_daemon" @handler.stop_daemon end |