omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / node / tests / fake-serve.om
| 1 | note seed/node/tests/fake-serve.om -- a recorded completion server for the |
| 2 | note DRA-291 view tests: the real omnode-infer talks HTTP to it, but answers |
| 3 | note come from a map of recorded turns instead of a model. OM-2; compose |
| 4 | note after seed/net/http.om and seed/net/notls.om. |
| 5 | note fake-serve PORT MAP DELAY_MS |
| 6 | note MAP holds entries "<klen> <vlen>\n" then klen key bytes and vlen value |
| 7 | note bytes. Every answer comes DELAY_MS ms after the request was read: POST |
| 8 | note /v1/completions whose body contains a key gets 200 with that key's |
| 9 | note value as the body; any other request 404. |
| 10 | note One request at a time, 127.0.0.1 only. Prints PORT n, then one line |
| 11 | note per request: F status key-index ms. |
| 12 | text fsurl "/v1/completions" |
| 13 | text fsjson "application/json" |
| 14 | text fsctype "Content-Type" |
| 15 | text fsport "PORT " |
| 16 | text fsf "F " |
| 17 | text fssp " " |
| 18 | text fsnl "\n" |
| 19 | global fsmap uchar 2097152 |
| 20 | global fsn ulong 1 |
| 21 | global fsk ulong 1024 |
| 22 | |
| 23 | fn fs_num i ulong to ulong |
| 24 | b = new uchar 32 0 |
| 25 | n = argread i b 0 32 |
| 26 | if or lt n 1 gt n 9 |
| 27 | ret not ulong 0 |
| 28 | end |
| 29 | v = ulong 0 |
| 30 | for j ulong ulong n |
| 31 | c = ulong get b j |
| 32 | if or lt c 48 gt c 57 |
| 33 | ret not ulong 0 |
| 34 | end |
| 35 | v = add mul v 10 sub c 48 |
| 36 | end |
| 37 | ret v |
| 38 | end |
| 39 | |
| 40 | note Load MAP: false when unreadable or malformed. fsk[4k..]: key offset, |
| 41 | note key length, value offset, value length. |
| 42 | fn fs_load to bool |
| 43 | p = new uchar 1024 0 |
| 44 | n = argread 2 p 0 1000 |
| 45 | if lt n 1 |
| 46 | ret false |
| 47 | end |
| 48 | fd = fopen p 0 ulong n 524288 0 |
| 49 | if lt fd 0 |
| 50 | ret false |
| 51 | end |
| 52 | t = ulong 0 |
| 53 | for it ulong 4096 |
| 54 | r = read int fd fsmap t sub 2097152 t |
| 55 | if le r 0 |
| 56 | break |
| 57 | end |
| 58 | t = add t ulong r |
| 59 | end |
| 60 | x = fclose int fd |
| 61 | ret fs_index t |
| 62 | end |
| 63 | |
| 64 | fn fs_index t ulong to bool |
| 65 | p = ulong 0 |
| 66 | k = ulong 0 |
| 67 | for it ulong 256 |
| 68 | if ge p t |
| 69 | put fsn 0 k |
| 70 | ret true |
| 71 | end |
| 72 | a = fs_dnum p |
| 73 | p = add p add fs_dlen p 1 |
| 74 | b = fs_dnum p |
| 75 | p = add p add fs_dlen p 1 |
| 76 | if gt add add p a b t |
| 77 | ret false |
| 78 | end |
| 79 | o = mul k 4 |
| 80 | put fsk o p |
| 81 | put fsk add o 1 a |
| 82 | put fsk add o 2 add p a |
| 83 | put fsk add o 3 b |
| 84 | p = add add p a b |
| 85 | k = add k 1 |
| 86 | end |
| 87 | ret false |
| 88 | end |
| 89 | |
| 90 | fn fs_dlen p ulong to ulong |
| 91 | for i ulong 12 |
| 92 | c = ulong get fsmap add p i |
| 93 | if or lt c 48 gt c 57 |
| 94 | ret i |
| 95 | end |
| 96 | end |
| 97 | ret 12 |
| 98 | end |
| 99 | |
| 100 | fn fs_dnum p ulong to ulong |
| 101 | v = ulong 0 |
| 102 | for i ulong fs_dlen p |
| 103 | v = add mul v 10 sub ulong get fsmap add p i 48 |
| 104 | end |
| 105 | ret v |
| 106 | end |
| 107 | |
| 108 | note The entry whose key occurs in body b[0, n), or -1. |
| 109 | fn fs_find b view uchar 1048576 n ulong to long |
| 110 | for k ulong get fsn 0 bound 256 |
| 111 | o = mul k 4 |
| 112 | ko = get fsk o |
| 113 | kl = get fsk add o 1 |
| 114 | if and gt kl 0 le kl n |
| 115 | for s ulong add sub n kl 1 |
| 116 | if fs_at b s ko kl |
| 117 | ret long k |
| 118 | end |
| 119 | end |
| 120 | end |
| 121 | end |
| 122 | ret neg long 1 |
| 123 | end |
| 124 | |
| 125 | fn fs_at b view uchar 1048576 s ulong ko ulong kl ulong to bool |
| 126 | for i ulong kl |
| 127 | if ne get b add s i get fsmap add ko i |
| 128 | ret false |
| 129 | end |
| 130 | end |
| 131 | ret true |
| 132 | end |
| 133 | |
| 134 | fn fs_out a view uchar * n ulong to void |
| 135 | x = write 1 a 0 n |
| 136 | end |
| 137 | |
| 138 | fn fs_dec v ulong to void |
| 139 | d = new uchar 20 0 |
| 140 | k = ulong 20 |
| 141 | x = v |
| 142 | for it ulong 20 |
| 143 | k = sub k 1 |
| 144 | put d k uchar add rem x 10 48 |
| 145 | x = div x 10 |
| 146 | if eq x 0 |
| 147 | break |
| 148 | end |
| 149 | end |
| 150 | fs_out view part d k sub 20 k sub 20 k |
| 151 | end |
| 152 | |
| 153 | fn fs_log st ulong k long ms ulong to void |
| 154 | fs_out fsf len fsf |
| 155 | fs_dec st |
| 156 | fs_out fssp 1 |
| 157 | if lt k 0 |
| 158 | fs_dec ulong 999 |
| 159 | else |
| 160 | fs_dec ulong k |
| 161 | end |
| 162 | fs_out fssp 1 |
| 163 | fs_dec ms |
| 164 | fs_out fsnl 1 |
| 165 | end |
| 166 | |
| 167 | fn fs_one fd int cs edit ulong 320 io edit uchar 131072 bd edit uchar 1048576 dl ulong to void |
| 168 | t0 = monons |
| 169 | r = hinit cs io fd 1 |
| 170 | hsettmo cs 5000000000 5000000000 |
| 171 | hsetbdl cs 5000000000 |
| 172 | r = hreqrd cs io |
| 173 | if le r 0 |
| 174 | x = hclose cs |
| 175 | ret |
| 176 | end |
| 177 | put cs HBCAP 1048576 |
| 178 | n = hbodyall cs io bd |
| 179 | k = neg long 1 |
| 180 | if and gt n 0 hispath cs io fsurl |
| 181 | k = fs_find bd ulong n |
| 182 | end |
| 183 | x = sleepns long mul dl 1000000 |
| 184 | if lt k 0 |
| 185 | r = hrsperr cs io 404 |
| 186 | x = hclose cs |
| 187 | fs_log ulong 404 k ulong div sub monons t0 1000000 |
| 188 | ret |
| 189 | end |
| 190 | fs_reply cs io bd ulong k |
| 191 | x = hclose cs |
| 192 | fs_log ulong 200 k ulong div sub monons t0 1000000 |
| 193 | end |
| 194 | |
| 195 | note Answer with entry k's value (copied into vb, a global is never a call |
| 196 | note argument). |
| 197 | fn fs_reply cs edit ulong 320 io edit uchar 131072 vb edit uchar 1048576 k ulong to void |
| 198 | o = mul k 4 |
| 199 | vo = get fsk add o 2 |
| 200 | vl = get fsk add o 3 |
| 201 | for i ulong vl |
| 202 | put vb i get fsmap add vo i |
| 203 | end |
| 204 | put cs HKEEP 0 |
| 205 | r = hrspst cs io 200 |
| 206 | r = hfe r hhname cs io fsctype |
| 207 | r = hfe r hhval cs io fsjson |
| 208 | r = hfe r hhend cs io 0 vl |
| 209 | r = hfe r hput cs io view part vb 0 vl |
| 210 | r = hfe r hwfin cs io |
| 211 | end |
| 212 | |
| 213 | fn main to int |
| 214 | cs = new ulong 320 0 |
| 215 | io = new uchar 131072 0 |
| 216 | bd = new uchar 1048576 0 |
| 217 | a = new uchar 16 0 |
| 218 | if ne argcnt 4 |
| 219 | ret 2 |
| 220 | end |
| 221 | po = fs_num 1 |
| 222 | dl = fs_num 3 |
| 223 | if or or eq po 0 gt po 65535 or eq po 18091 gt dl 600000 |
| 224 | ret 2 |
| 225 | end |
| 226 | if not fs_load |
| 227 | ret 3 |
| 228 | end |
| 229 | r = haddr4 a 2130706433 po |
| 230 | lf = hlisten a 16 |
| 231 | if lt lf 0 |
| 232 | ret 4 |
| 233 | end |
| 234 | fs_out fsport len fsport |
| 235 | fs_dec hport a |
| 236 | fs_out fsnl 1 |
| 237 | for it ulong 1000000000 |
| 238 | c = haccept int lf neg long 1 |
| 239 | if ge c 0 |
| 240 | fs_one int c cs io bd dl |
| 241 | end |
| 242 | end |
| 243 | ret 0 |
| 244 | end |