Class: ChainReactor::ChainfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/chain-reactor/chainfile_parser.rb

Overview

Parse the chain file and register reaction code blocks and network addresses.

Instance Method Summary collapse

Constructor Details

#initialize(chainfile, config, logger) ⇒ ChainfileParser

Set up a parser with a File object representing the chain file.



20
21
22
23
24
25
26
# File 'lib/chain-reactor/chainfile_parser.rb', line 20

def initialize(chainfile,config,logger)
  @file = chainfile
  @config = config
  @reactions = 0
  @reactor = Reactor.new(logger)
  @log = logger
end

Instance Method Details

#address(addr) ⇒ Object

Set the address to bind to.



56
57
58
# File 'lib/chain-reactor/chainfile_parser.rb', line 56

def address(addr)
  @config.address = addr
end

#logfile(logfile) ⇒ Object

Set the log file path.



71
72
73
# File 'lib/chain-reactor/chainfile_parser.rb', line 71

def logfile(logfile)
  @config.log_file = logfile
end

#multithreaded(multithreaded = true) ⇒ Object

Set whether to run multithreaded.



76
77
78
# File 'lib/chain-reactor/chainfile_parser.rb', line 76

def multithreaded(multithreaded = true)
  @config.multithreaded = !!multithreaded
end

#parseObject

Parse the chain file and return a reactor object.

A ChainfileParserError is raised if the chain file has invalid syntax.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chain-reactor/chainfile_parser.rb', line 31

def parse
  begin
    eval @file.read
  rescue Exception => e
    raise ChainfileParserError, e
  end

  if @reactions.zero?
    @log.error { "No reactions registered, no reason to continue" }
    raise 'No reactions registered, no reason to continue'
  else
    @log.info { "Registered #{@reactions} reactions in the chain file" }
  end

  @reactor
end

#pidfile(pidfile) ⇒ Object

Set the pid file path.



66
67
68
# File 'lib/chain-reactor/chainfile_parser.rb', line 66

def pidfile(pidfile)
  @config.pid_file = pidfile
end

#port(port) ⇒ Object

Set the port to listen on.



61
62
63
# File 'lib/chain-reactor/chainfile_parser.rb', line 61

def port(port)
  @config.port = port
end

#react_to(addresses, args = {}, &block) ⇒ Object

Register a reaction block, with IP addresses, arguments and a code block



49
50
51
52
53
# File 'lib/chain-reactor/chainfile_parser.rb', line 49

def react_to(addresses,args = {},&block)
  addresses = [*addresses]
  @reactions += 1
  @reactor.add(addresses,args,block)
end