Class: P3

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

Constant Summary collapse

P3_START =
'~'.ord
P3_END =
'~'.ord
P3_ESC =
'#'.ord

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, &block) ⇒ P3

Returns a new instance of P3.



13
14
15
16
17
18
19
20
# File 'lib/p3.rb', line 13

def initialize(hash={},&block)
  @p3mode=false
  @p3start=0
  @p3buf=[]
  @p3esc=false
  @clients={}
  @block=block
end

Instance Method Details

#inchar(ch) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/p3.rb', line 153

def inchar ch
  #puts "got:#{ch}"
  if ch==P3_START and not @p3esc and not @p3mode
    @p3mode=true
    @p3start=Time.now
    @p3buf=[]
  elsif ch==P3_END and not @p3esc and @p3mode
    @p3mode=false
    p3_packet_in @p3buf
    @p3buf=[]
  else
    if not @p3mode
      return false #not done, please process .. show to user
    else
      if @p3esc
        @p3esc=false
      elsif ch==P3_ESC
        @p3esc=true
        return
      end
      @p3buf<<ch
    end
  end
  return true #char was done -- do not process
end

#p3_packet_in(buf) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/p3.rb', line 89

def p3_packet_in buf
  #puts "packet in #{buf}"
  pac= unpack buf
  return if not pac
  pp pac
  pp @clients
  if pac[:proto]=="U"
    if not @clients[pac[:mac]]
      puts "new client #{pac[:mac]}"
      @clients[pac[:mac]]={socket: UDPSocket.new,created:Time.now,count_r:0, count_s:0}
      @clients[pac[:mac]][:thread]=Thread.new(pac[:mac]) do |my_mac|
        loop do
          begin
            r,stuff=@clients[my_mac][:socket].recvfrom(2000) #get_packet --high level func!
            ip=stuff[2]
            port=stuff[1]
            #puts "got reply '#{r}' from server #{ip}:#{port} to our mac #{my_mac}"
            pac={
              proto:'U',
              mac: my_mac,
              ip: ip,
              port:port,
              data:r,
            }
            #pp pac
            # received return packet from server!
            if @block
              @block.call pac
            end
            #$sp.write pack pac
            @clients[my_mac][:last_r]=Time.now
            @clients[my_mac][:count_r]+=1
           rescue => e
            puts "thread dies..."
            p e
            p e.backtrace
          end
        end
      end
      pp @clients
    end
    @clients[pac[:mac]][:socket].send(pac[:data].pack("C*"), 0, pac[:ip], pac[:port])
    _,port,_,_ = @clients[pac[:mac]][:socket].addr
    @clients[pac[:mac]][:gw_port]=port
    @clients[pac[:mac]][:last_s]=Time.now
    @clients[pac[:mac]][:count_s]+=1
    pp @clients
  end
end

#pack(pac) ⇒ Object

builds p3 packet from pac object



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

def pack pac #builds p3 packet from  pac object
  #pp pac
  begin
    buf=[]
    buf<<pac[:proto].ord
    macs=pac[:mac].split(":")
    buf<<macs[0].to_i(16)
    buf<<macs[1].to_i(16)
    buf<<0
    buf<<0
    ips=pac[:ip].split(".")
    buf<<ips[3].to_i
    buf<<ips[2].to_i
    buf<<ips[1].to_i
    buf<<ips[0].to_i
    buf<<pac[:port]/0x100
    buf<<(pac[:port]&0xff)
    buf<<pac[:data].size
    buf+=pac[:data].unpack("C*")
    check=0
    buf.each do |b|
      check^=b
    end
    buf<<check
    #pp buf
    #pp "~#{buf.pack("C*")}~"
    return "~#{buf.pack("C*")}~"
  rescue => e
    p e
    p e.backtrace
  end
  return nil
end

#shutdownObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/p3.rb', line 139

def shutdown
  @clients.each do |k,c|
    if c[:socket]
      c[:socket].close
      puts "closed #{k}: #{c[:socket]}"
    end
    if c[:thread]
      c[:thread].kill
      puts "killed #{k}: #{c[:thread]}"
    end
  end
  @clients={}
end

#unpack(buf) ⇒ Object

buils object from packet byte array



22
23
24
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
# File 'lib/p3.rb', line 22

def unpack buf #buils object from packet byte array
  blen=buf.size
  #puts "GOT: buf<#{buf}> buflen=#{buf.size}"
  return if blen<4
  check2=0
  buf[0...blen-1].each do |b|
    check2^=b
  end
  i=0
  proto=buf[i].chr
  i+=1
  mac=sprintf "%02X:%02X",buf[i],buf[i+1]
  i+=4
  ip=sprintf "%d.%d.%d.%d",buf[i+3],buf[i+2],buf[i+1],buf[i]
  i+=4
  port=buf[i]*0x100+buf[i+1]
  i+=2
  len=buf[i]
  i+=1
  check=buf[i+len]
  data=buf[i...i+len]
  pac={
    proto:proto,
    mac: mac,
    ip: ip,
    port:port,
    data:data,
  }
  #puts "proto=#{proto},mac=#{mac},ip=#{ip},socket=#{socket},len=#{len},check=#{check}==#{check2},data='#{data.pack("C*")}'"
end