Class: BeerBot::Kernel
- Inherits:
-
Object
- Object
- BeerBot::Kernel
- Defined in:
- lib/beerbot/kernel.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#bot ⇒ Object
Returns the value of attribute bot.
-
#codec ⇒ Object
Returns the value of attribute codec.
-
#config ⇒ Object
Returns the value of attribute config.
-
#conn ⇒ Object
Returns the value of attribute conn.
-
#dispatcher ⇒ Object
Returns the value of attribute dispatcher.
-
#scheduler ⇒ Object
Returns the value of attribute scheduler.
Instance Method Summary collapse
-
#action(to, msg) ⇒ Object
Convenience method to do something (/me).
-
#echo ⇒ Object
Toggle whether inputs and outputs show on the repl screen.
-
#initialize(config, conn, codec, bot, dispatcher, scheduler) ⇒ Kernel
constructor
Initialize all parts of the system here.
-
#join(chan) ⇒ Object
Convenience method to join a channel.
-
#leave(chan) ⇒ Object
Convenience method to leave a channel.
-
#say(to, msg) ⇒ Object
Convenience method to say something to channel or someone.
-
#start ⇒ Object
Start the connection.
Constructor Details
#initialize(config, conn, codec, bot, dispatcher, scheduler) ⇒ Kernel
Initialize all parts of the system here.
config should be a hash, normally BeerBot::Config.
Note BeerBot::Config should be loaded before we initialize here.
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/beerbot/kernel.rb', line 40 def initialize config, conn, codec, bot, dispatcher, scheduler @echo = true @path = File.(File.dirname(__FILE__)+'/..') @module_path = config['moduledir'] @config = config @bot = bot @conn = conn @codec = codec @dispatcher = dispatcher @scheduler = scheduler # Dispatcher thread takes stuff coming from the connection and does # something with it... @dispatcher_thread = InOut.new(inq:@conn.queue,outq:@conn.writeq) {|input| str,raw = input event,*args = @codec.decode(str) replies = @dispatcher.receive(event,args) @codec.encode(replies) } @dispatcher_thread.start! # Schedule dispatcher thread. # # These are responses that were prepared and scheduled earlier and # which also need to be dispatched. @scheduler_thread = InOut.new(inq:@scheduler.queue,outq:@conn.writeq) {|cron_job| puts "<< scheduler #{cron_job.inspect}" if @echo puts "<< scheduler #{@scheduler.time}" if @echo @codec.encode(cron_job.run) } @scheduler_thread.start! # Active messaging queue. # # 'config' will be injected into bot modules. # config.out should be a queue that we can dequeue. @active_thread = InOut.new(inq:@config.out,outq:@conn.writeq) {|replies| puts "<< active #{replies}" if @echo # TODO: almost identical logic in the dispatcher class (in # @dispatcher_thread). case replies when String # assume protocol string eg irc replies when Hash,Array,Proc @codec.encode(BotMsg.to_a(replies)) else [] end } @active_thread.start! # Set up a repl in a separate thread. # # In pry, you can then do: # @conn.writeq.enq @codec.join('#chan1') # @conn.write @codec.join('#chan1') Pry.config.prompt = Proc.new {|_| "pry> "} @pry_thread = Thread.new { binding.pry } # Initialize bot and its modules... @bot.init(@config) @bot.update_config(@config) # Do stuff once we've identified with the server... # # Join channels. # Start the scheduler. @conn.ready? { channels = config['channels'] if channels then channels.each{|chan| self.join(chan) } end @scheduler.start } end |
Instance Attribute Details
#bot ⇒ Object
Returns the value of attribute bot.
32 33 34 |
# File 'lib/beerbot/kernel.rb', line 32 def bot @bot end |
#codec ⇒ Object
Returns the value of attribute codec.
32 33 34 |
# File 'lib/beerbot/kernel.rb', line 32 def codec @codec end |
#config ⇒ Object
Returns the value of attribute config.
32 33 34 |
# File 'lib/beerbot/kernel.rb', line 32 def config @config end |
#conn ⇒ Object
Returns the value of attribute conn.
32 33 34 |
# File 'lib/beerbot/kernel.rb', line 32 def conn @conn end |
#dispatcher ⇒ Object
Returns the value of attribute dispatcher.
32 33 34 |
# File 'lib/beerbot/kernel.rb', line 32 def dispatcher @dispatcher end |
#scheduler ⇒ Object
Returns the value of attribute scheduler.
32 33 34 |
# File 'lib/beerbot/kernel.rb', line 32 def scheduler @scheduler end |
Instance Method Details
#action(to, msg) ⇒ Object
Convenience method to do something (/me).
151 152 153 |
# File 'lib/beerbot/kernel.rb', line 151 def action to,msg @conn.writeq.enq(@codec.action(to,msg)) end |
#echo ⇒ Object
Toggle whether inputs and outputs show on the repl screen.
Call this from the pry repl.
132 133 134 135 |
# File 'lib/beerbot/kernel.rb', line 132 def echo @echo = !@echo @conn.echo = @echo end |
#join(chan) ⇒ Object
Convenience method to join a channel.
157 158 159 |
# File 'lib/beerbot/kernel.rb', line 157 def join chan @conn.writeq.enq(@codec.join(chan)) end |
#leave(chan) ⇒ Object
Convenience method to leave a channel.
163 164 165 |
# File 'lib/beerbot/kernel.rb', line 163 def leave chan @conn.writeq.enq(@codec.leave(chan)) end |
#say(to, msg) ⇒ Object
Convenience method to say something to channel or someone.
145 146 147 |
# File 'lib/beerbot/kernel.rb', line 145 def say to,msg @conn.writeq.enq(@codec.msg(to,msg)) end |
#start ⇒ Object
Start the connection.
139 140 141 |
# File 'lib/beerbot/kernel.rb', line 139 def start @conn.open.join end |