@Override public void flush() throws OpenException { while ( _modulatorStatus != ModulatorStatus.IDLE ) { synchronized ( this ) { try { wait( LoopSleepTime.MIN.getMilliseconds() ); } catch ( InterruptedException e ) {} } } }
/** * Wait for last #throwException to finish. */ private void waitForExceptionHandling() { long theStartTime = System.currentTimeMillis(); while ( _threadCount.get() > 0 && _exception != null && System.currentTimeMillis() - theStartTime < LoopSleepTime.NORM.getMilliseconds() ) { try { Thread.sleep( LoopSleepTime.MIN.getMilliseconds() ); } catch ( InterruptedException ignored ) {} } } }
/** * {@inheritDoc} */ @Override public void dispose() { _isDisposed = true; _exception = null; synchronized ( this ) { notifyAll(); // In case we notified just before a thread entered the // #catchException() method. This can happen in a #catchException() // loop. That thread is notified now: try { wait( LoopSleepTime.MAX.getMilliseconds() ); } catch ( InterruptedException ignored ) {} notifyAll(); } }
private void flush() { if ( _dataLength > 0 ) { byte[] data = new byte[_dataLength]; for ( int i = 0; i < _dataLength; i++ ) { data[i] = _dataBuffer.get( i ); } initDataBuffer(); _dataLength = 0; // _byteSender.writeDatagrams( data ); for ( byte eData : data ) { try { _datagramQueue.offer( eData, LoopSleepTime.MAX.getMilliseconds(), TimeUnit.MILLISECONDS ); } catch ( InterruptedException ignore ) {} } } }
/** * {@inheritDoc} */ @Override default short[] readDatagrams( int aBlockSize ) throws OpenException, InterruptedException { short[] theBlock = new short[aBlockSize]; int i = 0; while ( hasDatagram() && i < aBlockSize ) { theBlock[i] = readDatagram(); i++; } if ( i == 0 ) { while ( !hasDatagram() ) { Thread.sleep( LoopSleepTime.NORM.getMilliseconds() ); while ( hasDatagram() && i < aBlockSize ) { theBlock[i] = readDatagram(); i++; } } } if ( i == aBlockSize ) { return theBlock; } return Arrays.copyOfRange( theBlock, 0, i ); }
/** * {@inheritDoc} */ @Override default byte[] readDatagrams( int aBlockSize ) throws OpenException, InterruptedException { byte[] theBlock = new byte[aBlockSize]; int i = 0; while ( hasDatagram() && i < aBlockSize ) { theBlock[i] = readDatagram(); i++; } if ( i == 0 ) { while ( !hasDatagram() ) { Thread.sleep( LoopSleepTime.NORM.getMilliseconds() ); } while ( hasDatagram() && i < aBlockSize ) { theBlock[i] = readDatagram(); i++; } } if ( i == aBlockSize ) { return theBlock; } return Arrays.copyOfRange( theBlock, 0, i ); } }
Thread.sleep( LoopSleepTime.NORM.getMilliseconds() ); while ( hasDatagram() && i < aBlockSize ) { theBlock[i] = readDatagram();
/** * Pushes a datagram into the receiver and puts it into the blocking queue * containing the so far received datagrams. Them datagrams can be retrieved * via {@link #readDatagram()}: use {@link #hasDatagram()} to test * beforehand whether there is a datagram available. * * @param aDatagram The datagram to be pushed at the end of the blocking * queue; to be retrieved with the {@link #readDatagram()} method. * @throws OpenException the open exception */ protected void pushDatagram( DATA aDatagram ) throws OpenException { if ( !isOpened() ) { throw new OpenException( "Unable to push datagram <" + aDatagram.getClass().getName() + "> as the connection is NOT OPEN; connection status is " + getConnectionStatus() + "." ); } RetryCounter theRetryCounter = new RetryCounterImpl( IoRetryCount.MAX.getNumber() ); try { while ( !_datagramQueue.offer( aDatagram, LoopSleepTime.MAX.getMilliseconds(), TimeUnit.MILLISECONDS ) && theRetryCounter.nextRetry() ) { warn( "Trying to offer (add) a datagram to the datagram queue, though the queue is full, this is retry # <" + theRetryCounter.getRetryCount() + ">, aborting after <" + theRetryCounter.getRetryNumber() + "> retries. Retrying now after a delay of <" + (LoopSleepTime.MAX.getMilliseconds() / 1000) + "> seconds..." ); if ( !theRetryCounter.hasNextRetry() ) { throw new OpenException( "Unable to process the datagram after <" + theRetryCounter.getRetryNumber() + "> retries, aborting retries, dismissing datagram \"" + aDatagram.toString() + "\"!", null, null ); } } } catch ( InterruptedException ignored ) {} }
/** * Pushes a datagram into the receiver and puts it into the blocking queue * containing the so far received datagrams. Them datagrams can be retrieved * via {@link #readDatagram()}: * * Use {@link AbstractByteReceiver} extension's * {@link AbstractByteReceiver#hasDatagram()} to test beforehand whether * there is a datagram available. * * @param aDatagram The datagram to be pushed at the end of the blocking * queue; to be retrieved with the {@link #readDatagram()} method. * @throws OpenException the open exception */ protected void pushDatagram( byte aDatagram ) throws OpenException { RetryCounter theRetryCounter = new RetryCounterImpl( IoRetryCount.MAX.getNumber() ); try { while ( !_datagramQueue.offer( aDatagram, LoopSleepTime.MAX.getMilliseconds(), TimeUnit.MILLISECONDS ) && theRetryCounter.nextRetry() ) { warn( "Trying to offer (add) a datagram to the datagram queue, though the queue is full, this is retry # <" + theRetryCounter.getRetryCount() + ">, aborting after <" + theRetryCounter.getRetryNumber() + "> retries. Retrying now after a delay of <" + (LoopSleepTime.MAX.getMilliseconds() / 1000) + "> seconds..." ); if ( !theRetryCounter.hasNextRetry() ) { throw new OpenException( "Unable to process the datagram after <" + theRetryCounter.getRetryNumber() + "> retries, aborting retries, dismissing datagram \"" + aDatagram + "\"!", null, null ); } } } catch ( InterruptedException ignored ) {} }
/** * Pushes a datagram into the receiver and puts it into the blocking queue * containing the so far received datagrams. Them datagrams can be retrieved * via {@link #readDatagram()}: use {@link #hasDatagram()} to test * beforehand whether there is a datagram available. * * @param aDatagram The datagram to be pushed at the end of the blocking * queue; to be retrieved with the {@link #readDatagram()} method. * @throws OpenException the open exception */ protected void pushDatagram( short aDatagram ) throws OpenException { if ( !isOpened() ) { throw new OpenException( "Unable to push datagram <" + aDatagram + "> as the connection is NOT OPEN; connection status is " + getConnectionStatus() + "." ); } RetryCounter theRetryCounter = new RetryCounterImpl( IoRetryCount.MAX.getNumber() ); try { while ( !_datagramQueue.offer( aDatagram, LoopSleepTime.MAX.getMilliseconds(), TimeUnit.MILLISECONDS ) && theRetryCounter.nextRetry() ) { warn( "Trying to offer (add) a datagram to the datagram queue, though the queue is full, this is retry # <" + theRetryCounter.getRetryCount() + ">, aborting after <" + theRetryCounter.getRetryNumber() + "> retries. Retrying now after a delay of <" + (LoopSleepTime.MAX.getMilliseconds() / 1000) + "> seconds..." ); if ( !theRetryCounter.hasNextRetry() ) { throw new OpenException( "Unable to process the datagram after <" + theRetryCounter.getRetryNumber() + "> retries, aborting retries, dismissing datagram \"" + aDatagram + "\"!", null, null ); } } } catch ( InterruptedException ignored ) {} }
while ( (isBusy()) && theRetryTimeout.hasNextRetry() && isOpened() ) { if ( ENABLE_EXTENDED_DEBUG_LOGGING ) { LOGGER.info( "Wait loop <" + theRetryTimeout.getRetryCount() + "> while being BUSY for <" + LoopSleepTime.NORM.getMilliseconds() + "> ms." );
/** * {@inheritDoc} */ @Override protected synchronized void close( CloseConnectionMessage aJob ) { ControlFlowUtility.throwIllegalStateException( isDestroyed() ); if ( ENABLE_EXTENDED_DEBUG_LOGGING ) { LOGGER.info( "CLOSE called on <" + getClass().getName() + "> with job <" + aJob + ">; connection status is " + getConnectionStatus() + "." ); } if ( !isClosed() ) { signOffAllProxies(); _instanceHandler.lock(); RetryTimeout theRetryTimeout = new RetryTimeoutImpl( IoTimeout.NORM.getMilliseconds(), RetryLoopCount.NORM_NUM_RETRY_LOOPS.getNumber() ); while ( (isBusy()) && theRetryTimeout.hasNextRetry() ) { if ( ENABLE_EXTENDED_DEBUG_LOGGING ) LOGGER.info( "Wait loop <" + theRetryTimeout.getRetryCount() + "> while being BUSY for <" + LoopSleepTime.NORM.getMilliseconds() + "> ms." ); theRetryTimeout.nextRetry(); } super.close( aJob ); if ( isBusy() ) { LOGGER.warn( "Still being BUSY even after reaching the timeout of <" + IoTimeout.NORM.getMilliseconds() + "> ms, closing connection nonetheless." ); } try { super.close(); } catch ( CloseException e ) { LOGGER.warn( "Unable to close malfunctioning connection.", e ); } onClosed(); } }
/** * {@inheritDoc} */ @Override public CipherVersion next() { CipherVersion eCipherVersion = null; RetryCounter theRetryCounter = new RetryCounterImpl( RetryCount.NORM.getNumber(), LoopSleepTime.MIN.getMilliseconds() ); while ( theRetryCounter.nextRetry() ) { try { eCipherVersion = _cipherVersionGenerator.next(); // ------------------------------------------------------------- // Hotspot for persisting the cipher in a DB or a storage using // some kind of asymmetric encryption mechanism // ------------------------------------------------------------- _encryptionServer.addCipherVersion( _namespace, toEncryptedCipherVersion( eCipherVersion ) ); return eCipherVersion; } catch ( CipherUidAlreadyInUseException e ) { if ( !theRetryCounter.hasNextRetry() ) { // @formatter:off LOGGER.warn( "Failed to add a cipher UID \"" + eCipherVersion.getUniversalId() + "\", retry count is <" + theRetryCounter.getRetryCount() + "> of <" + theRetryCounter.getRetryNumber() + "> (waiting for <" + ( theRetryCounter.getNextRetryDelayInMs() / 1000 ) + "> seconds before next retry): \"" + ExceptionUtility.toMessage( e ) + "\"" ); // @formatter:on } } } throw new UnexpectedForwardSecrecyRuntimeException( "Unable to generate a Chipher-Version within <" + theRetryCounter.getRetryNumber() + "> tries, aborting as else we might loop infinte time!" ); }