Class: Sfp::Agent::Handler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/sfpagent/agent.rb

Overview

 A class that handles each request.

Instance Method Summary collapse

Constructor Details

#initialize(server, logger) ⇒ Handler

Returns a new instance of Handler.



506
507
508
# File 'lib/sfpagent/agent.rb', line 506

def initialize(server, logger)
  @logger = logger
end

Instance Method Details

#do_GET(request, response) ⇒ Object

Process HTTP Get request

uri: /pid => save daemon’s PID to a file /state => return the current state /model => return the current model /schemata => return the schemata of a module /modules => return a list of available modules



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/sfpagent/agent.rb', line 519

def do_GET(request, response)
  status = 400
  content_type, body = ''
  if not trusted(request.peeraddr[2])
    status = 403
  else
    path = (request.path[-1,1] == '/' ? request.path.chop : request.path)
    if path == '/pid' and (request.peeraddr[2] == 'localhost' or request.peeraddr[3] == '127.0.0.1')
      status, content_type, body = save_pid

    elsif path == '/state'
      status, content_type, body = get_state

    elsif path == '/sfpstate'
      status, content_type, body = get_state({:as_sfp => true})

    elsif path =~ /^\/state\/.+/
      status, content_type, body = get_state({:path => path[7, path.length-7]})

    elsif path =~ /^\/sfpstate\/.+/
      status, content_type, body = get_state({:path => path[10, path.length-10]})

    elsif path == '/model'
      status, content_type, body = get_model

    elsif path == '/bsig'
      status, content_Type, body = get_bsig

    elsif path =~ /^\/schemata\/.+/
      status, content_type, body = get_schemata({:module => path[10, path.length-10]})

    elsif path == '/modules'
      status, content_type, body = [200, 'application/json', JSON.generate(Sfp::Agent.get_modules)]

    elsif path == '/agents'
      status, content_type, body = [200, 'application/JSON', JSON.generate(Sfp::Agent.get_agents)]

    elsif path == '/log'
      status, content_type, body = [200, 'text/plain', Sfp::Agent.get_log(100)]

    end
  end

  response.status = status
  response['Content-Type'] = content_type
  response.body = body
end

#do_POST(request, response) ⇒ Object

 Handle HTTP Post request

uri: /execute => receive an action’s schema and execute it



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/sfpagent/agent.rb', line 572

def do_POST(request, response)
  status = 400
  content_type, body = ''
  if not self.trusted(request.peeraddr[2])
    status = 403
  else
    path = (request.path[-1,1] == '/' ? ryyequest.path.chop : request.path)
    if path == '/execute'
      status, content_type, body = self.execute({:query => request.query})
    end
  end

  response.status = status
  response['Content-Type'] = content_type
  response.body = body
end

#do_PUT(request, response) ⇒ Object

uri: /model => receive a new model and save to cached file /modules => save the module if parameter “module” is provided

delete the module if parameter "module" is not provided

/agents => save the agents’ list if parameter “agents” is provided

delete all agents if parameter "agents" is not provided


595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/sfpagent/agent.rb', line 595

def do_PUT(request, response)
  status = 400
  content_type, body = ''
  if not self.trusted(request.peeraddr[2])
    status = 403
  else
    path = (request.path[-1,1] == '/' ? ryyequest.path.chop : request.path)

    if path == '/model'
      status, content_type, body = self.set_model({:query => request.query})

    elsif path =~ /\/modules\/.+/
      status, content_type, body = self.manage_modules({:name => path[9, path.length-9],
                                                        :query => request.query})

    elsif path == '/modules'
      status, content_type, body = self.manage_modules({:delete => true})

    elsif path == '/agents'
      status, content_type, body = self.manage_agents({:query => request.query})

    elsif path == '/bsig'
      status, content_type, body = self.set_bsig({:query => request.query})

    elsif path == '/bsig/satisfier'
      status, content_type, body = self.satisfy_bsig_request({:query => request.query})

    end
  end

  response.status = status
  response['Content-Type'] = content_type
  response.body = body
end

#execute(p = {}) ⇒ Object



708
709
710
711
712
713
714
715
# File 'lib/sfpagent/agent.rb', line 708

def execute(p={})
  return [400, '', ''] if not p[:query].has_key?('action')
  begin
    return [200, '', ''] if Sfp::Agent.execute_action(JSON[p[:query]['action']])
  rescue
  end
  [500, '', '']
end

#get_bsig(p = {}) ⇒ Object



739
740
741
742
743
744
745
746
747
748
749
# File 'lib/sfpagent/agent.rb', line 739

def get_bsig(p={})
  bsig = Sfp::Agent.get_bsig

  # The BSig model is not exist
  return [404, '', ''] if bsig.nil?

  # The BSig model is exist, and then send it in JSON
  return [200, 'application/json', JSON.generate(bsig)] if !!bsig

  [500, '', '']
end

#get_modelObject



695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/sfpagent/agent.rb', line 695

def get_model
  model = Sfp::Agent.get_model

  # The model is not exist.
  return [404, '', ''] if model.nil?

  # The model is exist, and then send it in JSON.
  return [200, 'application/json', JSON.generate(model)] if !!model

  # There is an error when retrieving the model!
  [500, '', '']
end

#get_schemata(p = {}) ⇒ Object



657
658
659
660
661
662
663
664
665
# File 'lib/sfpagent/agent.rb', line 657

def get_schemata(p={})
  begin
    module_name, _ = p[:module].split('/', 2)
    return [200, 'application/json', Sfp::Agent.get_schemata(module_name)]
  rescue Exception => e
    @logger.error "Sending schemata [Failed]\n#{e}"
  end
  [500, '', '']
end

#get_state(p = {}) ⇒ Object



667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/sfpagent/agent.rb', line 667

def get_state(p={})
  state = Sfp::Agent.get_state(!!p[:as_sfp])

  # The model is not exist.
  return [404, 'text/plain', 'There is no model!'] if state.nil?

  if !!state
    state = state.at?("$." + p[:path].gsub(/\//, '.')) if !!p[:path]
    return [200, 'application/json', JSON.generate({'state'=>state})]
  end

  # There is an error when retrieving the state of the model!
  [500, '', '']
end

#manage_agents(p = {}) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/sfpagent/agent.rb', line 630

def manage_agents(p={})
  begin
    if p[:query].has_key?('agents')
      return [200, '', ''] if Sfp::Agent.set_agents(JSON[p[:query]['agents']])
    else
      return [200, '', ''] if Sfp::Agent.set_agents({})
    end
  rescue Exception => e
    @logger.error "Saving agents list [Failed]\n#{e}\n#{e.backtrace.join("\n")}"
  end
  [500, '', '']
end

#manage_modules(p = {}) ⇒ Object



643
644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/sfpagent/agent.rb', line 643

def manage_modules(p={})
  if p[:delete]
    return [200, '', ''] if Sfp::Agent.uninstall_all_modules
  else
    p[:name], _ = p[:name].split('/', 2)
    if p[:query].has_key?('module')
      return [200, '', ''] if Sfp::Agent.install_module(p[:name], p[:query]['module'])
    else
      return [200, '', ''] if Sfp::Agent.uninstall_module(p[:name])
    end
  end
  [500, '', '']
end

#satisfy_bsig_request(p = {}) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/sfpagent/agent.rb', line 751

def satisfy_bsig_request(p={})
  return [400, '', ''] if not p[:query]

Sfp::Agent.logger.info Sfp::Agent.bsig_engine.to_s

  return [500, '', ''] if Sfp::Agent.bsig_engine.nil?

  req = p[:query]
  return [200, '', ''] if Sfp::Agent.bsig_engine.receive_goal_from_agent(req['id'].to_i, JSON[req['goal']], req['pi'].to_i)

  [500, '', '']
end

#save_pidObject



717
718
719
720
721
722
723
724
# File 'lib/sfpagent/agent.rb', line 717

def save_pid
  begin
    File.open(PIDFile, 'w', 0644) { |f| f.write($$.to_s) }
    return [200, '', $$.to_s]
  rescue Exception
  end
  [500, '', '']
end

#set_bsig(p = {}) ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/sfpagent/agent.rb', line 726

def set_bsig(p={})
  if p[:query] and p[:query].has_key?('bsig')
    # If setting the BSig model was success, then return '200' status
    return [200, '', ''] if Sfp::Agent.set_bsig(JSON[p[:query]['bsig']])
  else
    # Deleting the existing BSig model by setting with Nil, if it's success then return '200' status.
    return [200, '', ''] if Sfp::Agent.set_bsig(nil)
  end

  # There is an error on setting/deleting the BSig model
  [500, '', '']
end

#set_model(p = {}) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/sfpagent/agent.rb', line 682

def set_model(p={})
  if p[:query] and p[:query].has_key?('model')
    # If setting the model was success, then return '200' status.
    return [200, '', ''] if Sfp::Agent.set_model(JSON[p[:query]['model']])
  else
    # Removing the existing model by setting an empty model, if it's success then return '200' status.
    return [200, '', ''] if Sfp::Agent.set_model({})
  end

  # There is an error on setting the model!
  [500, '', '']
end

#trusted(address) ⇒ Object



764
765
766
# File 'lib/sfpagent/agent.rb', line 764

def trusted(address)
  true
end