You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by zh...@apache.org on 2008/05/28 03:56:25 UTC

svn commit: r660779 - in /incubator/shindig/trunk/features: features.txt pubsub/ pubsub/feature.xml pubsub/pubsub-router.js pubsub/pubsub.js

Author: zhen
Date: Tue May 27 18:56:25 2008
New Revision: 660779

URL: http://svn.apache.org/viewvc?rev=660779&view=rev
Log:
Added "pubsub", an OpenSocial 0.8 feature for gadget-to-gadget communication.


Added:
    incubator/shindig/trunk/features/pubsub/
    incubator/shindig/trunk/features/pubsub/feature.xml
    incubator/shindig/trunk/features/pubsub/pubsub-router.js
    incubator/shindig/trunk/features/pubsub/pubsub.js
Modified:
    incubator/shindig/trunk/features/features.txt

Modified: incubator/shindig/trunk/features/features.txt
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/features.txt?rev=660779&r1=660778&r2=660779&view=diff
==============================================================================
--- incubator/shindig/trunk/features/features.txt (original)
+++ incubator/shindig/trunk/features/features.txt Tue May 27 18:56:25 2008
@@ -17,3 +17,4 @@
 features/tabs/feature.xml
 features/views/feature.xml
 features/oauth/feature.xml
+features/pubsub/feature.xml

Added: incubator/shindig/trunk/features/pubsub/feature.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/pubsub/feature.xml?rev=660779&view=auto
==============================================================================
--- incubator/shindig/trunk/features/pubsub/feature.xml (added)
+++ incubator/shindig/trunk/features/pubsub/feature.xml Tue May 27 18:56:25 2008
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you 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.
+-->
+<feature>
+  <name>pubsub</name>
+  <dependency>rpc</dependency>
+  <gadget>
+    <script src="pubsub.js"/>
+  </gadget>
+  <container>
+    <script src="pubsub-router.js"/>
+  </container>
+</feature>

Added: incubator/shindig/trunk/features/pubsub/pubsub-router.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/pubsub/pubsub-router.js?rev=660779&view=auto
==============================================================================
--- incubator/shindig/trunk/features/pubsub/pubsub-router.js (added)
+++ incubator/shindig/trunk/features/pubsub/pubsub-router.js Tue May 27 18:56:25 2008
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/**
+ * @fileoverview Container-side message router for PubSub, a gadget-to-gadget
+ * communication library.
+ */
+
+var gadgets = gadgets || {};
+
+/**
+ * @static
+ * @class Routes PubSub messages.
+ * @name gadgets.pubsubrouter
+ */
+gadgets.pubsubrouter = (function() {
+  var gadgetIdToSpecUrl;
+  var subscribers = {};
+  var onSubscribe;
+  var onUnsubscribe;
+  var onPublish;
+
+  function router(command, channel, message) {
+    var gadgetId = this.f;
+    var sender = gadgetIdToSpecUrl(gadgetId);
+    if (sender) {
+      switch (command) {
+      case 'subscribe':
+        if (onSubscribe && onSubscribe(gadgetId, channel)) {
+          break;
+        }
+        if (!subscribers[channel]) {
+          subscribers[channel] = {};
+        }
+        subscribers[channel][gadgetId] = true;
+        break;
+      case 'unsubscribe':
+        if (onUnsubscribe && onUnsubscribe(gadgetId, channel)) {
+          break;
+        }
+        if (subscribers[channel]) {
+          delete subscribers[channel][gadgetId];
+        }
+        break;
+      case 'publish':
+        if (onPublish && onPublish(gadgetId, channel, message)) {
+          break;
+        }
+        var channelSubscribers = subscribers[channel];
+        if (channelSubscribers) {
+          for (var subscriber in channelSubscribers) {
+            gadgets.rpc.call(subscriber, 'pubsub', null, channel, sender, message);
+          }
+        }
+        break;
+      default:
+        throw new Error('Unknown pubsub command');
+      }
+    }
+  }
+
+  return /** @scope gadgets.pubsubrouter */ {
+    /**
+     * Initializes the PubSub message router.
+     * @param {function} gadgetIdToSpecUrlHandler Function that returns the full
+     *                   gadget spec URL of a given gadget id. For example:
+     *                   function(id) { return idToUrlMap[id]; }
+     * @param {object} opt_callbacks Optional event handlers. Supported handlers:
+     *                 opt_callbacks.onSubscribe: function(gadgetId, channel)
+     *                   Called when a gadget tries to subscribe to a channel.
+     *                 opt_callbacks.onUnsubscribe: function(gadgetId, channel)
+     *                   Called when a gadget tries to unsubscribe from a channel.
+     *                 opt_callbacks.onPublish: function(gadgetId, channel, message)
+     *                   Called when a gadget tries to publish a message.
+     *                 All these event handlers may reject a particular PubSub
+     *                 request by returning true.
+     */
+    init: function(gadgetIdToSpecUrlHandler, opt_callbacks) {
+      if (typeof gadgetIdToSpecUrlHandler != 'function') {
+        throw new Error('Invalid handler');
+      }
+      if (typeof opt_callbacks === 'object') {
+        onSubscribe = opt_callbacks.onSubscribe;
+        onUnsubscribe = opt_callbacks.onUnsubscribe;
+        onPublish = opt_callbacks.onPublish;
+      }
+      gadgetIdToSpecUrl = gadgetIdToSpecUrlHandler;
+      gadgets.rpc.register('pubsub', router);
+    }
+  };
+})();
+

Added: incubator/shindig/trunk/features/pubsub/pubsub.js
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/features/pubsub/pubsub.js?rev=660779&view=auto
==============================================================================
--- incubator/shindig/trunk/features/pubsub/pubsub.js (added)
+++ incubator/shindig/trunk/features/pubsub/pubsub.js Tue May 27 18:56:25 2008
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+
+/**
+ * @fileoverview Gadget-side PubSub library for gadget-to-gadget communication.
+ */
+
+var gadgets = gadgets || {};
+
+/**
+ * @static
+ * @class Provides operations for making rpc calls.
+ * @name gadgets.pubsub
+ */
+gadgets.pubsub = function() {
+  var listeners = {};
+
+  function router(channel, sender, message) {
+    var listener = listeners[channel];
+    if (typeof listener === 'function') {
+      listener(sender, message);
+    }
+  }
+
+  return /** @scope gadgets.pubsub */ {
+    /**
+     * Publishes a message to a channel.
+     * @param {string} channel Channel name.
+     * @param {string} message Message to publish.
+     */
+    publish: function(channel, message) {
+      gadgets.rpc.call('..', 'pubsub', null, 'publish', channel, message);
+    },
+
+    /**
+     * Subscribes to a channel.
+     * @param {string} channel Channel name.
+     * @param {function} callback Callback function that receives messages.
+     *                   For example:
+     *                   function(sender, message) {
+     *                     if (isTrustedGadgetSpecUrl(sender)) {
+     *                       processMessage(message);
+     *                     }
+     *                   }
+     */
+    subscribe: function(channel, callback) {
+      listeners[channel] = callback;
+      gadgets.rpc.register('pubsub', router);
+      gadgets.rpc.call('..', 'pubsub', null, 'subscribe', channel);
+    },
+
+    /**
+     * Unsubscribes from a channel.
+     * @param {string} channel Channel name.
+     */
+    unsubscribe: function(channel) {
+      delete listeners[channel];
+      gadgets.rpc.call('..', 'pubsub', null, 'unsubscribe', channel);
+    }
+
+  };
+}();
+