views:

320

answers:

4

I'm playing with an embedded Linux device and looking for a way to get my application code to communicate with a web interface. I need to show some status information from the application on the devices web interface and also would like to have a way to inform the application of any user actions like uploaded files etc. PHP-seems to be a good way to make the interface, but the communication part is harder. I have found the following options, but not sure which would be the easiest and most convenient to use.

Sockets. Have to enable sockets for the PHP first to try this. Don't know if enabling will take much more space.

Database. Seems like an overkill solution.

Shared file. Seems like a lot of work.

Named pipes. Tried this with some success, but not sure if there will be problems with for example on simultaneous page loads. Maybe sockets are easier?

What would be the best way to go? Is there something I'm totally missing? How is this done in those numerous commercial Linux based network switches?

+1  A: 

What kind of device is it?

If you work with something like a shared file, how will the device be updated?

How will named pipes run into concurrency problems that sockets will avoid?

In terms of communication from the device to PHP, a file seems perfect. PHP can use something basic like file_get_contents(), the device can just write to the file. If you're worried about the moment in time the file is updated to a quick length check.

In terms of PHP informing the device of what to do, I'm also leaning towards files. Have the device watch a directory, and have the script create a file there with something like file_put_contents($path . uniqid(), $command); That way should two scripts run at the exact sime time, you simply have two files for the device to work with.

preinheimer
It's an ARM9 device with 16MB Flash and 64MB RAM.I don't like the shared file approach because the application needs to constantly update the file and poll for files from the scripts. But yes, the unique file naming would solve the concurrency problem.
Purple Tentacle
A: 

Embedded linux boxes for routing with web interface don't use PHP. They use CGI and have shell scripts deliver the web page.

For getting information from the application to the web interface, the Shared file option seems most reasonable to me. The application can just write information into the file which is read by PHP.

The other way round it looks not so good at first. PHP supports locking of files, but it most probably doesn't work on a system level. Perhaps one solution is that in fact every PHP script which has information for the application creates it own file (with a unique id filename, e.g. based on timestamp + random value). The application could watch a designated directory for these files to pop-up. After processing them, it could just delete them. For that, the application only needs write permission on the directory (so file ownership is not an issue).

ypnos
CGI would give some space saving, but PHP seems so much easier that I think I'm ready to sacrifice the 3MB of 6MB available.
Purple Tentacle
+1  A: 

I recently did something very similar using sockets, and it worked really well. I had a Java application that communicates with the device, which listened on a server socket, and the PHP application was the client.

So in your case, the PHP client would initialize the connection, and then the server can reply with the status of the device.

There's plenty of tutorials on how to do client/server socket communication with most languages, so it shouldn't take too long to figure out.

Murat Ayfer
Sounds good. I think I'll have to try this. I would have to figure out a simple protocol for this. Any suggestions?
Purple Tentacle
Yes, use plain text for the protocol -- avoid the silly little voice that says "ooh binary is so fast". With text you avoid endianess issues and you can debug a lot easier.
gnud
A: 

If possible, use shell scripts.

I did something similar, i wrote a video surveillance application. The video part is handled by motion (a great FOSS package). The application is a turn-key solution on standardized hardware, used to monitor slot-machine casinos. It serves as a kiosk system locally and is accessible via internet. I wrote all UI code in PHP, the local display is a tightly locked down KDE desktop with a full screen browser defaulting to localhost. I used shell scripts to interact with motion and the OS.

On a second thought: If you can use self-compiled applications on the device: Write a simple program that returns the value you want and use PHP's exec() or passthru() or system().