Monitors on data structures

Hi

I am trying to use monitors for the first time in ruby. I am familiar
with the concept but am having some problems making things work.

Background: I have a program that uses nmap-parser to parse large nmap
scans and uses the call back to deliver results for each host. The call
back is threaded.

I want to build lists of IP addresses for further investigation from the
callback routine. I have made a class AddressList:

class AddressList < Array
include MonitorMixin

def initialise # redundant ?
super
end

def <<(addr)
synchronise do # make sure we don’t clobber another thread
super( addr )
end
end

end

we need to protect the push code so that to threads don’t collide so I
put a synchronise block around it.

What appears to happen is that the code never exits the synchronise
block.

puts before the block get executed but ones after do not and nothing is
ever added to the list.

Clearly I’m missing something.

The callback routine:

callback = proc do |host|

  return unless host.status == 'up'
  host.scripts do |script|
    s = script.output.gsub(/\n/, ':')
    next if s =~ / 2008 R2|ERROR/;
    next unless s =~ /Vista|2008/
    $addresses << host.addr
  end # scripts
  schedule if $addresses.size > BATCH_SIZE
end

yes, I’m looking for machines running smb2 to get them before the worm
does :slight_smile:

BTW I do have this program working without treads but I want to know why
this does not work !

Russell

On Thu, 01 Oct 2009 08:22:26 +0900, Russell F. wrote:

callback routine. I have made a class AddressList:
synchronise do # make sure we don’t clobber another thread
block.
return unless host.status == ‘up’
does :slight_smile:

BTW I do have this program working without treads but I want to know why
this does not work !

Russell

synchronize is spelled with a z. You misspelled it in the example you
posted here. Is it possible that you misspelled it in your actual code,
and the NoMethodError is being raised in a place where it doesn’t
generate output to alert you to the problem?

Ken B. wrote:

On Thu, 01 Oct 2009 08:22:26 +0900, Russell F. wrote:

callback routine. I have made a class AddressList:
synchronise do # make sure we don’t clobber another thread
block.
return unless host.status == ‘up’
does :slight_smile:

BTW I do have this program working without treads but I want to know why
this does not work !

Russell

synchronize is spelled with a z. You misspelled it in the example you
posted here. Is it possible that you misspelled it in your actual code,
and the NoMethodError is being raised in a place where it doesn’t
generate output to alert you to the problem?

Of course. American spelling, sigh…

Yup! that fixed it! Thanks very much Ken

Need a “pragma: Ex British Empire” :slight_smile: :slight_smile:

I wonder why the NoMethodError does not appear?

On Fri, 02 Oct 2009 07:49:40 +0900, Russell F. wrote:

why this does not work !

Yup! that fixed it! Thanks very much Ken

Need a “pragma: Ex British Empire” :slight_smile: :slight_smile:

I wonder why the NoMethodError does not appear?

I guess threads just supress them in general.

irb(main):001:0> Thread.start{raise NoMethodError}
=> #<Thread:0x7fe5ee8ce930 dead>
irb(main):002:0>

On 2 oct. 2009, at 05:08, Ken B. wrote:

I guess threads just supress them in general.

irb(main):001:0> Thread.start{raise NoMethodError}
=> #<Thread:0x7fe5ee8ce930 dead>
irb(main):002:0>

irb(main):001:0> Thread.start { raise NoMethodException }
=> #<Thread:0x100577848 dead>

irb(main):002:0> Thread.abort_on_exception = true
=> true

irb(main):003:0> Thread.start { raise NoMethodException }
(irb):3:in irb_binding': uninitialized constant NoMethodException (NameError) from (irb):3:instart’
from (irb):3:in irb_binding' from /opt/local/lib/ruby/1.8/irb/workspace.rb:52:inirb_binding’
from /opt/local/lib/ruby/1.8/irb/workspace.rb:52

On 2 oct. 2009, at 10:25, Luc H. wrote:

(irb):3:in `irb_binding’: uninitialized constant NoMethodException
(NameError)

Interesting, I somewhat managed to screw up the name of the exception,
which thankfully causes another exception, still demonstrating the
usefulness of Thread.abort_on_exception :slight_smile:

On Fri, Oct 2, 2009 at 1:25 AM, Luc H. [email protected] wrote:

=> #<Thread:0x100577848 dead>
from /opt/local/lib/ruby/1.8/irb/workspace.rb:52

Or you can save the reference to the thread and join it:

irb(main):003:0> t = Thread.start{raise NoMethodError}
=> #<Thread:0x7f5ee476a058 dead>
irb(main):004:0> t.join
NoMethodError: NoMethodError
from (irb):3
from (irb):4:in `join’
from (irb):4
from :0

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs