Example #1
0
  /*
   *
   *   Date      By	Description
   * MM/DD/YYYY
   * ----------  --	-----------
   * 10/30/2003  INB	Created.
   *
   */
  public final void run() {
    try {
      if (debug) {
        System.err.println(getName() + " starting filters.");
      }
      for (int idx = 0; idx < filters.length; ++idx) {
        filters[idx].debug = debug;
        filters[idx].start();
      }

      if (debug) {
        System.err.println(getName() + " waiting for filters.");
      }
      for (int idx = 0; idx < filters.length; ++idx) {
        filters[idx].join();
      }

      if (debug) {
        System.err.println(getName() + " results:");
      }
      setStatus(0);
      for (int idx = 0; idx < filters.length; ++idx) {
        if (debug) {
          System.err.println(filters[idx].getSourceName() + " " + filters[idx].getStatus());
        }
        setStatus(Math.max(getStatus(), filters[idx].getStatus()));
      }

    } catch (Exception e) {
      if (debug) {
        e.printStackTrace();
      }
      setStatus(1);
    }
  }
  private void runWork() {
    long lastTime = System.currentTimeMillis();
    try {
      //			while(isRunning())
      //			{
      //				doOneCycle();
      //				long actualWait = (waitTime * 1000) - (System.currentTimeMillis() - lastTime);
      //				lastTime = System.currentTimeMillis();
      //				System.out.println("Actual Wait Time = " + actualWait);
      //				if (actualWait < 0) actualWait = 0;
      //				Thread.sleep(actualWait);
      //			}

      // modified to do one cycle only so that it can be
      // scripted; tew Aug 02, 2004
      if (isRunning()) doOneCycle();

    } catch (Exception se) {
      se.printStackTrace();
    }
    fetchDataThread = null;
  }
    private PlugInChannelMap handleRegistration(PlugInChannelMap picm) throws Exception {
      // 	picm.PutTime( (System.currentTimeMillis()/1000.0), 0.0);

      try {
        if (((picm.GetName(0).equals("...")) || (picm.GetName(0).equals("*")))) {

          double otime = ctreader.oldTime(sourceFolder);
          double ntime = ctreader.newTime(sourceFolder);
          if (debug)
            System.err.println(
                "handleRegistration picm[0]: "
                    + picm.GetName(0)
                    + ", oldtime: "
                    + otime
                    + ", newtime: "
                    + ntime);

          picm.PutTime(otime, ntime - otime);
          if (regPicm
              != null) { // round about way to cache registration. actual RBNB pre-register does not
            // provide time limits
            if (debug) System.err.println("returning pre-fetched registration...");
            picm.Clear();
            for (int i = 0; i < regPicm.NumberOfChannels(); i++) {
              picm.Add(regPicm.GetName(i));
              picm.PutDataAsString(i, regString(regPicm.GetName(i)));
              picm.PutMime(i, "text/xml");
            }
            return picm; // return pre-fetched registration
          }

          ArrayList<String> chanlist = ctreader.listChans(sourceFolder);
          if (chanlist == null) {
            throw (new Exception("Oops, no channels for sourceFolder: " + sourceFolder));
          }
          picm.Clear();
          for (int i = 0; i < chanlist.size(); i++) {
            int j = picm.Add(chanlist.get(i));
            if (debug) System.err.println("register[" + j + "]: " + picm.GetName(j));
            picm.PutDataAsString(i, regString(chanlist.get(i)));
            picm.PutMime(i, "text/xml");
          }
        } else {
          CTmap ctmap = PI2CTmap(picm); // ignore time limits registration request
          double otime = ctreader.oldTime(sourceFolder, ctmap);
          double ntime = ctreader.newTime(sourceFolder, ctmap);
          picm.PutTime(otime, ntime - otime);
          if (debug)
            System.err.println(
                "handleRegistration picm[0]: "
                    + picm.GetName(0)
                    + ", oldtime: "
                    + otime
                    + ", newtime: "
                    + ntime);

          int nchan = picm.NumberOfChannels();
          if (debug)
            System.err.println("reg-request channel: " + picm.GetName(0) + ", num: " + nchan);
          for (int i = 0; i < nchan; i++) { // put start/end time for each channel individually?
            picm.PutDataAsString(i, regString(picm.GetName(i)));
            picm.PutMime(i, "text/xml");
          }
        }
      } catch (Exception e) {
        System.err.println("handleRegistration exception: " + e);
        e.printStackTrace();
      }
      //	 plugin.Flush(picm);
      return (picm);
    }
    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
        }
      }
    }
  private void doOneCycle() {
    System.out.println("One Cycle:");
    //		System.out.println("Wait time = " + waitTime);
    System.out.println("Duration time = " + durationTime);

    long startTime = System.currentTimeMillis();
    double dataEndTime = 0.0;

    if (endTimeString != null)
      try {
        dataEndTime = getTimeOrDouble(endTimeString);
      } catch (Throwable ignore) {
      }

    DataVideoSink.DEBUG = true;
    //		DataVideoSink.setUglyOffest(uglyOffset);
    DataVideoSink dataVideoSink = new DataVideoSink();
    dataVideoSink.connect(getServer(), sinkName + "_data");

    double[][] data = new double[channelPathArray.length][0];
    double[][] time = new double[channelPathArray.length][0];

    // collectionTime (long) in secods
    double duration = (double) durationTime; // in seconds

    if (dataVideoSink.connect(getServer(), sinkName)) {
      for (int i = 0; i < channelPathArray.length; i++) {
        String dataSourcePath = channelPathArray[i];
        if (dataEndTime > 0.0)
          dataVideoSink.fetchData(dataSourcePath, dataEndTime - duration, duration, "absolute");
        else
          // with "newest" time is figured backward from the present...
          dataVideoSink.fetchData(dataSourcePath, 0.0, duration, "newest");
        data[i] = dataVideoSink.getDataArray();
        time[i] = dataVideoSink.getTimeArray();
      }

      //			for (int i = 0; i < channelPathArray.length; i ++)
      //			{
      //				System.out.println("Array lengths (" + i + ") are: "
      //					+ data[i].length + "," + time[i].length);
      //			}

      double[] referenceTime = time[0];
      double[][] filledData = new double[channelPathArray.length][referenceTime.length];
      int[] indexFront = new int[channelPathArray.length]; // initally zero
      int[] lastUsed = new int[channelPathArray.length];

      // force initial condition; no index last used
      for (int i = 0; i < lastUsed.length; i++) lastUsed[i] = -1;

      // for each possible data slot determine the data with the correct time
      // filled in slots are eather duplications or zero
      for (int timeIndex = 0; timeIndex < referenceTime.length; timeIndex++) {
        // channel 0 is the referance channel, it's data value are always there
        filledData[0][timeIndex] = data[0][timeIndex];
        for (int channelIndex = 1; channelIndex < channelPathArray.length; channelIndex++) {
          // System.out.println(" " + timeIndex + "," + channelIndex
          //	+ "," + lastUsed[channelIndex] + "," + indexFront[channelIndex]);

          if (lastUsed[channelIndex] == indexFront[channelIndex]) {
            // filling data with and index that was already used
            if (repeatMissingValue)
              filledData[channelIndex][timeIndex] = data[channelIndex][indexFront[channelIndex]];
            else filledData[channelIndex][timeIndex] = 0.0;
          } else {
            filledData[channelIndex][timeIndex] = data[channelIndex][indexFront[channelIndex]];
            lastUsed[channelIndex] = indexFront[channelIndex];
          }
          // System.out.print(" " + timeIndex + "," + channelIndex
          //	+ "," + referenceTime[timeIndex] + "," + time[channelIndex][indexFront[channelIndex]]);
          // if (referenceTime[timeIndex] >= time[channelIndex][indexFront[channelIndex]])
          // System.out.println(" yes");
          // else
          // System.out.println(" no");
          if (referenceTime[timeIndex] >= time[channelIndex][indexFront[channelIndex]]) {
            if (indexFront[channelIndex] < (data[channelIndex].length - 1))
              ++indexFront[channelIndex];
          }
        }

        //				System.out.println("Time Index = " + timeIndex);
        //				System.out.println("Filled data: ");
        //				for (int i = 0; i < channelPathArray.length; i++)
        //				{
        //					System.out.print("  " + filledData[i][timeIndex]);
        //				}
        //				System.out.println();
        //				System.out.println("Last Used and Front are: ");
        //				for (int i = 0; i < channelPathArray.length; i++)
        //				{
        //					System.out.println("  " + i + ": " + lastUsed[i] + "," + indexFront[i]);
        //				}

      }

      try {
        writeDataToFile(shortNameArray, referenceTime, filledData);
      } catch (IOException e) {
        e.printStackTrace();
      }

      if ((videoChannelPath != null) && (!videoChannelPath.equals(""))) {
        if (!probeVideo()) System.out.println("No matching video available ");
        else {
          String outputURL = "file:" + archiveDirectory + "/" + movieFileName;
          try {
            dataVideoSink.makeMovie(userGivenFrameRate, outputURL, videoChannelPath, itemsToSkip);
          } catch (Exception e1) {
            e1.printStackTrace();
          }
        }
      }
    }
    dataVideoSink.disconnect();
    long endTime = System.currentTimeMillis();
    long elaspedTime = endTime - startTime;
    System.out.println("Cycle took " + elaspedTime + " milliseconds.");
  }