Jeroen
Jeroen

Reputation: 16825

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? 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...

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.

EDIT: After some comments of you guys, I concluded that I am required to use 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. 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.

Upvotes: 0

Views: 1777

Answers (3)

Kyle_the_hacker
Kyle_the_hacker

Reputation: 1434

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.

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.

Upvotes: 1

Loki Astari
Loki Astari

Reputation: 264471

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).

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.
    : 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).

  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.
    : 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.

  7. Once you have it working with 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.

Upvotes: 0

zaufi
zaufi

Reputation: 7129

to be able to run application in background and have ability to close console where it was started, you may use nohup utility. 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.

and it will be relatively easy, then to turn you app into a full functional daemon.

Upvotes: 1

Related Questions