mu/html/092socket.mu.html

216 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 092socket.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muData { color: #ffff00; }
.muControl { color: #c0a020; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Wrappers around socket primitives that are easier to test.</span>
<span class="Comment"># To test server operations, just run a real client against localhost.</span>
<span class="muScenario">scenario</span> example-server-test [
<span class="Constant">local-scope</span>
<span class="Comment"># test server without a fake on a random (real) port</span>
<span class="Comment"># that way repeatedly running the test will give ports time to timeout and</span>
<span class="Comment"># close before reusing them</span>
make-random-nondeterministic
port:num <span class="Special">&lt;-</span> random-in-range <span class="Constant">0/real-random-numbers</span>,<span class="Constant"> 8000</span>,<span class="Constant"> 8100</span>
run [
socket:num <span class="Special">&lt;-</span> $open-server-socket port
assert socket, <span class="Constant">[ </span>
<span class="Constant">F - example-server-test: $open-server-socket failed]</span>
handler-routine:number <span class="Special">&lt;-</span> start-running serve-one-request socket, example-handler
]
source:&amp;:source:char <span class="Special">&lt;-</span> start-reading-from-network <span class="Constant">0/real-resources</span>, <span class="Constant">[localhost/]</span>, port
response:text <span class="Special">&lt;-</span> drain source
10:@:char/<span class="Special">raw</span> <span class="Special">&lt;-</span> copy *response
memory-should-contain [
10:array:character <span class="Special">&lt;-</span> <span class="Constant">[abc]</span>
]
socket <span class="Special">&lt;-</span> $close-socket socket
]
<span class="Comment"># helper just for this scenario</span>
<span class="muRecipe">def</span> example-handler query:text<span class="muRecipe"> -&gt; </span>response:text [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="muControl">return</span> <span class="Constant">[abc]</span>
]
<span class="Comment"># To test client operations, use `assume-resources` with a filename that</span>
<span class="Comment"># begins with a hostname. (Filenames starting with '/' are assumed to be</span>
<span class="Comment"># local.)</span>
<span class="muScenario">scenario</span> example-client-test [
<span class="Constant">local-scope</span>
assume-resources [
<span class="Constant">[example.com/]</span> <span class="Special">&lt;-</span> [
<span class="Constant"> |abc|</span>
]
]
run [
source:&amp;:source:char <span class="Special">&lt;-</span> start-reading-from-network resources, <span class="Constant">[example.com/]</span>
]
contents:text <span class="Special">&lt;-</span> drain source
10:@:char/<span class="Special">raw</span> <span class="Special">&lt;-</span> copy *contents
memory-should-contain [
10:array:character <span class="Special">&lt;-</span> <span class="Constant">[abc</span>
<span class="Constant">]</span>
]
]
<span class="muData">type</span> request-handler = (recipe text<span class="muRecipe"> -&gt; </span>text)
<span class="muRecipe">def</span> serve-one-request socket:num, request-handler:request-handler<span class="muRecipe"> -&gt; </span>socket:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
session:num <span class="Special">&lt;-</span> $accept socket
assert session, <span class="Constant">[ </span>
<span class="Constant">F - example-server-test: $accept failed]</span>
contents:&amp;:source:char, sink:&amp;:sink:char <span class="Special">&lt;-</span> new-channel<span class="Constant"> 30</span>
start-running receive-from-socket session, sink
query:text <span class="Special">&lt;-</span> drain contents
response:text <span class="Special">&lt;-</span> call request-handler, query
write-to-socket session, response
session <span class="Special">&lt;-</span> $close-socket session
]
<span class="muRecipe">def</span> start-reading-from-network resources:&amp;:resources, uri:text<span class="muRecipe"> -&gt; </span>contents:&amp;:source:char [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
port:num, port-found?:boolean <span class="Special">&lt;-</span> <span class="Constant">next-ingredient</span>
<span class="muControl">break-if</span> port-found?
port <span class="Special">&lt;-</span> copy <span class="Constant">80/http-port</span>
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> resources
<span class="Comment"># fake network</span>
contents <span class="Special">&lt;-</span> start-reading-from-fake-resource resources, uri
<span class="muControl">return</span>
<span class="Delimiter">}</span>
<span class="Comment"># real network</span>
host:text, path:text <span class="Special">&lt;-</span> split-at uri, <span class="Constant">47/slash</span>
socket:num <span class="Special">&lt;-</span> $open-client-socket host, port
assert socket, <span class="Constant">[contents]</span>
req:text <span class="Special">&lt;-</span> interpolate <span class="Constant">[GET _ HTTP/1.1]</span>, path
request-socket socket, req
contents:&amp;:source:char, sink:&amp;:sink:char <span class="Special">&lt;-</span> new-channel<span class="Constant"> 10000</span>
start-running receive-from-client-socket-and-close socket, sink
]
<span class="muRecipe">def</span> request-socket socket:num, s:text<span class="muRecipe"> -&gt; </span>socket:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
write-to-socket socket, s
$write-to-socket socket, <span class="Constant">13/cr</span>
$write-to-socket socket, <span class="Constant">10/lf</span>
<span class="Comment"># empty line to delimit request</span>
$write-to-socket socket, <span class="Constant">13/cr</span>
$write-to-socket socket, <span class="Constant">10/lf</span>
]
<span class="muRecipe">def</span> receive-from-socket socket:num, sink:&amp;:sink:char<span class="muRecipe"> -&gt; </span>sink:&amp;:sink:char, socket:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="Constant"> +next-attempt</span>
c:char, found?:bool, eof?:bool, error:num <span class="Special">&lt;-</span> $read-from-socket socket
<span class="muControl">break-if</span> eof?
<span class="muControl">break-if</span> error
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> found?
sink <span class="Special">&lt;-</span> write sink, c
<span class="Delimiter">}</span>
<span class="Delimiter">{</span>
<span class="muControl">break-if</span> found?
switch
<span class="Delimiter">}</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
sink <span class="Special">&lt;-</span> close sink
]
<span class="muRecipe">def</span> receive-from-client-socket-and-close socket:num, sink:&amp;:sink:char<span class="muRecipe"> -&gt; </span>sink:&amp;:sink:char, socket:num [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
sink <span class="Special">&lt;-</span> receive-from-socket socket, sink
socket <span class="Special">&lt;-</span> $close-socket socket
]
<span class="muRecipe">def</span> write-to-socket socket:num, s:text [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
len:num <span class="Special">&lt;-</span> length *s
i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
<span class="Delimiter">{</span>
done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len
<span class="muControl">break-if</span> done?
c:char <span class="Special">&lt;-</span> index *s, i
$write-to-socket socket, c
i <span class="Special">&lt;-</span> add i,<span class="Constant"> 1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
]
<span class="Comment"># like split-first, but don't eat the delimiter</span>
<span class="muRecipe">def</span> split-at text:text, delim:char<span class="muRecipe"> -&gt; </span>x:text, y:text [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Comment"># empty text? return empty texts</span>
len:num <span class="Special">&lt;-</span> length *text
<span class="Delimiter">{</span>
empty?:bool <span class="Special">&lt;-</span> equal len,<span class="Constant"> 0</span>
<span class="muControl">break-unless</span> empty?
x:text <span class="Special">&lt;-</span> new <span class="Constant">[]</span>
y:text <span class="Special">&lt;-</span> new <span class="Constant">[]</span>
<span class="muControl">return</span>
<span class="Delimiter">}</span>
idx:num <span class="Special">&lt;-</span> find-next text, delim,<span class="Constant"> 0</span>
x:text <span class="Special">&lt;-</span> copy-range text,<span class="Constant"> 0</span>, idx
y:text <span class="Special">&lt;-</span> copy-range text, idx, len
]
<span class="muScenario">scenario</span> text-split-at [
<span class="Constant">local-scope</span>
x:text <span class="Special">&lt;-</span> new <span class="Constant">[a/b]</span>
run [
y:text, z:text <span class="Special">&lt;-</span> split-at x, <span class="Constant">47/slash</span>
10:@:char/<span class="Special">raw</span> <span class="Special">&lt;-</span> copy *y
20:@:char/<span class="Special">raw</span> <span class="Special">&lt;-</span> copy *z
]
memory-should-contain [
10:array:character <span class="Special">&lt;-</span> <span class="Constant">[a]</span>
20:array:character <span class="Special">&lt;-</span> <span class="Constant">[/b]</span>
]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->