Jan,
Ok, I've followed your advice to the best of my abilities and I have a
demo that sort of works. The problem is that it only works MOST of the
time, so there seems to be som kind of synchronization issues that I
don't really understand.
I have attached code (server in Prolog and client in HTML +
Javascript). The code is very simple (I guess that's why it doesn't
work ALL the time.) You run it by simply loading the Prolog file and
then direct your browser to <
http://localhost:5000/solver>. (Warning:
This exposes your Prolog process to the world!). You may want to run
the demo in Firefox, using Firebug to monitor the HTTP traffic.
Problems that I see are:
- Clicking Next (to get the next solution to a goal) sometimes doesn't
come back at all. The HTTP request just hangs there, forever. I do not
understand why.
- There is also a problem with using the session ID as the name of the
thread that runs the actual Prolog goal. Sometimes the name is already
taken. I understand why this is happening (the thread is still
running), but I don't know what to do about it (aborting the running
thread by force?)
I do intend to make this into a nice demo for the examples directory,
but that would make sense only if it works. So I'm hoping for some
kind of clue!
Best regards,
Torbjörn
On Fri, Jun 19, 2009 at 9:40 AM, Jan Wielemaker<
J.Wielemaker@...> wrote:
> On Thursday 18 June 2009 14:57:27 Torbjörn Lager wrote:
>> Hello,
>>
>> Please have a look at the following code:
>> :- use_module(library('http/thread_httpd')).
>> :- use_module(library('http/http_dispatch')).
>> :- use_module(library('http/json')).
>> :- use_module(library('http/http_json')).
>> :
>> :- http_handler('/doit', doit, [spawn([alias(test)])]).
>>
>> doit(_Requestl) :-
>> reply_json(json([a=result])),
>> thread_get_message(Event),
>> process(Event).
>>
>> :- http_server(http_dispatch, [port(5000)]).
>>
>> The call to thread_get_message/1 blocks (as it should), which seems to
>> imply that no JSON is returned to the browser (until I kill the
>> server). Does it have to be that way (I don't see why)? Is there any
>> way I can send JSON to the client and still keep the thread on the
>> server running?
>
> Not easily. The whole infrastructure is designed to deal with finishing
> the reply on completion of the handler. What you can do is enable
> session management (you need that anyway) and create a thread that is
> associated to the session. That is where you have the state-full
> computation and you send messages back and forth between the HTTP
> handlers and the session thread to communicate queries and results.
>
> Would be nice to have a demo using this scenario in the HTTP server
> examples directory ...
>
> More in general, I'm looking for good examples for this (and other)
> packages. If you have things to share or are willing to write examples,
> please share them.
>
> Cheers --- Jan
>
>> (What I'm trying to find here is a way to ask for one solution at the
>> time for a Prolog goal entered in a web interface. I have written code
>> that should have worked, had only the above worked... Maybe there are
>> other ideas for how to accomplish what I want to do? I've looked at
>> Uwe Lestas N-Queens demo, but it striked me as too complicated, and I
>> really would like to work against the SWI webserver...)
>>
>> Thanks in advance!
>>
>> Best regards,
>> Torbjörn
>
>
>
>
--
Torbjörn Lager
Professor of General and Computational Linguistics
Department of Philosophy, Linguistics and Theory of Science
Box 200, SE-405 30 Göteborg, Sweden
Phone: +46317864413