Mercurial > repos > icfp2013
annotate src/requests.tp @ 25:bb80f86c5048
Added code for sending and decoding the responses of evalRequests
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 10 Aug 2013 16:31:48 -0700 |
parents | e1109e33b796 |
children | 18a043613dae |
rev | line source |
---|---|
10
79c1db5e7ebd
sending broken file for mike to check error
William Morgan <bill@mrgn.org>
parents:
diff
changeset
|
1 #{ |
18 | 2 |
20 | 3 strJoin <- :str arr { |
4 acc <- "" | |
5 arr foreach: :i el { | |
6 if: i = 0 { | |
7 acc <- (string: el) | |
8 } else: { | |
9 acc <- acc . ", " . (string: el) | |
10 } | |
11 } | |
12 acc | |
13 //arr fold: "" with: :acc el {acc . el} | |
14 } | |
15 | |
18 | 16 quote <- :str { |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
17 "\"" . str . "\"" |
18 | 18 } |
19 | |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
20 println <- :str { |
23 | 21 print: str . "\n" |
22 } | |
23 | |
24 | |
25 | |
26 | |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
27 evalId <- :id args { |
12
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
28 #{ |
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
29 string <- { |
20 | 30 idStr <- (quote: "id") . ":" . (quote: id) |
31 argsStr <- (quote: "arguments") . ":" . (quote: (strJoin: "," args)) | |
18 | 32 "{" . idStr . "," . argsStr . "}" |
12
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
33 } |
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
34 } |
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
35 } |
10
79c1db5e7ebd
sending broken file for mike to check error
William Morgan <bill@mrgn.org>
parents:
diff
changeset
|
36 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
37 evalProgram <- :program args { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
38 args <- args map: :el { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
39 "\"0x" . (hex: el) . "\"" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
40 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
41 #{ |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
42 string <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
43 progStr <- (quote: "program") . ":" . (quote: program) |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
44 argsStr <- (quote: "arguments") . ":[" . (strJoin: "," args) . "]" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
45 "{" . progStr . "," . argsStr . "}" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
46 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
47 sendWithKey <- :key { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
48 print: "Sending: " . string . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
49 cli <- http client: "icfpc2013.cloudapp.net" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
50 resp <- cli post: string toPath: "/eval?auth=" . key withType: "application/json" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
51 evalResponse: resp |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
52 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
53 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
54 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
55 |
15
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
56 guessRequest <- :id :prog { |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
57 #{ |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
58 string <- { |
17
566960135ea1
Small bit of cleanup now that the string escaping bug is fixed.
Mike Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
59 idStr <- "\"id\":\"" . id . "\"" |
566960135ea1
Small bit of cleanup now that the string escaping bug is fixed.
Mike Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
60 progStr <- "\"program\":\"" . prog . "\"" |
566960135ea1
Small bit of cleanup now that the string escaping bug is fixed.
Mike Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
61 "{" . idStr . "," . progStr . "}" |
15
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
62 } |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
63 } |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
64 } |
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
65 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
66 evalResponse <- :httpResp { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
67 _code <- httpResp statusCode |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
68 bod <- httpResp body |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
69 print: "Response code: " . (string: _code) . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
70 print: bod . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
71 decoded <- json decode: bod |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
72 _status <- "error" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
73 if: _code = 200 { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
74 _status <- decoded get: "status" withDefault: "error" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
75 } else: { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
76 print: "bad status" |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
77 } |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
78 if: _status = "ok" { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
79 _outputs <- (decoded get: "outputs" withDefault: #[]) map: :num { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
80 (num from: 2) parseHex64 |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
81 } |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
82 #{ |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
83 status <- { "ok" } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
84 outputs <- { _outputs } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
85 string <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
86 str <- "OK:" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
87 foreach: _outputs :idx val { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
88 str <- str . "\n" . (string: idx) . ": 0x" . (hex: val) |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
89 } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
90 str |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
91 } |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
92 print <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
93 print: string . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
94 } |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
95 } |
24
e1109e33b796
some work that probably needs to be trashed on requests.tp
Mike Pavone <pavone@retrodev.com>
parents:
23
diff
changeset
|
96 } else: { |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
97 _message <- decoded get: "message" withDefault: "" |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
98 #{ |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
99 status <- { "error" } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
100 message <- { _message } |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
101 string <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
102 "Error: " . _message |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
103 } |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
104 print <- { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
105 print: string . "\n" |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
106 } |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
107 } |
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
108 } |
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
109 } |
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
110 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
111 main <- :args { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
112 print: ((evalId: "someId" #[1u64 2u64 3u64]) string) . "\n" |
15
18ec9131f594
one more compiler bug for mike.
William Morgan <bill@mrgn.org>
parents:
14
diff
changeset
|
113 print: ((guessRequest: "someId" "someProg") string) . "\n" |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
114 |
25
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
115 if: (args length) > 1 { |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
116 key <- args get: 1 |
bb80f86c5048
Added code for sending and decoding the responses of evalRequests
Mike Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
117 print: ((evalProgram: "(lambda (input) (shl1 input))" #[1u64 0xEFFFFFFFFFFFFFu64]) sendWithKey: key) |
22
a4837071b73d
some attempts at parsing an eval response
William Morgan <bill@mrgn.org>
parents:
21
diff
changeset
|
118 } |
12
7d8b8f82cbef
Help Bill work around some compiler bugs
Mike Pavone <pavone@retrodev.com>
parents:
10
diff
changeset
|
119 } |
10
79c1db5e7ebd
sending broken file for mike to check error
William Morgan <bill@mrgn.org>
parents:
diff
changeset
|
120 } |