XMLRPC

  XMLRPC 为以http为传输协议,使用xml格式化数据来执行远程过程调用, 区别于本地过程调用, 即发生在不同主机之间。

  属于分布式计算的一种简单实现,比web service简单易用。xml语言被多种语言广泛支持,是一种可扩展的标记语言,

xmlrpc被多种平台实现,以此提供的服务可以客户端和服务器端使用不同语言,

具有平台无关性,比socket发送网络字节序具有优势, 不用考虑大小端问题, 且报文内容遵循规定格式, 易于处理和阅读。

  此为UseLand公司制定的一套标准,http://xmlrpc.scripting.com/default.html, 角色分服务器端和客户端, 可以被不同语言实现,

c --- http://xmlrpc-c.sourceforge.net/

lua --- https://github.com/timn/lua-xmlrpc

php --- https://github.com/gggeek/phpxmlrpc/releases/tag/2.2.2

java --- http://ws.apache.org/xmlrpc/

luaXMLRPC

  luaXMLRPC是lua语言实现的一套软件包, 可以访问 和 提供 xmlrpc服务, 即服务器和客户端角色都支持。

https://github.com/timn/lua-xmlrpc

  

  客户端主要实现功能, 将lua table数据转换为 XML RPC message,

然后通过luaSocket库以http协议发送出去,

对于响应回来的XML RPC message 使用XML解析器 luaExpat 反向解析为 lua table,和返回值。

The http.lua file implements a simple stand-alone client based on LuaSocket 2.0.2. The following function is provided:

call (url, method [, params])
Execute the call to method at location url with the given params (if any). The method and params parameters will be just passed to clEncode function. The result is the same as clDecode function: a boolean indicating whether the call was successful or not, followed by the response value (if successful) or by the faultString and the faultCode (if the call fails).

  服务器端, CGILUA接收到XML RPC message后, 使用luaExpat解析为lua table, 后调用相应方法,

将执行结果组装成XML RPC response message, 发送回客户端。

  服务器端注册方法接口:

srvMethods (tab_or_func)Register the methods on the server. The parameter can be a table or a dispatching function. If a table is given it can have one level of objects with the corresponding methods. If a function is given, it will replace the dispatcher.

  luaXMLRPC提供的一些库函数,参见来自下载包帮助文档:

  

Library functions

The xmlrpc.lua file implements the functions that encode and decode XML-RPC messages and transform data types between the Lua and XML-RPC. The functions are:

clEncode (method_name [, params]) => method_call
Build a XML-RPC document containing a methodCall element. It receives a string with the method's name and an optional list of parameters. The result is a string containing the XML-RPC document.
clDecode (method_response) => ok, results
Disassemble the server response into a Lua object. It receives a string containing the XML-RPC document representing the methodResponse element. The result is a boolean indicating wether the call was successful or not followed by the resulting objects (typically a methodResponse has only one value so only one Lua object will be returned). In case of error the false value is followed by the XMLRPC faultString and the faultCode. This values are extracted from the fault element.
srvDecode (method_call) => method_name, list_params
Disassemble the client request into a method's name and a table with the list of parameters. It receives a string containing the XML-RPC document representing the methodCall element. The result is a string with the name of the method to be called and a Lua table with the arguments to the call.
srvEncode (object, is_fault) => method_response
Build a XML-RPC document containing a methodResponse element. It receives a Lua object (a number, a string, a table, a "created typed value" etc.) with the return value of the call. The result is a string containing the XML-RPC document. Note that XML-RPC defines that a response only returns one value so a Lua function that returns more than one value has to pack them into a table to guarantee that all of them will be returned. The second parameter (is_fault) can be used to force a fault element to be generated instead of a params. In this case, the Lua object must be a table with the members faultCode and faultString.
srvMethods (tab_or_func)
Register the methods on the server. The parameter can be a table or a dispatching function. If a table is given it can have one level of objects with the corresponding methods. If a function is given, it will replace the dispatcher.
dispatch (method_name) => function
Returns a Lua function that implements the method call. Note that the object is encapsulated into that function so that the call will be turned into a real method call.

luaXMLRPC client + phpXMLRPC server 实例

  php服务器端运行环境, 需要先行安装 xamp 软件包(https://www.apachefriends.org/zh_cn/index.html),

xamp为 x系统(包括 Linux windows mac) + a (Apache web server)+ m (MySql database)+ p (php server script interpretor),

为流行的php开发环境。

  phpXMLRPC服务器端, xmlrpc还不是php的原始库,需要下载,下载地址 http://phpxmlrpc.sourceforge.net/。

下载后,解压后,将其的lib目录拷贝出来并命名为libphpxmlrpc, 拷贝到 php 的 xamp/php/pear(珍珠)目录。

使用下面博客中 加法 服务例子(http://blog.csdn.net/flyingfalcon/article/details/2229488),

在\xampp\htdocs目录下建立一个php文件 xmlrpc_server.php:

<?php

include ("libphpxmlrpc/xmlrpc.inc");
include ("libphpxmlrpc/xmlrpcs.inc"); if ($_SERVER['REQUEST_METHOD'] != 'POST')
exit(0); $add_sig = array(array($xmlrpcString, $xmlrpcInt, $xmlrpcInt));
$add_doc = "Add the two integer together"; function add($params)
{
global $xmlrpcerruser; $val = php_xmlrpc_decode($params); $ret = $val[0] + $val[1]; return new xmlrpcresp(new xmlrpcval($ret, "int"));
} $server = new xmlrpc_server(array(
"add" => array(
"function" => "add",
"signature" => $add_sig,
"docstring" => $add_doc
))); ?>

  luaXMLRPC 客户端,按照github下载的压缩包中的帮助文档, 将 src文件夹命名为 xmlrpc,

拷贝到 LUA_PATH 目录即可, 即与 lua.exe 平级。

Lua XML-RPC is composed by three Lua files:

init.lua
Main source file providing the API
http.lua
API to call XML-RPC via HTTP
server.lua
Server side API

These files should be copied to a directory named xmlrpc created in your LUA_PATH.

  luaXMLRPC客户端代码,放到test.lua中, 与lua.exe平级,功能 实现 加法 XMLRPC 请求,

require "xmlrpc.http"

local ok, res = xmlrpc.http.call ("http://127.0.0.1/xmlrpc_server.php", "add", , )

if not ok then
print ("not ok")
print("error="..res)
else
print ("ok")
print("sum ="..res);
end

  客户端运行结果

  

LuaXMLRPC笔记的更多相关文章

  1. git-简单流程(学习笔记)

    这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...

  2. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  3. SQL Server技术内幕笔记合集

    SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...

  4. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  5. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  6. NET Core-学习笔记(三)

    这里将要和大家分享的是学习总结第三篇:首先感慨一下这周跟随netcore官网学习是遇到的一些问题: a.官网的英文版教程使用的部分nuget包和我当时安装的最新包版本不一致,所以没法按照教材上给出的列 ...

  7. springMVC学习笔记--知识点总结1

    以下是学习springmvc框架时的笔记整理: 结果跳转方式 1.设置ModelAndView,根据view的名称,和视图渲染器跳转到指定的页面. 比如jsp的视图渲染器是如下配置的: <!-- ...

  8. 读书笔记汇总 - SQL必知必会(第4版)

    本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...

  9. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

随机推荐

  1. CentOS6.4 配置iptables

    如果没有安装iptables可以直接用yum安装 yum install -t iptables 检查iptables服务的状态, service iptables status 如果出现“iptab ...

  2. topcoder SRM 623 DIV2 CatAndRat

    解决本题的一个关键点就是当Cat进入时,此时Rat在哪个位置? 注意移动方向可以随时改变,由于是圆环,故离入口最远点的距离是pi*R,即圆的一半, 当cat进入时(cat的速度大于rat的速度,否则不 ...

  3. ACM: 还是畅通工程-并查集-最小生成树-解题报

    还是畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查乡村交通 ...

  4. 【BZOJ】2216: [Poi2011]Lightning Conductor

    题意 给一个长度为\(n\)的序列\(a_i\),对于每个\(1 \le i \le n\),找到最小的非负整数\(p\)满足 对于任意的\(j\), \(a_j \le a_i + p - \sqr ...

  5. 【POJ】3974 Palindrome

    http://poj.org/problem?id=3974 题意:求s的最长回文串.(|s|<=1000000) #include <cstdio> #include <cs ...

  6. 纪念逝去的岁月——C++实现一个队列(使用类模板)

    1.代码 2.运行结果 1.代码 #include <stdio.h> #include <string.h> template <typename T> clas ...

  7. javascrit2.0完全参考手册(第二版) 第1章第1节 在XHTML文档中增加javascript

    通常,向文档中增加script脚本使用<script>元素,在HTML中增加脚本的方式有4中: (1)放到<script></script>块中: (2)<s ...

  8. [Android] adb.exe换了位置

    好久没有做android开发了,今天重新下载了新的sdk,发现adb.exe从sdk/tools里面消失了,添加了系统环境变量的路径就还是没法调动adb.exe命令,网上搜了一下原理是存在了新版的sd ...

  9. Load Mental Ray in Maya 2015

    In Maya 2015, we usually use mental ray to render our model, some new users may not see the mental r ...

  10. android-数据存储之手机内部file存储

    一.基础概要 1.说明: 1>应用程序运行需要一些较大的数据或者图片可保存在手机内部 2>文件类型:任意 3>路径:/data/data/packageName/files/ 4&g ...