我试图在仿真器上编写一个包装器,它提供了一个web,这样用户就可以在服务器上生成模拟并收集结果。
我最初的设计是在Apache中运行一个php模拟StartSimulation.php,它将分叉和执行模拟,并将结果输送到一个文件中。但是,您不能在apache模块中使用php进行分叉和执行。此外,在这个实现中,它变得有点混乱,因为没有管理孤立模拟的进程,所以很难杀死它们并对它们进行计数。使用标准web服务器似乎有点困难,因为没有全局状态来存储正在运行的进程(模拟)列表。
模拟器是用c++编写的,但是应该尽可能少地修改它。我更喜欢为它编写一个包装器,并执行模拟器,捕获它的标准输出,并使用户能够访问它。
基本上,包装器应该有一个带有3个命令的web访问API。
1.)start_simulation --它基本上是对模拟器的一个实例进行分叉&执行,在表中记录它的pid,并将其标准输出连接到已知的缓冲区。包装器不应该允许模拟器的超过16个实例在服务器上运行。如果成功,它将向用户返回一个模拟代码。否则,它将返回错误代码。
2.)停止模拟-获取在(1)中返回的模拟代码,并杀死相关进程。
3.)get_process -获取(1)中返回的模拟代码,查找包含该进程的标准输出的已知缓冲区,并将该信息返回给用户。
我应该为此编写一个自定义应用服务器吗?如果是这样的话,是否有一个很好的包和一些初学者代码?
谢谢凯尔
发布于 2012-04-26 14:16:21
我知道这是个老掉牙的问题,但希望这对某人来说还是有帮助的.
拥有进程管理(叉/exec/等待/杀死/信号等)在通过http请求直接从Apache调用的PHP脚本中,绝对不是正确的方法。就像你说的,它很快就会变得凌乱:)
我建议通过http调用的PHP脚本只是一个简单服务器进程的命令代理。如果PHP是您的首选语言,则可以以这种方式实现这两种语言。
例如,您可以在消息队列中这样做,如下所示。
。
让我们用一些PHP代码来说明这一点。为了简洁起见,我把这些琐碎和简单的东西保留下来。
PHP脚本(web表单目标)
这就是我们解释传入表单请求并将其转换为服务器进程的消息的地方。
<?php
/*
* Responses are sent as simple text strings to be interpreted by
* the client-side JavaScript handling this AJAX call. Responses
* starting with 'ERR:' are errors, responses starting with 'ACK:'
* are acknowledgements. Simply check the first few characters of
* the response in the client-side JavaScript.
*/
header('Content-Type: text/plain; charset=UTF-8);
/*
* Here we define some message types, one per command. These should correspond
* to the command string sent from the web form
*/
$command = array(
'START_SIM' => 1,
'STOP_SIM' => 2,
'GET_STATUS' => 3,
'GET_OUTOUT' => 4
);
$queue_id = 0xbeef; /* An arbitrary message queue id */
$cmd = $_REQUEST['command']; /* The command from the web form */
/* get simulator instance id to act upon, if specified */
if (isset($_REQUEST['id']))
$sim_id = $_REQUEST['id'];
else
$sim_id = ''; /* no sim id? probably a create command */
/* check the message queue exists... */
if (msg_queue_exists($queue_id) === false) {
echo 'ERR:Message queue not found!';
return;
}
/* open the message queue and pass the command on to the server... */
if (($qhandle = msg_get_queue($queue_id)) === false) {
echo 'ERR:Failed to open message queue to server';
return;
}
if (msg_send($qhandle, $command[$cmd], $sim_id) === false)
echo 'ERR:Failed to send command';
else
echo 'ACK:Command sent ok';
?>PHP (在web服务器上单独运行)
这里有一个同样简单的服务器..。
<?php
/*
* assume the same queue id's and defines as in the
* client code above, etc..
*/
if (($qhandle = msg_get_queue($queue_id)) === false) {
/* emit failure message to log file etc.. */
...
return;
}
while (1) {
if (msg_receive($qhandle, 0, $msgtype, $message,
true, 0, $rc) === false) {
/* log error message ... */
} else {
/*
* Get the client id (in case you want to send
* a reply back to the client) and the
* message data, which is the simulation id.
*
* Remember that we use the message type to
* indicate the command being requested
*/
$client = $message['client'];
$sim_id = $message['msg'];
evaluate_command($client, $msgtype, $sim_id);
}
}
?>显然,这非常简单,没有错误检查,您需要自己编写"evaluate_command()“函数。我只是草草地写下来来说明这个想法(我已经把它写在袖口上了,所以它可能也充满了其他的错误!)
https://stackoverflow.com/questions/7150564
复制相似问题