Class: P2p2::P1Worker

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

Instance Method Summary collapse

Constructor Details

#initialize(p2pd_host, p2pd_port, room, appd_host, appd_port, dst_chunk_dir, tund_chunk_dir) ⇒ P1Worker

initialize



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

def initialize( p2pd_host, p2pd_port, room, appd_host, appd_port, dst_chunk_dir, tund_chunk_dir )
  @p2pd_addr = Socket.sockaddr_in( p2pd_port, p2pd_host )
  @room = room
  @appd_addr = Socket.sockaddr_in( appd_port, appd_host )
  @dst_chunk_dir = dst_chunk_dir
  @tund_chunk_dir = tund_chunk_dir
  @custom = P2p2::P1Custom.new
  @mutex = Mutex.new
  @reads = []
  @writes = []
  @roles = {}     # sock => :dotr / :dst / :tund
  @dst_infos = {} # dst => {}

  dotr, dotw = IO.pipe
  @dotw = dotw
  add_read( dotr, :dotr )
  new_a_tund
end

Instance Method Details

#loopingObject

looping



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
# File 'lib/p2p2/p1_worker.rb', line 29

def looping
  puts "#{ Time.new } looping"
  loop_heartbeat
  loop_check_status

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

    @mutex.synchronize do
      # 先写,再读
      ws.each do | sock |
        case @roles[ sock ]
        when :dst
          write_dst( sock )
        when :tund
          write_tund( sock )
        end
      end

      rs.each do | sock |
        case @roles[ sock ]
        when :dotr
          read_dotr( sock )
        when :dst
          read_dst( sock )
        when :tund
          read_tund( sock )
        end
      end
    end
  end
rescue Interrupt => e
  puts e.class
  quit!
end

#quit!Object

quit!



68
69
70
71
72
73
74
75
76
77
# File 'lib/p2p2/p1_worker.rb', line 68

def quit!
  if !@tund.closed? && @tund_info[ :tun_addr ]
    # puts "debug1 send tund fin"
    data = [ 0, TUND_FIN ].pack( 'Q>C' )
    @tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
  end

  # puts "debug1 exit"
  exit
end