Mat
Mat

Reputation: 181

Make a web server carry a program

What I want to do is the following: make a server carry a program which analyses a picture (which is sent to the web server) and returns a result, the program already exists and is written in C# and C++. For interest, this program analyzes a pattern/grid to determine how much pressure is exerted on a metallic object, and takes no more than one second to do its analysis.

I would like to know:

  1. How can I store a program on the web server (alternatively how can I store it so that I can access it from the web server)?
  2. How can I call upon the program through the web server?
  3. I suppose the above calls for some programming with languages such as PHP, but where is this code written/executed?

Upvotes: 1

Views: 115

Answers (2)

halfer
halfer

Reputation: 20487

The simple solution to this problem is the run your program synchronously inside your web request (this means that your web page has to wait for the program to deliver its results). This means that, in your PHP page (or a controller, if you are using an MVC-like architecture) you use system() or similar to run your program on the server. This will probably output results to standard output, or maybe to a file or a database. If your program takes a second to run, this is probably an acceptable delay, as long as you have a low number of users.

To do this:

  • Store your console program somewhere on the server, "C:\My Programs\My Analyser\Analyser.exe" is fine.
  • You should know the path location of your image file. You can then pass that pathname as a parameter to your console program.
  • You can then retrieve the results of the call, either from stdout or elsewhere, depending on how your console program returns its analysis

That said, a better way to run this it is to run a job server, so that your program is run asynchronously (this means that your job server accepts a request to run the program, and it will be run when a slot becomes free). This will mean your web page will run faster, and your server won't get overloaded with too many people running your program at the same time. The downside is that the architecture of your system is more complicated, and you'll have to change your UI to cope with unprocessed jobs.

If you are interested in setting up a job server, consider something like Gearman. You'll have to poll the server to see if a job has been processed, or have your job process write a completion flag to your database, which can be checked trivially by your web script.

Upvotes: 0

bwoebi
bwoebi

Reputation: 23787

I'd really do this with PHP instead at webserver level.

PHP has a function named shell_exec. This command can invoke a program.

$result = shell_exec("./your_program ".escapeshellargs($image_path));

Under the condition that the webserver has the rights to execute the program (should be chmod mode 755).

(P.s. This should work on *nix, for Windows I am unsure)

(If you aren't able to compile your program on the server, cross-compile it and then upload it)

Upvotes: 1

Related Questions