Class: Librr::CmdServer::CmdServerHandler

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::HttpServer, Logger::ClassLogger
Defined in:
lib/librr/cmd_server.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger::ClassLogger

#debug, #info, #logger

Class Method Details

.set_server(server) ⇒ Object



27
28
29
# File 'lib/librr/cmd_server.rb', line 27

def self.set_server(server)
  @@server = server
end

Instance Method Details

#handle_cmd(params) ⇒ Object



41
42
43
44
45
46
47
48
49
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
# File 'lib/librr/cmd_server.rb', line 41

def handle_cmd(params)
  self.debug "on receive: #{params.to_s}"
  case params['cmd']
  when 'ping'
    'pong'

  when 'stop'
    self.info "daemon stopping.."
    EM.next_tick{
      EM.stop
    }

  when 'restart'
    self.info "daemon restarting.."
    EM.next_tick{
      EM.stop
      # todo
    }

  when 'add'
    EM.next_tick{
      @@server.monitor.add_directory(params['dir'])
    }

  when 'remove'
    EM.next_tick{
      @@server.monitor.remove_directory(params['dir'])
    }

  when 'list'
    @@server.monitor.dirs.to_a

  when 'reindex'
    EM.next_tick{
      @@server.monitor.reindex
    }

  when 'search'
    @@server.indexer.search(
                     params['text'],
                     rows: params['rows'],
                     all: params['all'],
                     location: params['location'],
                     )

  else
    raise Exception, "cmd unknown: #{params['cmd']}"

  end
end

#process_http_requestObject



31
32
33
34
35
36
37
38
39
# File 'lib/librr/cmd_server.rb', line 31

def process_http_request
  # puts @http_request_uri
  response = EM::DelegatedHttpResponse.new(self)
  response.status = 200
  response.content_type 'application/json'
  params = Rack::Utils.parse_nested_query(@http_post_content)
  response.content = JSON.dump(self.handle_cmd(params))
  response.send_response
end