RPC over Internet with JS

11 downloads 116 Views 1MB Size Report
Example. Il Signore degli Anelli, di John Ronald Reuel Tolkien,. Bompiani. Title. Author di John Ronald Reuel Tolkien,. Bompiani. Editor ...
RPC OVER INTERNET

Web API: RPC over Internet • RPC calls mapped onto HTTP (GET or POST) • Synchronous call • Asynchronous call • with callbacks • External data representation • JSON (Java Simple Object Notation) • http://www.json.org/json-it.html

• XML

• Protocols • REST, SOAP, JSONP, JSON-RPC, XML-RPC,…

JSON

JSON • Two fundamental structures:

See JSON Lint

XML in a nutshell • XML (eXtensible Markup Language) is a language derived

from SGML (Standard Generalized Markup Language), from which HTML also derives. • The key notion in the markup language familty is a

markup, something that describes some aspect of the data • In

HTML markups define the appearance of the document, whereas in XML they define the meaning of the data

XML in a nutshell • An XML document is a tree • Data appear inside elements • An element not only contains the data itself but also

information describing the meaning of the data

Example Title

Author

Il Signore degli Anelli, di John Ronald Reuel Tolkien, Bompiani.

Editor

Example Book Title Author Editor

Elements

Il Signore degli Anelli John R. R. Tolkien Bompiani

Tips : Browser allows to see the structure of the document

Elements and attribute Data Il Signore degli Anelli

Attribute (key/value pair) Tag Il Signore degli Anelli

Well formed document

Valid document • Document that follows composition rules about the its

structure • Two solutions • Document Type Definition • Easier, less ‘powerfull’

• XML Schema • More complex, more powerfull

WFD and validation XML Document

Syntax check

Grammar

Semantic check

XML-RPC

Overview of XML-RPC • Data Model • Scalar • Struct • Array • Messages • Request message • Response message • Error message

Scalar types Tag

Type

Example

or

four-byte signed integer

-12



0 (false) or 1 (true)

1



string

hello world



double-precision signed floating point number

-12.214



date/time



base64-encoded binary

20101504T09:30:00

eW91IGNhbid0IHJlYWQgdGhpcyE=

Struct type lowerBound 18 upperBound 139

Array type 12 Egypt 0 -31

Request message • Root element: methodCall • contains a MethodName element and a params

element • MethodName contains the name of the procedure being called • Params is a list of values for the parameters

Example XXX 123 12.3

Example XXX 123 12.3

methodCall methodName XXX

params param

value

param

value

i4

123

double 12.3

Example Scuola XXX 123 12.3 methodCall params methodName param i4

param double

methodCall methodName XXX

params param

value

param

value

i4

123

double 12.3

Complete request message POST /xmlrpc HTTP 1.0 User-Agent: … Host: … Content-type: text/xml Content-length: .. Circle_Area 12.2

Reply message 1

Fault message faultCode 4 faultString Too many parameters.

JSON-RPC 2.0 • JSON-RPC is a stateless, transport agnostic, light-weight

remote procedure call (RPC) protocol • Much like XML-RPC • No formal grammar • Definition of request object and reply object

• New feature: batch requests, notification

Some example --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1} {"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2} {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3} {"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4} [ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"}, {"foo": "boo"}, {"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"}, {"jsonrpc": "2.0", "method": "get_data", "id": "9"} ]