blob: 32fd764aed4857504a6c8d95387866b4d28cdaa2 (
plain) (
blame)
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
|
package xyz.driver.core
package messaging
import java.nio.ByteBuffer
/** A topic is a named group of messages that all share a common schema.
* @tparam Message type of messages sent over this topic */
trait Topic[Message] {
/** Name of this topic (must be unique). */
def name: String
/** Convert a message to its wire format that will be sent over a bus. */
def serialize(message: Message): ByteBuffer
/** Convert a message from its wire format. */
def deserialize(message: ByteBuffer): Message
}
object Topic {
/** Create a new "raw" topic without a schema, providing access to the underlying bytes of messages. */
def raw(name0: String): Topic[ByteBuffer] = new Topic[ByteBuffer] {
def name = name0
override def serialize(message: ByteBuffer): ByteBuffer = message
override def deserialize(message: ByteBuffer): ByteBuffer = message
}
/** Create a topic that represents data as UTF-8 encoded strings. */
def string(name0: String): Topic[String] = new Topic[String] {
def name = name0
override def serialize(message: String): ByteBuffer = {
ByteBuffer.wrap(message.getBytes("utf-8"))
}
override def deserialize(message: ByteBuffer): String = {
val bytes = new Array[Byte](message.remaining())
message.get(bytes)
new String(bytes, "utf-8")
}
}
}
|