Feature request, empty constructor in c++

Discussion in 'Module Connector (MC)' started by Øystein Bjørndal, Sep 13, 2017.

  1. Øystein Bjørndal

    Øystein Bjørndal New Member

    Initialising the ModuleConnector frequently fails for me, so I wanted to write something to catch
    the frequent
    Code:
    libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: unable to open file: /dev/cu.usbmodemFD121
    that I keep getting (on my mac). This is a "temporary" error and simply re-starting the program seems to work.

    The following does however fail to compile, since ModuleConnector does not supply an empty constructor.
    Code:
       
        using namespace XeThru;
        // Create and setup ModuleConnector object
        const ModuleConnector mc;    // fails, as there is no empty constructor for ModuleConnector
        try {
          mc = ModuleConnector(device_name, 3);
        } catch (const std::runtime_error e) {
          std::cout << "Failed to connect to device name, trying again" << std::endl;
          usleep(1000);
          mc = ModuleConnector(device_name, 3);
        }
       
    Is there a better way to solve this?
    Could you add an empty ModuleConnector constructor?
    Or better, make the one that exist a bit more robust?
     
  2. Christian Rødli Amble

    Christian Rødli Amble New Member Staff Member

    This is an oversight and should have been provided. As a workaround you can give it an empty device name to skip connecting and then use the open() method.
     
  3. Øystein Bjørndal

    Øystein Bjørndal New Member

    Cool, I looked for that option in the documentation, another undocumented gem!

    For future reference, heres my hack for opening a connection:
    Code:
        using namespace XeThru;
    
        // Create and setup ModuleConnector object
        ModuleConnector mc("", 3);
        int opened = 1;
        int max_retries = 10;
        int retry_count = 0;
        while (opened != 0) {
          try {
            opened = mc.open(device_name); // note: return value not documented, lets hope its 0 on success.
          } catch (const std::runtime_error e) {
            if (retry_count > max_retries) {
              throw e;
            } else {
              std::cerr << "Failed to connect to device name, trying again" << std::endl;
              usleep(1000*retry_count); // linear backoff
          }
          retry_count += 1;
          }
        }
    Seems to almost consistently fail at least once.