Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

subscribe api

  • Loading branch information...
commit 700f5f36543aa835b4f60275289f11cfc4f784c3 1 parent 0cec5d5
Christophe Hamerling chamerling authored
55 client/subscribe.js
View
@@ -0,0 +1,55 @@
+/**
+ * PLAY live monitoring
+ *
+ * Some subcribe stuff...
+ *
+ * Copyright(c) 2012 Christophe Hamerling <chamerling@linagora.com>
+ * MIT Licensed
+ */
+
+var request = require('request')
+ , uuid = require('node-uuid');
+
+var url = 'http://localhost:3000/api/v1/monitoring/wsn/';
+var i = 0;
+
+var id = setInterval(function() {
+ var message = {
+ uuid: uuid.v1(),
+ subscriber: "http://localhost:7373/subcriber",
+ type: "newSubscribeRequest",
+ topic: {
+ name: "TestTopicSubscribe",
+ ns: "http://play.ow2.org/subscribe",
+ prefix:"p"
+ },
+ timestamp: new Date().getTime()
+ }
+
+ console.log('Sending subscribe message # ' + i + ' : ' , message);
+ i++;
+
+ if (i == 1000) {
+ // kill me...
+ clearInterval(id);
+ }
+
+ var start = new Date().getTime();
+ request.post(
+ {
+ url: url,
+ method: 'post',
+ body: message,
+ json: true
+ },
+ function(error, response, body) {
+ var diff = new Date().getTime() - start;
+ if (error) {
+ console.log('Error ', error);
+ } else {
+ console.log('Response for request # ' + i + ' (' + diff + ' ms) : ' + response.statusCode + ' : ', body);
+ }
+ })
+ }, 1000);
+
+
159 lib/api/monitoring/wsn.js
View
@@ -21,12 +21,16 @@ var noop = function() {};
exports.path = '/monitoring/wsn';
+// Redis keys
var outnotifyerror = 'OutNotifyError'
, innotifyerror = 'InNotifyError'
, innotifyinput = 'InNotifyInput'
, innotifyoutput = 'InNotifyOutput'
- , outnotify = 'OutNotify';
-
+ , outnotify = 'OutNotify'
+ , subscriberequest = 'SubscribeRequest'
+ , subscriberesponse = 'SubscribeResponse'
+ , unsubscriberequest = 'UnsubscribeRequest'
+ , unsubscriberesponse = 'UnsubscribeResponse';
/**
* Creates the WSN API
@@ -140,9 +144,97 @@ exports.init = function(prefix, cb) {
});
/**
+ * Get subscribe requests
+ */
+ app.get(prefix + exports.path + '/subscribe/request', middleware.api, function(req, res) {
+ redis.hgetall(subscriberequest, function (err, reply) {
+ if (err) {
+ res.send(500, err);
+ }
+
+ var result = {};
+ if (reply === null) {
+ res.json(200, result);
+ } else {
+ Object.keys(reply).forEach(function (topic) {
+ result[topic] = reply[topic];
+ });
+ }
+
+ res.json(200, result);
+ });
+ });
+
+ /**
+ * Get subscribe response
+ */
+ app.get(prefix + exports.path + '/subscribe/response', middleware.api, function(req, res) {
+ redis.hgetall(subscriberesponse, function (err, reply) {
+ if (err) {
+ res.send(500, err);
+ }
+
+ var result = {};
+ if (reply === null) {
+ res.json(200, result);
+ } else {
+ Object.keys(reply).forEach(function (topic) {
+ result[topic] = reply[topic];
+ });
+ }
+
+ res.json(200, result);
+ });
+ });
+
+ /**
+ * Get unsubscribe request
+ */
+ app.get(prefix + exports.path + '/unsubscribe/request', middleware.api, function(req, res) {
+ redis.hgetall(unsubscriberequest, function (err, reply) {
+ if (err) {
+ res.send(500, err);
+ }
+
+ var result = {};
+ if (reply === null) {
+ res.json(200, result);
+ } else {
+ Object.keys(reply).forEach(function (topic) {
+ result[topic] = reply[topic];
+ });
+ }
+
+ res.json(200, result);
+ });
+ });
+
+ /**
+ * Get unsubscribe response
+ */
+ app.get(prefix + exports.path + '/unsubscribe/response', middleware.api, function(req, res) {
+ redis.hgetall(unsubscriberesponse, function (err, reply) {
+ if (err) {
+ res.send(500, err);
+ }
+
+ var result = {};
+ if (reply === null) {
+ res.json(200, result);
+ } else {
+ Object.keys(reply).forEach(function (topic) {
+ result[topic] = reply[topic];
+ });
+ }
+
+ res.json(200, result);
+ });
+ });
+
+ /**
* Post new monitoring data, check benchmarks files for payload format.
*/
- app.post(prefix + exports.path, middleware.business, function(req, res) {
+ app.post(prefix + exports.path + "/", middleware.business, function(req, res) {
var notify = {
date : new Date().toGMTString(),
@@ -192,6 +284,10 @@ exports.init = function(prefix, cb) {
redis.del(innotifyerror);
redis.del(innotifyinput);
redis.del(innotifyoutput);
+ redis.del(subscriberequest);
+ redis.del(subscriberesponse);
+ redis.del(unsubscriberequest);
+ redis.del(unsubscriberesponse);
res.json(200, {status : 'done'});
});
@@ -264,6 +360,52 @@ var handlers = {
newOutNotify : function(notify, cb) {
redis.hincrby(outnotify, notify.data.topic.ns + '/' + notify.data.topic.name, 1);
push_notify(notify, noop);
+ },
+
+ /**
+ * Handle subscription request
+ *
+ * @param subscribe
+ * @param cb
+ */
+ newSubscribeRequest : function(subscribe, cb) {
+ console.log('got a newSubscribeRequest ', subscribe);
+ redis.hincrby(subscriberequest, subscribe.data.topic.ns + '/' + subscribe.data.topic.name, 1);
+ push_subscribe(subscribe, noop);
+ if (cb) cb();
+ },
+
+ /**
+ *
+ * @param subscribe
+ * @param cb
+ */
+ newSubscribeResponse : function(subscribe, cb) {
+ console.log('got a newSubscribeResponse ', subscribe);
+ redis.hincrby(subscriberesponse, subscribe.data.topic.ns + '/' + subscribe.data.topic.name, 1);
+ if (cb != null) cb();
+ },
+
+ /**
+ *
+ * @param subscribe
+ * @param cb
+ */
+ newUnsubscribeRequest : function(subscribe, cb) {
+ console.log('got a newUnsubscribeRequest ', subscribe);
+ redis.hincrby(unsubscriberequest, subscribe.data.topic.ns + '/' + subscribe.data.topic.name, 1);
+ if (cb != null) cb();
+ },
+
+ /**
+ *
+ * @param subscribe
+ * @param cb
+ */
+ newUnsubscribeResponse : function(subscribe, cb) {
+ console.log('got a newUnsubscribeResponse ', subscribe);
+ redis.hincrby(unsubscriberesponse, subscribe.data.topic.ns + '/' + subscribe.data.topic.name, 1);
+ if (cb != null) cb();
}
}
@@ -290,3 +432,14 @@ var push_error = function(notify, cb) {
io.sockets.emit('notify_error', {type: 'error', message: notify});
if (cb) cb();
}
+
+/**
+ * Push subscribe requests to socket.io
+ *
+ * @param subscribe
+ * @param cb
+ */
+var push_subscribe = function(subscribe, cb) {
+ io.sockets.emit('subscribe', {type :'subscribe', message : subscribe});
+ if (cb) cb();
+}
1  public/errors.html
View
@@ -35,6 +35,7 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
1  public/index.html
View
@@ -36,6 +36,7 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
1  public/livecharts.html
View
@@ -116,6 +116,7 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
1  public/notify.html
View
@@ -34,6 +34,7 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
1  public/streamerror.html
View
@@ -34,6 +34,7 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
</ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
125 public/streamsubscribe.html
View
@@ -0,0 +1,125 @@
+<html>
+ <head>
+ <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.css">
+ <style type="text/css">
+ body {
+ padding-top: 60px;
+ padding-bottom: 40px;
+ }
+
+ .sidebar-nav {
+ padding: 9px 0;
+ }
+ </style>
+ <link href="assets/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
+
+ <script src="/socket.io/socket.io.js"></script>
+ </head>
+ <body>
+
+ <div class="navbar navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container">
+ <a class="btn btn-navbar" data-toggle="collapse"
+ data-target=".nav-collapse">
+ <span class="icon-bar"></span>
+ <span class="icon-bar"></span>
+ <span class="icon-bar"></span>
+ </a>
+ <a class="brand" href="/">PLAY.mon</a>
+ <div class="nav-collapse" id="main-menu">
+ <ul class="nav" id="main-menu-left">
+ <li class="dropdown"><a class="dropdown-toggle"
+ data-toggle="dropdown" href="#">Live <b class="caret"></b></a>
+ <ul class="dropdown-menu" id="swatch-menu">
+ <li><a href="/notify.html">Notify</a></li>
+ <li><a href="/streamerror.html">Errors</a></li>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
+ </ul>
+ </li>
+ <li class="dropdown"><a class="dropdown-toggle"
+ data-toggle="dropdown" href="#">Charts <b class="caret"></b></a>
+ <ul class="dropdown-menu" id="swatch-menu">
+ <li><a href="/livecharts.html" target="_blank">Live</a></li>
+ </ul>
+ </li>
+ <li class="dropdown"><a class="dropdown-toggle"
+ data-toggle="dropdown" href="#">WSN <b class="caret"></b></a>
+ <ul class="dropdown-menu" id="swatch-menu">
+ <li><a href="/errors.html">Subscribers Errors</a></li>
+ <li><a href="/wsninout.html">In Out</a></li>
+ <li><a href="/wsnstore.html">Last</a></li>
+ </ul>
+ </li>
+ <li class="dropdown">
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#">API <b class="caret"></b></a>
+ <ul class="dropdown-menu" id="swatch-menu">
+ <li><a href="/api/v1/stats" target="_blank">Stats</a></li>
+ </ul>
+ </li>
+
+<!--
+ <li class="dropdown"><a class="dropdown-toggle"
+ data-toggle="dropdown" href="#">Services <b class="caret"></b></a>
+ <ul class="dropdown-menu" id="swatch-menu">
+ <li><a href="@{RegistryController.services()}">List</a></li>
+ <li><a href="@{RegistryController.load()}">Load</a></li>
+ </ul>
+ </li>
+-->
+ </ul>
+ <ul class="nav pull-right" id="main-menu-right">
+ <li class="dropdown" id="preview-menu">
+ <a class="dropdown-toggle" data-toggle="dropdown" href="#">Admin <b class="caret"></b></a>
+ <ul class="dropdown-menu">
+ <li><a href="#"><b>Connect...</b></a></li>
+ </ul>
+ </li>
+ </ul>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div class="container">
+
+ <div id="remove" class="alert alert-success">
+ <button type="button" class="close" data-dismiss="alert">×</button>
+ <div id="waiting">Waiting subscribe message...</div>
+ </div>
+
+ <h3>Subscribes</h3>
+ <div id="socketout"></div>
+</div>
+
+ <script>
+ var socket = io.connect();
+ var nb = 0;
+
+ socket.on('subscribe', function(data) {
+ nb++;
+ var receivedAt = new Date();
+ $('#socketout').prepend('<div class=\"alert alert-info\">New subscribe #' + nb + ' at ' + receivedAt.toGMTString() + '</div><pre><code>' + JSON.stringify(data.message, null, 4) + '' + '</code></pre><br><br>');
+ // remove olldies...
+ // TODO
+
+ $('#remove').remove();
+ });
+
+ </script>
+ <script src="assets/javascript/jquery-1.7.2.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-transition.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-alert.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-modal.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-dropdown.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-scrollspy.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-tab.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-tooltip.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-popover.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-button.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-collapse.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-carousel.js"></script>
+ <script src="assets/bootstrap/js/bootstrap-typeahead.js"></script>
+
+ </body>
+</html>
3  public/wsninout.html
View
@@ -35,7 +35,8 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
- </ul>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
+ </ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
data-toggle="dropdown" href="#">Charts <b class="caret"></b></a>
3  public/wsnstore.html
View
@@ -35,7 +35,8 @@
<ul class="dropdown-menu" id="swatch-menu">
<li><a href="/notify.html">Notify</a></li>
<li><a href="/streamerror.html">Errors</a></li>
- </ul>
+ <li><a href="/streamsubscribe.html">Subscribes</a></li>
+ </ul>
</li>
<li class="dropdown"><a class="dropdown-toggle"
data-toggle="dropdown" href="#">Charts <b class="caret"></b></a>
Please sign in to comment.
Something went wrong with that request. Please try again.