"Fossies" - the Fresh Open Source Software Archive

Member "muscle/html/muscle-by-example/examples/dataio/example_6_child_process.cpp" (28 Nov 2019, 3064 Bytes) of package /linux/privat/muscle7.52.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include "system/SetupSystem.h"  // for CompleteSetupSystem
    2 #include "dataio/ChildProcessDataIO.h"
    3 #include "dataio/StdinDataIO.h"
    4 #include "util/SocketMultiplexer.h"
    5 #include "util/String.h"
    6 
    7 using namespace muscle;
    8 
    9 static void PrintExampleDescription()
   10 {
   11    printf("\n");
   12    printf("This program demonstrates using a ChildProcessDataIO object to launch a child\n");
   13    printf("process and then communicate with it by writing data to its stdin and reading\n");
   14    printf("data from its stdout.\n");
   15    printf("\n");
   16    printf("This program will launch the \"example_2_tcp_server\" program as a child\n");
   17    printf("process, and then let you interact with it in the usual way.\n");
   18    printf("\n");
   19 }
   20 
   21 int main(int argc, char ** argv)
   22 {
   23    CompleteSetupSystem css;
   24 
   25    PrintExampleDescription();
   26 
   27 #ifdef MUSCLE_AVOID_CPLUSPLUS11
   28    Queue<String> childArgv; (void) childArgv.AddTail("./example_2_tcp_server");  // support for pre-C++11 compilers
   29 #else
   30    Queue<String> childArgv = {"./example_2_tcp_server"};
   31 #endif
   32    ChildProcessDataIO cpIO(false);  // false == non-blocking
   33    if (cpIO.LaunchChildProcess(childArgv) != B_NO_ERROR)
   34    {
   35       printf("Unable to launch child process!  Perhaps the example2_tcp_server executable isn't in the current directory, or doesn't have execute permission set?\n");
   36       return 10;
   37    }
   38 
   39    StdinDataIO stdinIO(false);  // false == non-blocking I/O for stdin
   40    SocketMultiplexer sm;
   41    while(true)
   42    {
   43       // Tell the SocketMultiplexer what sockets to listen to
   44       sm.RegisterSocketForReadReady(stdinIO.GetReadSelectSocket().GetFileDescriptor());
   45       sm.RegisterSocketForReadReady(cpIO.GetReadSelectSocket().GetFileDescriptor());
   46 
   47       // Wait here until something happens
   48       (void) sm.WaitForEvents();
   49 
   50       // Time to read from stdin?
   51       if (sm.IsSocketReadyForRead(stdinIO.GetReadSelectSocket().GetFileDescriptor()))
   52       {
   53          char inputBuf[1024];
   54          const int numBytesRead = stdinIO.Read(inputBuf, sizeof(inputBuf));
   55          if (numBytesRead >= 0)
   56          {
   57             printf("Read %i bytes from stdin, forwarding them to the child process.\n", numBytesRead);
   58             const int numBytesWritten = cpIO.Write(inputBuf, numBytesRead);
   59             if (numBytesWritten != numBytesRead)
   60             {
   61                printf("Error writing to the child process!\n");
   62             }
   63          }
   64          else break;  // EOF on stdin; time to go away
   65       }
   66 
   67       // Time to read from the child process's stdout?
   68       if (sm.IsSocketReadyForRead(cpIO.GetReadSelectSocket().GetFileDescriptor()))
   69       {
   70          char inputBuf[1024];
   71          const int numBytesRead = cpIO.Read(inputBuf, sizeof(inputBuf)-1);
   72          if (numBytesRead >= 0)
   73          {
   74             inputBuf[numBytesRead] = '\0';  // ensure NUL termination 
   75             printf("Child Process sent this to me: [%s]\n", String(inputBuf).Trim()());
   76          }
   77          else 
   78          {
   79             printf("Child process has exited!\n");
   80             break;
   81          }
   82       }
   83    }
   84 
   85    printf("Program exiting.\n");
   86    return 0;
   87 }