Jump to content

Communication W/ Non-traditional Server


DKP

Recommended Posts

My company has software that allows customers to visualize the results of a simulation. Using this software, for example, one could visualize how students at a school may react to a fire drill. The user can see the floor plan of the school and icons on the screen representing each student would show the path each one might take.The reason I'm posting to this forum is because I'm trying to figure out what would be required to perform visualization of our simulations over the web. I'd like the simulation to be running on the server, while the visualization is being performed in the client's browser. I've written a browser plugin, which is performing the visualization, and I can communicate w/ the simulation server using standard TCP/IP. However, I'd also like it if our customers could make small modifications to the simulation. For example, start and stop the simulation, add an obstacle, query some statistics about one of the entities, etc.I've been researching several technologies on W3C (JavaScript, AJAX, PHP, SOAP, WSDL, Web Services). It seems to me that we could provide this type of functionality to our customers via a JavaScript API (i.e. customers could write JavaScript that called our functions, which in turn would communicate w/ the simulation server). The real question though, is how do I make the JavaScript API calls communicate w/ our C / C++ simulation engine? The closest thing I've seen on this sight is the Web Service example using SOAP. Is this the best way to go? Can this be done in a reasonable fashion w/o ASP? Using ASP may be ok if that's significantly easier, but our software runs on both Windows and Linux, so I'd like to keep my options open.I realize this is a rather non-traditional use of these technologies, as I'm really no trying to update the web page itself, but I'm hoping I can make use of these capabilities to accomplish what I'm trying to do.Any information anyone can provide would be much appreciated.

Link to comment
Share on other sites

For the client, I would imagine Javascript would be the best bet to send data to the server, it wouldn't mess with the existing page. For the server API to receive data, you can probaby do several things. First, you would have to match up the web server's session for the user with the C++ process ID, so that when a request comes in using a certain web session you'll know which instance of the simulation they're trying to work with. Since you're dealing with sessions, an obvious thing to use might be PHP to receive the user's data and send a system command to a program that can get the data and send a message or signal to the running simulation process. So in addition to the sim engine and the PHP gateway, you would also need a little command-line application that PHP can execute and send the data to, which in turn will pass the data along to the running sim. If PHP could pass along the process ID and whatever data you have, that should be enough to control the sim for that user. The main problem might be getting the PID in the first place and storing it in the user's PHP session. If a user pulls up the page and uses your plugin to start a sim, then they already have a web server session going, so if you have PHP start the sim with a system command then there might be a way for PHP to get the PID that it just started. Does any of that make sense?

Link to comment
Share on other sites

So if I understand you correctly you're saying 1. let the user write JavaScript code on the client and provide them w/ a library of JavaScript functions 2. the JavaScript library functions will wrap code to send HTTP requests back to the server 3. on the server a PHP script will receive the message and execute a command line application (C++ or other language) 4. the command line application communicates w/ the running simulation application via some means (shared memory, socket, etc.) 5. the simulation application receives the message from the command line application and processes it 6. the simulation application communicates w/ the browser plug-in via some predetermined protocolIs that about right? I guess the piece I was missing the PHP execution of the application. Looking at the PHP site, they said you couldn't interface PHP w/ C++, which I guess you're not doing directly, but this is a nice work around. What PHP command can I use to do this?You're point about the identifying a particular client w/ a particular execution of the simulation is well taken. That could be tricky to sort out. However, it's likely that clients would have to log into our site in order to view the simulation. Perhaps an initial HTTP request from the client could return a unique Id that could be used w/ subsequent requests to identify which client.

Link to comment
Share on other sites

I'm not sure if you'll want to allow the user to write code, I was thinking the API would have a predefined set of functions and the user would just push buttons (or whatever) to run the functions (like stop, start, etc). Either way would work though, I guess it would just be more difficult to allow arbitrary code through and to run it.PHP has a series of function to run system commands:http://www.php.net/manual/en/book.exec.phpExec would probably be the one to use. I've used exec to run a command-line unzip utility on an uploaded file, for example.PHP can automatically keep track of which user is sending requests. PHP has good session support, where it sends the user a cookie with a unique ID and uses that ID for subsequent requests. You can store data in the session (which gets saved on the server) and recall that user's session data for each request they make (assuming the session cookie hasn't expired). So that's the easy part - the hard part would be having a client sign in and starting their session, triggering the simulation to start, getting the PID of the process that runs the sim, and associating that PID with the PHP session. Some of those system command functions for PHP will return the program output to PHP, but only if the program terminates. That might be one way to do it though, have PHP call a "kick-off" application that in turn starts the actual sim (maybe by forking a child process), gets the PID of the sim, outputs it to the console and terminates (leaving the sim running in another process). PHP will receive the PID as output and can save that in the user's session.If you want to see a basic session example, this script will set a session variable if it's not set, or else display the value of it. You can run this in a browser (from a server with PHP support) and keep hitting refresh and you should see that it sets the variable the first time, and each refresh it finds the value already there and just prints it. PHP exposes the session variables as a global array called $_SESSION.

<?phpsession_start();if (isset($_SESSION['test'])){  echo 'session is set: ' . $_SESSION['test'];}else{  echo 'setting the session';  $_SESSION['test'] = 'testval';}?>

Link to comment
Share on other sites

I tried this, but so far no success. Using the php site you pointed me to, I created a php script of the following. IN the long run of course, the executable would be my app for communicating w/ the simulator and the args would be the request from the client, but for starters I thought I'd just start w/ something simple like bringing up notepad. Can you see anything that I'm doing wrong?

<?php$exe = "c:\\windows\\system32\\notepad.exe";$args = ""; //$_REQUEST['args']if (substr(php_uname(), 0, 7) == "Windows") {    pclose(popen("start /B " . $exe . " " . escapeshellarg($args), "r"));   } else {    exec($exe . " " . escapeshellarg($args) . " > /dev/null &");   }?>

Link to comment
Share on other sites

Sorry, bit of a false alarm. If I substitute my program and arguments the php code in the previous post it does work. Still a little curious as to why this doesn't work for notepad (permissions?), but as long as my program works I guess that's all that matters.

Link to comment
Share on other sites

Using my application, everything works fine, but when I substitute notepad, I don't see any errors (at least no dialogs pop up) and notepad never shows up. No worries. At least my application is working, which was the important thing.Thanks for your help.

Link to comment
Share on other sites

A quick follow up question: I noticed in some of the php documentation that there is socket support w/ php. Is it possible to just send a socket message to the simulation directly from php instead of executing an application that sends the message? Have you ever used sockets this way? It appears that in order to use them all I need to do is un-comment the following line from the php.ini file. Is that it?;extension=php_sockets.dll

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...