Skip to content

Commit

Permalink
A few enhancements to the simpleagent example nanite.
Browse files Browse the repository at this point in the history
  • Loading branch information
grempe committed Jan 2, 2009
1 parent d2416aa commit 1017b0a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README
Expand Up @@ -208,7 +208,7 @@ Now open a new shell and run the same command again.

You should see something like the follow as the output of each:
"advertise_services"
["/simple/hello"]
["/simple/echo"]

Now we need to run a mapper. Mappers can be run from within your Merb or Rails app, from
an interactive irb shell, or from the command line. For this example we'll run it from the
Expand Down Expand Up @@ -237,7 +237,7 @@ This assumes that you have the agents running as indicated in the example above.

$ cd nanite
$ ./bin/nanite-mapper -i -u mapper -p testing -v /nanite
>> Nanite.request('/simple/hello') {|res| p res }
>> Nanite.request('/simple/echo') {|res| p res }

By default this will dispatch to the agent with the lowest reported load average.

Expand Down
25 changes: 24 additions & 1 deletion examples/cli.rb
Expand Up @@ -4,13 +4,36 @@
require 'nanite'
require 'nanite/mapper'

# cli.rb
#
# You will need to have run the bin/rabbitconf script at least one time before you
# run this so the expected users and vhosts are in place in RabbitMQ.
#
# You should also have started the 'simpleagent' nanite in a separate shell by running:
#
# cd <NANITE>/examples/simpleagent
# nanite
#
# This test script takes a little more than 16 seconds to run since we start a new
# mapper within, and pause while we wait for it to initialize, receive pings from
# available agents (which have a default ping time of 15 seconds), and register
# those agents and their methods. When this process is presumed complete after
# 16 seconds we can finally send the nanite agent the task to execute.

Nanite.identity = Nanite.gensym

EM.run {
AMQP.start(:host => 'localhost', :user => 'mapper', :pass => 'testing', :vhost => '/nanite')

# start up a new mapper with a ping time of 15 seconds
Nanite.mapper = Nanite::Mapper.new(15)

# have this run after 16 seconds so we can be pretty sure that the mapper
# has already received pings from running nanites and registered them.
EM.add_timer(16) do
Nanite.request("/simple/hello", '') do |res|

# call our /simple/echo nanite, and pass it a string to echo back
Nanite.request("/simple/echo", "hello world!") do |res|
p res
EM.stop_event_loop
end
Expand Down
8 changes: 5 additions & 3 deletions examples/simpleagent/actors/simple.rb
@@ -1,9 +1,11 @@
# you can execute this nanite from the cli.rb command line example app

class Simple < Nanite::Actor

expose :hello
expose :echo

def hello(payload)
"hello nanite"
def echo(payload)
"Nanite said #{payload.empty? ? "nothing at all" : payload} @ #{Time.now.to_s}"
end

end
Expand Down

0 comments on commit 1017b0a

Please sign in to comment.