Class: P2p2::P2Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/p2p2/p2_worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(paird_host, paird_port, title, shadow_host, shadow_port) ⇒ P2Worker

initialize



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/p2p2/p2_worker.rb', line 7

def initialize( paird_host, paird_port, title, shadow_host, shadow_port )
  @paird_host = paird_host
  @paird_port = paird_port
  @title = title
  @shadow_addr = Socket.sockaddr_in( shadow_port, shadow_host )
  @reads = []
  @writes = []
  @roles = {} # sock => :dotr / :shadow / :ctl / :tun / :src
  @src_infos = ConcurrentHash.new
  @tun_infos = ConcurrentHash.new

  new_a_pipe
  new_a_shadow
end

Instance Method Details

#loopingObject

looping



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/p2p2/p2_worker.rb', line 25

def looping
  puts "#{ Time.new } looping"
  loop_check_state

  loop do
    rs, ws = IO.select( @reads, @writes )

    rs.each do | sock |
      role = @roles[ sock ]

      case role
      when :dotr then
        read_dotr( sock )
      when :shadow then
        read_shadow( sock )
      when :src then
        read_src( sock )
      when :ctl then
        read_ctl( sock )
      when :tun then
        read_tun( sock )
      else
        puts "#{ Time.new } read unknown role #{ role }"
        close_sock( sock )
      end
    end

    ws.each do | sock |
      role = @roles[ sock ]

      case role
      when :tun then
        write_tun( sock )
      when :src then
        write_src( sock )
      else
        puts "#{ Time.new } write unknown role #{ role }"
        close_sock( sock )
      end
    end
  end
rescue Interrupt => e
  puts e.class
  quit!
end

#quit!Object

quit!



74
75
76
77
# File 'lib/p2p2/p2_worker.rb', line 74

def quit!
  # puts "debug exit"
  exit
end