Skip to content
Browse files

Improved the sample code

  • Loading branch information...
1 parent 3e9a260 commit 44e22271fbba027fa101c5d3ffb39fbbb36632dd @jettro committed Oct 25, 2012
View
4 office-events/app/App.groovy
@@ -1,5 +1,7 @@
/**
- * This verticle installs the modules and the worker verticle that make the application run.
+ * Main vertivle for the office events application. This verticle
+ * installs the modules and the worker verticle that make the
+ * application run.
*
* @author Jettro Coenradie
*/
View
14 office-events/app/backup/Backup.groovy
@@ -5,9 +5,10 @@ import org.vertx.groovy.core.buffer.Buffer
import org.vertx.groovy.core.eventbus.EventBus
/**
- * This is a worker verticle. The backup reads the MongoDB invites collection from MongoDB and writes it as a backup
- * to a file. Since this can take some time we do not want to block one of the threads that handle incoming requests.
- * Therefore we make a worker verticle out of this one.
+ * This is a worker verticle. The backup reads the MongoDB invites
+ * collection from MongoDB and writes it as a backup to a file.
+ * This can take some time, we do not want to block one of the
+ * event loops. Therefore we make a worker verticle out of this one.
*
* @author Jettro Coenradie
*/
@@ -19,7 +20,8 @@ EventBus eventBus = vertx.eventBus
eventBus.registerHandler("message.backup.create") {message ->
log.info "Start creating the backup and send a reply message that the backup is created."
- eventBus.send("vertx.persist", ["action": "find", "collection": "invites", "matcher": [:]]) { reply ->
+ eventBus.send("vertx.persist",
+ ["action": "find", "collection": "invites", "matcher": [:]]) { reply ->
log.info reply.body
def buffer = new Buffer()
buffer.appendString(JsonOutput.prettyPrint(JsonOutput.toJson(reply.body)))
@@ -28,6 +30,4 @@ eventBus.registerHandler("message.backup.create") {message ->
}
}
-
-
-log.info "This is a worker module"
+log.info "The backup worker module is started."
View
15 office-events/mods/invitations/invitation.rb
@@ -1,3 +1,9 @@
+# This is the invitation module. The module contains two handlers, one
+# for creating a new invitation and one to accept an invitation.
+#
+# Messages are send to the vertx.persist address that shouls somehow
+# store the invitations and the registrations
+
require "vertx"
include Vertx
@@ -18,8 +24,13 @@
'registeredPersons' => 0
}
}) do |replyMessage|
- theMessage = {'id'=>replyMessage.body['_id'],'message'=>receivedMessage,'maxPersons'=>receivedMaxPersons}
- EventBus.send("message.send.invitation",theMessage)
+ invitation = {
+ 'id'=>replyMessage.body['_id'],
+ 'message'=>receivedMessage,
+ 'maxPersons'=>receivedMaxPersons,
+ 'type'=>'invitation'
+ }
+ EventBus.publish("message.all.clients",invitation)
end
end
View
26 office-events/mods/notifications/notification.js
@@ -1,22 +1,22 @@
+/*
+ * Responsible for converting the received Notification json object in string format
+ * into an Object. This object is published to all clients. Before publication we
+ * add the notification type to it.
+ */
+
load('vertx.js');
var logger = vertx.logger;
-var eb = vertx.eventBus;
-
-var senderAddress = "message.send.notification";
+var eventBus = vertx.eventBus;
var handler = function (message) {
- var parse = JSON.parse(message);
- logger.info("Received a message from" + parse.message);
- eb.send(senderAddress, parse.message);
+ var notification = JSON.parse(message);
+ logger.info("Received a notification" + notification.message);
+ notification.type = "notification";
+ eventBus.publish("message.all.clients", notification);
};
-var address = "notification.received";
-eb.registerHandler(address, handler);
-
-function vertxStop() {
- eb.unregisterHandler(address, handler);
-}
+eventBus.registerHandler("notification.received", handler);
-logger.info("Started the notification module listening to " + address);
+logger.info("Started the notification module.");
View
18 office-events/mods/website/BasicHttpServer.groovy
@@ -3,33 +3,20 @@ import org.vertx.groovy.core.http.RouteMatcher
import org.vertx.groovy.core.http.impl.DefaultHttpServer
/**
+ * Web server to handle all incoming web requests.
+ *
* @author Jettro Coenradie
*/
def log = container.logger
EventBus eventBus = vertx.eventBus
-eventBus.registerHandler("message.send.notification") {message ->
- publishMessageToEventBus(message.body, "notification", log, eventBus)
-}
-
eventBus.registerHandler("message.send.invitation") {message ->
def theMessage = message.body
theMessage.put("type", "invitation")
log.info "Received a message to send to a client ${theMessage}"
eventBus.publish("message.forclients", theMessage)
}
-def publishMessageToEventBus(message, type, log, eventBus) {
- log.info "Received a message to send to a client ${message} of type ${type}"
-
- def theMessage = [:]
- theMessage.put("message", message)
- theMessage.put("type", type)
-
- eventBus.publish("message.forclients", theMessage)
-}
-
-
RouteMatcher routeMatcher = new RouteMatcher()
routeMatcher.get("/") {req ->
@@ -38,6 +25,7 @@ routeMatcher.get("/") {req ->
routeMatcher.post("/rest") {req ->
req.bodyHandler { body ->
+// def notification =["message":body.toString()]
eventBus.send("notification.received", body.toString())
}
View
20 office-events/mods/website/static/index.html
@@ -60,11 +60,15 @@
eb.onopen = function () {
$("#status_info").text("Connected");
- subscribe("message.forclients");
+ $("#status_info").removeClass("alert-error");
+ $("#status_info").addClass("alert-success");
+ subscribe("message.all.clients");
};
eb.onclose = function () {
$("#status_info").text("Not connected");
+ $("#status_info").removeClass("alert-success");
+ $("#status_info").addClass("alert-error");
eb = null;
};
}
@@ -89,15 +93,21 @@
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Vert.x sample</a>
-
- <div id="status_info">Not Connected</div>
</div>
</div>
</div>
<div class="container">
- <h1>Polyglot Message Center</h1>
+ <div class="row">
+ <div class="span8">
+ <h1>Polyglot Message Center</h1>
+
+ <p>Shows the different message types that are send using the vert.x event bus.</p>
+ </div>
+ <div class="span4">
+ <div id="status_info" class="alert alert-error">Not Connected</div>
+ </div>
+ </div>
- <p>Shows the different message types that are send using the vert.x event bus.</p>
<div class="row">
<div class="span8">

0 comments on commit 44e2227

Please sign in to comment.
Something went wrong with that request. Please try again.