Class: MappingServer::StatefulProtocol

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
EM::Protocols::LineText2
Defined in:
lib/MINT-core/manager/mapping_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatefulProtocol

Returns a new instance of StatefulProtocol.



15
16
17
18
19
20
21
# File 'lib/MINT-core/manager/mapping_server.rb', line 15

def initialize
  @mapping_store = {}

  # saves if a registered mapping is detail
  @registered_as_detail = {}
  super()
end

Instance Attribute Details

#managerObject

Returns the value of attribute manager.



13
14
15
# File 'lib/MINT-core/manager/mapping_server.rb', line 13

def manager
  @manager
end

Instance Method Details

#forward_callback(mapping, data) ⇒ Object



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/MINT-core/manager/mapping_server.rb', line 23

def forward_callback(mapping,data)
  p "in forward #{mapping} #{data}"

  if (data[:result])
    d = data[:result]
    if not d.nil? and not d.empty? and not @mapping_store[mapping]
      #a= d.first
      #b = a[1]['name']
      if d.first[1].is_a? Hash
        @mapping_store[mapping] =  {:name => d.first[1]['name']}

      else

        @mapping_store[mapping] =  {:name => d.first[1].map{|x| x['name']}}
      end
    end
  end
  json_data = (@mapping_store[mapping]!=nil)?MultiJson.encode(data.merge(@mapping_store[mapping])) :  MultiJson.encode(data)
  if data[:mapping_state] and data[:mapping_state] == :succeeded
    send_data("INFO%"+mapping+"%"+json_data+"\r\n")
    p "sent: INFO%"+mapping+"%"+json_data
    @mapping_store[mapping]=nil
  elsif  @registered_as_detail[mapping]
    send_data("DETAIL%"+mapping+"%"+json_data+"\r\n")
    p "sent: DETAIL%"+mapping+"%"+json_data
    @mapping_store[mapping]=nil
  end

end

#receive_line(data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/MINT-core/manager/mapping_server.rb', line 53

def receive_line(data)
  begin
    p "server received:#{data}"
    d = data.split('%')

    case d[0]
      when "REGISTER"
        @manager.register_callback(d[1], method(:forward_callback))
        @registered_as_detail[d[1]] = d[2].eql?('DETAIL')?true:false
        send_data("REGISTERED%#{d[1]}%#{d[2]}\r\n")
      when "LIST"
        mappings = @manager.get_mappings
        mappings.each {|m| send_data("MAPPING%#{m}\r\n")}
      else
        p "ERROR\r\nReceived Unknown data:#{data}\r\n "
    end
  end
rescue Statemachine::TransitionMissingException => bang
  puts "ERROR\n#{bang}"
end