1
1
package org .codeoverflow .chatoverflow .requirement .service .rcon
2
2
3
- import java .io .{DataInputStream , InputStream , OutputStream }
3
+ import java .io .{DataInputStream , IOException , InputStream , OutputStream }
4
4
import java .net .{Socket , SocketException }
5
5
import java .nio .{ByteBuffer , ByteOrder }
6
6
import java .util .Random
@@ -16,13 +16,8 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
16
16
private var outputStream : OutputStream = _
17
17
private var inputStream : InputStream = _
18
18
private var requestId : Int = 0
19
- private var loggedIn = false
20
19
21
20
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
- }
26
21
logger debug s " Sending $command to RCON "
27
22
requestId += 1
28
23
if (write(2 , command.getBytes(" ASCII" ))) {
@@ -39,32 +34,50 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
39
34
logger info s " Starting rcon connection to ${credentials.get.getValue(" address" ).get}"
40
35
var port : Int = 25575
41
36
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
+ }
43
45
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?"
44
58
return false
45
59
}
46
60
}
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.
52
63
Thread .sleep(5000 )
53
- true
64
+ loggedIn
54
65
}
55
66
56
- private def login (): Unit = {
67
+ private def login (): Boolean = {
57
68
requestId = new Random ().nextInt(Integer .MAX_VALUE )
58
69
logger info " Logging RCON in..."
59
70
val password = credentials.get.getValue(" password" ).get
60
71
if (write(3 , password.getBytes(" ASCII" ))) {
61
72
if (read() == null ) {
62
73
logger error " Could not log in to RCON Server. Password is Wrong!"
74
+ return false
63
75
} else {
64
76
logger debug " Login to RCON was successful"
65
- loggedIn = true
77
+ return true
66
78
}
67
79
}
80
+ false
68
81
}
69
82
70
83
private def write (packageType : Int , payload : Array [Byte ]): Boolean = {
@@ -83,10 +96,6 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
83
96
outputStream.write(byteBuffer.array())
84
97
outputStream.flush()
85
98
} 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
- }
90
99
case e : SocketException => {
91
100
logger error " Connection Error to RCON Server. This request will not be sended!"
92
101
return false
@@ -116,13 +125,11 @@ class RconConnector(override val sourceIdentifier: String) extends Connector(sou
116
125
}
117
126
}
118
127
119
- private [rcon] def isLoggedIn : Boolean = loggedIn
120
-
121
128
/**
122
129
* This stops the activity of the connector, e.g. by closing the platform connection.
123
130
*/
124
131
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 }! "
126
133
socket.close()
127
134
true
128
135
}
0 commit comments