Permalink
Browse files

moved parse and serialize methods into separate classes; created joap…

… specific stanzas
  • Loading branch information...
flosse committed Jan 3, 2012
1 parent 31a24bd commit 5f55d2e8ccb2b564cec66cfe0fd61aba8c22ff02
View
@@ -1 +1,2 @@
lib/*.coffee
+spec/*.coffee
View
@@ -23,11 +23,13 @@ comp = new xmpp.Component
port : "8888"
classes = {}
+objects = {}
router = new joap.Router comp
router.on "action", (action, clazz, instance, iq) ->
- console.log action.type
+ if clazz? and instance? and action.type is "read"
+ router.sendResponse joap.serialize(objects[clazz][instance], action), iq
router.on "read", (action, clazz, instance, iq) ->
console.log "read iq received"
View
@@ -0,0 +1,82 @@
+ltx = require "ltx"
+joap = require "./node-xmpp-joap"
+
+JOAP_NS = "jabber:iq:joap"
+RPC_NS = "jabber:iq:rpc"
+JOAP_STANZAS = ["describe", "read","add", "edit", "delete", "search"]
+
+class Parser
+
+ @getType: (xml) ->
+ if Parser.isJOAPStanza xml then xml.getName().toLowerCase()
+ else if Parser.isRPCStanza xml then "rpc"
+
+ @isJOAPStanza: (xml) ->
+ (xml.name in JOAP_STANZAS and xml.attrs?.xmlns is JOAP_NS)
+
+ @isRPCStanza: (xml) ->
+ (xml.name is "query" and xml.attrs?.xmlns is RPC_NS)
+
+ @parse: (xml) ->
+
+ if typeof xml in ["string", "number", "boolean"] then xml
+
+ else if xml instanceof Array
+ (Parser.parse c for c in xml)
+
+ else if xml instanceof ltx.Element
+
+ if Parser.isJOAPStanza xml
+ action = {
+ type: Parser.getType xml
+ }
+ attrs = {}
+
+ a = xml.getChildren "attribute"
+ if a.length > 0
+ attrs[c.name] = c.value for c in Parser.parse a
+ action.attributes = attrs
+ n = xml.getChildren "name"
+ action.limits = Parser.parse(n) if n.length > 0
+ action
+ else if Parser.isRPCStanza xml
+ call = xml.getChild "methodCall"
+ {
+ type: Parser.getType xml
+ method: call.getChildText "methodName"
+ params: Parser.parse call.getChild "params"
+ }
+ else
+
+ child = xml.children?[0]
+
+ switch xml.getName()
+ when "string", "name"
+ child
+ when "i4", "int", "double"
+ child * 1
+ when "boolean"
+ (child is "true" or child is "1")
+ when "value"
+ Parser.parse child
+ when "struct"
+ struct = {}
+ members = (Parser.parse m for m in xml.getChildren "member")
+ struct[m.name] = m.value for m in members
+ struct
+ when "array"
+ Parser.parse xml.getChild "data"
+ when "params"
+ (Parser.parse c.getChild "value" for c in xml.getChildren "param")
+ when "data"
+ data = []
+ for d in xml.getChildren "value"
+ data.push Parser.parse d
+ data
+ when "member", "attribute"
+ {
+ name: xml.getChildText "name"
+ value: Parser.parse xml.getChild "value"
+ }
+
+exports.Parser = Parser
View
@@ -1,5 +1,6 @@
ltx = require "ltx"
events = require "events"
+joap = require "./node-xmpp-joap"
JOAP_NS = "jabber:iq:joap"
RPC_NS = "jabber:iq:rpc"
@@ -10,14 +11,14 @@ class Router extends events.EventEmitter
constructor: (@xmpp) ->
@xmpp.on "stanza", (iq) =>
- if iq.name is "iq" and (Router.isJOAPStanza xml or Router.isRPCStanza xml)
+ if iq.name is "iq" and (joap.isJOAPStanza xml or joap.isRPCStanza xml)
child = iq.children?[0]
to = iq.attrs.to
clazz = to.split('@')[0]
instance = to.split('/')[1]
- action = Router.parse child
+ action = joap.parse child
@emit action.type, action, clazz, instance, iq
@emit "action", action, clazz, instance, iq
@@ -29,113 +30,30 @@ class Router extends events.EventEmitter
to: iq.attrs.from
from: iq.attrs.to
- err
- .c(action, xmlns: JOAP_NS).up()
- .c("error", code: code)
- .t(msg)
+ if action.type isnt "rpc"
+ if action.type in JOAP_STANZAS
+ err.c(action.type, xmlns: JOAP_NS).up()
+ err
+ .c("error", code: code)
+ .t(msg)
+ else if action.type is "rpc"
+ err
+ .c("query", xmlns: RPC_NS)
+ .c("methodResponse")
+ .c("fault")
+ .cnode(new joap.Value { faultCode: code, faultString: msg })
@xmpp.send err
- @getType: (xml) ->
- if Router.isJOAPStanza xml then xml.getName().toLowerCase()
- else if Router.isRPCStanza xml then "rpc"
+ sendResponse: (data, iq) ->
- @isJOAPStanza: (xml) ->
- (xml.name in JOAP_STANZAS and xml.attrs?.xmlns is JOAP_NS)
-
- @isRPCStanza: (xml) ->
- (xml.name is "query" and xml.attrs?.xmlns is RPC_NS)
-
- @parse: (xml) ->
-
- if typeof xml in ["string", "number", "boolean"] then xml
-
- else if xml instanceof Array
- (Router.parse c for c in xml)
-
- else if xml instanceof ltx.Element
-
- if Router.isJOAPStanza xml
- action = {
- type: Router.getType xml
- }
- attrs = {}
-
- a = xml.getChildren "attribute"
- if a.length > 0
- attrs[c.name] = c.value for c in Router.parse a
- action.attributes = attrs
- n = xml.getChildren "name"
- action.limits = Router.parse(n) if n.length > 0
- action
- else if Router.isRPCStanza xml
- call = xml.getChild "methodCall"
- {
- type: Router.getType xml
- method: call.getChildText "methodName"
- params: Router.parse call.getChild "params"
- }
- else
-
- child = xml.children?[0]
-
- switch xml.getName()
- when "string", "name"
- child
- when "i4", "int", "double"
- child * 1
- when "boolean"
- (child is "true" or child is "1")
- when "value"
- Router.parse child
- when "struct"
- struct = {}
- members = (Router.parse m for m in xml.getChildren "member")
- struct[m.name] = m.value for m in members
- struct
- when "array"
- Router.parse xml.getChild "data"
- when "params"
- (Router.parse c.getChild "value" for c in xml.getChildren "param")
- when "data"
- data = []
- for d in xml.getChildren "value"
- data.push Router.parse d
- data
- when "member", "attribute"
- {
- name: xml.getChildText "name"
- value: Router.parse xml.getChild "value"
- }
-
- @serialize: (val) ->
-
- switch typeof val
-
- when "string"
- (new ltx.Element "string").t(val)
-
- when "number"
- if val % 1 is 0
- (new ltx.Element "i4").t(val.toString())
- else
- (new ltx.Element "double").t(val.toString())
-
- when "boolean"
- (new ltx.Element "boolean").t(if val is true then "1" else "0")
+ res = new ltx.Element "iq",
+ id: iq.attrs.id
+ type:'result'
+ to: iq.attrs.from
+ from: iq.attrs.to
- when "object"
- if val instanceof Array
- vals = (Router.serialize v for v in val)
- el = (new ltx.Element "array").c("data")
- for v in vals
- el.c("value").cnode(v).up()#+.up()
- el.tree()
- else
- struct = new ltx.Element "struct"
- for own k,v of val
- struct.c("member").c("name").t(k.toString())
- .up().c("value").cnode(Router.serialize v)
- struct.tree()
+ res.cnode data
+ @xmpp.send res
exports.Router = Router
View
@@ -0,0 +1,59 @@
+ltx = require "ltx"
+joap = require "./node-xmpp-joap"
+
+JOAP_NS = "jabber:iq:joap"
+RPC_NS = "jabber:iq:rpc"
+JOAP_STANZAS = ["describe", "read","add", "edit", "delete", "search"]
+
+Element = ltx.Element
+
+class Serializer
+
+ @serialize: (val, action) ->
+
+ if action?
+ if action.type isnt "rpc"
+ el = (new Element action.type, {xmlns:JOAP_NS})
+ switch action.type
+ when "read"
+ for k,v of val
+ el.cnode(new joap.Attribute k,v)
+ el
+ when "add"
+ el.c("newAddress").t(val).up()
+ when "edit"
+ el.c("newAddress").t(val) if val?
+ el
+ when "delete"
+ el
+ when "search"
+ for v in val
+ el.c("item").t(v.toString()).up()
+ el
+
+ else if action.type is "rpc"
+ el = (new Element "query", {xmlns:RPC_NS})
+
+ else if val?
+
+ switch typeof val
+
+ when "string"
+ (new Element "string").t(val)
+
+ when "number"
+ if val % 1 is 0
+ (new Element "int").t(val.toString())
+ else
+ (new Element "double").t(val.toString())
+
+ when "boolean"
+ (new Element "boolean").t(if val is true then "1" else "0")
+
+ when "object"
+ if val instanceof Array
+ new joap.Array val
+ else
+ new joap.Struct val
+
+exports.Serializer = Serializer
View
@@ -1 +1,20 @@
-exports.Router = require("./Router").Router
+stanza = require("./stanza")
+
+exports.Router = require("./Router").Router
+
+exports.Attribute = stanza.Attribute
+exports.Member = stanza.Member
+exports.Struct = stanza.Struct
+exports.Array = stanza.Array
+exports.Value = stanza.Value
+
+Parser = require("./Parser").Parser
+
+exports.Parser = Parser
+exports.parse = Parser.parse
+exports.isJOAPStanza = Parser.isJOAPStanza
+exports.isRPCStanza = Parser.isRPCStanza
+
+Serializer = require("./Serializer").Serializer
+exports.Serializer = Serializer
+exports.serialize = Serializer.serialize
View
@@ -0,0 +1,43 @@
+ltx = require "ltx"
+joap = require "./node-xmpp-joap"
+
+class KeyVal extends ltx.Element
+ constructor: (name, key, value) ->
+ super name
+ @c("name")
+ .t(key.toString()).up()
+ .cnode(new Value value).up().up()
+
+class Attribute extends KeyVal
+
+ constructor: (key, value) ->
+ super "attribute", key, value
+
+class Member extends KeyVal
+
+ constructor: (key, value) ->
+ super "member", key, value
+
+class Struct extends ltx.Element
+ constructor: (obj) ->
+ super "struct"
+ for own k,v of obj
+ @cnode(new Member k,v).up()
+
+class Array extends ltx.Element
+ constructor: (arr) ->
+ super "array"
+ data = @c "data"
+ for v in arr
+ data.cnode(new Value v)
+
+class Value extends ltx.Element
+ constructor: (val) ->
+ super "value"
+ @cnode(joap.serialize val)
+
+exports.Attribute = Attribute
+exports.Member = Member
+exports.Struct = Struct
+exports.Array = Array
+exports.Value = Value
Oops, something went wrong.

0 comments on commit 5f55d2e

Please sign in to comment.