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

Commit e9a4520

Browse files
committed
Add Basic RCON Support
Add Connector Can Send Message TODO: Error Catching Response from server
1 parent 0e3f9b4 commit e9a4520

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.rcon
2+
3+
import java.io.{InputStream, OutputStream}
4+
import java.net.Socket
5+
import java.nio.{ByteBuffer, ByteOrder}
6+
import java.util.Random
7+
8+
import org.codeoverflow.chatoverflow.WithLogger
9+
import org.codeoverflow.chatoverflow.connector.Connector
10+
11+
class RconConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger {
12+
override protected var requiredCredentialKeys: List[String] = List("password", "address")
13+
override protected var optionalCredentialKeys: List[String] = List("port")
14+
15+
private var socket: Socket = _
16+
private var outputStream: OutputStream = _
17+
private var inputStream: InputStream = _
18+
private var requestId: Int = 0
19+
20+
def sendCommand(command: String): String = {
21+
logger debug s"Sending $command to RCON"
22+
requestId += 1
23+
write(2, command.getBytes("ASCII"))
24+
""
25+
}
26+
27+
28+
/**
29+
* Starts the connector, e.g. creates a connection with its platform.
30+
*/
31+
override def start(): Boolean = {
32+
logger info s"Starting rcon connection to ${credentials.get.getValue("address").get}"
33+
var port: Int = 25575
34+
if (credentials.get.exists("port")) {
35+
port = credentials.get.getValue("port").get.toInt
36+
if (port < 1 || port > 65535) {
37+
return false
38+
}
39+
}
40+
socket = new Socket(credentials.get.getValue("address").get, port)
41+
outputStream = socket.getOutputStream
42+
inputStream = socket.getInputStream
43+
login()
44+
true
45+
}
46+
47+
private def login(): Unit = {
48+
requestId = new Random().nextInt(Integer.MAX_VALUE)
49+
logger info "Logging RCON in..."
50+
val password = credentials.get.getValue("password").get
51+
write(3, password.getBytes("ASCII"))
52+
logger debug "RCON Login sent"
53+
}
54+
55+
private def write(packageType: Int, payload: Array[Byte]): Boolean = {
56+
val length = 4 + 4 + payload.length + 1 + 1
57+
var byteBuffer: ByteBuffer = ByteBuffer.allocate(length + 4)
58+
byteBuffer.order(ByteOrder.LITTLE_ENDIAN)
59+
60+
byteBuffer.putInt(length)
61+
byteBuffer.putInt(requestId)
62+
byteBuffer.putInt(packageType)
63+
byteBuffer.put(payload)
64+
byteBuffer.put(0x00.toByte)
65+
byteBuffer.put(0x00.toByte)
66+
67+
outputStream.write(byteBuffer.array())
68+
outputStream.flush()
69+
true
70+
}
71+
72+
/**
73+
* This stops the activity of the connector, e.g. by closing the platform connection.
74+
*/
75+
override def stop(): Boolean = {
76+
logger info s"Stopped RCON connector to ${credentials.get.getValue("address")}!"
77+
socket.close()
78+
true
79+
}
80+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.codeoverflow.chatoverflow.requirement.service.rcon.impl
2+
3+
import org.codeoverflow.chatoverflow.WithLogger
4+
import org.codeoverflow.chatoverflow.api.io.output.RconOutput
5+
import org.codeoverflow.chatoverflow.registry.Impl
6+
import org.codeoverflow.chatoverflow.requirement.OutputImpl
7+
import org.codeoverflow.chatoverflow.requirement.service.rcon.RconConnector
8+
9+
@Impl(impl = classOf[RconOutput], connector = classOf[RconConnector])
10+
class RconOutputImpl extends OutputImpl[RconConnector] with RconOutput with WithLogger {
11+
override def sendCommand(command: String): String = sourceConnector.get.sendCommand(command)
12+
13+
/**
14+
* Start the input, called after source connector did init
15+
*
16+
* @return true if starting the input was successful, false if some problems occurred
17+
*/
18+
override def start(): Boolean = true
19+
}

0 commit comments

Comments
 (0)