1% BEGIN LICENSE BLOCK
2% Version: CMPL 1.1
3%
4% The contents of this file are subject to the Cisco-style Mozilla Public
5% License Version 1.1 (the "License"); you may not use this file except
6% in compliance with the License.  You may obtain a copy of the License
7% at www.eclipse-clp.org/license.
8% 
9% Software distributed under the License is distributed on an "AS IS"
10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11% the License for the specific language governing rights and limitations
12% under the License. 
13% 
14% The Original Code is  The ECLiPSe Constraint Logic Programming System. 
15% The Initial Developer of the Original Code is  Cisco Systems, Inc. 
16% Portions created by the Initial Developer are
17% Copyright (C) 2006 Cisco Systems, Inc.  All Rights Reserved.
18% 
19% Contributor(s): 
20% 
21% END LICENSE BLOCK
22
23/**************************************************************************
24 
25     Demo program for peer multitasking
26
27  Running the query init(N) will create N remote peers running copies
28  of the tcl demo program. The tcl demo program is assumed to be in 
29  the same directory as this program, its ECLiPSe counterpart.
30
31  Each tcl remote peer can participate in peer multitasking. A peer
32  multitasking phase is started by the query go_multi/0. To hand control
33  over to a specific peer, use go_one/1.
34
35  This program is described in the Tcl Peer Multitasking chapter of the
36  Embedding Manual
37
38 **************************************************************************/
39
40init(N) :-
41        (for(I,1,N) do 
42             remote_connect_setup(localhost/Port, Peer, Sock),
43             % start the remote peer program...
44             exec([wish,"example_multi.tcl", "--", "-h", "localhost", "-p", Port],
45                  [], _Pid),
46             (remote_connect_accept(Peer, Sock, 10, post_attach(Peer), "", _) ->
47                  true ; close(Sock)
48             )
49        ).
50
51post_attach(Peer) :-
52        recorda(multi_peers, Peer),
53        printf("Created peer %w%n", [Peer]),
54        set_event_handler(Peer, disconnect_handler/1).
55
56
57disconnect_handler(Peer) :-
58        erase(multi_peers, Peer),
59        writeln(finished-Peer).
60
61go_multi :-
62        writeln("start multitasking phase...."),
63        block(peer_do_multitask(demo), peer_multitask_empty,
64                      writeln("No peers are registered for multitasking.")),
65        writeln("end multitasking phase....").
66
67go_one(Peer) :-
68        printf("transferring to peer %w%n", [Peer]),
69        once(recorded(multi_peers, Peer)), 
70        remote_yield(Peer),
71        printf("Return from peer %w%n", [Peer]).
72
73
74
75