不断地从Linux (Ubuntu 16.04) c++上的非终止程序中获取数据

时间:2021-05-20 23:39:04

I have a situation where I rely on a third party stats polling application; the program is black boxed so I can only rely on whatever outputs that program gives.

我的情况是,我依赖于第三方统计调查应用程序;程序是黑框的,所以我只能依赖程序给出的输出。

Here's the idea:

想法是这样的:

  • Start get_stats thread
  • 开始get_stats线程
  • get_stats calls stats -p command (kind of like popen("...") )
  • get_stats调用stats -p命令(类似popen(“…”))
  • get_stats will continuously update some set of variables depending on the outputs from stats -p
  • get_stats将根据stats -p的输出不断更新一些变量集

Here's the catch: the provided stats program doesn't terminate, so popen() will not work for me. Ideally, I'd want get_stats thread to continuously update the internal vars based on the stats outputs. However, some occasional lag (~1 second or so) is totally acceptable.

这里的问题是:所提供的stats程序不会终止,所以popen()对我不起作用。理想情况下,我希望get_stats线程根据stats输出不断更新内部vars。然而,一些偶然的滞后(约1秒左右)是完全可以接受的。

Is there a way to do something like this? I assume it'll be platform specific, so as mentioned in the title, it'll be nice if you have a solution/idea for Ubuntu 16.04.

有办法做这样的事吗?我假设它是特定于平台的,正如标题中提到的,如果你有Ubuntu 16.04的解决方案或想法,那就太好了。


Edit: Here's a better solution for my specific problem with gazebo:

编辑:这里有一个更好的解决我的gazebo的问题:

The algorithm gz stats uses is the following:

gz stats使用的算法如下:

  • Get sim_time and real_time (up to 20 instances)
  • 获取sim_time和real_time(最多20个实例)
  • Find avg delta_sim by doing avg(sim_time[0 ~ 20] - sim_time[0])
  • 通过avg查找avg delta_sim (sim_time[0 ~ 20] - sim_time[0])
  • Repeat above to find avg delta_real
  • 重复以上步骤找到avg delta_real
  • sim_time / real_time = RTF on the Gazebo GUI
  • Gazebo GUI上的sim_time / real_time = RTF

Note: I used avg(...) to explain the algorithm, but the better alternative is to do sum(). avg isn't necessary because sim_time and real_time will have the same size.

注意:我使用avg(…)来解释算法,但是更好的选择是做sum()。avg没有必要,因为sim_time和real_time大小相同。

1 个解决方案

#1


2  

Use netcat or socat, run the black box application in the background, and connect to the listening TCP or UDP port. All you need to write, will be a socket program in C/C++ that handles data which comes from the socket. You can handle errors, wrong outputs, disconnects, etc. through socket programming.

使用netcat或socat,在后台运行黑盒应用程序,并连接到正在监听的TCP或UDP端口。您所需要编写的,将是C/ c++中的套接字程序,它处理来自套接字的数据。您可以通过套接字编程处理错误、错误输出、断开连接等。

This can be a good start:

这是一个好的开始:

nc -l -p 1234 -e /path/to/app

nc -l -p 1234 -e /path/to/app

Because you said that the application is Gazebo, you can use libgazebo7-dev library and control gazebo from C/C++ as described here.

因为您说应用程序是Gazebo,所以您可以使用libgazebo7-dev库并从C/ c++中控制Gazebo。

#1


2  

Use netcat or socat, run the black box application in the background, and connect to the listening TCP or UDP port. All you need to write, will be a socket program in C/C++ that handles data which comes from the socket. You can handle errors, wrong outputs, disconnects, etc. through socket programming.

使用netcat或socat,在后台运行黑盒应用程序,并连接到正在监听的TCP或UDP端口。您所需要编写的,将是C/ c++中的套接字程序,它处理来自套接字的数据。您可以通过套接字编程处理错误、错误输出、断开连接等。

This can be a good start:

这是一个好的开始:

nc -l -p 1234 -e /path/to/app

nc -l -p 1234 -e /path/to/app

Because you said that the application is Gazebo, you can use libgazebo7-dev library and control gazebo from C/C++ as described here.

因为您说应用程序是Gazebo,所以您可以使用libgazebo7-dev库并从C/ c++中控制Gazebo。