-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
634ef45
commit 213ca99
Showing
4 changed files
with
72 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...on-ws-client-server-common/src/main/scala/functions/helidon/ws/InOutMessageProtocol.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package functions.helidon.ws | ||
|
||
import functions.model.* | ||
import io.helidon.common.buffers.BufferData | ||
|
||
import java.io.{ByteArrayOutputStream, PrintWriter} | ||
|
||
class InOutMessageProtocol(invokerMap: InvokerMap): | ||
private val im = invokerMap.map: | ||
case (c4, i) => | ||
(c4.toRawCoordinates, i) | ||
|
||
def incoming(buffer: BufferData): BufferData = | ||
val receiverId = buffer.readInt32() | ||
val corId = buffer.readLong() | ||
val coordsLength = buffer.readUnsignedInt32() | ||
val coordsByteData = new Array[Byte](coordsLength.toInt) | ||
buffer.read(coordsByteData) | ||
val coordsStr = new String(coordsByteData, "UTF-8") | ||
val coordinates4 = Coordinates4.unapply(coordsStr) | ||
val dataLen = buffer.readInt32() | ||
val data = new Array[Byte](dataLen) | ||
buffer.read(data) | ||
val argLen = buffer.readInt32() | ||
val arg = new Array[Byte](argLen) | ||
buffer.read(arg) | ||
try | ||
val f = im(coordinates4.toRawCoordinates) | ||
val response = f(ReceiverInput(data, arg)) | ||
val buf = BufferData.growing(response.length + 12) | ||
buf.writeInt32(receiverId) | ||
buf.write(0) | ||
buf.write(longToBytes(corId)) | ||
buf.write(response) | ||
buf | ||
catch | ||
case t: Throwable => | ||
val bos = new ByteArrayOutputStream | ||
val w = new PrintWriter(bos) | ||
t.printStackTrace(w) | ||
w.close() | ||
val data = bos.toByteArray | ||
val buf = BufferData.growing(data.length + 12) | ||
buf.writeInt32(receiverId) | ||
buf.write(1) | ||
buf.write(longToBytes(corId)) | ||
buf.write(data) | ||
buf | ||
|
||
private def longToBytes(x: Long): Array[Byte] = | ||
val buffer = new Array[Byte](8) | ||
for (i <- 0 until 8) | ||
// Shift the long value 8*(7-i) bits to the right and take the lowest 8 bits | ||
buffer(i) = (x >> (8 * (7 - i))).toByte | ||
buffer |
54 changes: 5 additions & 49 deletions
54
helidon-ws-server/src/main/scala/functions/helidon/ws/ServerWsListener.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,13 @@ | ||
package functions.helidon.ws | ||
|
||
import functions.model.{Coordinates4, ReceiverInput} | ||
import functions.model.InvokerMap | ||
import io.helidon.common.buffers.BufferData | ||
import io.helidon.websocket.{WsListener, WsSession} | ||
|
||
import java.io.{ByteArrayOutputStream, PrintWriter} | ||
class ServerWsListener(invokerMap: InvokerMap) extends WsListener: | ||
|
||
class ServerWsListener(invokerMap: Map[Coordinates4, ReceiverInput => Array[Byte]]) extends WsListener: | ||
|
||
private val im = invokerMap.map: | ||
case (c4, i) => | ||
(c4.toRawCoordinates, i) | ||
private val protocol = new InOutMessageProtocol(invokerMap) | ||
|
||
override def onMessage(session: WsSession, buffer: BufferData, last: Boolean): Unit = | ||
val receiverId = buffer.readInt32() | ||
val corId = buffer.readLong() | ||
val coordsLength = buffer.readUnsignedInt32() | ||
val coordsByteData = new Array[Byte](coordsLength.toInt) | ||
buffer.read(coordsByteData) | ||
val coordsStr = new String(coordsByteData, "UTF-8") | ||
val coordinates4 = Coordinates4.unapply(coordsStr) | ||
val dataLen = buffer.readInt32() | ||
val data = new Array[Byte](dataLen) | ||
buffer.read(data) | ||
val argLen = buffer.readInt32() | ||
val arg = new Array[Byte](argLen) | ||
buffer.read(arg) | ||
try | ||
val f = im(coordinates4.toRawCoordinates) | ||
val response = f(ReceiverInput(data, arg)) | ||
val buf = BufferData.growing(response.length + 12) | ||
buf.writeInt32(receiverId) | ||
buf.write(0) | ||
buf.write(longToBytes(corId)) | ||
buf.write(response) | ||
session.send(buf, true) | ||
catch | ||
case t: Throwable => | ||
val bos = new ByteArrayOutputStream | ||
val w = new PrintWriter(bos) | ||
t.printStackTrace(w) | ||
w.close() | ||
val data = bos.toByteArray | ||
val buf = BufferData.growing(data.length + 12) | ||
buf.writeInt32(receiverId) | ||
buf.write(1) | ||
buf.write(longToBytes(corId)) | ||
buf.write(data) | ||
session.send(buf, true) | ||
|
||
private def longToBytes(x: Long): Array[Byte] = | ||
val buffer = new Array[Byte](8) | ||
for (i <- 0 until 8) | ||
// Shift the long value 8*(7-i) bits to the right and take the lowest 8 bits | ||
buffer(i) = (x >> (8 * (7 - i))).toByte | ||
buffer | ||
val out = protocol.incoming(buffer) | ||
session.send(out, true) |