Hi Beth
So here's a slightly modified approach. The problems you describe are:
1) Users (note the plural) who don't use the supplied value (let's call them wasted values)
2) Need to maintain a gapless sequence
To these I'm going to add a couple of things that we need to consider:
3) Concurrency - what happens when more than one person uses the system at the same time, and
4) Latency - how fast we can handle requests for this control value, even if we end up wasting it!
First off, because you have identified users then the concurrency and latency problems are real, and must be dealt with. Neither of them is very difficult, but you do have to consider them. If more than one user can access your system, it is only a question of when not whether a collision will occur. So we need to figure out the maximum number of users (we assume, in a somewhat perverse manner, that they will all try and access the system at the same time), and how long we are prepared to make them wait while we service "all the others". Let's assume that we have 10 users (YMMV), and we are prepared to wait up to 3 seconds for the system to respond. (Bear in mind that this is a one-time part of the usage - and, with any luck, this planning won't be needed for most of the time).
Those figures mean that we have to be able to create a value in less than 2 divided by 10 (= 1/5) seconds. In general the processing must occur in
My earlier suggestion was to open a file, get a value, hide the file until we have saved the new seed value
I think we can meet both of your needs with a slight modification:
Look for a specific file
Rename it to a random name
Work with the file
Rename it back to the known name
This uses the concurrency management of the file system to do the really tricky stuff, ad gives us a lot more freedom in how we process the file.
If we look for the file and can't find it, we need to back off and wait for a while, before trying again. After a certain number of tries (MaxTries) we give up. This is basically the algorithm used for Ethernet, and that's an overwhelmingly successful way of handling both issues. I'm happy to copy it!
So our high level code looks like this:
If we have no problems getting the control value, we get in and out very fast, with only the overhead of one attempt to manage the file (which has so far not yet made an appearance). Then we can wait a short while, up to MaxTries (10 in this case) times before giving up.
MakeAttempt is the main procedure that will do all the file processing, and BackOff takes care of waiting and - if necessary - providing some sort of visual feedback.
Here's what BackOff lookslike, at least as a starting point:
Note that WaitTime is a global variable - we may want to set its value in some sort of initialization routine, rather than every time we hit backoff.
Make the attempt implements the rest of the algorithm:
So - there we have the outline, in four procedures. They're all pretty skeletal as yet, but I'll get back to you later with some more fleshed out code. Along the way, you might like to think of a file-type that can store a SeedValue in one place, a whole list of possibly WastedValues in another, and a set of ActiveValues (which may yet turn out to be wasted) in yet a third place. (Hint, it's near and dear to the users of this Forum!
Talk with you soon!
Tony
Bookmarks