@@ -15,10 +15,12 @@ import scala.collection.mutable.ListBuffer
15
15
*/
16
16
class TwitchChatConnector (override val sourceIdentifier : String ) extends Connector (sourceIdentifier) with WithLogger {
17
17
private val twitchChatListener = new TwitchChatListener
18
+ private val connectionListener = new TwitchChatConnectListener (onConnect)
18
19
private val oauthKey = " oauth"
19
20
override protected var requiredCredentialKeys : List [String ] = List (oauthKey)
20
21
override protected var optionalCredentialKeys : List [String ] = List ()
21
22
private var bot : PircBotX = _
23
+ private var status : Option [(Boolean , String )] = None
22
24
private val channels = ListBuffer [String ]()
23
25
24
26
def addMessageEventListener (listener : MessageEvent => Unit ): Unit = {
@@ -63,6 +65,7 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
63
65
.setName(credentials.get.credentialsIdentifier)
64
66
.setServerPassword(password.getOrElse(" " ))
65
67
.addListener(twitchChatListener)
68
+ .addListener(connectionListener)
66
69
.buildConfiguration()
67
70
} else {
68
71
logger error " No credentials set!"
@@ -71,33 +74,47 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
71
74
72
75
}
73
76
77
+ /**
78
+ * Gets called by the TwitchChatConnectListener when the bot has connected.
79
+ * Saves the passed information into the status variable.
80
+ */
81
+ private def onConnect (success : Boolean , msg : String ): Unit = {
82
+ status.synchronized {
83
+ // tell the thread which starts the connector that the status has been reported
84
+ status.notify()
85
+ status = Some ((success, msg))
86
+ }
87
+ }
88
+
74
89
/**
75
90
* Starts the connector, e.g. creates a connection with its platform.
76
91
*/
77
92
override def start (): Boolean = {
78
93
bot = new PircBotX (getConfig)
79
94
startBot()
80
- true
81
95
}
82
96
83
- private def startBot (): Unit = {
84
-
85
- var errorCount = 0
86
-
97
+ private def startBot (): Boolean = {
87
98
new Thread (() => {
88
99
bot.startBot()
89
100
}).start()
90
101
91
- while (bot.getState != PircBotX .State .CONNECTED && errorCount < 30 ) {
92
- logger info " Waiting while the bot is connecting..."
93
- Thread .sleep(100 )
94
- errorCount += 1
102
+ logger info " Waiting while the bot is connecting and logging in..."
103
+ status.synchronized {
104
+ status.wait(10000 )
105
+ }
106
+
107
+ if (status.isEmpty) {
108
+ logger error " Bot couldn't connect within timeout of 10 seconds."
109
+ return false
95
110
}
96
111
97
- if (errorCount >= 30 ) {
98
- logger error " Fatal. Unable to start bot."
112
+ val (success, msg) = status.get
113
+ if (! success) {
114
+ logger error s " Bot couldn't connect. Reason: $msg. "
99
115
}
100
116
117
+ success
101
118
}
102
119
103
120
/**
@@ -106,6 +123,8 @@ class TwitchChatConnector(override val sourceIdentifier: String) extends Connect
106
123
override def stop (): Boolean = {
107
124
bot.sendIRC().quitServer()
108
125
bot.close()
126
+ status = None
127
+ channels.clear()
109
128
true
110
129
}
111
130
}
0 commit comments