Ejemplo n.º 1
0
 public void peerHasPiece(PEPeer peer) {
   try {
     class_mon.enter();
     // System.out.println("piece: #"+pieceNumber+" , old level:"+level+",
     // timeFirstDistributed:"+timeFirstDistributed+ "/
     // timetoreachanotherpeer:"+timeToReachAnotherPeer);
     if (level < 2) {
       // first time that somebody tells us this piece exists elsewhere
       // if peer=null, it is due to bitfield scan, so firstreceiver gets NULL
       firstReceiver = peer;
       timeFirstDistributed = SystemTime.getCurrentTime();
       // numberOfPeersWhenFirstReceived = manager.getNbPeers();
       level = 2;
     } else {
       // level=2 or 3 and we arrive here when somebody has got the piece, either BT_HAVE or
       // bitfield scan
       // if we are here due to bitfield scan, 'peer' = null --> do nothing
       // if level=2 --> mark piece redistributed, set speedstatus of firstreceiver, bump level to
       // 3
       // if level=3 --> this piece has been already seen at 3rd party earlier, do nothing
       if (peer != null && firstReceiver != null && level == 2) {
         timeToReachAnotherPeer = (int) (SystemTime.getCurrentTime() - timeFirstDistributed);
         firstReceiver.setUploadHint(timeToReachAnotherPeer);
         level = 3;
       }
     }
   } finally {
     class_mon.exit();
   }
 }
  /**
   * Perform the actual version check by connecting to the version server.
   *
   * @param data_to_send version message
   * @return version reply
   * @throws Exception if the server check connection fails
   */
  private Map performVersionCheck(
      Map data_to_send, boolean use_az_message, boolean use_http, boolean v6) throws Exception {
    Exception error = null;
    Map reply = null;

    if (use_http) {

      try {
        reply = executeHTTP(data_to_send, v6);

        reply.put("protocol_used", "HTTP");

        error = null;
      } catch (IOException e) {
        error = e;
      } catch (Exception e) {
        Debug.printStackTrace(e);
        error = e;
      }
    }

    if (reply == null && use_az_message) {

      try {
        reply = executeAZMessage(data_to_send, v6);

        reply.put("protocol_used", "AZMSG");
      } catch (IOException e) {
        error = e;
      } catch (Exception e) {
        Debug.printStackTrace(e);
        error = e;
      }
    }
    if (error != null) {

      throw (error);
    }

    if (Logger.isEnabled())
      Logger.log(
          new LogEvent(
              LOGID,
              "VersionCheckClient server "
                  + "version check successful. Received "
                  + reply.size()
                  + " reply keys."));

    if (v6) {

      last_check_time_v6 = SystemTime.getCurrentTime();

    } else {

      last_check_time_v4 = SystemTime.getCurrentTime();
    }

    return reply;
  }
Ejemplo n.º 3
0
 /**
  * Test method for {@link
  * org.openiot.gsn.wrappers.AbstractWrapper#sendToWrapper(java.lang.Object)}. Test to see what is
  * the behavior if the wrapper is disabled.
  *
  * @throws OperationNotSupportedException
  * @throws SQLException
  */
 @Test(expected = GSNRuntimeException.class)
 public void testSendToWrapper2() throws OperationNotSupportedException, SQLException {
   SystemTime systemTimeWrapper = new SystemTime();
   systemTimeWrapper.setActiveAddressBean(new AddressBean("system-time"));
   assertTrue(systemTimeWrapper.initialize());
   Thread thread = new Thread(systemTimeWrapper);
   thread.start();
   systemTimeWrapper.releaseResources();
   systemTimeWrapper.sendToWrapper("bla");
 }
Ejemplo n.º 4
0
  public static boolean hasFullLicence() {
    if (featman == null) {
      // Debug.out("featman null");
      Set<String> featuresInstalled = UtilitiesImpl.getFeaturesInstalled();
      return featuresInstalled.contains("dvdburn");
    }

    boolean full = false;
    FeatureDetails[] featureDetails = featman.getFeatureDetails("dvdburn");
    // if any of the feature details are still valid, we have a full
    for (FeatureDetails fd : featureDetails) {
      int state = fd.getLicence().getState();
      if (state == Licence.LS_CANCELLED || state == Licence.LS_REVOKED) {
        continue;
      }
      long now = SystemTime.getCurrentTime();
      Long lValidUntil = (Long) fd.getProperty(FeatureDetails.PR_VALID_UNTIL);
      if (lValidUntil != null && lValidUntil.longValue() >= now) {
        full = true;
        break;
      }
      Long lValidOfflineUntil = (Long) fd.getProperty(FeatureDetails.PR_OFFLINE_VALID_UNTIL);
      if (lValidOfflineUntil != null && lValidOfflineUntil.longValue() >= now) {
        full = true;
        break;
      }
    }

    return full;
  }
Ejemplo n.º 5
0
  /**
   * Prints out the given debug message to System.out, prefixed by the calling class name, method
   * and line number, appending the stacktrace of the given exception.
   */
  public static void out(final String _debug_msg, final Throwable _exception) {
    String header = "DEBUG::";
    header = header + new Date(SystemTime.getCurrentTime()).toString() + "::";
    String className;
    String methodName;
    int lineNumber;
    String trace_trace_tail = null;

    try {
      throw new Exception();
    } catch (Exception e) {
      StackTraceElement[] st = e.getStackTrace();

      StackTraceElement first_line = st[2];
      className = first_line.getClassName() + "::";
      methodName = first_line.getMethodName() + "::";
      lineNumber = first_line.getLineNumber();

      trace_trace_tail = getCompressedStackTrace(e, 3, 200, false);
    }

    diagLoggerLogAndOut(header + className + (methodName) + lineNumber + ":", true);
    if (_debug_msg.length() > 0) {
      diagLoggerLogAndOut("  " + _debug_msg, true);
    }
    if (trace_trace_tail != null) {
      diagLoggerLogAndOut("    " + trace_trace_tail, true);
    }
    if (_exception != null) {
      diagLoggerLogAndOut(_exception);
    }
  }
  protected boolean checkConnectionId(String client_address, long id) {
    try {
      random_mon.enter();

      Long key = new Long(id);

      connectionData data = (connectionData) connection_id_map.get(key);

      if (data == null) {

        // System.out.println( "TRTrackerServerProcessorUDP: rejected:" + id + ", data not found" );

        return (false);

      } else {

        if (SystemTime.getMonotonousTime() - data.getTime() > CONNECTION_ID_LIFETIME) {

          return (false);
        }
      }

      boolean ok = data.getAddress().equals(client_address);

      // System.out.println( "TRTrackerServerProcessorUDP: tested:" + id + "/" + client_address + "
      // -> " + ok );

      return (ok);

    } finally {

      random_mon.exit();
    }
  }
Ejemplo n.º 7
0
 private void loadCache() throws SQLException, IOException {
   Connection connection = null;
   PreparedStatement query = null;
   PreparedStatement insert = null;
   ResultSet rs = null;
   try {
     connection = dataSource.getConnection();
     query = connection.prepareStatement(SQL_GET_SEQNUMS);
     setSessionIdParameters(query, 1);
     rs = query.executeQuery();
     if (rs.next()) {
       cache.setCreationTime(SystemTime.getUtcCalendar(rs.getTimestamp(1)));
       cache.setNextTargetMsgSeqNum(rs.getInt(2));
       cache.setNextSenderMsgSeqNum(rs.getInt(3));
     } else {
       insert = connection.prepareStatement(SQL_INSERT_SESSION);
       int offset = setSessionIdParameters(insert, 1);
       insert.setTimestamp(offset++, new Timestamp(cache.getCreationTime().getTime()));
       insert.setInt(offset++, cache.getNextTargetMsgSeqNum());
       insert.setInt(offset, cache.getNextSenderMsgSeqNum());
       insert.execute();
     }
   } finally {
     JdbcUtil.close(sessionID, rs);
     JdbcUtil.close(sessionID, query);
     JdbcUtil.close(sessionID, insert);
     JdbcUtil.close(sessionID, connection);
   }
 }
Ejemplo n.º 8
0
  protected void fileDirty(TranscodeFileImpl file, int type, Object data) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();

        } else {

          device_files_last_mod = SystemTime.getMonotonousTime();
        }
      }

      device_files_dirty = true;

    } catch (Throwable e) {

      Debug.out("Failed to load device file", e);
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileChanged(file, type, data);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }
  }
Ejemplo n.º 9
0
  /**
   * Initializes the state of this node system. This includes generating and connecting the nodes in
   * the system.
   */
  public void initialize() {
    // Determine the number of nodes in the network
    int numNodes = rand.nextInt(MAX_NUM_NODES + 1);
    while (numNodes < MIN_NUM_NODES) numNodes = rand.nextInt(MAX_NUM_NODES + 1);

    System.out.println("Number of nodes: " + numNodes);

    // Create the nodes
    for (int i = 0; i < numNodes; i++) {
      Node node = new Node(new EmptyStrategy(), SystemTime.getTime());

      // TODO
      // Determine this node's peer table size
      node.setPeerTableSize(10);
      nodes.add(node);
    }

    // Randomly connect nodes
    for (Node node : nodes) {
      for (int index = 0; index < node.getPeerTableSize(); index++) {
        // Find a random node not in this node's peer table
        int nextNeighbor = rand.nextInt(nodes.size());

        // Peer should not know the neighbor and the neighbor should
        // be this peer
        while (node.knowsPeer(nextNeighbor) || nextNeighbor == node.getID()) {
          nextNeighbor = rand.nextInt(nodes.size());
        }

        node.setPeer(index, nodes.get(nextNeighbor));
      }
    }
  }
  protected void writeStats() {
    synchronized (this) {
      if (!config_enabled) {

        return;
      }

      int period = config_period;

      long now = SystemTime.getMonotonousTime() / 1000;

      // if we have a 1 second period then now-last-write_time will often be 0 (due to the
      // rounding of SystemTime) and the stats won't be written - hence the check against
      // (period-1). Its only

      if (now - last_write_time < (period - 1)) {

        return;
      }

      last_write_time = now;

      try {
        String dir = config_dir;

        dir = dir.trim();

        if (dir.length() == 0) {

          dir = File.separator;
        }

        String file_name = dir;

        if (!file_name.endsWith(File.separator)) {

          file_name = file_name + File.separator;
        }

        String file = config_file;

        if (file.trim().length() == 0) {

          file = DEFAULT_STATS_FILE_NAME;
        }

        file_name += file;

        if (Logger.isEnabled())
          Logger.log(new LogEvent(LOGID, "Stats Logged to '" + file_name + "'"));

        new StatsWriterImpl(core).write(file_name);

      } catch (Throwable e) {
        Logger.log(new LogEvent(LOGID, "Stats Logging fails", e));
      }
    }
  }
Ejemplo n.º 11
0
  @Test
  public void testRemovingUselessData() throws SQLException, InterruptedException {
    SystemTime wrapper = new SystemTime();
    sm.executeCreateTable(wrapper.getDBAliasInStr(), new DataField[] {}, true);
    wrapper.setActiveAddressBean(
        new AddressBean("system-time", new KeyValueImp(SystemTime.CLOCK_PERIOD_KEY, "100")));
    assertTrue(wrapper.initialize());
    Thread thread = new Thread(wrapper);
    InputStream is = new InputStream();
    StreamSource ss =
        new StreamSource()
            .setAlias("my-stream")
            .setAddressing(new AddressBean[] {new AddressBean("system-time")})
            .setSqlQuery("select * from wrapper where TIMED <0")
            .setRawHistorySize("2")
            .setInputStream(is);
    ss.setSamplingRate(1);
    ss.setWrapper(wrapper);
    assertTrue(ss.validate());
    assertEquals(wrapper.getTimerClockPeriod(), 100);
    thread.start();
    Thread.sleep(1000);

    ResultSet rs =
        sm.executeQueryWithResultSet(
            new StringBuilder("select count(*) from ").append(wrapper.getDBAliasInStr()),
            sm.getConnection());
    assertTrue(rs.next());
    //    System.out.println(rs.getInt(1));
    assertTrue(
        rs.getInt(1) <= (AbstractWrapper.GARBAGE_COLLECT_AFTER_SPECIFIED_NO_OF_ELEMENTS * 2));
    wrapper.releaseResources();
  }
Ejemplo n.º 12
0
  public void alive() {
    last_seen = SystemTime.getCurrentTime();

    if (!online) {

      online = true;

      setDirty(false);
    }
  }
Ejemplo n.º 13
0
  public static void openStreamPlusWindow(final String referal) {
    String msgidPrefix;
    String buttonID;
    long plusExpiryTimeStamp = FeatureManagerUI.getPlusExpiryTimeStamp();
    if (plusExpiryTimeStamp < 0 || plusExpiryTimeStamp >= SystemTime.getCurrentTime()) {
      msgidPrefix = "dlg.stream.plus.";
      buttonID = "Button.upgrade";
    } else {
      buttonID = "Button.renew";
      msgidPrefix = "dlg.stream.plus.renew.";
      if (!MessageText.keyExistsForDefaultLocale(msgidPrefix + "text")) {
        msgidPrefix = "dlg.stream.plus.";
      }
    }
    final String f_msgidPrefix = msgidPrefix;
    final VuzeMessageBox box =
        new VuzeMessageBox(
            MessageText.getString(msgidPrefix + "title"),
            MessageText.getString(msgidPrefix + "text"),
            new String[] {
              MessageText.getString(buttonID), MessageText.getString("Button.cancel"),
            },
            0);
    box.setButtonVals(new Integer[] {BUTTON_UPGRADE, SWT.CANCEL});

    box.setSubTitle(MessageText.getString(msgidPrefix + "subtitle"));
    box.addResourceBundle(
        FeatureManagerUI.class, SkinPropertiesImpl.PATH_SKIN_DEFS, "skin3_dlg_streamplus");
    box.setIconResource("image.header.streamplus");

    box.setListener(
        new VuzeMessageBoxListener() {
          public void shellReady(Shell shell, SWTSkinObjectContainer soExtra) {
            SWTSkin skin = soExtra.getSkin();
            skin.createSkinObject("dlg.stream.plus", "dlg.stream.plus", soExtra);
            SWTSkinObject soSubText = skin.getSkinObject("trial-info", soExtra);
            if (soSubText instanceof SWTSkinObjectText) {
              ((SWTSkinObjectText) soSubText).setTextID(f_msgidPrefix + "subtext");
            }
          }
        });

    box.open(
        new UserPrompterResultListener() {
          public void prompterClosed(int result) {
            if (result == BUTTON_UPGRADE) {
              SBC_PlusFTUX.setSourceRef("dlg-stream" + (referal == null ? "" : "-" + referal));

              MultipleDocumentInterface mdi = UIFunctionsManager.getUIFunctions().getMDI();
              mdi.showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_PLUS);
            }
          }
        });
  }
Ejemplo n.º 14
0
 private void log(String message, String type) {
   out.println(
       "<"
           + UtcTimestampConverter.convert(SystemTime.getDate(), includeMillis)
           + ", "
           + sessionID
           + ", "
           + type
           + "> ("
           + message
           + ")");
 }
Ejemplo n.º 15
0
  public static void printStackTrace(Throwable e, Object context) {
    String header = "DEBUG::";
    header = header + new Date(SystemTime.getCurrentTime()).toString() + "::";
    String className = "?::";
    String methodName = "?::";
    int lineNumber = -1;

    try {
      throw new Exception();
    } catch (Exception f) {
      StackTraceElement[] st = f.getStackTrace();

      for (int i = 1; i < st.length; i++) {
        StackTraceElement first_line = st[i];
        className = first_line.getClassName() + "::";
        methodName = first_line.getMethodName() + "::";
        lineNumber = first_line.getLineNumber();

        // skip stuff generated by the logger

        if (className.indexOf(".logging.") != -1 || className.endsWith(".Debug::")) {

          continue;
        }

        break;
      }
    }

    diagLoggerLogAndOut(header + className + (methodName) + lineNumber + ":", true);

    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos));

      if (context != null) {
        pw.print("  ");
        pw.println(context);
      }
      pw.print("  ");
      e.printStackTrace(pw);

      pw.close();

      String stack = baos.toString();

      diagLoggerLogAndOut(stack, true);
    } catch (Throwable ignore) {

      e.printStackTrace();
    }
  }
Ejemplo n.º 16
0
 public static boolean hasFullLicence() {
   if (featman == null) {
     // Debug.out("featman null");
     Set<String> featuresInstalled = UtilitiesImpl.getFeaturesInstalled();
     return featuresInstalled.contains("dvdburn");
   }
   licenceDetails fullFeatureDetails = getFullFeatureDetails();
   long now = SystemTime.getCurrentTime();
   return fullFeatureDetails != null
       && fullFeatureDetails.expiry > now
       && fullFeatureDetails.displayedExpiry > now;
 }
Ejemplo n.º 17
0
  public static licenceDetails getFullFeatureDetails() {
    if (featman == null) {
      Debug.out("featman null");
      return null;
    }

    FeatureDetails[] featureDetails = featman.getFeatureDetails("dvdburn");
    // if any of the feature details are still valid, we have a full
    for (FeatureDetails fd : featureDetails) {
      long now = SystemTime.getCurrentTime();
      Long lValidUntil = (Long) fd.getProperty(FeatureDetails.PR_VALID_UNTIL);
      if (lValidUntil != null && lValidUntil.longValue() >= now) {
        return new licenceDetails(
            lValidUntil.longValue(), fd.getLicence().getKey(), fd.getLicence().getState());
      }
      Long lValidOfflineUntil = (Long) fd.getProperty(FeatureDetails.PR_OFFLINE_VALID_UNTIL);
      if (lValidOfflineUntil != null && lValidOfflineUntil.longValue() >= now) {
        return new licenceDetails(
            lValidOfflineUntil.longValue(), fd.getLicence().getKey(), fd.getLicence().getState());
      }
    }

    Licence bestLicence = null;
    Licence[] licences = featman.getLicences();
    for (Licence licence : licences) {
      FeatureDetails[] details = licence.getFeatures();
      boolean isTrial = false;
      for (FeatureDetails fd : details) {
        Object property = fd.getProperty(FeatureDetails.PR_IS_TRIAL);
        if ((property instanceof Number) && ((Number) property).intValue() == 1) {
          isTrial = true;
          break;
        }
      }
      if (isTrial) {
        continue;
      }
      int state = licence.getState();
      if (state == Licence.LS_AUTHENTICATED) {
        bestLicence = licence;
        break;
      } else {
        bestLicence = licence;
      }
    }

    if (bestLicence != null) {
      return new licenceDetails(0, bestLicence.getKey(), bestLicence.getState());
    }

    return null;
  }
Ejemplo n.º 18
0
 public void updateTime() {
   if (level < 2)
     // not yet distributed, no effect on speed status
     return;
   if (timeToReachAnotherPeer > 0)
     // piece has been seen elsewhere, no effect on speed status any more
     return;
   if (firstReceiver == null)
     // piece was found due to bitfield scan, no idea how it got distributed
     return;
   int timeToSend = (int) (SystemTime.getCurrentTime() - timeFirstDistributed);
   if (timeToSend > firstReceiver.getUploadHint()) firstReceiver.setUploadHint(timeToSend);
 }
Ejemplo n.º 19
0
  protected void loadDeviceFile() throws IOException {

    device_files_last_mod = SystemTime.getMonotonousTime();

    if (device_files_ref != null) {

      device_files = device_files_ref.get();
    }

    if (device_files == null) {

      Map map = FileUtil.readResilientFile(getDeviceFile());

      device_files = (Map<String, Map<String, ?>>) map.get("files");

      if (device_files == null) {

        device_files = new HashMap<String, Map<String, ?>>();
      }

      device_files_ref = new WeakReference<Map<String, Map<String, ?>>>(device_files);

      log("Loaded device file for " + getName() + ": files=" + device_files.size());
    }

    final int GC_TIME = 15000;

    new DelayedEvent(
        "Device:gc",
        GC_TIME,
        new AERunnable() {
          public void runSupport() {
            synchronized (DeviceImpl.this) {
              if (SystemTime.getMonotonousTime() - device_files_last_mod >= GC_TIME) {

                if (device_files_dirty) {

                  saveDeviceFile();
                }

                device_files = null;

              } else {

                new DelayedEvent("Device:gc2", GC_TIME, this);
              }
            }
          }
        });
  }
Ejemplo n.º 20
0
  public static String appendFeatureManagerURLParams(String url) {
    long remainingUses = FeatureManagerUI.getRemaining();
    long plusExpiryTimeStamp = FeatureManagerUI.getPlusExpiryDisplayTimeStamp();
    String plusRenewalCode = FeatureManagerUI.getPlusRenewalCode();

    String newURL = url + (url.contains("?") ? "&" : "?");
    newURL += "mode=" + FeatureManagerUI.getMode();
    if (plusExpiryTimeStamp != 0) {
      newURL += "&remaining_plus=" + (plusExpiryTimeStamp - SystemTime.getCurrentTime());
    }
    newURL += "&remaining=" + remainingUses;
    if (plusRenewalCode != null) {
      newURL += "&renewal_code=" + plusRenewalCode;
    }

    return newURL;
  }
 protected void accept_loop() {
   while (isRunning()) {
     try {
       SocketChannel client_channel = server_channel.accept();
       last_accept_time = SystemTime.getCurrentTime();
       client_channel.configureBlocking(false);
       listener.newConnectionAccepted(server_channel, client_channel);
     } catch (AsynchronousCloseException e) {
       /* is thrown when stop() is called */
     } catch (Throwable t) {
       Debug.out(t);
       try {
         Thread.sleep(500);
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
 }
  /** Start the server and begin accepting incoming connections. */
  public void start() {
    try {
      this_mon.enter();

      if (!isRunning()) {
        try {
          server_channel = ServerSocketChannel.open();

          server_channel.socket().setReuseAddress(true);
          if (receive_buffer_size > 0)
            server_channel.socket().setReceiveBufferSize(receive_buffer_size);

          server_channel.socket().bind(bind_address, 1024);

          if (Logger.isEnabled())
            Logger.log(new LogEvent(LOGID, "TCP incoming server socket " + bind_address));

          AEThread accept_thread =
              new AEThread("VServerSelector:port" + bind_address.getPort()) {
                public void runSupport() {
                  accept_loop();
                }
              };
          accept_thread.setDaemon(true);
          accept_thread.start();
        } catch (Throwable t) {
          Debug.out(t);
          Logger.log(
              new LogAlert(
                  LogAlert.UNREPEATABLE,
                  "ERROR, unable to bind TCP incoming server socket to " + bind_address.getPort(),
                  t));
        }

        last_accept_time = SystemTime.getCurrentTime(); // init to now
      }
    } finally {

      this_mon.exit();
    }
  }
Ejemplo n.º 23
0
  public long getFeatureFlags() {
    long now = SystemTime.getCurrentTime();

    if (now > last_feature_flag_cache_time && now - last_feature_flag_cache_time < 60000) {

      return (last_feature_flag_cache);
    }

    Map m = getMostRecentVersionCheckData();

    long result;

    if (m == null) {

      result = 0;

    } else {

      byte[] b_feat_flags = (byte[]) m.get("feat_flags");

      if (b_feat_flags != null) {

        try {

          result = Long.parseLong(new String((byte[]) b_feat_flags));

        } catch (Throwable e) {

          result = 0;
        }
      } else {

        result = 0;
      }
    }

    last_feature_flag_cache = result;
    last_feature_flag_cache_time = now;

    return (result);
  }
Ejemplo n.º 24
0
    public void run() {
      while (true) {

        synchronized (currentLock) {
          try {
            target.run();

          } catch (Throwable e) {

            DebugLight.printStackTrace(e);

          } finally {

            target = null;

            debug = null;

            currentLock.released = true;

            currentLock.notifyAll();
          }
        }

        if (isInterrupted() || !Thread.currentThread().isDaemon()) {

          break;

        } else {

          synchronized (daemon_threads) {
            last_active_time = SystemTime.getCurrentTime();

            if (last_active_time < last_timeout_check
                || last_active_time - last_timeout_check > THREAD_TIMEOUT_CHECK_PERIOD) {

              last_timeout_check = last_active_time;

              while (daemon_threads.size() > 0 && daemon_threads.size() > MIN_RETAINED) {

                threadWrapper thread = (threadWrapper) daemon_threads.getFirst();

                long thread_time = thread.last_active_time;

                if (last_active_time < thread_time
                    || last_active_time - thread_time > THREAD_TIMEOUT) {

                  daemon_threads.removeFirst();

                  thread.retire();

                } else {

                  break;
                }
              }
            }

            if (daemon_threads.size() >= MAX_RETAINED) {

              return;
            }

            daemon_threads.addLast(this);

            setName("AEThread2:parked[" + daemon_threads.size() + "]");

            // System.out.println( "AEThread2: queue=" + daemon_threads.size() + ",creates=" +
            // total_creates + ",starts=" + total_starts );
          }

          sem.reserve();

          if (target == null) {

            break;
          }
        }
      }
    }
Ejemplo n.º 25
0
 protected void setDebug(Object d) {
   debug = new Object[] {d, SystemTime.getMonotonousTime()};
 }
  public long[] getTotalUsageInPeriod(int period_type) {
    if (start_of_week == -1) {

      COConfigurationManager.addAndFireParameterListeners(
          new String[] {"long.term.stats.weekstart", "long.term.stats.monthstart"},
          new ParameterListener() {
            public void parameterChanged(String name) {
              start_of_week = COConfigurationManager.getIntParameter("long.term.stats.weekstart");
              start_of_month = COConfigurationManager.getIntParameter("long.term.stats.monthstart");
            }
          });
    }

    Calendar calendar = new GregorianCalendar();

    calendar.setTimeInMillis(SystemTime.getCurrentTime());

    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 0);

    long top_time = calendar.getTimeInMillis() + DAY_IN_MILLIS - 1;

    if (period_type == PT_CURRENT_DAY) {

    } else if (period_type == PT_CURRENT_WEEK) {

      // sun = 1, mon = 2 etc

      int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);

      if (day_of_week == start_of_week) {

      } else if (day_of_week > start_of_week) {

        calendar.add(Calendar.DAY_OF_WEEK, -(day_of_week - start_of_week));

      } else {

        calendar.add(Calendar.DAY_OF_WEEK, -(7 - (start_of_week - day_of_week)));
      }

    } else {

      if (start_of_month == 1) {

        calendar.set(Calendar.DAY_OF_MONTH, 1);

      } else {

        int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);

        if (day_of_month == start_of_month) {

        } else if (day_of_month > start_of_month) {

          calendar.set(Calendar.DAY_OF_MONTH, start_of_month);

        } else {

          calendar.add(Calendar.MONTH, -1);

          calendar.set(Calendar.DAY_OF_MONTH, start_of_month);
        }
      }
    }

    long bottom_time = calendar.getTimeInMillis();

    return (getTotalUsageInPeriod(new Date(bottom_time), new Date(top_time)));
  }
  public long[] getTotalUsageInPeriod(Date start_date, Date end_date) {
    synchronized (this) {
      long[] result = new long[STAT_ENTRY_COUNT];

      long start_millis = start_date.getTime();
      long end_millis = end_date.getTime();

      long now = SystemTime.getCurrentTime();

      long now_day = (now / DAY_IN_MILLIS) * DAY_IN_MILLIS;

      if (end_millis > now) {

        end_millis = now;
      }

      long start_day = (start_millis / DAY_IN_MILLIS) * DAY_IN_MILLIS;
      long end_day = (end_millis / DAY_IN_MILLIS) * DAY_IN_MILLIS;

      if (start_day > end_day) {

        return (result);
      }

      long start_offset = start_millis - start_day;

      start_offset = start_offset / MIN_IN_MILLIS;

      boolean offset_cachable = start_offset % 60 == 0;

      System.out.println(
          "start="
              + debug_utc_format.format(start_date)
              + ", end="
              + debug_utc_format.format(end_date)
              + ", offset="
              + start_offset);

      MonthCache month_cache = null;

      for (long this_day = start_day; this_day <= end_day; this_day += DAY_IN_MILLIS) {

        String[] bits = utc_date_format.format(new Date(this_day)).split(",");

        String year_str = bits[0];
        String month_str = bits[1];
        String day_str = bits[2];

        int year = Integer.parseInt(year_str);
        int month = Integer.parseInt(month_str);
        int day = Integer.parseInt(day_str);

        if (month_cache == null || !month_cache.isForMonth(year_str, month_str)) {

          if (month_cache != null && month_cache.isDirty()) {

            month_cache.save();
          }

          month_cache = getMonthCache(year_str, month_str);
        }

        boolean can_cache =
            this_day != now_day
                && (this_day > start_day || (this_day == start_day && offset_cachable))
                && this_day < end_day;

        long cache_offset = this_day == start_day ? start_offset : 0;

        if (can_cache) {

          long[] cached_totals = month_cache.getTotals(day, cache_offset);

          if (cached_totals != null) {

            for (int i = 0; i < cached_totals.length; i++) {

              result[i] += cached_totals[i];
            }

            continue;
          }
        } else {

          if (this_day == now_day) {

            if (day_cache != null) {

              if (day_cache.isForDay(year_str, month_str, day_str)) {

                long[] cached_totals = day_cache.getTotals(cache_offset);

                if (cached_totals != null) {

                  for (int i = 0; i < cached_totals.length; i++) {

                    result[i] += cached_totals[i];
                  }

                  continue;
                }

              } else {

                day_cache = null;
              }
            }
          }
        }

        String current_rel_file =
            bits[0] + File.separator + bits[1] + File.separator + bits[2] + ".dat";

        File stats_file = new File(stats_dir, current_rel_file);

        if (!stats_file.exists()) {

          if (can_cache) {

            month_cache.setTotals(day, cache_offset, new long[0]);
          }
        } else {

          LineNumberReader lnr = null;

          try {
            System.out.println("Reading " + stats_file);

            lnr = new LineNumberReader(new FileReader(stats_file));

            long file_start_time = 0;

            long[] file_totals = null;

            long[] file_result_totals = new long[STAT_ENTRY_COUNT];

            long[] session_start_stats = null;
            long session_start_time = 0;
            long session_time = 0;

            while (true) {

              String line = lnr.readLine();

              if (line == null) {

                break;
              }

              // System.out.println( line );

              String[] fields = line.split(",");

              if (fields.length < 6) {

                continue;
              }

              String first_field = fields[0];

              if (first_field.equals("s")) {

                session_start_time = Long.parseLong(fields[2]) * MIN_IN_MILLIS;

                if (file_totals == null) {

                  file_totals = new long[STAT_ENTRY_COUNT];

                  file_start_time = session_start_time;
                }

                session_time = session_start_time;

                session_start_stats = new long[STAT_ENTRY_COUNT];

                for (int i = 3; i < 9; i++) {

                  session_start_stats[i - 3] = Long.parseLong(fields[i]);
                }
              } else if (session_start_time > 0) {

                session_time += MIN_IN_MILLIS;

                int field_offset = 0;

                if (first_field.equals("e")) {

                  field_offset = 3;
                }

                long[] line_stats = new long[STAT_ENTRY_COUNT];

                for (int i = 0; i < 6; i++) {

                  line_stats[i] = Long.parseLong(fields[i + field_offset]);

                  file_totals[i] += line_stats[i];
                }

                if (session_time >= start_millis && session_time <= end_millis) {

                  for (int i = 0; i < 6; i++) {

                    result[i] += line_stats[i];

                    file_result_totals[i] += line_stats[i];
                  }
                }

                // System.out.println( getString( line_stats ));
              }
            }

            System.out.println(
                "File total: start="
                    + debug_utc_format.format(file_start_time)
                    + ", end="
                    + debug_utc_format.format(session_time)
                    + " - "
                    + getString(file_totals));

            if (can_cache) {

              month_cache.setTotals(day, cache_offset, file_result_totals);

              if (cache_offset != 0) {

                month_cache.setTotals(day, 0, file_totals);
              }
            } else {

              if (this_day == now_day) {

                if (day_cache == null) {

                  System.out.println("Creating day cache");

                  day_cache = new DayCache(year_str, month_str, day_str);
                }

                day_cache.setTotals(cache_offset, file_result_totals);

                if (cache_offset != 0) {

                  day_cache.setTotals(0, file_totals);
                }
              }
            }

          } catch (Throwable e) {

            Debug.out(e);

          } finally {

            if (lnr != null) {

              try {
                lnr.close();

              } catch (Throwable e) {
              }
            }
          }
        }
      }

      if (month_cache != null && month_cache.isDirty()) {

        month_cache.save();
      }

      System.out.println("    -> " + getString(result));

      return (result);
    }
  }
  private void write(int record_type, long[] line_stats) {
    synchronized (this) {
      try {
        final long now = SystemTime.getCurrentTime();

        final long now_mins = now / (1000 * 60);

        String[] bits = utc_date_format.format(new Date(now)).split(",");

        String year = bits[0];
        String month = bits[1];
        String day = bits[2];

        String current_rel_file = year + File.separator + month + File.separator + day + ".dat";

        String line;

        String stats_str = "";

        if (record_type == RT_SESSION_START) {

          // absolute values

          for (int i = 0; i < line_stats.length; i++) {

            stats_str += "," + line_stats[i];

            line_stats_prev[i] = 0;
          }

          day_cache = null;

        } else {

          // relative values

          long[] diffs = new long[STAT_ENTRY_COUNT];

          for (int i = 0; i < line_stats.length; i++) {

            long diff = line_stats[i] - line_stats_prev[i];

            session_total += diff;

            diffs[i] = diff;

            stats_str += "," + diff;

            line_stats_prev[i] = line_stats[i];

            stat_averages[i].update(diff);
          }

          if (day_cache != null) {

            if (day_cache.isForDay(year, month, day)) {

              day_cache.addRecord(now_mins, diffs);
            }
          }
        }

        if (record_type != RT_SESSION_STATS) {

          line =
              (record_type == RT_SESSION_START ? "s," : "e,")
                  + VERSION
                  + ","
                  + now_mins
                  + stats_str;

        } else {

          line = stats_str.substring(1);
        }

        if (writer == null || !writer_rel_file.equals(current_rel_file)) {

          // first open of a file or file switch

          if (writer != null) {

            // file switch

            if (record_type != RT_SESSION_START) {

              writer.println(line);
            }

            writer.close();

            if (writer.checkError()) {

              writer = null;

              throw (new IOException("Write faled"));
            }

            writer = null;
          }

          // no point in opening a new file just to record the session-end

          if (record_type != RT_SESSION_END) {

            File file = new File(stats_dir, current_rel_file);

            file.getParentFile().mkdirs();

            writer = new PrintWriter(new FileWriter(file, true));

            writer_rel_file = current_rel_file;

            if (record_type == RT_SESSION_START) {

              writer.println(line);

            } else {

              // first entry in a new file, files always start with a session-start so they
              // can be processed in isolation so reset the session data and start a new one

              st_p_sent += line_stats[0];
              st_d_sent += line_stats[1];
              st_p_received += line_stats[2];
              st_d_received += line_stats[3];
              st_dht_sent += line_stats[4];
              st_dht_received += line_stats[5];

              ss_p_sent += line_stats[0];
              ss_d_sent += line_stats[1];
              ss_p_received += line_stats[2];
              ss_d_received += line_stats[3];
              ss_dht_sent += line_stats[4];
              ss_dht_received += line_stats[5];

              stats_str = "";

              long[] st_stats =
                  new long[] {
                    st_p_sent, st_d_sent, st_p_received, st_d_received, st_dht_sent, st_dht_received
                  };

              for (int i = 0; i < st_stats.length; i++) {

                stats_str += "," + st_stats[i];

                line_stats_prev[i] = 0;
              }

              line = "s," + VERSION + "," + now_mins + stats_str;

              writer.println(line);
            }
          }
        } else {

          writer.println(line);
        }

      } catch (Throwable e) {

        Debug.out("Failed to write long term stats", e);

      } finally {

        if (writer != null) {

          if (record_type == RT_SESSION_END) {

            writer.close();
          }

          if (writer.checkError()) {

            Debug.out("Failed to write long term stats");

            writer.close();

            writer = null;

          } else {

            if (record_type == RT_SESSION_END) {

              writer = null;
            }
          }
        }
      }
    }

    if (record_type != RT_SESSION_END) {

      final List<LongTermStatsListener> to_fire = new ArrayList<LongTermStatsListener>();

      for (Object[] entry : listeners) {

        long diff = session_total - (Long) entry[2];

        if (diff >= (Long) entry[1]) {

          entry[2] = session_total;

          to_fire.add((LongTermStatsListener) entry[0]);
        }
      }

      if (to_fire.size() > 0) {

        dispatcher.dispatch(
            new AERunnable() {
              @Override
              public void runSupport() {
                for (LongTermStatsListener l : to_fire) {

                  try {
                    l.updated(LongTermStatsImpl.this);

                  } catch (Throwable e) {

                    Debug.out(e);
                  }
                }
              }
            });
      }
    }
  }
Ejemplo n.º 29
0
  public TranscodeFileImpl allocateFile(
      TranscodeProfile profile, boolean no_xcode, DiskManagerFileInfo file, boolean for_job)
      throws TranscodeException {
    TranscodeFileImpl result = null;

    setError(KEY_FILE_ALLOC_ERROR, null);

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            result = new TranscodeFileImpl(this, key, device_files);

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }

        if (result == null) {

          String ext = profile.getFileExtension();

          String target_file = file.getFile(true).getName();

          if (ext != null && !no_xcode) {

            int pos = target_file.lastIndexOf('.');

            if (pos != -1) {

              target_file = target_file.substring(0, pos);
            }

            target_file += ext;
          }

          target_file = allocateUniqueFileName(target_file);

          File output_file = getWorkingDirectory(true);

          if (!output_file.canWrite()) {

            throw (new TranscodeException(
                "Can't write to transcode folder '" + output_file.getAbsolutePath() + "'"));
          }

          output_file = new File(output_file.getAbsoluteFile(), target_file);

          result =
              new TranscodeFileImpl(
                  this, key, profile.getName(), device_files, output_file, for_job);

          result.setSourceFile(file);

          device_files_last_mod = SystemTime.getMonotonousTime();

          device_files_dirty = true;

        } else {

          result.setSourceFile(file);

          result.setProfileName(profile.getName());
        }
      }
    } catch (Throwable e) {

      setError(KEY_FILE_ALLOC_ERROR, Debug.getNestedExceptionMessage(e));

      throw (new TranscodeException("File allocation failed", e));
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileAdded(result);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }

    return (result);
  }
Ejemplo n.º 30
0
  protected void deleteFile(TranscodeFileImpl file, boolean delete_contents, boolean remove)
      throws TranscodeException {
    if (file.isDeleted()) {

      return;
    }

    if (delete_contents) {

      File f = file.getCacheFile();

      int time = 0;

      while (f.exists() && !f.delete()) {

        if (time > 3000) {

          log("Failed to remove file '" + f.getAbsolutePath() + "'");

          break;

        } else {

          try {
            Thread.sleep(500);

          } catch (Throwable e) {

          }

          time += 500;
        }
      }
    }

    if (remove) {

      try {
        // fire the listeners FIRST as this gives listeners a chance to extract data
        // from the file before it is deleted (otherwise operations fail with 'file has been
        // deleted'

        for (TranscodeTargetListener l : listeners) {

          try {
            l.fileRemoved(file);

          } catch (Throwable e) {

            Debug.out(e);
          }
        }

        synchronized (this) {
          if (device_files == null) {

            loadDeviceFile();

          } else {

            device_files_last_mod = SystemTime.getMonotonousTime();
          }

          device_files.remove(file.getKey());

          device_files_dirty = true;
        }

      } catch (Throwable e) {

        throw (new TranscodeException("Delete failed", e));
      }
    }
  }