Class: Cwmp::Handler

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

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Handler

Returns a new instance of Handler.



46
47
48
# File 'lib/cwmp/acs.rb', line 46

def initialize obj
    @acs = obj
end

Instance Method Details

#call(env) ⇒ Object



50
51
52
53
54
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
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
# File 'lib/cwmp/acs.rb', line 50

def call(env)
    req = Rack::Request.new(env)
    len = req.content_length.to_i

    begin
        if len == 0
            message_type = ''
        else
            body = req.body.read
            mess = Cwmp::Message::parse_from_text body
            message_type = mess.message_type
        end

        if message_type == "Inform"
            doc = mess.parsed_xml_doc
            manufacturer = doc.css("Manufacturer").text
            oui = doc.css("OUI").text
            serial_number = doc.css("SerialNumber").text
            event_codes = doc.css("EventCode").map { |n| n.text }
            parameters = {}
            doc.css("ParameterValueStruct").map { |it| parameters[it.element_children[0].text] = it.element_children[1].text }

            ck = Digest::MD5.hexdigest("#{serial_number}#{Time.new.to_i}")

            if !@acs.cpes.has_key? serial_number
                cpe = AcsCpe.new serial_number
                cpe.conn_req = parameters['InternetGatewayDevice.ManagementServer.ConnectionRequestURL']
                cpe.session_cookie = ck
                @acs.cpes[serial_number] = cpe
            end
            puts "got Inform from #{req.ip}:#{req.port} [sn #{serial_number}] with eventcodes #{event_codes.join(", ")}"

            inform_response = Cwmp::Message::inform_response
            response = Rack::Response.new inform_response.xml, 200, {'Connection' => 'Keep-Alive', 'Server' => 'ruby-cwmp'}
            response.set_cookie("sessiontrack", {:value => ck, :path => "/", :expires => Time.now+24*60*60})
            response.finish
        elsif message_type == "TransferComplete"
            puts "got TransferComplete"
        else
            cookie = req.cookies['sessiontrack']
            @acs.cpes.each do |k,c|
                cpe = c if c.session_cookie = cookie
            end

            if len == 0
                puts "got Empty Post"
            else
                puts "got #{message_type}"
                doc = mess.parsed_xml_doc
                case message_type
                    when "GetParameterValuesResponse"
                        doc.css("ParameterValueStruct").each do |node|
                            puts "#{node.element_children[0].text}: #{node.element_children[1].text}"
                        end
                    when "Fault"
                        puts "#{doc.css("faultstring").text}: #{doc.css("FaultString").text}"
                end

                if cpe.req_currently_in_service != nil
                    cpe.req_currently_in_service.cb.call(mess)
                end
            end


            # Got Empty Post or a Response. Now check for any event to send, otherwise 204
            if cpe.queue.size > 0
                m = cpe.queue.pop
                cpe.req_currently_in_service = m
                response = Rack::Response.new m.message, 200, {'Connection' => 'Keep-Alive', 'Server' => 'ruby-cwmp'}
                response.finish
            else
                puts "sending 204"
                [204, {"Connection" => "Close", 'Server' => 'ruby-cwmp'}, ""]
            end
        end
    rescue Exception => e
        puts e
        puts e.backtrace
        raise e
    end
end