-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodem_web_server.rb
More file actions
executable file
·79 lines (60 loc) · 1.93 KB
/
Copy pathmodem_web_server.rb
File metadata and controls
executable file
·79 lines (60 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
require 'webrick'
require 'SMS.rb'
include WEBrick
# The modem servlet responds to request to send a message:
# It adds the message to the modem communicator threads
# queue.
class ModemServlet < HTTPServlet::AbstractServlet
attr_accessor :mountpoint
def initialize(server, modem_communicator)
super(server)
@modem_communicator, @mountpoint = modem_communicator, @modem_communicator.name
end
def do_GET(req, res)
errors, res.body = [], ""
@logger.info "http query params: " + req.query.inspect + "\n" unless req.query['debug'].nil?
sms = SMS.new(req.query['message'].to_s, @modem_communicator.name, req.query['to'].to_s)
if (sms.message.nil? or sms.message.empty?)
errors << "empty message"
end
if (sms.to.nil? or sms.to.empty?)
errors << "empty to field"
end
# Attempt to add to queue:
begin
@modem_communicator.send_sms(sms) if errors.empty?
rescue Exception => e
@logger.error e.to_s
errors << e.to_s
end
if errors.empty?
status = "<status>OK</status>\n"
else
status = "<status>FAILED</status>\n"
end
error_string = ""
errors.each { |e| error_string << "<error>#{e}</error>\n" }
res.body = "<send-response>\n" + status + error_string + "</send-response>\n"
res['Content-Type'] = "text/html"
raise HTTPStatus::OK
end
alias :do_POST :do_GET # let's accept POST request too.
end
class ModemWebServer
def initialize(config, logger)
@config, @logger = config, logger
@s = HTTPServer.new(:Port => @config['http-port'].to_s, :Logger => @logger)
end
def connect(modem_communicator)
mountpoint = "/" + modem_communicator.name
@logger.info "mounting servlet at " + mountpoint
@s.mount mountpoint, ModemServlet, modem_communicator
end
def start
@logger.info "Starting Modem Web Server"
@s.start
end
def shutdown
@s.shutdown
end
end