Object Relational Mapping Libraries for XMPP Strict but easy XMPP - - PowerPoint PPT Presentation

object relational mapping libraries for xmpp
SMART_READER_LITE
LIVE PREVIEW

Object Relational Mapping Libraries for XMPP Strict but easy XMPP - - PowerPoint PPT Presentation

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts Object Relational Mapping Libraries for XMPP Strict but easy XMPP programming, comparing with traditional libraries Sergey Dobrov XMPP


slide-1
SLIDE 1

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Object Relational Mapping Libraries for XMPP

Strict but easy XMPP programming, comparing with traditional libraries Sergey Dobrov XMPP Summit 14, Portland OR, 2013.

slide-2
SLIDE 2

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-3
SLIDE 3

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

whoami –verbose

XMPP fan, was developing stuff for fun since 2005 (games-plugins, transports, services etc.)

slide-4
SLIDE 4

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

whoami –verbose

XMPP fan, was developing stuff for fun since 2005 (games-plugins, transports, services etc.) Founder of JRuDevels: small community of Russian XMPP devs

slide-5
SLIDE 5

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

whoami –verbose

XMPP fan, was developing stuff for fun since 2005 (games-plugins, transports, services etc.) Founder of JRuDevels: small community of Russian XMPP devs

Bombus, JAJC, J2J, Vacuum-IM...

slide-6
SLIDE 6

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

whoami –verbose

XMPP fan, was developing stuff for fun since 2005 (games-plugins, transports, services etc.) Founder of JRuDevels: small community of Russian XMPP devs

Bombus, JAJC, J2J, Vacuum-IM... JaWiki: large knowledge warehouse about XMPP in Russian.

slide-7
SLIDE 7

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

whoami –verbose

XMPP fan, was developing stuff for fun since 2005 (games-plugins, transports, services etc.) Founder of JRuDevels: small community of Russian XMPP devs

Bombus, JAJC, J2J, Vacuum-IM... JaWiki: large knowledge warehouse about XMPP in Russian. Habahaba, LiJ and other experimental services.

slide-8
SLIDE 8

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Why another library?

XMPP is a beauty and complete platform to solve many problems but it can be hard for newbies to follow all the rules. It could be solved with some framework which will make some borders and dictate the good style.

slide-9
SLIDE 9

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Why another library?

XMPP is a beauty and complete platform to solve many problems but it can be hard for newbies to follow all the rules. It could be solved with some framework which will make some borders and dictate the good style. There are libraries which provide abstraction level for application developers but there’s no abstractions to implement XEPs.

slide-10
SLIDE 10

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Why another library?

XMPP is a beauty and complete platform to solve many problems but it can be hard for newbies to follow all the rules. It could be solved with some framework which will make some borders and dictate the good style. There are libraries which provide abstraction level for application developers but there’s no abstractions to implement XEPs. The quality of JS libraries is not satisfactory.

slide-11
SLIDE 11

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-12
SLIDE 12

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Problems

Despite of XML completeness and harmony it contains many hidden rocks for the newbie programmers which may lead to the serious architecture pitfalls:

slide-13
SLIDE 13

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Problems

Despite of XML completeness and harmony it contains many hidden rocks for the newbie programmers which may lead to the serious architecture pitfalls:

1 Many new programmers think: “Ah! Will parse it with

regexps and construct with concatenation”.

slide-14
SLIDE 14

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Problems

Despite of XML completeness and harmony it contains many hidden rocks for the newbie programmers which may lead to the serious architecture pitfalls:

1 Many new programmers think: “Ah! Will parse it with

regexps and construct with concatenation”.

1

Will collide with XML Entities.

2

Will do the same work again and again.

3

Will not care of specific things like XML namespaces and prefixes.

slide-15
SLIDE 15

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Problems

Despite of XML completeness and harmony it contains many hidden rocks for the newbie programmers which may lead to the serious architecture pitfalls:

1 Many new programmers think: “Ah! Will parse it with

regexps and construct with concatenation”.

1

Will collide with XML Entities.

2

Will do the same work again and again.

3

Will not care of specific things like XML namespaces and prefixes.

2 XML namespaces and prefixes. 1

Often missed even when XML parser is used.

slide-16
SLIDE 16

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Problems

Despite of XML completeness and harmony it contains many hidden rocks for the newbie programmers which may lead to the serious architecture pitfalls:

1 Many new programmers think: “Ah! Will parse it with

regexps and construct with concatenation”.

1

Will collide with XML Entities.

2

Will do the same work again and again.

3

Will not care of specific things like XML namespaces and prefixes.

2 XML namespaces and prefixes. 1

Often missed even when XML parser is used.

3 Even when XML parser is used it’s often an excessive work to

construct elements.

slide-17
SLIDE 17

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What can we do?

Obvious things:

1 Invent another abstraction level to handle these problems: 1

Parse and construct XML elements from and to programming language’s usual data primitives.

slide-18
SLIDE 18

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What can we do?

Obvious things:

1 Invent another abstraction level to handle these problems: 1

Parse and construct XML elements from and to programming language’s usual data primitives.

2

Validate elements providing ready to use general XMPP specific validators to make the borders.

slide-19
SLIDE 19

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What can we do?

Obvious things:

1 Invent another abstraction level to handle these problems: 1

Parse and construct XML elements from and to programming language’s usual data primitives.

2

Validate elements providing ready to use general XMPP specific validators to make the borders.

3

The most popular programming paradigm for now is object

  • riented, so we need some ORM.
slide-20
SLIDE 20

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Simple Example

Let’s analyze a usual XML Stanza: <message from=’juliet@example.com/balcony’ id=’ktx72v49’ to=’romeo@example.net’ type=’chat’> <body>Art thou not Romeo, and a Montague?</body> </message>

slide-21
SLIDE 21

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Possible Mapping

How would we want to see it as object? message.from_ = JID(’juliet’, ’example.com’, ’balcony’) message.id = ’ktx72v49’ message.to = JID(’romeo’, ’example.net’) message.type_ = ’chat’ message.body = ’Art thou not Romeo, and a Montague?’ Note The “from” and “to” attributes are JID objects which can implement additional properties and methods such as bare() etc.

slide-22
SLIDE 22

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Twilix Message

Let’s see how such object definition will be look like in Twilix. For the beginning, lets define the Element: class Message(VElement): Note VElement here is a Twisted’s Element subclass which has a metaclass which collects class’ fields and converts them into internal structures which will be used to parse and build elements. This idea was inspired from django forms and models. JSLiX has another approach here: it uses special objects called “definition” which describes stanzas in the same way. Given document and the definition, JSLiX can generate JS object by this definition and vice-versa: given object and definition it can generate a document.

slide-23
SLIDE 23

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Twilix Message

Let’s see how such object definition will be look like in Twilix. Let’s now define what tag name we want use: class Message(VElement): elementName = ’message’

slide-24
SLIDE 24

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Twilix Message

Let’s see how such object definition will be look like in Twilix. Obviously, we need to define a namespace as well: class Message(VElement): elementName = ’message’ elementUri = ’jabber:client’ Note

1 This namespace will be correctly added to XML when build

and correctly parsed when parse even if it’s given as prefix.

2 Will raise WrongElement exception if an element doesn’t suit

to the definition (i.e. tag name or namespace doesn’t equal)

slide-25
SLIDE 25

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Twilix Message

Let’s see how such object definition will be look like in Twilix. The last step is to define the data we will put into an element: class Message(VElement): elementName = ’message’ elementUri = ’jabber:client’ to = fields.JidAttr(’to’, required=False) from_ = fields.JidAttr(’from’, required=False) type_ = fields.StringAttr(’type’, required=False) id = fields.StringAttr(’id’, required=False) body = fields.StringNode(’body’, required=False)

slide-26
SLIDE 26

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Twilix Message

to = fields.JidAttr(’to’, required=False) from_ = fields.JidAttr(’from’, required=False) type_ = fields.StringAttr(’type’, required=False) id = fields.StringAttr(’id’, required=False) body = fields.StringNode(’body’, required=False) Note So easy! ;) JidAttr will automatically convert an attribute’s content into a JID object and will sure that it’s correct. The first argument for a field is an XML tag name or an XML attribute name. The StringNode will find a ’body’ child element with the same namespace as for the current element.

slide-27
SLIDE 27

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX analogue

JSLiX has another approach: you define an element definition which can be used both to parse and build XML. Parsed XML is just a JS object. var message_definition = jslix.Element({ element_name: ’message’, xmlns: ’jabber:client’, to: new fields.JIDAttr(’to’, false), from: new fields.JIDAttr(’from’, false), id: new fields.StringAttr(’id’, false), type: new fields.StringAttr(’type’, false) }); Note It’s not simply looks similar. ;)

slide-28
SLIDE 28

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Data Validation

How validation is processing? Each field has methods to convert values from and to programming language types (for example, xs:dateTime will be converted to python’s datetime and vice-versa. In these methods it’s possible to raise an exception if data can’t be converted.) Element can have it’s own validation rules: class ValidateTypeIq(Iq): type_ = fields.StringAttr(’type’, required=False) def clean_type_(self, value): if value not in (’set’, ’get’, ’result’, ’error’): raise ElementParseError, "Wrong iq type" # We can convert value here too return value

slide-29
SLIDE 29

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

XML Fields

What is XML Field?

slide-30
SLIDE 30

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

XML Fields

What is XML Field?

1 Object that holds rules of how to parse a piece of an element

slide-31
SLIDE 31

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

XML Fields

What is XML Field?

1 Object that holds rules of how to parse a piece of an element 2 Responsible of getting that piece of data from an element and

put it back

slide-32
SLIDE 32

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

XML Fields

What is XML Field?

1 Object that holds rules of how to parse a piece of an element 2 Responsible of getting that piece of data from an element and

put it back

3 It has special methods to convert data to the internal

programming language types and be sure that the data can be

  • converted. So:
slide-33
SLIDE 33

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

XML Fields

What is XML Field?

1 Object that holds rules of how to parse a piece of an element 2 Responsible of getting that piece of data from an element and

put it back

3 It has special methods to convert data to the internal

programming language types and be sure that the data can be

  • converted. So:

1

It’s possible to reduce programmers work by implemented most frequently used types. For example, it abuses to convert xs:dateTime or base64 manually.

slide-34
SLIDE 34

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

XML Fields

What is XML Field?

1 Object that holds rules of how to parse a piece of an element 2 Responsible of getting that piece of data from an element and

put it back

3 It has special methods to convert data to the internal

programming language types and be sure that the data can be

  • converted. So:

1

It’s possible to reduce programmers work by implemented most frequently used types. For example, it abuses to convert xs:dateTime or base64 manually.

4 For Node fields it’s also possible to switch on a “listed” mode

which will mean that this field can contain a list of such values which will be converted into a sequence of similar elements

slide-35
SLIDE 35

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Queries

It’s possible to define handlers not for top level stanzas but also for queries elements like this: class Query(VElement): elementName = ’query’ elementUri = ’jabber:iq:version’ # This points to the top level stanza type parentClass = Iq # Some fields here def getHandler(self): ’’’ Here the code which will be executed if version get query will be received’’’ def setHandler(self): pass

slide-36
SLIDE 36

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-37
SLIDE 37

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What dispatcher is?

1 Some object which handles incoming and outgoing stanzas 1

Tries to parse incoming stanzas with registered definitions and if parsing succeeded then start handler.

slide-38
SLIDE 38

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What dispatcher is?

1 Some object which handles incoming and outgoing stanzas 1

Tries to parse incoming stanzas with registered definitions and if parsing succeeded then start handler.

2

If parse has failed it can skip the handler or return an error and break the handlers chain depending on the raised exception.

2 Probably need different dispatchers for different tasks:

slide-39
SLIDE 39

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What dispatcher is?

1 Some object which handles incoming and outgoing stanzas 1

Tries to parse incoming stanzas with registered definitions and if parsing succeeded then start handler.

2

If parse has failed it can skip the handler or return an error and break the handlers chain depending on the raised exception.

2 Probably need different dispatchers for different tasks: 1

Clients

slide-40
SLIDE 40

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What dispatcher is?

1 Some object which handles incoming and outgoing stanzas 1

Tries to parse incoming stanzas with registered definitions and if parsing succeeded then start handler.

2

If parse has failed it can skip the handler or return an error and break the handlers chain depending on the raised exception.

2 Probably need different dispatchers for different tasks: 1

Clients

2

Transport or services

slide-41
SLIDE 41

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What dispatcher is?

1 Some object which handles incoming and outgoing stanzas 1

Tries to parse incoming stanzas with registered definitions and if parsing succeeded then start handler.

2

If parse has failed it can skip the handler or return an error and break the handlers chain depending on the raised exception.

2 Probably need different dispatchers for different tasks: 1

Clients

2

Transport or services

3

XMPP Servers?

slide-42
SLIDE 42

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

What dispatcher is?

1 Some object which handles incoming and outgoing stanzas 1

Tries to parse incoming stanzas with registered definitions and if parsing succeeded then start handler.

2

If parse has failed it can skip the handler or return an error and break the handlers chain depending on the raised exception.

2 Probably need different dispatchers for different tasks: 1

Clients

2

Transport or services

3

XMPP Servers?

4

Not implemented yet. Current dispatcher implementation tries to suit any possible needs but it’s messy.

slide-43
SLIDE 43

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Each XMPP error also has a mapping to a programming language

  • exception. So it’s possible to say:

exception = errors.ItemNotFoundException( reason=’Something went wrong’) print exception.content.toXml() And get this: <error type=’cancel’> <text xmlns=’urn:ietf:params:xml:ns:xmpp-stanzas’> Something went wrong </text> <item-not-found xmlns=’urn:ietf:params:xml:ns:xmpp-stanzas’/> </error>

slide-44
SLIDE 44

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Despite the fact XMPP is an asynchronous protocol it’s now possible, thanks to generator functions and @inlineCallbacks decorator, to write a code for it in much simpler synchronous style (not available in JS yet). query = SomeQuery() iq = Iq(to=’somenode@someserver’, type_=’get’) iq.link(query) try: result = yield dispatcher.send(iq) except FeatureNotImplementedException: print ’Client does not support this feature’ else: print result.some_result

slide-45
SLIDE 45

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Or in the case of handler: class SomeQuery(Query): authed = fields.FlagNode(’authed’) @inlineCallbacks def getHandler(self): if not self.correct: # sends not-authorized error back raise errors.NotAuthorizedException( reason=’Authorization required’) # sends blank iq result back return self.iq.makeResult()

slide-46
SLIDE 46

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-47
SLIDE 47

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Feature Twilix JSLiX Pyxmpp2 Wokkel JSJaC Strophe Roster yes not yet yes yes no no Disco yes not yet yes yes no no SI FT yes no no no no no Forms yes not yet yes yes no no Jingle not yet coming no no yes? yes MUC partial not yet yes yes no no Note It seems that python libraries have much higher quality and number of supported features. Usually, porting supported protocols from Twilix to JSLiX is easy because of similar approach with such much harder to design good API than actually implement it. Also, would be good to point out that the same features on other libraries take much more code because they parse XML naive even when they use ORM approach (Wokkel and PyXMPP2)

slide-48
SLIDE 48

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Is it used anywhere now? Are there any problems?

1 Only in my own projects, because 1

Need to be optimized yet

2

Has no documentation

3

Need more tests

4

Need internatiolization framework

slide-49
SLIDE 49

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Is it used anywhere now? Are there any problems?

1 Only in my own projects, because 1

Need to be optimized yet

2

Has no documentation

3

Need more tests

4

Need internatiolization framework

2 Habahaba (jslix+twilix)

slide-50
SLIDE 50

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Is it used anywhere now? Are there any problems?

1 Only in my own projects, because 1

Need to be optimized yet

2

Has no documentation

3

Need more tests

4

Need internatiolization framework

2 Habahaba (jslix+twilix) 3 Live Jabber 1

Aggregator, Many gateways to other networks

2

Simple framework to write new blogging gateways

slide-51
SLIDE 51

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Is it used anywhere now? Are there any problems?

1 Only in my own projects, because 1

Need to be optimized yet

2

Has no documentation

3

Need more tests

4

Need internatiolization framework

2 Habahaba (jslix+twilix) 3 Live Jabber 1

Aggregator, Many gateways to other networks

2

Simple framework to write new blogging gateways

4 New version of J2J (yet in construction)

slide-52
SLIDE 52

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-53
SLIDE 53

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Software Version Protocol Implementation example

So, how to implement the “jabber:iq:version” protocol in Twilix? First of all we’ll define VersionQuery element: class VersionQuery(Query): elementUri = ’jabber:iq:version’ client_name = StringNode(’name’, required=False) client_version = StringNode(’version’, required=False) client_os = StringNode(’os’, required=False)

slide-54
SLIDE 54

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Software Version Protocol Implementation example

Then we’ll define a class which will represent a protocol: class ClientVersion(object): def __init__(self, dispatcher, client_name=None, client_version=None, client_os=None): self.dispatcher = dispatcher self.client_name = client_name self.client_version = client_version self.client_os = client_os def __unicode__(self): return ’%s %s running on %s’ % (self.client_name, self.client_version, self.client_os) We’ll need dispatcher to have a place where to send our stanzas and client name, version and os to respond to queries with.

slide-55
SLIDE 55

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Software Version Protocol Implementation example

Then we’ll need a method that will initialize the protocol handlers and, if needed, add appropriate feature to the disco. def init(self, disco=None): self.dispatcher.registerHandler( (self.ClientVersion, self)) if disco is not None: disco.root_info.addFeatures( Feature(var=’jabber:iq:version’)) What’s the reason to do it in a separate method instead of contructor? The protocol will define also a method to query another’s entity version. We could want to use it but hide our own version then we won’t invoke the second init.

slide-56
SLIDE 56

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Software Version Protocol Implementation example

Now let’s define the method to query other’s version: def getVersion(self, jid, from_=None): if from_ is None: from_ = self.dispatcher.myjid query = self.VersionQuery(host=self, parent=Iq(type_=’get’, to=jid, from_=from_)) # will return a Deferred object return self.dispatcher.send(query)

slide-57
SLIDE 57

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

As we can see the method returns deferred object, so it can be used with yield too: try: entity_version = yield version.getVersion( ’user@example.com’) except FeatureNotImplementedException: print ’Feature not implemented’ else: print str(entity_version)

slide-58
SLIDE 58

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Software Version Protocol Implementation example

Finally, we should define handlers. def getHandler(self): iq = self.iq.makeResult() query = VersionQuery(client_name=self.host.client_name, client_version=self.host.client_version, client_os=self.host.client_os, parent=iq) return query def setHandler(self): # this protocol doesn’t support set IQs raise errors.BadRequestException() That’s it! Now our client will reply to the version queries and will be able to request versions from another clients.

slide-59
SLIDE 59

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Counter-example: PyXMPP2

Element parser: @classmethod def from_xml(cls, element): name = None version = None

  • s_name = None

for child in element: if child.tag == NAME_TAG: name = child.text elif child.tag == VERSION_TAG: version = child.text elif child.tag == OS_TAG:

  • s_name = child.text

return cls(name, version, os_name)

slide-60
SLIDE 60

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Counter-example: PyXMPP2

Element builder: def as_xml(self): element = ElementTree.Element(QUERY_TAG) if self.name is None and self.version is None and self.os_name return element sub = ElementTree.SubElement(element, NAME_TAG) sub.text = self.name sub = ElementTree.SubElement(element, VERSION_TAG) sub.text = self.version if self.os_name: sub = ElementTree.SubElement(element, OS_TAG) sub.text = self.os_name return element

slide-61
SLIDE 61

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Counter-example: PyXMPP2

Handler is very similar to Twilix’s but that’s the request version subroutine: stanza = Iq(to_jid = target_jid, stanza_type = "get") payload = VersionPayload() stanza.set_payload(payload) def wrapper(stanza): payload = stanza.get_payload(VersionPayload) if payload is None: if error_callback: error_callback(stanza) else: logger.warning("Invalid version query response." else: callback(payload) stanza_processor.set_response_handlers(stanza, wrapper, error_callback) stanza_processor.send(stanza)

slide-62
SLIDE 62

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Counter-example: JSJaC + Jappix

Similar problems in the Jappix web client which uses JSJaC: that’s how it handles request: else if((iqQueryXMLNS == NS_VERSION) && (iqType == ’get’)) var iqQuery = iqResponse.setQuery(NS_VERSION); iqQuery.appendChild(iq.buildNode(’name’, {’xmlns’: NS_VERSION}, ’Jappix’)); iqQuery.appendChild(iq.buildNode(’version’, {’xmlns’: NS_VERSION}, JAPPIX_VERSION)); iqQuery.appendChild(iq.buildNode(’os’, {’xmlns’: NS_VERSION}, BrowserDetect.OS)); con.send(iqResponse);}

slide-63
SLIDE 63

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

urn:xmpp:delay parsing from the XMPP-FTW: var NS = exports.NS = ’urn:xmpp:delay’ exports.parse = function(stanza, data) { var element, reason if (!(element = stanza.getChild(’delay’, NS))) return var delay = { when: element.attrs.stamp } if (element.attrs.from) delay.from = element.attrs.from if (!!(reason = element.getText())) delay.reason = reason data.delay = delay }

slide-64
SLIDE 64

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

...aaand building: exports.build = function(stanza, data) { if (!data || (typeof data !== ’object’)) return var attrs = { xmlns: NS } if (data.when) attrs.stamp = data.when if (data.from) attrs.from = data.from var delay = stanza.c(’delay’, attrs) if (data.reason) delay.t(data.reason) }

slide-65
SLIDE 65

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

The same with JSLiX: delay: stanzas.Element({ element_name: ’delay’, xmlns: ’urn:xmpp:delay’, from: new fields.JIDAttr(’from’, false), stamp: new fields.DateTimeAttr(’stamp’, true), description: new fields.StringNode(null, false, false, undefined, true) })

slide-66
SLIDE 66

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

Note that:

1 This small JSLiX definition will handle both parsing and

building.

slide-67
SLIDE 67

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

Note that:

1 This small JSLiX definition will handle both parsing and

building.

2 It will also automatically convert JID to special JID object in

JS which will handle validation and special methods like bare()

slide-68
SLIDE 68

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

Note that:

1 This small JSLiX definition will handle both parsing and

building.

2 It will also automatically convert JID to special JID object in

JS which will handle validation and special methods like bare()

3 And timestamp will be as well converted to Date() ;)

slide-69
SLIDE 69

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. XMPP-FTW

Note that:

1 This small JSLiX definition will handle both parsing and

building.

2 It will also automatically convert JID to special JID object in

JS which will handle validation and special methods like bare()

3 And timestamp will be as well converted to Date() ;) 4 Bonus: JSLiX will sure that all data is correct before provide

it to handlers

slide-70
SLIDE 70

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. stanza.io

1 https://github.com/legastero/stanza.io

slide-71
SLIDE 71

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. stanza.io

1 https://github.com/legastero/stanza.io 2 I had no time to look deep enough but the approach seems

very similar to JSLiX

slide-72
SLIDE 72

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. stanza.io

1 https://github.com/legastero/stanza.io 2 I had no time to look deep enough but the approach seems

very similar to JSLiX

3 But nested elements use manual parsing and building just as

in other libs leading to the same problems.

slide-73
SLIDE 73

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

JSLiX vs. stanza.io

1 https://github.com/legastero/stanza.io 2 I had no time to look deep enough but the approach seems

very similar to JSLiX

3 But nested elements use manual parsing and building just as

in other libs leading to the same problems.

4 Did not dive into the dispatch mechanisms because the

previous thing made me sad.

slide-74
SLIDE 74

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-75
SLIDE 75

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Just an idea: XMPP clients support a limit number of XEPs, so there is a subset of sufficient definitions for them to work. But this way we can move XML processing to the server side and on client side use only JSON: { from: ’juliet@example.com/balcony’, to: ’romeo@example.net’, id: ’ktx72v49’, type: ’chat’, body: ’Art thou not Romeo, and a Montague?’, __definition__: ’stanzas.Message’ } It’s really easy to convert XMPP services to web REST APIs that way. The data looks this way in jslix now but ” definition ” is a definition object instead of string, so other dispatcher approach is

  • needed. It’s also possible to move this string from JSON to URL:

http://example.com/xmpp-api/message/

slide-76
SLIDE 76

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Table Of Contents

1

Introduction

2

XML ORM Approach XML Elements

3

Dispatcher Key points Errors Synchronous XMPP

4

Current State Implemented Features Where is it used?

5

Comparing examples Software Version Protocol

6

JSON-XMPP (a bonus)

7

Contacts

slide-77
SLIDE 77

Introduction XML ORM Approach Dispatcher Current State Comparing examples JSON-XMPP (a bonus) Contacts

Contacts You can contact me via email and XMPP: binary@JRuDevels.org

  • r sergey.dobrov@hidevlab.com.

My github is: http://github.com/jbinary Website: http://hidevlab.com/ Thanks for your attention!