/** * Sets whether or not this throttle is switching bandwidth on/off. */ public void setSwitching(boolean switching) { log.debug("setSwitching:" + switching); if(this.switching != switching) { fixBytesPerTick(switching); } this.switching = switching; }
/** * Write a single byte to the delegate stream, possibly blocking if * necessary to ensure that throughput doesn't exceed the limits. * * @param b the byte to write. * @throws IOException if an I/O error occurs on the OutputStream. */ @Override public void write(final int b) throws IOException { throttle.request(1); //Note that _request never returns zero. delegate.write(b); }
/** * Creates a new bandwidth throttle at the given throttle rate, * only allowing bandwidth to be used every other second if * switching is true. * The default windows size T is used. The bytes per windows N * is calculated from bytesPerSecond. * * @param bytesPerSecond the limits in bytes (not bits!) per second * (not milliseconds!) * @param switching true if we should only allow bandwidth to be used * every other second. */ public BandwidthThrottle(float bytesPerSecond, boolean switching) { this.setRate(bytesPerSecond); this.setSwitching(switching); }
@Override public boolean validate(final Transfer transfer) { final NSPopUpButton popup = Rococoa.cast(item.view(), NSPopUpButton.class); popup.selectItemAtIndex(popup.indexOfItemWithRepresentedObject(String.valueOf((int) transfer.getBandwidth().getRate()))); return true; } });
public UploadTransfer(final Host host, final List<TransferItem> roots, final Filter<Local> f, final Comparator<Local> comparator) { super(host, roots, new BandwidthThrottle(PreferencesFactory.get().getFloat("queue.upload.bandwidth.bytes"))); this.filter = f; this.comparator = comparator; }
/** * Creates a new bandwidth throttle at the given throttle rate. * The default windows size T is used. The bytes per windows N * is calculated from bytesPerSecond. * * @param bytesPerSecond the limits in bytes (not bits!) per second * (not milliseconds!) */ public BandwidthThrottle(float bytesPerSecond) { this.setRate(bytesPerSecond); }
/** * Blocks until the caller can send at least one byte without violating * bandwidth constraints. Records the number of byte sent. * * @param desired the number of bytes the caller would like to send * @return the number of bytes the sender is expected to send, which * is always greater than one and less than or equal to desired */ public synchronized int request(int desired) { if(UNLIMITED == rate) { return desired; } waitForBandwidth(); int result = Math.min(desired, availableBytes); availableBytes -= result; return result; }
/** * Waits until data is _availableBytes. */ private void waitForBandwidth() { while(true) { long now = System.currentTimeMillis(); updateWindow(now); if(availableBytes != 0) { break; } try { if(log.isInfoEnabled()) { log.info(String.format("Throttling bandwidth for %d milliseconds", nextTickTime - now)); } Thread.sleep(nextTickTime - now); } catch(InterruptedException e) { log.error(e.getMessage(), e); } } }
@Override public <T> T serialize(final Serializer dict) { dict.setStringForKey(this.getType().name(), "Type"); dict.setObjectForKey(host, "Host"); if(destination != null) { dict.setObjectForKey(destination, "Destination"); } dict.setListForKey(new ArrayList<Serializable>(selected.values()), "Destinations"); dict.setListForKey(new ArrayList<Serializable>(selected.keySet()), "Roots"); dict.setStringForKey(uuid, "UUID"); dict.setStringForKey(String.valueOf(this.getSize()), "Size"); dict.setStringForKey(String.valueOf(this.getTransferred()), "Current"); if(timestamp != null) { dict.setStringForKey(String.valueOf(timestamp.getTime()), "Timestamp"); } if(bandwidth != null) { dict.setStringForKey(String.valueOf(bandwidth.getRate()), "Bandwidth"); } return dict.getSerialized(); }
public CopyTransfer(final Host source, final Host destination, final Map<Path, Path> selected) { this(source, destination, selected, new BandwidthThrottle(PreferencesFactory.get().getFloat("queue.download.bandwidth.bytes"))); }
/** * @param bytesPerSecond Maximum number of bytes to transfer by second */ public void setBandwidth(final float bytesPerSecond) { if(log.isDebugEnabled()) { log.debug(String.format("Throttle bandwidth to %s bytes per second", bytesPerSecond)); } bandwidth.setRate(bytesPerSecond); }
public <T> T serialize(final Serializer dict) { dict.setStringForKey(this.getType().name(), "Type"); dict.setObjectForKey(host, "Host"); dict.setListForKey(roots, "Items"); dict.setStringForKey(uuid, "UUID"); if(size != null) { dict.setStringForKey(String.valueOf(size), "Size"); } if(transferred != null) { dict.setStringForKey(String.valueOf(transferred), "Current"); } if(timestamp != null) { dict.setStringForKey(String.valueOf(timestamp.getTime()), "Timestamp"); } if(bandwidth != null) { dict.setStringForKey(String.valueOf(bandwidth.getRate()), "Bandwidth"); } return dict.getSerialized(); }
public SyncTransfer(final Host host, final TransferItem item, final TransferAction action) { super(host, Collections.singletonList(item), new BandwidthThrottle(PreferencesFactory.get().getFloat("queue.upload.bandwidth.bytes"))); this.init(); this.item = item; this.action = action; }
transfer.getBandwidth().setRate(Float.parseFloat(bandwidthObj.toString()));
/** * Read an array of bytes from this InputStream. * * @param data the bytes to read. * @param offset the index in the array to start at. * @param len the number of bytes to read. * @throws IOException if an I/O error occurs on the InputStream. */ @Override public int read(byte[] data, int offset, int len) throws IOException { return delegate.read(data, offset, throttle.request(len)); } }
/** * Sets the throttle to the given throttle rate. The default windows size * T is used. The bytes per windows N is calculated from bytesPerSecond. * * @param bytesPerSecond the limits in bytes (not bits!) per second * (not milliseconds!) */ public void setRate(float bytesPerSecond) { if(bytesPerSecond < 0) { rate = UNLIMITED; } else { if(log.isDebugEnabled()) { log.debug(String.format("Set rate to %s bytes per second", bytesPerSecond)); } rate = bytesPerSecond; bytesPerTick = (int) (bytesPerSecond / TICKS_PER_SECOND); } if(switching) { this.fixBytesPerTick(true); } }
@Override public <T> T serialize(final Serializer dict) { dict.setStringForKey(this.getType().name(), "Type"); dict.setObjectForKey(host, "Host"); dict.setListForKey(roots, "Items"); dict.setStringForKey(uuid, "UUID"); dict.setStringForKey(String.valueOf(this.getSize()), "Size"); dict.setStringForKey(String.valueOf(this.getTransferred()), "Current"); if(timestamp != null) { dict.setStringForKey(String.valueOf(timestamp.getTime()), "Timestamp"); } if(bandwidth != null) { dict.setStringForKey(String.valueOf(bandwidth.getRate()), "Bandwidth"); } if(action != null) { dict.setStringForKey(action.name(), "Action"); } return dict.getSerialized(); }
public DownloadTransfer(final Host host, final List<TransferItem> roots, final Filter<Path> f, final Comparator<Path> comparator) { super(host, roots, new BandwidthThrottle(PreferencesFactory.get().getFloat("queue.download.bandwidth.bytes"))); this.filter = f; this.comparator = comparator; this.symlinkResolver = new DownloadSymlinkResolver(roots); }
/** * Write bytes[offset...offset+totalLength-1] to the delegate stream, * possibly blocking if necessary to ensure that throughput doesn't exceed * the limits. * * @param data the bytes to write. * @param offset the index in the array to start at. * @param totalLength the number of bytes to write. * @throws IOException if an I/O error occurs on the OutputStream. */ @Override public void write(byte[] data, int offset, int totalLength) throws IOException { //Note that we delegate directly to out. Do NOT call super.write(); //that calls this.write() resulting in HALF the throughput. while(totalLength > 0) { int length = throttle.request(totalLength); delegate.write(data, offset, length); totalLength -= length; offset += length; } } }
for(NSUInteger index = iterator.firstIndex(); !index.equals(NSIndexSet.NSNotFound); index = iterator.indexGreaterThanIndex(index)) { final Transfer transfer = collection.get(index.intValue()); if(BandwidthThrottle.UNLIMITED == transfer.getBandwidth().getRate()) { if(BandwidthThrottle.UNLIMITED == bytes) { item.setState(selected > 1 ? NSCell.NSMixedState : NSCell.NSOnState); final int bandwidth = (int) transfer.getBandwidth().getRate(); if(bytes == bandwidth) { item.setState(selected > 1 ? NSCell.NSMixedState : NSCell.NSOnState);