Use macro `org-eval-in-environment' in `org-batch-store-agenda-views'
[org-mode.git] / contrib / scripts / StartOzServer.oz
blob56940a37e1bba9b0224fe03b0e9b697aab2d4af0
1 %%% *************************************************************
2 %%% Copyright (C) 2009 Torsten Anders (www.torsten-anders.de)
3 %%% This program is free software; you can redistribute it and/or
4 %%% modify it under the terms of the GNU General Public License
5 %%% as published by the Free Software Foundation; either version 2
6 %%% of the License, or (at your option) any later version.
7 %%% This program is distributed in the hope that it will be useful,
8 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
9 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 %%% GNU General Public License for more details.
11 %%% *************************************************************
14 %% This code implements the Oz-side of the Org-babel Oz interface. It
15 %% creates a socket server (to which org-babel-oz.el then
16 %% connects). Any input to this socket must be an Oz expression. The
17 %% input is fed to the OPI oz compiler, and the results are send back
18 %% via the socket.
22 declare
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26 %% Accessing the OPI compiler
29 MyCompiler = Emacs.condSend.compiler
32 /* % testing
34 %% Feed an expression (result browsed)
35 {MyCompiler enqueue(setSwitch(expression true))}
36 {Browse
37 {MyCompiler enqueue(feedVirtualString("1 + 2" return(result: $)))}}
38 {MyCompiler enqueue(setSwitch(expression false))}
40 %% It is really the OPI: I can use declare!
41 {MyCompiler enqueue(setSwitch(expression false))}
42 {MyCompiler enqueue(feedVirtualString("declare X=3\n{Browse X*X}"))}
44 %% Note: expressions starting with keyword declare need keyword in
45 {MyCompiler enqueue(setSwitch(expression true))}
46 {Browse
47 {MyCompiler enqueue(feedVirtualString("declare X=3\nin X*X" return(result: $)))}}
48 {MyCompiler enqueue(setSwitch(expression false))}
50 %% Alternatively you use a session with multiple feeds: first declare (statement), and then feed an expression
51 {MyCompiler enqueue(setSwitch(expression false))}
52 {MyCompiler enqueue(feedVirtualString("declare X=7" return))}
53 {MyCompiler enqueue(setSwitch(expression true))}
54 {Browse
55 {MyCompiler enqueue(feedVirtualString("X*X" return(result: $)))}}
56 {MyCompiler enqueue(setSwitch(expression false))}
58 %% !!?? does not work?
59 %% return nil in case of any error (division by 0)
60 {MyCompiler enqueue(setSwitch(expression true))}
61 {Browse
62 {MyCompiler enqueue(feedVirtualString(
63 {Accum ["try\n"
64 % "skip\n" % do something in any case..
65 "1 div 0" % my code
66 % "1" % my code
67 "\ncatch E then {Error.printException E}\n"
68 "error\n" % always return nil
69 "end\n"]
70 List.append}
71 return(result: $)))}}
72 {MyCompiler enqueue(setSwitch(expression false))}
75 %% !! catching some exceptions does not work??
77 %% exception is not catched
78 try {Bla} catch E then {Error.printException E} {Browse nil} end
80 %% exception is catched
81 try {Browse 1 div 0} catch E then {Error.printException E} {Browse nil} end
82 {Browse ok}
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 %% Socket interface
95 %% Create socket
98 MyPort = 6001
100 /** %% Creates a TCP socket server. Expects a Host (e.g., 'localhost') and a PortNo and returns a server plus its corresponding client. This client is an instance of Open.socket, and is the interface for reading and writing into the socket.
101 %% MakeServer blocks until the server listens. However, waiting until a connection has been accepted happens in its own thread (i.e. MakeServer does only block until the server listens).
102 %% NB: A port can be used only once, so assign it carefully. In case this postnnumber was shortly used before, you may need to wait a bit before reusing it.
103 %% */
104 %% !! Alternatively, let it assign automatically and output the port number..
106 %% NOTE: for supporting multiple connections see http://www.mozart-oz.org/documentation/op/node13.html#section.sockets.accept
107 proc {MakeServer Host PortNo ?MyServer ?MyClient}
108 proc {Accept MyClient}
109 thread H in % P
110 %% suspends until a connection has been accepted
111 {MyServer accept(host:H
112 acceptClass:Open.socket
113 accepted:?MyClient)}
114 % {Myserver accept(host:H port:P)} % suspends until a connection has been accepted
115 %% !!?? port number of client is usually created randomly..
116 {System.showInfo "% connection accepted from host "#H}
118 %% !!???
119 %% If Accept is called recursively, then server accepts multiple connections. These share the same compiler instance (e.g. variable bindings are shared). For multiple independent compiler instances call the OzServer application multiple times.
120 %% However, how shall the output for multiple connections be sorted?? Would using the different client sockets created with the Server accept method work?
121 %% NB: The number of clients accepted concurrently must be limited to the number set by {MyServer listen}
122 % {Accept}
125 MyServer = {New Open.socket init}
126 %% To avoid problems with portnumbers, the port could be assigned automatically and then output..
127 %%{MyServer bind(port:PortNo)}
128 {MyServer bind(host:Host takePort:PortNo)}
129 {MyServer listen}
130 {System.showInfo "% OzServer started at host "#Host#" and port "#PortNo}
131 MyClient = {Accept}
134 MySocket = {MakeServer localhost MyPort _/*MyServer*/}
138 %% Read socket input
141 declare
142 %% Copied from OzServer/source/Socket.oz
143 local
144 proc {Aux Socket Size Stream}
145 In = {Socket read(list:$
146 size:Size)}
148 {Wait In}
149 %% !! Is this the right way to stop the processing??
151 %% abort condition when client stream ended (i.e. nothing was sent)
152 if In == nil
153 then {System.showInfo "socket stream ended"}
154 Stream = nil
155 else Stream = In | {Aux Socket Size}
159 /** %% The socket Server returns a stream of the strings it receives. The Server always waits until someone writes something into the socket, then the input is immediately written to a stream and the Server waits again.
160 %% */
161 proc {ReadToStream Socket Size Xs}
162 thread {Aux Socket Size Xs} end
166 /* % test
168 MyStream = {ReadToStream MySocket 1024}
172 /* % test
174 %% writing
175 {MySocket write(vs:"this is a test")}
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 %% Send socket input to compiler and send results back to socket
185 %% NOTE: Input code must be expression
186 thread
187 {ForAll {ReadToStream MySocket 1024}
188 proc {$ Code}
189 Result
190 %% Catch any exception (so the will not cause blocking) and return nil in that case
191 FullCode = {Accum ["try\n"
192 % "skip\n" % do something in any case..
193 Code
194 "\ncatch E then {Error.printException E}\n"
195 "error\n" % in case of an error, return 'error'
196 "end\n"]
197 List.append}
199 %% ?? Should I make setting switches etc atomic?
200 {MyCompiler enqueue(setSwitch(expression true))}
201 {MyCompiler enqueue(feedVirtualString(FullCode return(result: ?Result)))}
202 {MyCompiler enqueue(setSwitch(expression false))}
204 {Wait Result}
205 {MySocket write(vs: if {VirtualString.is Result}
206 then Result
207 else {Value.toVirtualString Result 1000 1000}
208 end)}
209 {Show 'Org-babel result: '#Result}
210 end}
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 %% Aux defs
220 /** %% Binds the accumulation of the binary function Fn on all neighbors in Xs to Y. E.g., Accum returns the sum in Xs if Fn is Number.'+'.
221 %% */
222 proc {Accum Xs Fn Y}
223 {List.foldL Xs.2 Fn Xs.1 Y}