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

Commit c509b13

Browse files
committed
Change requested implementations in RCON service.
Only pass true to start if start and login are succesful But than always start Impl Also quit if port is no valid number
1 parent 2315ecb commit c509b13

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/RconConnector.scala

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codeoverflow.chatoverflow.requirement.service.rcon
22

3-
import java.io.{DataInputStream, InputStream, OutputStream}
3+
import java.io.{DataInputStream, IOException, InputStream, OutputStream}
44
import java.net.{Socket, SocketException}
55
import java.nio.{ByteBuffer, ByteOrder}
66
import java.util.Random
@@ -16,13 +16,8 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
1616
private var outputStream: OutputStream = _
1717
private var inputStream: InputStream = _
1818
private var requestId: Int = 0
19-
private var loggedIn = false
2019

2120
def sendCommand(command: String): String = {
22-
if (!loggedIn) {
23-
logger error "Could not execute RCON Command due to wrong password or no connection"
24-
return null
25-
}
2621
logger debug s"Sending $command to RCON"
2722
requestId += 1
2823
if (write(2, command.getBytes("ASCII"))) {
@@ -39,32 +34,50 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
3934
logger info s"Starting rcon connection to ${credentials.get.getValue("address").get}"
4035
var port: Int = 25575
4136
if (credentials.get.exists("port")) {
42-
port = credentials.get.getValue("port").get.toInt
37+
try{
38+
port = credentials.get.getValue("port").get.toInt
39+
} catch {
40+
case e: NumberFormatException => {
41+
logger error "Please enter a valid port"
42+
return false
43+
}
44+
}
4345
if (port < 1 || port > 65535) {
46+
logger error "Please enter a valid port"
47+
return false
48+
}
49+
}
50+
try {
51+
socket = new Socket(credentials.get.getValue("address").get, port)
52+
socket.setKeepAlive(true)
53+
outputStream = socket.getOutputStream
54+
inputStream = socket.getInputStream
55+
} catch {
56+
case e: IOException => {
57+
logger error "No Connection to RCON Server. Is it up?"
4458
return false
4559
}
4660
}
47-
socket = new Socket(credentials.get.getValue("address").get, port)
48-
socket.setKeepAlive(true)
49-
outputStream = socket.getOutputStream
50-
inputStream = socket.getInputStream
51-
login()
61+
val loggedIn = login()
62+
// Sleeping here to allow the (minecraft) server to start its own rcon procedure. Otherwise it caused errors in my tests.
5263
Thread.sleep(5000)
53-
true
64+
loggedIn
5465
}
5566

56-
private def login(): Unit = {
67+
private def login(): Boolean = {
5768
requestId = new Random().nextInt(Integer.MAX_VALUE)
5869
logger info "Logging RCON in..."
5970
val password = credentials.get.getValue("password").get
6071
if (write(3, password.getBytes("ASCII"))) {
6172
if (read() == null) {
6273
logger error "Could not log in to RCON Server. Password is Wrong!"
74+
return false
6375
} else {
6476
logger debug "Login to RCON was successful"
65-
loggedIn = true
77+
return true
6678
}
6779
}
80+
false
6881
}
6982

7083
private def write(packageType: Int, payload: Array[Byte]): Boolean = {
@@ -83,10 +96,6 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
8396
outputStream.write(byteBuffer.array())
8497
outputStream.flush()
8598
} catch {
86-
case e: NullPointerException => {
87-
logger error "There was and is no Connection to the RCON Server, please try restarting."
88-
return false
89-
}
9099
case e: SocketException => {
91100
logger error "Connection Error to RCON Server. This request will not be sended!"
92101
return false
@@ -116,13 +125,11 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
116125
}
117126
}
118127

119-
private[rcon] def isLoggedIn: Boolean = loggedIn
120-
121128
/**
122129
* This stops the activity of the connector, e.g. by closing the platform connection.
123130
*/
124131
override def stop(): Boolean = {
125-
logger info s"Stopped RCON connector to ${credentials.get.getValue("address")}!"
132+
logger info s"Stopped RCON connector to ${credentials.get.getValue("address").get}!"
126133
socket.close()
127134
true
128135
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/impl/RconInputImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RconInputImpl extends InputImpl[RconConnector] with RconInput with WithLog
1515
*
1616
* @return true if starting the input was successful, false if some problems occurred
1717
*/
18-
override def start(): Boolean = sourceConnector.get.isLoggedIn
18+
override def start(): Boolean = true
1919

2020
override def stop(): Boolean = true
2121
}

src/main/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/impl/RconOutputImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RconOutputImpl extends OutputImpl[RconConnector] with RconOutput with With
1717
*
1818
* @return true if starting the input was successful, false if some problems occurred
1919
*/
20-
override def start(): Boolean = sourceConnector.get.isLoggedIn
20+
override def start(): Boolean = true
2121

2222
/**
2323
* Stops the output, called before source connector will shutdown

0 commit comments

Comments
 (0)