Emulating a Persistent Running Script with Multiple Clients

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Netherwolf
Forum Newbie
Posts: 1
Joined: Thu Mar 27, 2008 9:50 am

Emulating a Persistent Running Script with Multiple Clients

Post by Netherwolf »

I'm new at this, and I'll readily admit I don't know the lingo, so please, be gentle with this noob. Here's my project in a nutshell.
  1. A client contacts the web host with a XMLHttpRequest to a PHP script.
  2. The script sets up a dialog with a remote serial server.
  3. The script sends and receives data as necessary and logs everything.
  4. The script closes the remote session and returns the XML package to the client.
  5. The client receives the data, processes it, and may or may not restart from the beginning.
Sounds easy enough. Here are the conditions:
  1. Any additional client that logs in will ask for data via the same XMLHttpRequest.
  2. The remote host is a serial server. It doesn't understand the concept of multiple sessions.
  3. The PHP script that returns the XML must be intelligent enough to know whether a session is already in progress.
  4. This would be blinkingly easy if I could run the script perpetually. For now, let's assume that I don't have that option.
The only part that worries me is maintaining one-at-a-time exclusive access to the serial server. I want this to work on any PHP-enabled web host I put it on. The methodology I've worked out is as follows, but since this is my first attempt, I want your opinions.
  1. Once the request comes in, the script locks a key file.
  2. When the dialog with the remote serial server is finished, the script unlocks the key file and writes a timestamp to a list index file.
  3. Any concurrent instance of the script will try to lock the file, but fail.
  4. It will then read the last timestamp from the list index file.
  5. It will access that log, read the data, and package up an XML to send back.
  6. The illusion is that the script is persistently running, sending and receiving data, when in fact, its life is measured in abrupt instances.
So, forgiving my noobness, what am I missing before I put this in code?
EDIT: The careful reader will notice that any concurrent client will be getting "slightly stale data." That's perfectly fine. I'm monitoring conditions that won't change suddenly, and I'm not worried about "slightly stale."
Post Reply