Class: Probe::ThreadExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/domain-probe/thread_executor.rb

Constant Summary collapse

MAX_RECURSIVE_QUERY_DEPTH =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 10) ⇒ ThreadExecutor




13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/domain-probe/thread_executor.rb', line 13

def initialize(size = 10)
  @a_records     = []
  @aaaa_records  = []
  @cname_records = []
  @mx_records    = []
  @mr_records    = []
  @txt_records   = []
  @hinfo_records = []
  @srv_records   = []
  @spf_records   = []
  @soa_records   = []
  @ns_records    = []
  @mutex         = Mutex.new
  @jobs          = Queue.new
  @workers       = start_threads(size)
end

Instance Attribute Details

#a_recordsObject

Returns the value of attribute a_records.



30
31
32
# File 'lib/domain-probe/thread_executor.rb', line 30

def a_records
  @a_records
end

#aaaa_recordsObject

Returns the value of attribute aaaa_records.



31
32
33
# File 'lib/domain-probe/thread_executor.rb', line 31

def aaaa_records
  @aaaa_records
end

#cname_recordsObject

Returns the value of attribute cname_records.



32
33
34
# File 'lib/domain-probe/thread_executor.rb', line 32

def cname_records
  @cname_records
end

#domainObject

Returns the value of attribute domain.



42
43
44
# File 'lib/domain-probe/thread_executor.rb', line 42

def domain
  @domain
end

#hinfo_recordsObject

Returns the value of attribute hinfo_records.



36
37
38
# File 'lib/domain-probe/thread_executor.rb', line 36

def hinfo_records
  @hinfo_records
end

#jobsObject

Returns the value of attribute jobs.



43
44
45
# File 'lib/domain-probe/thread_executor.rb', line 43

def jobs
  @jobs
end

#mr_recordsObject

Returns the value of attribute mr_records.



34
35
36
# File 'lib/domain-probe/thread_executor.rb', line 34

def mr_records
  @mr_records
end

#mutexObject

Returns the value of attribute mutex.



41
42
43
# File 'lib/domain-probe/thread_executor.rb', line 41

def mutex
  @mutex
end

#mx_recordsObject

Returns the value of attribute mx_records.



33
34
35
# File 'lib/domain-probe/thread_executor.rb', line 33

def mx_records
  @mx_records
end

#ns_recordsObject

Returns the value of attribute ns_records.



38
39
40
# File 'lib/domain-probe/thread_executor.rb', line 38

def ns_records
  @ns_records
end

#soa_recordsObject

Returns the value of attribute soa_records.



40
41
42
# File 'lib/domain-probe/thread_executor.rb', line 40

def soa_records
  @soa_records
end

#spf_recordsObject

Returns the value of attribute spf_records.



39
40
41
# File 'lib/domain-probe/thread_executor.rb', line 39

def spf_records
  @spf_records
end

#srv_recordsObject

Returns the value of attribute srv_records.



37
38
39
# File 'lib/domain-probe/thread_executor.rb', line 37

def srv_records
  @srv_records
end

#txt_recordsObject

Returns the value of attribute txt_records.



35
36
37
# File 'lib/domain-probe/thread_executor.rb', line 35

def txt_records
  @txt_records
end

#workersObject

Returns the value of attribute workers.



44
45
46
# File 'lib/domain-probe/thread_executor.rb', line 44

def workers
  @workers
end

Instance Method Details

#execute_util_finishedObject



64
65
66
67
68
69
70
71
# File 'lib/domain-probe/thread_executor.rb', line 64

def execute_util_finished
  
   self.workers.length.times do
     post_task { throw :done }
   end
   
   self.workers.map(&:join)
end

#post_result(record, zone) ⇒ Object



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
# File 'lib/domain-probe/thread_executor.rb', line 79

def post_result record,zone
  
  return unless record && record.kind_of?(Net::DNS::RR)
   #A little overhead here
   self.mutex.synchronize {
     record_hash = record.to_hash(zone) 
     case record.type
     when "A" 
       self.a_records << record_hash unless self.a_records.include? record_hash
     when "AAAA"
       self.aaaa_records << record_hash unless self.aaaa_records.include? record_hash
     when "CNAME"
       self.cname_records << record_hash unless self.cname_records.include? record_hash               
     when "NS"
       self.ns_records << record_hash unless self.ns_records.include? record_hash
     when "MX"
       self.mx_records << record_hash unless self.mx_records.include? record_hash
     when "MR"
       self.mr_records << record_hash unless self.mr_records.include? record_hash 
     when "TXT"
       self.txt_records << record_hash unless self.txt_records.include? record_hash
     when "SRV"
       self.srv_records << record_hash unless self.srv_records.include? record_hash
     when "HINFO"
       self.hinfo_records << record_hash unless self.hinfo_records.include? record_hash 
     when "SPF"
       self.spf_records << record_hash unless self.spf_records.include? record_hash 
     when "SOA"
       self.soa_records << record_hash unless self.soa_records.include? record_hash 
     else
       #nothing to do?
     end
   }
end

#post_task(*args, &block) ⇒ Object



75
76
77
# File 'lib/domain-probe/thread_executor.rb', line 75

def post_task(*args, &block)
  @jobs << [block, args] if block_given?
end

#start_threads(size = 10) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/domain-probe/thread_executor.rb', line 46

def start_threads(size = 10)
 
 raise "Live threads are still there" if self.workers && !self.workers.select{|t| t.status}.empty?
  
 self.workers       = Array.new(size) do |i|
     Thread.new do
       catch(:done) do
         loop do
             job, args = self.jobs.pop
             job.call(*args)
         end
       end
     end
   end
end

#submit_task(domain, type, zone, nameservers) ⇒ Object



116
117
118
119
120
121
# File 'lib/domain-probe/thread_executor.rb', line 116

def submit_task(domain,type,zone,nameservers)
    post_task(domain,type,zone,nameservers) { |domain,type,zone,nameservers|
        depth = 0
        resolve_recursive(domain,type,zone,nameservers,depth)
    }
end