https://kotlinlang.org logo
Title
o

otakusenpai

08/30/2018, 2:33 PM
Hello guys, My program calls a function inside a coroutine, which calls a plugin. Now this plugin requires to wait for a specified period of time. I tried java.util.Timer and it didnt work(i put 60 secs but even after that it didnt work). Now i think i'll try this https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/java.util.-timer/schedule.html . But what Im unsure about is that if a plugin can have a coroutine call inside it(also, im not sure if Schedule is a coroutine thingy). Thanks.
m

marstran

08/30/2018, 2:38 PM
Timer.schedule
is not a coroutine-construct. What kind of "plugin" are you talking about?
o

otakusenpai

08/30/2018, 2:39 PM
its a simple plugin which processes text and returns a value
the text can hav a string like "60s" or "450D"
i process tht string into seconds, then passed it to Timer.schedule()
m

marstran

08/30/2018, 2:40 PM
Do you mean a library? Can you give some example code?
o

otakusenpai

08/30/2018, 2:40 PM
well, its a messy bit of code
m

marstran

08/30/2018, 2:41 PM
I think I just need to see the API of that plugin
m

marstran

08/30/2018, 2:47 PM
So "onCommand" is the function you wait for?
o

otakusenpai

08/30/2018, 2:48 PM
no, onCommand is a general function tht always returns a string. That command takes a string as parameter and processes it and then returns some other string
currently tht part of the code isnt updated to the repos but heres a snippet
else if(strList.get(1) == "ban") { var found = false if(size <= 3) { retData = "Usage: ${specialChar}chan ban #channel prefix or " + "${specialChar}chan ban #channel prefix except or " + "${specialChar}chan ban #channel prefix time or " + "${specialChar}chan ban #channel prefix except time." + " Time can be in the following format: 6D or 6d(day), 10M or 10m(minute)." + " Following are some common literals for time: Second - s/S, Minute - m/M, Hour - h/H, Day - d,D. " + "Others need not be implemented." type = 1 } else if(size > 3) { for(i in channelList) { if(i.retName() == strList.get(2)) { if(size == 4) { // 1st if(isPrefix(strList.get(3))) { retData = SendBan(strList.get(2),strList.get(3)) type = 2 } } else if(size == 5) { // 2nd n 3rd if(isPrefix(strList.get(3))) { if(isPrefix(strList.get(4))) { // 2nd retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4)) type = 2 } else if(parseTimeInput(strList.get(4)).third) { // 3rd val data = parseTimeInput(strList.get(4)) val timeSecs = setTimeInNumbers(data.second) Timer().schedule(timeSecs) { retData = SendBan(strList.get(2),strList.get(3)) } } } } else if(size == 6) { if(isPrefix(strList.get(3)) && isPrefix(strList.get(4)) && parseTimeInput(strList.get(5)).third) { val data = parseTimeInput(strList.get(4)) val timeSecs = setTimeInNumbers(data.second) Timer().schedule(timeSecs) { retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4)) } } } } } } }
m

marstran

08/30/2018, 2:51 PM
Put the code inside triple backticks: ```
o

otakusenpai

08/30/2018, 2:53 PM
else if(strList.get(1) == "ban") {
                       var found = false
                       if(size <= 3) {
                           retData = "Usage: ${specialChar}chan ban #channel prefix or " +
                                   "${specialChar}chan ban #channel prefix except or " +
                                   "${specialChar}chan ban #channel prefix time or " +
                                   "${specialChar}chan ban #channel prefix except time." +
                                   " Time can be in the following format: 6D or 6d(day), 10M or 10m(minute)." +
                                   " Following are some common literals for time: Second - s/S, Minute - m/M, Hour - h/H, Day - d,D. " +
                                   "Others need not be implemented."
                           type = 1
                       } else if(size > 3) {
                           for(i in channelList) {
                               if(i.retName() == strList.get(2)) {
                                   if(size == 4) { // 1st
                                       if(isPrefix(strList.get(3))) {
                                           retData = SendBan(strList.get(2),strList.get(3))
                                           type = 2
                                       }
                                   } else if(size == 5) { // 2nd n 3rd
                                       if(isPrefix(strList.get(3))) {
                                           if(isPrefix(strList.get(4))) { // 2nd
                                               retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4))
                                               type = 2
                                           } else if(parseTimeInput(strList.get(4)).third) { // 3rd
                                               val data = parseTimeInput(strList.get(4))
                                               val timeSecs = setTimeInNumbers(data.second)
                                               Timer().schedule(timeSecs) {
                                                   retData = SendBan(strList.get(2),strList.get(3))
                                               }
                                           }
                                       }
                                   } else if(size == 6) {
                                       if(isPrefix(strList.get(3)) && isPrefix(strList.get(4)) &&
                                               parseTimeInput(strList.get(5)).third) {
                                           val data = parseTimeInput(strList.get(4))
                                           val timeSecs = setTimeInNumbers(data.second)
                                           Timer().schedule(timeSecs) {
                                               retData = SendBanwithExcept(strList.get(2),strList.get(3),strList.get(4))
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
m

marstran

08/30/2018, 2:53 PM
You can edit your original comment. 😉
o

otakusenpai

08/30/2018, 2:53 PM
Timer().schedule(timeSecs) {
                                                   retData = SendBan(strList.get(2),strList.get(3))
                                               }
this part of the code needs to execute SendBan and wait for timeSecs seconds
idk, i dont remember what schedule did
like how do you put a function call and store data in a variable at the same time
m

marstran

08/30/2018, 2:58 PM
You're kinda prone to race conditions when you assign stuff to
retData
like that.
o

otakusenpai

08/30/2018, 2:59 PM
ok
so should i declare a val somethingFoo = SendBan(...) and then at the end of the onCommand assign tht variable to retData
or is there a better way ?
m

marstran

08/30/2018, 3:00 PM
Also, the timer will probably fire long after you have returned from your function, so at that time the
retData
variable no longer exists.
Assuming
retData
is defined inside your function, like it is in
onCommand
.
o

otakusenpai

08/30/2018, 3:01 PM
basically the idea is this the if statement checks ban, calls SendBan and waits timesecs, then sends a SendUnban
m

marstran

08/30/2018, 3:02 PM
Ok. Instead of assigning the
SendBan
to a variable, I think you should send it as a message on a channel.
o

otakusenpai

08/30/2018, 3:02 PM
like how?
m

marstran

08/30/2018, 3:02 PM
The component which is reading from
retData
should instead subscribe to the channel, and act on the
SendBan
message.
o

otakusenpai

08/30/2018, 3:02 PM
ok
so how do i create this channell, is it a coroutine or something ?
I guess you could use an actor.
You can read about both channels and actors there.
o

otakusenpai

08/30/2018, 3:04 PM
ok
so can you tell me what basically happens here >
m

marstran

08/30/2018, 3:05 PM
Do you mean with
Timer.schedule
?
o

otakusenpai

08/30/2018, 3:05 PM
no this channel and actor
m

marstran

08/30/2018, 3:06 PM
That's too much info to write down in a chat-thread 😉 I think you will find everything you need in that coroutines-guide.
o

otakusenpai

08/30/2018, 3:06 PM
okies
thnx
m

marstran

08/30/2018, 3:06 PM
No problem
o

otakusenpai

08/30/2018, 3:09 PM
also one more thing, will using a coroutine inside a plugin do any harm ?
g

gildor

08/31/2018, 8:10 AM
What is “plugin”? What kind harm? Coroutines is just an abstraction and library for asyncronous programming
If you use coroutines I don’t understand why do you need Timer.schedule if you can just use
delay
from kotlinx.coroutines
👍 2