user8624414
user8624414

Reputation:

Adding web interface/control to c++ application

I have a c++ program on a raspberry pi (headless setup) that records data from an ADC every minute over a period of days and stores them in a file. I want to add an web interface to it that launches possibly many instances of the program, keeps track of how far along they are till completion, view the data possibly in a chart and download the recorded data after completion. Once launched, I want the instances to be running even after the client closes the web interface. What is the best way of doing this? The most troubling part of this for me seems to be maintaining a long running task in the background within a webserver which is conceptually short-lived (a session ends when the client closes the tab in the browser)


What I have considered so far

  1. Ready-made web servers like nginx and apache with CGI: But the conceptual problem is the same. If I arrange it so that a cgi application executes when a button is clicked, because the cgi application has to return before the webserver can send a response to the client, I can't see how it can do the job. Even if I launch it as a child process and detach it, then I will lose control over it for termination, checking progress etc. If somehow it is possible, is there a good cgi/fastcgi library in c++?
  2. Something like the wt framework: Same problem. WRun creates an object of the wtapplication class for each "session" (client) and frees them when the client disconnects or after a period of inactivity. So I'd lose track of my launched programs when the client that launches the program disconnects.

Is there any other way of doing this? Or some kind of solution that is employed in this scenario? Any other libraries/frameworks/solutions to consider?

Upvotes: -1

Views: 160

Answers (1)

m7913d
m7913d

Reputation: 11072

This is perfectly doable with Wt; see the Server Push example.

In it simplest form, you can just start a new background thread as follows:

std::thread([](){
  ... // your background task.
}).detach();

Have a look at the Server Push example, if you want some interaction between the worker thread and your GUI.

Upvotes: -1

Related Questions