// ---------------------------------------------------------------------------------
  public static void main(String[] args) {

    boolean printHelp = false;
    String rootFolder = "CTdata";
    int dirArg = 0;

    while ((dirArg < args.length) && args[dirArg].startsWith("-")) { // clugey arg parsing
      if (args[dirArg].equals("-r")) swapFlag = true;
      if (args[dirArg].equals("-h")) printHelp = true;
      if (args[dirArg].equals("-x")) debug = true;
      if (args[dirArg].equals("-p")) preRegister = true;
      if (args[dirArg].equals("-f")) rootFolder = args[++dirArg];
      if (args[dirArg].equals("-a")) rbnbServer = args[++dirArg];
      if (args[dirArg].equals("-t")) typeDefault = args[++dirArg].charAt(0);
      dirArg++;
    }

    if (args.length > dirArg) rootFolder = args[dirArg++];

    if (printHelp) {
      System.out.println(
          "CTplugin"
              + "\n -a<host>        \tdefault="
              + rbnbServer
              + "\n -t<typeDefault> \tdefault="
              + typeDefault
              + "\n -r(rvsbytes)	\tdefault=false"
              + "\n -x(debug)       \tdefault=false"
              + "\n -p(preRegister) \tdefault=false"
              + "\n -h(help)        \tdefault=false"
              + "\n <folder>      	\tdefault="
              + rootFolder);
      System.exit(0);
    }

    System.err.println("CTplugin, rootFolder: " + rootFolder + "...");
    // 	System.err.println("default fileType: "+typeDefault);
    new CTplugin(rootFolder); // start the plugin check

    try { // hang out
      while (true) Thread.sleep(1000);
    } catch (Exception e) {
      System.err.println("CTplugin exception (exiting): " + e);
    }
  }
    public void run() {
      if (debug) System.err.println("PIrun, sourceFolder: " + sourceFolder + ", sName: " + sName);

      PlugInChannelMap picm = new PlugInChannelMap();
      plugin = new PlugIn();

      try {
        plugin.OpenRBNBConnection(rbnbServer, sName);
        sink = new Sink();
        sink.OpenRBNBConnection(rbnbServer, sinkName);
      } catch (Exception e) {
        System.err.println("Error on connect: " + e);
        System.exit(0);
        //		    RBNBProcess.exit(0);
      }

      if (preRegister) {
        try {
          System.err.println("pre-registering channels for source: " + sName);
          picm.Add("...");
          regPicm = handleRegistration(picm);
          //					plugin.Register(picm);			// pre-register
          System.err.println("pre-register done: " + sName);
        } catch (Exception se) {
          System.err.println("Oops, exception on preRegister: " + se);
        }
      }

      // process is to wait for request, get data from sink, convert data, send response, repeat
      while (true) {
        try {
          if (debug) System.err.println("waiting on fetch...");
          picm = plugin.Fetch(-1); // block until request arrives
          if (debug) System.err.println("request picm: " + picm);

          if (picm.NumberOfChannels() == 0) {
            System.err.println("oops, no channels in request");
            continue;
          }

          if (picm.GetRequestReference().equals("registration")) {
            if (debug) System.err.println("registration request!");
            plugin.Flush(handleRegistration(picm));
            continue;
          } else {
            double tget = picm.GetRequestStart();
            double tdur = picm.GetRequestDuration();
            String tmode = picm.GetRequestReference();
            CTmap ctmap = PI2CTmap(picm);
            ctmap = ctreader.getDataMap(ctmap, sourceFolder, tget, tdur, tmode);
            picm = CT2PImap(picm, ctmap, tget, tdur, tmode);
            if (debug) System.err.println("Flush picm: " + picm + ", nframe: " + ctmap.size());
            if (picm == null) System.err.println("no channels!");
            else plugin.Flush(picm);
          }
        } catch (Exception e) {
          System.err.println("oops, exception: " + e + ", picm: " + picm);
          e.printStackTrace();
          try {
            Thread.sleep(1000);
            picm.PutDataAsString(0, "error: " + e);
            plugin.Flush(picm);
          } catch (Exception ee) {
          }
          ; // no busy loop
          //				System.exit(0);		// no infinite loops
        }
      }
    }