I have a separate program that runs on the command line. When the program starts, it waits for input from std::cin and then gives a response. After the response, the program goes back to waiting for more cin.
I want to send this program some input (a string) from an oF app in a way that won’t block the oF app. Right now if I do something like ofSystem(“otherApp”) the oF app will just hang indefinitely while it waits for ofSystem to return. Ideally my oF app would be able to send and receive to and from this external program, while still doing it’s own thing. I guess the first step is to figure out how to actually send the other program input after starting it. The second step would be getting the result back somehow.
What’s the best way to do this? Should I spawn a separate thread and then pipe things in and out from there? I was looking into the pstreams library, but it seems that it wouldn’t solve this blocking issue.
The outside application is non-oF and I was hoping not to modify it if at all possible. Surely there is a way to launch a separate application from within a threadedFunction? Is it trivial to set up a tcp or osc connection without the aid of oF?
In my tests it seems to just hang. Working off of the threadExample…
void threadedFunction(){
if(isThreadRunning()){
if(lock()){
msg = ofSystem("outsideProgram"); //everything dies here
unlock();
}
}
}
If you want to launch the other app and have the ofSystem cal return immediately, you can add an & after the program name. I.e. ofSystem(“outsideProgram &”);
But in_avail() doesn’t ever seem to return true. What’s the preferred way to get cin in oF? If I don’t check to make sure there is something in cin, it will also block my app.
OK, think I got this sorted. I found an example of a nonblocking cin that seems to be what I need. I modified it a bit and put it up here for anyone interested.
The real good stuff is in this little bash snippet:
Once my oF app is launched whatever I send to cout will get sent via textPipe to cin in my external app. Similarly, whatever my external app sends to cout my oF app will receive via regular pipe in cin.