Semaphore is a resource allocation technique to manage deadlocks and race conditions via priority queues. Putting it metaphorically, you have 3 bicycles for hire. Suppose, you hire it on a first come first serve basis. So if you have hired all your 3 bicycles and a fourth person requests it, he must wait until one of them has brought the bicycle back. There are chances that you may reserve the bike for someone, someone may cancel a already reserved bike and so on. Similar in case of processes that want to acquire some resources, semaphores are used.
In case of a distributed semaphore, there could be multiple resources that could be accessed by multiple processes. Assume a server hosts an application and can host multiple instances of it and the user can use the application if it is free. Let server be S1,S2,.. Sn while the application be A1,A2,...An and users be U1,U2,...Un. So if the first server could host three instances of the application then it would be denoted as S1A1,S1A2,S1A3. Also assume the application instances as slots or boxes for easy understanding. A slot is considered empty when it is free and full when it is busy. So, in this case, the following method is suggested
- User requests the application
- The request is passed to a priority queue.
- The queue runs a continuous background process to search for free slots and once a free slot is found, passes the request to the application to start processing
- Once a slot gets the request, they mark themselves busy and starts processing and marks themselves free when they finish processing
- The slots periodically broadcasts their status to the queue so that the queue could forward the requests once a slot becomes free
- This model is based on centralized message brokering. The queue that processes the request should always be available. There can also be broker-less model that could be implemented in a different way
- Semaphore Programming on Wikipedia
- The Little Book on Semaphores is a superb resources freely available
- Semaphore is based on shared memory model. Actor Model would give a new insight to solve this problem in a different way
Specific answer to the question in context
Looking at the code, I prefer the classical version of a semaphore. Java has a built-in semaphore package to do this and you could just easily build on it.
Specifically, I'd like to know what is a good first-step goal, or a simple subsystem I can implement.
Assuming only one instance of the application should be run on the LAN
- Use the package suggested above
- Create the semaphore with a built-in queue mechanism (similar to a stack) and implement it as a server
- When a user starts the application, it searches for the semaphore, decrements the count (in this case just 0 since only a single instance is to be allowed) and gives user the access to the application
- When an other user starts the application, it searches for the semaphore, since it is 0, puts the user in the queue and gives a message to the user that the application is busy
- When the first user exits the application, the semaphore is incremented, the resource becomes available, the user is intimated and he can now start the application.
- The semaphore can be implemented as a stand-alone server (possibly with a simple web interface) that could be coupled with the application.
The following caveats are worth thinking
- Queues may be flushed entirely or priority can be changed
- Coupling it with the application as a server, requires there would be only one version of the server on the LAN
- Prepare for crashes or unattended message queues (User request access but not available when the application is free)
I would probably suggest the following for better understanding
- Follow this link for better understanding
- Try SCALA. You can use all your existing JAVA libraries plus you get good concurrency
- Have a look at ZMQ. This could be the option if you abstract out the entire process into a separate messaging application that interacts with the resources
- Read about the AutoCAD licensing system (they implement a licensing model that matches few of your requirements)
Answer completely edited since the original answer is based on a much broader perspective