I believe the basic way to do it is that the function that processes the original request both returns the output and the continuation, then the next time the user connects it would use the continuation instead of the original function.
function doHTTPRequest(requestVars, renderPage)
{
(content, renderNextPage) = renderPage(requestVars);
return (content, function (newRequestVars) {
renderNextPage(newRequestVars);
});
}
It's a little awkward, and maybe not the best example of CPS. But basically what happens is the request gets handled and returns the output and something to do next. Then you return your output and some additional computation that will get executed later, in this case a function that renders a different page. This is computation that hasn't been evaluated, but which can be passed around to evaluate later. Note that this whole function could be replaced by renderPage, but I wanted to make things more explicit. A good language would let you write doHTTPRequest = renderPage;
Also, when your config would probably specify that it should call renderPage and so that function would get passed around or held by your controller/dispatcher until the actual doHTTPRequest is called. function handleSession() {
renderFormPage() // writes response with HTML for form
suspend() // wait for next request in the session
processForm() // process the form data - notice this is the same function
}
It's a very powerful technique and can make web programming very intuitive since you can have a single linear flow for a complex web interaction.Check out Chris Double's tutorial on this:
http://double.co.nz/scheme/modal-web-server.html
Or SISC-Web for a simple but complete implementation:
So what you need basically in the core of your server is some data structure (possibly keyed by the client's IP address and TCP port number?) that contains a bunch of functions with prepopulated arguments (curried functions?).
The prepopulated arguments are basically the state set by the previous request. When a new request comes in you somehow match it up with your data structure and execute?
I guess the continuation part is to force the I/O to happen at the right point, i.e make the next request depend on the execution of the first?
Of course Javascript is not really functional so that needn't be explicit.
One thing I am still a bit hazy on though. He mentions that continuations could be used in the context of a web app to maintain state between HTTP requests.
I'm not quite clear how this would actually work. Lets say you have something like this (in psuedocode):
Let's say we have a counter we want to increment on each request.
doHTTPRequest(requestVars)
{
}doHTTPRequest(doHTTPRequest)
Is that approximately how this works?
In which case, how would it work for doing more than 2 requests in a row?