Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit fa77b69

Browse files
author
cesmec
committed
Added EventMessage overload to event broadcast and some documentation
1 parent 20af921 commit fa77b69

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

src/main/scala/org/codeoverflow/chatoverflow/framework/manager/PluginManagerImpl.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import java.util
44

55
import org.codeoverflow.chatoverflow.WithLogger
66
import org.codeoverflow.chatoverflow.api.plugin.{PluginLogMessage, PluginManager}
7-
import org.codeoverflow.chatoverflow.ui.web.rest.events.EventsDispatcher
8-
import org.json4s.DefaultFormats
9-
import org.json4s.jackson.Serialization
7+
import org.codeoverflow.chatoverflow.ui.web.rest.events.{EventMessage, EventsDispatcher}
108

119
import scala.collection.JavaConverters._
1210
import scala.collection.mutable.ListBuffer
@@ -33,9 +31,11 @@ class PluginManagerImpl(pluginInstanceName: String, logOutputOnConsole: Boolean)
3331
logger info s"[$pluginInstanceName] $message"
3432
}
3533

36-
implicit val formats: DefaultFormats.type = DefaultFormats
37-
val data = Map(("message", message), ("timestamp", logMessage.getTimestamp.toString))
38-
EventsDispatcher.broadcast("instance", Serialization.write(Map(("name", pluginInstanceName), ("action", "log"), ("data", data))))
34+
EventsDispatcher.broadcast("instance", EventMessage("log", Map(
35+
("name", pluginInstanceName),
36+
("message", message),
37+
("timestamp", logMessage.getTimestamp.toString)
38+
)))
3939
}
4040

4141
/**

src/main/scala/org/codeoverflow/chatoverflow/instance/PluginInstance.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import org.codeoverflow.chatoverflow.api.plugin.{Plugin, PluginManager}
88
import org.codeoverflow.chatoverflow.framework.PluginCompatibilityState.PluginCompatibilityState
99
import org.codeoverflow.chatoverflow.framework.manager.{PluginManagerImpl, PluginManagerStub}
1010
import org.codeoverflow.chatoverflow.framework.{PluginCompatibilityState, PluginType}
11-
import org.codeoverflow.chatoverflow.ui.web.rest.events.EventsDispatcher
12-
import org.json4s.DefaultFormats
13-
import org.json4s.jackson.Serialization
11+
import org.codeoverflow.chatoverflow.ui.web.rest.events.{EventMessage, EventsDispatcher}
1412

1513
/**
1614
* A plugin instance holds all the general information of the plugin type and specific information of
@@ -152,8 +150,7 @@ class PluginInstance(val instanceName: String, pluginType: PluginType) extends W
152150
logger info s"Starting plugin '$instanceName' in new thread!"
153151
try {
154152
instanceThread = new Thread(() => {
155-
implicit val formats: DefaultFormats.type = DefaultFormats
156-
EventsDispatcher.broadcast("instance", Serialization.write(Map(("name", instanceName), ("action", "start"))))
153+
EventsDispatcher.broadcast("instance", EventMessage("start", Map(("name", instanceName))))
157154

158155
try {
159156

@@ -197,7 +194,7 @@ class PluginInstance(val instanceName: String, pluginType: PluginType) extends W
197194
requirement.asInstanceOf[Requirement[Output]].get().shutdown()
198195
})
199196

200-
EventsDispatcher.broadcast("instance", Serialization.write(Map(("name", instanceName), ("action", "stop"))))
197+
EventsDispatcher.broadcast("instance", EventMessage("stop", Map(("name", instanceName))))
201198
}
202199
})
203200
instanceThread.start()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package org.codeoverflow.chatoverflow.ui.web.rest.events
2+
3+
case class EventMessage[T](action: String, data: T)

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/events/EventsController.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class EventsController(implicit val swagger: Swagger) extends JsonServlet with S
3636
}
3737

3838
private def sendMessage(writer: PrintWriter, messageType: String, message: String): Unit = {
39+
/*
40+
Every message has the following format and ends with two line feeds (\n):
41+
event: [name of event]
42+
data: [first line]
43+
data: [second line]
44+
...
45+
46+
See also: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Examples
47+
*/
48+
3949
var msg = "event: " + messageType.replace("\n", "") + "\n"
4050
if (message != null)
4151
msg += "data: " + message.replace("\n", "\ndata: ") + "\n\n"

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/events/EventsDispatcher.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
package org.codeoverflow.chatoverflow.ui.web.rest.events
22

3+
import org.json4s.{DefaultFormats, Formats}
4+
import org.json4s.jackson.Serialization
5+
6+
/**
7+
* The EventsDispatcher is the central point for realtime communication to the clients
8+
*/
39
object EventsDispatcher {
410
private var controller: EventsController = _
11+
implicit val formats: Formats = DefaultFormats
512

13+
/**
14+
* Initializes the EventsDispatcher with the registered controller
15+
* Only to be used from the bootstrap
16+
* @param eventsController registered controller that accepts the incoming connections
17+
*/
618
def init(eventsController: EventsController): Unit = {
719
if (controller == null)
820
controller = eventsController
921
}
1022

23+
/**
24+
* Sends the message to all connected clients
25+
* @param messageType type of the message / event
26+
* @param message the message to send
27+
* @tparam T type of the message data
28+
*/
29+
def broadcast[T](messageType: String, message: EventMessage[T]): Unit = {
30+
broadcast(messageType, Serialization.write(message))
31+
}
32+
33+
/**
34+
* Sends the message to all connected clients
35+
* @param messageType type of the message / event
36+
* @param message the message to send
37+
*/
1138
def broadcast(messageType: String, message: String = null): Unit = {
1239
if (controller != null)
1340
controller.broadcast(messageType, message)
1441
}
1542

43+
/**
44+
* Sends a close message to all connected clients and closes the connections
45+
*/
1646
def close(): Unit = {
1747
if (controller != null)
1848
controller.closeConnections()

0 commit comments

Comments
 (0)