This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
bdew's profile picture

How to notify ComputerCraft that my peripheral is no longer valid?

Started by bdew, 12 October 2014 - 05:20 AM
bdew #1
Posted 12 October 2014 - 07:20 AM
This is my first go at implementing a peripheral so i might be missing something really obvious…

I have noticed that sometimes, when my peripheral is broken that ComputerCraft seems to hang on to the peripheral object, which in turn still references the TileEntity which is not valid anymore. The wrapped peripheral is still available from LUA at that point an calling any commands on it works.

I've worked around this so far by having my code check that the TE is still valid before executing any commands, this works but it still has the problem of having the "ghost" peripheral and TE stick around.

Is there some way to tell CC that it should drop the peripheral? Or maybe force-disconnect all computers?
theoriginalbit #2
Posted 12 October 2014 - 09:28 AM
to be honest I've never encountered this problem with any of my peripherals; to the extent that ComputerCraft even unmounts my IMounts incorrectly. is it possible for us to see your peripheral and peripheral provider classes in question?
bdew #3
Posted 12 October 2014 - 10:37 AM
I'm kinda in the middle of cleaning up and rewriting the whole thing, if the problem persists after i'm done and the code is fit for human consumption i'll post it :P/>

It might be a bit… complicated for the average java modder, it's in scala and uses Futures/Promises to pass on stuff to execute to the main server thread while the coroutine thread yields and waits for an event telling it the Future is completed.
theoriginalbit #4
Posted 12 October 2014 - 10:48 AM
ah okay. if you were using Java I was going to suggest using my Peripheral-Framework, but now you say that you're using Scala, it could be that.
bdew #5
Posted 13 October 2014 - 12:30 PM
So looks like the peripheral removal thing is actually a bug in CC - i was able to reproduce the same thing with vanilla CC peripherals

In other news, after refactoring everything i got it mostly working.

Another problem i ran into is that if my connection to the computer is broken while there is a call in progress, there's no way to interrupt ILuaContext.pullEvent that's waiting for my internal completion event. Posting new events at that point is impossible and interrupting it with Thread.interrupt() seems to corrupt the internal state of the LUA engine (which kinda makes sense, considering the coroutine starts executing, while a different one is already running).

So i added a check for "peripheral_detach" events. It seems kinda backwards to listen to that being fired on the computer, but the other option is leaving the coroutine hanging forever, so meh :P/>

Overall, once all the quirks and edge cases are handled, Futures seems to play pretty well with with how coroutines work in CC.

I will be opensourcing my code once it's a bit more polished if anyone is interested.