简体   繁体   English

如何通过Console / C ++与程序通信

[英]How to Communicate With a Program Through Console / C++

When I run an application through a console, for example, $application start , how can I communicate with said application? 当我通过控制台运行应用程序时,例如, $application start ,我该如何与所述应用程序通信? So I can for example do $application load --/home/application/files/file.txt --warn=0 --notice=0 and that running instance of the application would react to this... 所以我可以举例来说$application load --/home/application/files/file.txt --warn=0 --notice=0并且运行的应用程序实例会对此作出反应......

I do not want to keep listening to the console on the application side. 我不想继续在应用程序端监听控制台。 I want to be able to close the console, reopen it, and still interact with the program. 我希望能够关闭控制台,重新打开它,并仍然与程序进行交互。

The reason why I am doing this is because I want a master program that loads in different operations which it performs in the background. 我这样做的原因是因为我想要一个主程序加载它在后台执行的不同操作。 I want to be able to add operators, and remove operations. 我希望能够添加运算符,并删除操作。

Myself I have some experience with PHP and I know Apache has such behavior. 我自己有一些PHP的经验,我知道Apache有这样的行为。

EDIT: After some comments of you guys, I concluded that I am required to use IPC. 编辑:经过你们的一些评论,我得出结论,我需要使用IPC。 I have heard of this before but I never really understood how it works. 我之前听说过这个,但我从来没有真正理解它是如何工作的。 After some Googling and the WikiPedia links you showed me I concluded that there are a sh-t tun of ways of handling IPC. 经过一些谷歌搜索和WikiPedia链接,你告诉我,我得出的结论是,有一些处理IPC的方法。 I want to send packages of data to the main process, which one would be the best in my case? 我想将数据包发送到主进程,哪一个在我的情况下是最好的? My personal favorite atm is a message queue but that only seems to work within the same process. 我个人最喜欢的atm是一个消息队列,但它似乎只在同一个进程中工作。

Since @LokiAstari pointed out, that you may don't have much experience with C++, I would recommend you to read: How to parse command line parameters . 既然@LokiAstari指出,你可能没有太多的C ++经验,我建议你阅读: 如何解析命令行参数

I would then use a temporary file in /tmp to communicate with the main program, which run an infinite loop, waiting for modifications to the temporary file. 然后,我将使用/tmp的临时文件与主程序通信,主程序运行无限循环,等待对临时文件的修改。

to be able to run application in background and have ability to close console where it was started, you may use nohup utility. 为了能够在后台运行应用程序并能够在启动时关闭控制台,您可以使用nohup实用程序。 then first instance of you app should create some ("well known") IPC resource (message queue, FIFO, whatever), so further instances will communicate over it with the first instance. 那么你应用程序的第一个实例应该创建一些(“众所周知的”)IPC资源(消息队列,FIFO,无论如何),因此更多实例将通过它与第一个实例进行通信。

and it will be relatively easy, then to turn you app into a full functional daemon . 并且它将相对容易,然后将您的应用程序变成一个完整的功能守护进程

Personally I would do this in multiple stages. 我个人会在多个阶段做到这一点。
As otherwise you are going to be trying to solve to many different problems at once. 否则,您将尝试同时解决许多不同的问题。

What you are doing is writing a service (a long running application). 你正在做的是编写服务(一个长期运行的应用程序)。 Communication with the service by running a command usually involves running a different application that talks to the service (in apaches case the apache command starts the httpd service. Then subsequent commands talk to the httpd service). 通过运行命令与服务进行通信通常涉及运行与服务通信的不同应用程序(在apache情况下,apache命令启动httpd服务。然后后续命令与httpd服务通信)。

But to get this up and running it is easier to go through a few steps first. 但是为了使其正常运行,首先要完成几个步骤会更容易。

  1. Write an application that on startup read commands from a directory 编写一个应用程序,在启动时从目录中读取命令
    : So on startup you have a command directory. :所以在启动时你有一个命令目录。
    : You open each file (in order execute the file if it is valid) then re-name the file to show it was done. :您打开每个文件(按顺序执行该文件),然后重命名该文件以显示它已完成。

  2. Modify your application to run as a continious loop. 修改您的应用程序以作为连续循环运行。 All the loop does is look for events in job queue. 所有循环都是在作业队列中查找事件。
    : if it sees them execute the job. :如果它看到他们执行工作。
    : If no job there then sleep for 10 seconds. :如果那里没有工作,那么睡10秒钟。
    : On startup you just inject one job :在启动时你只需注入一份工作
    -> : It reads the command directory and creates a job for each file. - >:它读取命令目录并为每个文件创建一个作业。
    -> : The file job executes the file then renames the file to show it is done. - >:文件作业执行文件,然后重命名文件以显示文件已完成。

  3. Modify your service to use threading. 修改您的服务以使用线程。
    : Run the event loop in one thread. :在一个线程中运行事件循环。
    : Use locks and semaphores so that added items to the queue is thread safe. :使用锁和信号量,以便向队列添加的项是线程安全的。
    : When the application starts up you start the event loop (making sure it started then inject the job (as in 2). Then just waits for the event loop to finish (it will not). :当应用程序启动时,启动事件循环(确保它启动然后注入作业(如2)。然后等待事件循环完成(它不会)。

  4. Add a timer thread that fires very ten seconds to check the command directory 添加一个触发十秒钟的计时器线程来检查命令目录
    : All the timer should do is create a job and put it in the event queue. :所有计时器应该做的是创建一个作业并将其放入事件队列中。
    : Now you don't need to inject a job at startup its the timers job. :现在你不需要在启动时注入一个作业的定时器工作。

  5. Once you have all the above running you are ready to introduce a listener that will listen on a socket for indirect commands from another application. 完成上述所有操作后,您就可以引入一个侦听器,该侦听器将在套接字上侦听来自其他应用程序的间接命令。
    : Doing all the above in one go to run the service is going to be long an error prone for a beginner. :一次性执行上述所有操作对于初学者来说将是一个很长的错误。 I advice you to fallow through all the steps above to get to this state then ask another question about how to do IPC. 我建议你休息一下上面的所有步骤来达到这个状态然后再问一个关于如何做IPC的问题。
    : So add a new thread that listens on a socket (OK this is not the best technique but this is bootstrapping a beginner). :所以添加一个侦听套接字的新线程(好吧,这不是最好的技术,但这是引导初学者)。 When it receives input it creates a file in the command directory then places a job in the job queue. 当它接收输入时,它会在命令目录中创建一个文件,然后将作业放入作业队列中。

  6. You should now be able to test your command using the command line curl (or wget command) to send files to your service. 您现在应该能够使用命令行curl (或wget命令)测试您的命令,以便将文件发送到您的服务。

  7. Once you have it working with curl . 一旦你使用curl
    You can write a standalone application that converts command line arguments into command files and sends them to your service. 您可以编写一个独立的应用程序,将命令行参数转换为命令文件并将它们发送到您的服务。

  8. Convert your application from using files to having all the information in the job object. 将应用程序从使用文件转换为在作业对象中包含所有信息。

  9. Thats it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM