add http.cm and probe
This commit is contained in:
151
probe.ce
Normal file
151
probe.ce
Normal file
@@ -0,0 +1,151 @@
|
||||
// cell probe - Query a running probe server
|
||||
//
|
||||
// Usage:
|
||||
// cell probe List all targets and probes
|
||||
// cell probe <target> <name> Query a probe
|
||||
// cell probe <target> <name> k=v ... Query with arguments
|
||||
// cell probe --port=8080 game state Use a different port
|
||||
|
||||
var socket = use('socket')
|
||||
var json = use('json')
|
||||
|
||||
var host = "127.0.0.1"
|
||||
var port = 9000
|
||||
|
||||
def CRLF = "\r\n"
|
||||
|
||||
function request(method, path, body) {
|
||||
var fd = socket.socket("AF_INET", "SOCK_STREAM")
|
||||
var raw = null
|
||||
var hdr_end = null
|
||||
var _do = function() {
|
||||
socket.connect(fd, {address: host, port: port})
|
||||
var req = method + " " + path + " HTTP/1.1" + CRLF
|
||||
req = req + "Host: " + host + CRLF
|
||||
req = req + "Connection: close" + CRLF
|
||||
if (body != null) {
|
||||
req = req + "Content-Type: application/json" + CRLF
|
||||
req = req + "Content-Length: " + text(length(body)) + CRLF
|
||||
}
|
||||
req = req + CRLF
|
||||
if (body != null) req = req + body
|
||||
socket.send(fd, req)
|
||||
raw = text(socket.recv(fd, 65536))
|
||||
} disruption {
|
||||
raw = null
|
||||
}
|
||||
_do()
|
||||
socket.close(fd)
|
||||
if (raw == null) return null
|
||||
hdr_end = search(raw, CRLF + CRLF)
|
||||
if (hdr_end == null) return null
|
||||
return text(raw, hdr_end + 4)
|
||||
}
|
||||
|
||||
function print_targets(targets) {
|
||||
var keys = array(targets)
|
||||
var j = 0
|
||||
var p = 0
|
||||
var probes = null
|
||||
while (j < length(keys)) {
|
||||
probes = targets[keys[j]]
|
||||
log.console(keys[j])
|
||||
p = 0
|
||||
while (p < length(probes)) {
|
||||
log.console(" " + probes[p])
|
||||
p = p + 1
|
||||
}
|
||||
j = j + 1
|
||||
}
|
||||
}
|
||||
|
||||
function run() {
|
||||
var target = null
|
||||
var name = null
|
||||
var probe_args = {}
|
||||
var i = 0
|
||||
var eq = null
|
||||
var k = null
|
||||
var v = null
|
||||
var n = null
|
||||
|
||||
for (i = 0; i < length(args); i++) {
|
||||
if (args[i] == '--help' || args[i] == '-h') {
|
||||
log.console("Usage: cell probe [target] [name] [key=value ...]")
|
||||
log.console("")
|
||||
log.console(" cell probe List all targets and probes")
|
||||
log.console(" cell probe game state Query game/state")
|
||||
log.console(" cell probe game entity id=1 Query with arguments")
|
||||
log.console("")
|
||||
log.console("Options:")
|
||||
log.console(" --port=N Connect to port N (default 9000)")
|
||||
return
|
||||
} else if (starts_with(args[i], '--port=')) {
|
||||
port = number(text(args[i], 7))
|
||||
} else if (target == null) {
|
||||
target = args[i]
|
||||
} else if (name == null) {
|
||||
name = args[i]
|
||||
} else {
|
||||
eq = search(args[i], "=")
|
||||
if (eq != null) {
|
||||
k = text(args[i], 0, eq)
|
||||
v = text(args[i], eq + 1)
|
||||
n = number(v)
|
||||
if (n != null) {
|
||||
v = n
|
||||
} else if (v == "true") {
|
||||
v = true
|
||||
} else if (v == "false") {
|
||||
v = false
|
||||
} else if (v == "null") {
|
||||
v = null
|
||||
}
|
||||
probe_args[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var resp = null
|
||||
var body = null
|
||||
var data = null
|
||||
|
||||
if (target == null) {
|
||||
resp = request("GET", "/discover", null)
|
||||
} else {
|
||||
body = {target: target, name: name}
|
||||
if (length(array(probe_args)) > 0) body.args = probe_args
|
||||
resp = request("POST", "/probe", json.encode(body, false))
|
||||
}
|
||||
|
||||
if (resp == null) {
|
||||
log.error("could not connect to probe server on port " + text(port))
|
||||
return
|
||||
}
|
||||
|
||||
var _parse = function() {
|
||||
data = json.decode(resp)
|
||||
} disruption {
|
||||
data = null
|
||||
}
|
||||
_parse()
|
||||
|
||||
if (data == null) {
|
||||
log.error("invalid response from server")
|
||||
return
|
||||
}
|
||||
|
||||
if (!data.ok) {
|
||||
log.error(data.error)
|
||||
return
|
||||
}
|
||||
|
||||
if (target == null) {
|
||||
print_targets(data.targets)
|
||||
} else {
|
||||
log.console(json.encode(data.result, 2))
|
||||
}
|
||||
}
|
||||
run()
|
||||
|
||||
$stop()
|
||||
Reference in New Issue
Block a user