diff --git a/index.js b/index.js index 0c1bc31..17fc2aa 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib/vertxBus.js'); +module.exports = require('./lib/vertxbus.js'); diff --git a/lib/vertxBus.js b/lib/vertxBus.js deleted file mode 100644 index 6fce7cf..0000000 --- a/lib/vertxBus.js +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright 2011-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -SockJS = require("sockjs-client-ws"); - -var vertx = exports || {}; - -vertx.EventBus = function(url, options) { - - var that = this; - var sockJSConn = new SockJS.create(url, options); - sockJSConn.on("connection", function(){ - sockJSConn.onopen(); - }); - sockJSConn.on("data", function(message){ - sockJSConn.onmessage({data:message}); - }); - sockJSConn.on("close", function(){ - sockJSConn.onclose(); - }); - - var handlerMap = {}; - var replyHandlers = {}; - var state = vertx.EventBus.CONNECTING; - - that.onopen = null; - that.onclose = null; - - that.send = function(address, message, replyHandler) { - sendOrPub("send", address, message, replyHandler) - } - - that.publish = function(address, message, replyHandler) { - sendOrPub("publish", address, message, replyHandler) - } - - that.registerHandler = function(address, handler) { - checkSpecified("address", 'string', address); - checkSpecified("handler", 'function', handler); - checkOpen(); - var handlers = handlerMap[address]; - if (!handlers) { - handlers = [handler]; - handlerMap[address] = handlers; - // First handler for this address so we should register the connection - var msg = { type : "register", - address: address }; - sockJSConn.send(JSON.stringify(msg)); - } else { - handlers[handlers.length] = handler; - } - } - - that.unregisterHandler = function(address, handler) { - checkSpecified("address", 'string', address); - checkSpecified("handler", 'function', handler); - checkOpen(); - var handlers = handlerMap[address]; - if (handlers) { - var idx = handlers.indexOf(handler); - if (idx != -1) handlers.splice(idx, 1); - if (handlers.length == 0) { - // No more local handlers so we should unregister the connection - - var msg = { type : "unregister", - address: address}; - sockJSConn.send(JSON.stringify(msg)); - delete handlerMap[address]; - } - } - } - - that.close = function() { - checkOpen(); - state = vertx.EventBus.CLOSING; - sockJSConn.close(); - } - - that.readyState = function() { - return state; - } - - sockJSConn.onopen = function() { - state = vertx.EventBus.OPEN; - if (that.onopen) { - that.onopen(); - } - }; - - sockJSConn.onclose = function() { - state = vertx.EventBus.CLOSED; - if (that.onclose) { - that.onclose(); - } - }; - - sockJSConn.onmessage = function(e) { - var msg = e.data; - var json = JSON.parse(msg); - var body = json.body; - var replyAddress = json.replyAddress; - var address = json.address; - var replyHandler; - if (replyAddress) { - replyHandler = function(reply, replyHandler) { - // Send back reply - that.send(replyAddress, reply, replyHandler); - }; - } - var handlers = handlerMap[address]; - if (handlers) { - // We make a copy since the handler might get unregistered from within the - // handler itself, which would screw up our iteration - var copy = handlers.slice(0); - for (var i = 0; i < copy.length; i++) { - copy[i](body, replyHandler); - } - } else { - // Might be a reply message - var handler = replyHandlers[address]; - if (handler) { - delete replyHandlers[replyAddress]; - handler(body, replyHandler); - } - } - } - - function sendOrPub(sendOrPub, address, message, replyHandler) { - checkSpecified("address", 'string', address); - checkSpecified("message", 'object', message); - checkSpecified("replyHandler", 'function', replyHandler, true); - checkOpen(); - var envelope = { type : sendOrPub, - address: address, - body: message }; - if (replyHandler) { - var replyAddress = makeUUID(); - envelope.replyAddress = replyAddress; - replyHandlers[replyAddress] = replyHandler; - } - var str = JSON.stringify(envelope); - sockJSConn.send(str); - } - - function checkOpen() { - if (state != vertx.EventBus.OPEN) { - throw new Error('INVALID_STATE_ERR'); - } - } - - function checkSpecified(paramName, paramType, param, optional) { - if (!optional && !param) { - throw new Error("Parameter " + paramName + " must be specified"); - } - if (param && typeof param != paramType) { - throw new Error("Parameter " + paramName + " must be of type " + paramType); - } - } - - function isFunction(obj) { - return !!(obj && obj.constructor && obj.call && obj.apply); - } - - function makeUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" - .replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)})} - -} - -vertx.EventBus.CONNECTING = 0; -vertx.EventBus.OPEN = 1; -vertx.EventBus.CLOSING = 2; -vertx.EventBus.CLOSED = 3; \ No newline at end of file diff --git a/lib/vertxbus.js b/lib/vertxbus.js new file mode 100644 index 0000000..3414438 --- /dev/null +++ b/lib/vertxbus.js @@ -0,0 +1,935 @@ + + + + + +
+ + +
+ 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 ++ |
+
+ |
+