File: examples.html

package info (click to toggle)
lua-xmlrpc 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 244 kB
  • sloc: makefile: 11
file content (215 lines) | stat: -rw-r--r-- 6,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>Lua XML-RPC: XML-RPC interface to the Lua programming language</title>
    <link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  </head>
  <body>

    <div id="container">
      
      <div id="product">
	<div id="product_logo">
          <a href="http://www.keplerproject.org">
	    <img alt="Lua XML-RPC logo" src="luaxmlrpc.png"/>
	  </a>
	</div>
	<div id="product_name"><big><b>Lua XML-RPC</b></big></div>
	<div id="product_description">XML-RPC interface to the Lua programming language</div>
      </div> <!-- id="product" -->

      <div id="main">
	
	<div id="navigation">
	  <h1>Lua XML-RPC</h1>
	  <ul>
	    <li><a href="index.html">Home</a>
	      <ul>
		<li><a href="index.html#overview">Overview</a></li>
		<li><a href="index.html#status">Status</a></li>
		<li><a href="index.html#download">Download</a></li>
		<li><a href="index.html#history">History</a></li>
		<li><a href="index.html#credits">Credits</a></li>
		<li><a href="index.html#contact">Contact</a></li>
	      </ul>
	    </li>
	    <li><a href="manual.html">Manual</a>
	      <ul>
		<li><a href="manual.html#introduction">Introduction</a></li>
		<li><a href="manual.html#installation">Installation</a></li>
		<li><a href="manual.html#data_types">Data types</a></li>
		<li><a href="manual.html#functions">Library functions</a></li>
		<li><a href="manual.html#client">Client side</a></li>
		<li><a href="manual.html#server">Server side</a></li>
		<li><a href="manual.html#references">References</a></li>
	      </ul>
	    </li>
	    <li><strong>Examples</strong></li>
	    <li><a href="license.html">License</a></li>
	  </ul>
	</div> <!-- id="navigation" -->

	<div id="content">


	  <h2><a name="examples"></a>Examples</h2>

	  <h3><a name="client_example"></a>Client example</h3>

	  <p>Below is a small sample code displaying the use of the library in a
	    client application.</p>

	  <pre class="example">
	    require "xmlrpc.http"

	    local ok, res = xmlrpc.http.call (
	    "http://www.oreillynet.com/meerkat/xml-rpc/server.php", 
	    "system.listMethods")
	    print (ok)
	    for i, v in pairs(res) do print ('\t', i, v) end
	  </pre>


	  <h3><a name="types_example"></a>Type conversion example</h3>

	  <p>The next example shows how to force the conversion of types from
	    Lua to XML-RPC.</p>

	  <pre class="example">
	    require "xmlrpc"

	    double_array_type = xmlrpc.newArray ("double")
	    double_array = xmlrpc.newTypedValue ( { 1.1, 2, 3, 4 }, double_array_type)

	    double_array_array_type = xmlrpc.newArray (double_array_type)
	    double_array_array = xmlrpc.newTypedValue (
	    {
	    { 11, 12, 13, },
	    { 21, 22, 23, },
	    { 31, 32, 33, },
	    }, double_array_array_type)
	  </pre>

	  <p>The table <code>double_array_array</code> will be:</p>

	  <pre class="example">
	    &lt;array&gt;
	    &lt;data&gt;
	    &lt;value&gt;
	    &lt;array&gt;
            &lt;data&gt;
            &lt;value&gt;&lt;double&gt;11&lt;/double&gt;&lt;/value&gt;
            &lt;value&gt;&lt;double&gt;12&lt;/double&gt;&lt;/value&gt;
            &lt;value&gt;&lt;double&gt;13&lt;/double&gt;&lt;/value&gt;
            &lt;/data&gt;
	    &lt;/array&gt;
	    &lt;/value&gt;
	    &lt;value&gt;
	    &lt;array&gt;
            &lt;data&gt;
            &lt;value&gt;&lt;double&gt;21&lt;/double&gt;&lt;/value&gt;
            &lt;value&gt;&lt;double&gt;22&lt;/double&gt;&lt;/value&gt;
            &lt;value&gt;&lt;double&gt;23&lt;/double&gt;&lt;/value&gt;
            &lt;/data&gt;
	    &lt;/array&gt;
	    &lt;/value&gt;
	    &lt;value&gt;
	    &lt;array&gt;
            &lt;data&gt;
            &lt;value&gt;&lt;double&gt;31&lt;/double&gt;&lt;/value&gt;
            &lt;value&gt;&lt;double&gt;32&lt;/double&gt;&lt;/value&gt;
            &lt;value&gt;&lt;double&gt;33&lt;/double&gt;&lt;/value&gt;
            &lt;/data&gt;
	    &lt;/array&gt;
	    &lt;/value&gt;
	    &lt;/data&gt;
	    &lt;/array&gt;
	  </pre>



	  <h3><a name="server_example"></a>Server example</h3>

	  <p>Follows a small example of a server based on Xavante
	  WSAPI. You can call the service for example with
	  the <code>xmlrpc</code> tool
	  from <a href="http://xmlrpc-c.sourceforge.net/">XML-RPC
	  C</a> (<code>xmlrpc http://localhost:12345
	  hello_world</code>) or by
	  using <code>examples/client.lua</code> in the source
	  tarball.</p>

	  <pre class="example">
require("xavante")
require("xavante.httpd")
require("wsapi.xavante")
require("wsapi.request")
require("xmlrpc")

--- XML-RPC WSAPI handler
-- @param wsapi_env WSAPI environment
function wsapi_handler(wsapi_env)
   local headers = { ["Content-type"] = "text/xml" }
   local req = wsapi.request.new(wsapi_env)
   local method, arg_table = xmlrpc.srvDecode(req.POST.post_data)
   local func = xmlrpc.dispatch(method)
   local result = { pcall(func, unpack(arg_table or {})) }
   local ok = result[1]
   if not ok then
      result = { code = 3, message = result[2] }
   else
      table.remove(result, 1)
      if table.getn(result) == 1 then
         result = result[1]
      end
   end

   local r = xmlrpc.srvEncode(result, not ok)
   headers["Content-length"] = tostring(#r)

   local function xmlrpc_reply(wsapienv)
      coroutine.yield(r)
   end

   return 200, headers, coroutine.wrap(xmlrpc_reply)
end

-- XML-RPC exported functions
xmlrpc_exports = {}

--- Get simple string.
-- @return simple string
function xmlrpc_exports.hello_world()
   return "Hello World"
end

local rules = {{ match = ".", with = wsapi.xavante.makeHandler(wsapi_handler) }}
local config = { server = {host = "*", port = 12345}, defaultHost = { rules = rules} }

xmlrpc.srvMethods(xmlrpc_exports)
xavante.HTTP(config)

xavante.run()
</pre>

	  <p>Note that the package <code>post</code> and the function
	    <code>respond</code> should be provided by the cgi launcher.</p>

	</div> <!-- id="content" -->

      </div> <!-- id="main" -->

      <div id="about">
	<p>
	  <a href="http://validator.w3.org/check?uri=referer">
            valid
	  </a>
	</p>
      </div> <!-- id="about" -->

    </div> <!-- id="container" -->

  </body>
</html>