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

Troubles with new program of mine

Started by LDDestroier, 19 November 2016 - 03:55 AM
LDDestroier #1
Posted 19 November 2016 - 04:55 AM
I'm trying to make a program to take advantage of the fact that multiple computers can have the same ID and same hard drive. But it's not quite working.

What I expect to happen is I push a button, the event is saved to the hard drive (along with a bit of metadata to prevent infinite events), that change is picked up with .readLine(), and the event is queued. But it doesn't work most of the time. I would hold a letter for five seconds, and only two or three letters would show up on the other end.

The structure is sort of like client/server. Run 'syncro' on server, and for the client, hold 'Home' directly after running (0.5 second window of time)

Can someone help me get this functional?

http://pastebin.com/yGQWBTve
valithor #2
Posted 19 November 2016 - 06:57 PM
I can see one of two scenarios causing your problem.

Events are being put into the file, but are not being read/queued correctly. (problem is in your read function)
- Problem could be that the events have same metadata causing them to be discarded

Events are not being put into the file (problem is in your write function)

This could be tested by having the program saving events to a file write all of the events to a second file that is not automatically emptied by the reading program, granted I do not believe, based upon reading your code, that both programs have to be running at the same time. If that is true could run one check the file between and then run the other one.

Narrowing down which of the two functions has a problem would significantly improve our ability to try and fix it.
Edited on 19 November 2016 - 06:01 PM
Bomb Bloke #3
Posted 20 November 2016 - 04:23 AM
readEvents() opens a handle on your syncro file and then never lets go of it. It should drop that thing at the earliest opportunity! Whenever you need to read, cache all available lines into a table in one go, then pull them from that buffer as you process them. Wipe the file completely after each read, rather than trying to shuffle its contents every now and then.

If you feel you must hang on to sliceEventLog(), fix its off-by-one error:

  for a = #input-(newSize or 5)+1, #input do

Events may ("may") naturally have line breaks included in their data; gsub'ing textutil's output in such a general manner is potentially dangerous.

Your use of sleep(0) is likely a big problem. IIRC timer IDs are unique to each system and start counting from 0 after each reboot. I'm not sure how you'd deal with the possible collisions this could cause if that is the case.

This is a micro-optimisation, but for this sort of thing it becomes valid - table.insert() is slow. "myTable[#myTable + 1] =" is faster, manually managing your own variable to track your table's size is faster still.

table.remove(evtqueue,1) is even worse. It can also be avoided here with a counter - you don't always have to pull events from the front of your table.

Instead of concatenating your client status together with your event data, why not just put it directly into the evt table, against a key such as "isClient"? unpack() will ignore it if it's not numerically indexed.