Class: RNCP::NcpListener

Inherits:
Object
  • Object
show all
Includes:
Files, Networking
Defined in:
lib/rncp/listener.rb

Overview

Class used to listen for direct and Multicast/Broadcast connections, which then triggers the receving of data from the Pusher class.

Instance Method Summary collapse

Methods included from Files

#directory_list, #tar, #untar

Methods included from Networking

#bind_broadcast, #bind_multicast, #bind_tcp, #join_multicast

Constructor Details

#initializeNcpListener

Returns a new instance of NcpListener.



24
25
# File 'lib/rncp/listener.rb', line 24

def initialize
end

Instance Method Details

#listenObject

Listens for a direct connection from a source. Will wait until a connection is made and then decompresses files to current working directory.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rncp/listener.rb', line 30

def listen
  puts "[#] rncp Listener: creating new connection"
  l = bind_tcp
  puts "[*] waiting for connections"      

  sock, info = l.accept
  printf "[*] connection from %s:%s\n", info.ip_address, info.ip_port
  puts "[*] receiving..."

  data = ""
  while (sock.eof? == false)
    data += sock.gets()
  end

  untar data
  puts "[*] received: "

  puts "[#] finished"
  sock.close
  l.close
end

#pollObject

Waits for a Multicast or Broadcast message sent by a source and establishes a connection similar to #listen which then decompresses files to current working directory.



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
# File 'lib/rncp/listener.rb', line 55

def poll
  addr = nil
  msock = join_multicast
  bsock = bind_broadcast
  
  if msock.nil? == true && bsock.nil? == true
    puts "[!] cannot continue without atleast one announcement socket!"
    return 1
  end

  xsock = msock.nil? == false ? msock : bsock

  puts "[*] waiting for something-cast"
  loop do
    begin
      data, addr = xsock.recvfrom 1024
      if addr[1] == RNCP::PORT
        puts "[*] found pusher at #{addr[3]}:#{addr[1]}"
        puts "[#] Anouncement: #{data}"
        break
      else
        puts "[?] received garbase from #{addr}"
      end
    rescue Exception => e
      puts "exception #{e}"
    end # begin
  end #loop

  msock.close if msock.nil? == false
  bsock.close if bsock.nil? == false

  sock = TCPSocket::new addr[3], addr[1]
  sock.send "I am ready!", 0

  data = ""
  while (sock.eof? == false)
    data += sock.gets()
  end

  untar data
  puts "[*] received: "

  puts "[#] finished"
  sock.close
end