开发者

Ruby TCP "bot" using EventMachine - implementing a command dispatcher

开发者 https://www.devze.com 2023-04-08 23:36 出处:网络
I\'ve crafted a basic TCP client using EventMachine. Code: # run.rb EventMachine::run do EventMachine::connect $config_host, $config_port, Bot

I've crafted a basic TCP client using EventMachine. Code:

# run.rb
EventMachine::run do
  EventMachine::connect $config_host, $config_port, Bot
end

# bot.rb
module Bot
  def post_init
    # log us in and do any other spinup.
    sleep(1)
    send_data $config_login + "\n"
    EventMachine.add_periodic_timer($config_keepalive_duration) { send_data $config_keepalive_str + "\n" }
    @valid_command =  /^<#{$config_passphrase}:([^>:]+):(#\d+)>(.*)$/
  end

  def receive_data(data)
    if(ma = @valid_command.match(data))
      command, user, args = ma[1,3]
      args.strip!
      command.downcase!
      p "Received: #{command}, #{user}, #{args}"
      # and.. here we handle the command.
    end
  end
end

This all works quite well. The basic idea is that it should connect, listen for specially formatted commands, and execute开发者_开发百科 the command; in executing the command, there may be any number of "actions" taken that result in various data sent by the client.

But, for my next trick, I need to add the ability to actually handle the commands that Bot receives.

I'd like to do this using a dynamic library of event listeners, or something similar to that; ie, I have an arbitrary number of plugins that can register to listen for a specific command and get a callback from bot.rb. (Eventually, I'd like to be able to reload these plugins without restarting the bot, too.)

I've looked at the ruby_events gem and I think this makes sense but I'm having a little trouble of figuring out the best way to architect things. My questions include...

  1. I'm a little puzzled where to attach ruby_events listeners to - it just extends Object so it doesn't make it obvious how to implement it.
  2. Bot is a module, so I can't just call Bot.send_data from one of the plugins to send data - how can I interact with the EM connection from one of the plugins?

I'm completely open to any architectural revisions or suggestions of other gems that make what I'm trying to do easier, too.


I'm not sure exactly what you're trying to do, but one common pattern of doing this in EM is to define the command handlers as callbacks. So then the command logic can be pushed up out of the Bot module itself, which just handles the basic socket communication and command parsing. Think of how a web server dispatches to an application - the web server doesn't do the work, it just dispatches.

For example something like this

EM::run do 

  bot = EM::connect $config_host, $config_port, Bot

  bot.on_command_1 do |user, *args|
     # handle command_1 in a block
  end

  # or if you want to modularize it, something like this
  # where Command2Handler = lambda {|user, *args| #... }
  bot.on_command_2(&Command2Handler)

end

So then you just need to implement #on_command_1, #on_command_2, etc. in your Bot, which is just a matter of storing the procs to instance vars; then calling them once you parse the passed commands out of your receive_data.

A good, very readable example of this in production is TwitterStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号