Debian Codegraph @

doc/examples.html

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html>
4 <head>
5 <title>Lua XML-RPC: XML-RPC interface to the Lua programming language</title>
6 <link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/>
7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
8 </head>
9 <body>
10
11 <div id="container">
12
13 <div id="product">
14 <div id="product_logo">
15 <a href="http://www.keplerproject.org">
16 <img alt="Lua XML-RPC logo" src="luaxmlrpc.png"/>
17 </a>
18 </div>
19 <div id="product_name"><big><b>Lua XML-RPC</b></big></div>
20 <div id="product_description">XML-RPC interface to the Lua programming language</div>
21 </div> <!-- id="product" -->
22
23 <div id="main">
24
25 <div id="navigation">
26 <h1>Lua XML-RPC</h1>
27 <ul>
28 <li><a href="index.html">Home</a>
29 <ul>
30 <li><a href="index.html#overview">Overview</a></li>
31 <li><a href="index.html#status">Status</a></li>
32 <li><a href="index.html#download">Download</a></li>
33 <li><a href="index.html#history">History</a></li>
34 <li><a href="index.html#credits">Credits</a></li>
35 <li><a href="index.html#contact">Contact</a></li>
36 </ul>
37 </li>
38 <li><a href="manual.html">Manual</a>
39 <ul>
40 <li><a href="manual.html#introduction">Introduction</a></li>
41 <li><a href="manual.html#installation">Installation</a></li>
42 <li><a href="manual.html#data_types">Data types</a></li>
43 <li><a href="manual.html#functions">Library functions</a></li>
44 <li><a href="manual.html#client">Client side</a></li>
45 <li><a href="manual.html#server">Server side</a></li>
46 <li><a href="manual.html#references">References</a></li>
47 </ul>
48 </li>
49 <li><strong>Examples</strong></li>
50 <li><a href="license.html">License</a></li>
51 </ul>
52 </div> <!-- id="navigation" -->
53
54 <div id="content">
55
56
57 <h2><a name="examples"></a>Examples</h2>
58
59 <h3><a name="client_example"></a>Client example</h3>
60
61 <p>Below is a small sample code displaying the use of the library in a
62 client application.</p>
63
64 <pre class="example">
65 require "xmlrpc.http"
66
67 local ok, res = xmlrpc.http.call (
68 "http://www.oreillynet.com/meerkat/xml-rpc/server.php",
69 "system.listMethods")
70 print (ok)
71 for i, v in pairs(res) do print ('\t', i, v) end
72 </pre>
73
74
75 <h3><a name="types_example"></a>Type conversion example</h3>
76
77 <p>The next example shows how to force the conversion of types from
78 Lua to XML-RPC.</p>
79
80 <pre class="example">
81 require "xmlrpc"
82
83 double_array_type = xmlrpc.newArray ("double")
84 double_array = xmlrpc.newTypedValue ( { 1.1, 2, 3, 4 }, double_array_type)
85
86 double_array_array_type = xmlrpc.newArray (double_array_type)
87 double_array_array = xmlrpc.newTypedValue (
88 {
89 { 11, 12, 13, },
90 { 21, 22, 23, },
91 { 31, 32, 33, },
92 }, double_array_array_type)
93 </pre>
94
95 <p>The table <code>double_array_array</code> will be:</p>
96
97 <pre class="example">
98 &lt;array&gt;
99 &lt;data&gt;
100 &lt;value&gt;
101 &lt;array&gt;
102 &lt;data&gt;
103 &lt;value&gt;&lt;double&gt;11&lt;/double&gt;&lt;/value&gt;
104 &lt;value&gt;&lt;double&gt;12&lt;/double&gt;&lt;/value&gt;
105 &lt;value&gt;&lt;double&gt;13&lt;/double&gt;&lt;/value&gt;
106 &lt;/data&gt;
107 &lt;/array&gt;
108 &lt;/value&gt;
109 &lt;value&gt;
110 &lt;array&gt;
111 &lt;data&gt;
112 &lt;value&gt;&lt;double&gt;21&lt;/double&gt;&lt;/value&gt;
113 &lt;value&gt;&lt;double&gt;22&lt;/double&gt;&lt;/value&gt;
114 &lt;value&gt;&lt;double&gt;23&lt;/double&gt;&lt;/value&gt;
115 &lt;/data&gt;
116 &lt;/array&gt;
117 &lt;/value&gt;
118 &lt;value&gt;
119 &lt;array&gt;
120 &lt;data&gt;
121 &lt;value&gt;&lt;double&gt;31&lt;/double&gt;&lt;/value&gt;
122 &lt;value&gt;&lt;double&gt;32&lt;/double&gt;&lt;/value&gt;
123 &lt;value&gt;&lt;double&gt;33&lt;/double&gt;&lt;/value&gt;
124 &lt;/data&gt;
125 &lt;/array&gt;
126 &lt;/value&gt;
127 &lt;/data&gt;
128 &lt;/array&gt;
129 </pre>
130
131
132
133 <h3><a name="server_example"></a>Server example</h3>
134
135 <p>Follows a small example of a server based on Xavante
136 WSAPI. You can call the service for example with
137 the <code>xmlrpc</code> tool
138 from <a href="http://xmlrpc-c.sourceforge.net/">XML-RPC
139 C</a> (<code>xmlrpc http://localhost:12345
140 hello_world</code>) or by
141 using <code>examples/client.lua</code> in the source
142 tarball.</p>
143
144 <pre class="example">
145 require("xavante")
146 require("xavante.httpd")
147 require("wsapi.xavante")
148 require("wsapi.request")
149 require("xmlrpc")
150
151 --- XML-RPC WSAPI handler
152 -- @param wsapi_env WSAPI environment
153 function wsapi_handler(wsapi_env)
154 local headers = { ["Content-type"] = "text/xml" }
155 local req = wsapi.request.new(wsapi_env)
156 local method, arg_table = xmlrpc.srvDecode(req.POST.post_data)
157 local func = xmlrpc.dispatch(method)
158 local result = { pcall(func, unpack(arg_table or {})) }
159 local ok = result[1]
160 if not ok then
161 result = { code = 3, message = result[2] }
162 else
163 table.remove(result, 1)
164 if table.getn(result) == 1 then
165 result = result[1]
166 end
167 end
168
169 local r = xmlrpc.srvEncode(result, not ok)
170 headers["Content-length"] = tostring(#r)
171
172 local function xmlrpc_reply(wsapienv)
173 coroutine.yield(r)
174 end
175
176 return 200, headers, coroutine.wrap(xmlrpc_reply)
177 end
178
179 -- XML-RPC exported functions
180 xmlrpc_exports = {}
181
182 --- Get simple string.
183 -- @return simple string
184 function xmlrpc_exports.hello_world()
185 return "Hello World"
186 end
187
188 local rules = {{ match = ".", with = wsapi.xavante.makeHandler(wsapi_handler) }}
189 local config = { server = {host = "*", port = 12345}, defaultHost = { rules = rules} }
190
191 xmlrpc.srvMethods(xmlrpc_exports)
192 xavante.HTTP(config)
193
194 xavante.run()
195 </pre>
196
197 <p>Note that the package <code>post</code> and the function
198 <code>respond</code> should be provided by the cgi launcher.</p>
199
200 </div> <!-- id="content" -->
201
202 </div> <!-- id="main" -->
203
204 <div id="about">
205 <p>
206 <a href="http://validator.w3.org/check?uri=referer">
207 <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" />
208 </a>
209 </p>
210 </div> <!-- id="about" -->
211
212 </div> <!-- id="container" -->
213
214 </body>
215 </html>