Esempio n. 1
0
void AsyncPipeReader::handlerReady(uint16_t events) noexcept {
  DestructorGuard dg(this);
  CHECK(events & EventHandler::READ);

  VLOG(5) << "AsyncPipeReader::handlerReady() this=" << this << ", fd=" << fd_;
  assert(readCallback_ != nullptr);

  while (readCallback_) {
    // - What API does callback support?
    const auto movable = readCallback_->isBufferMovable(); // noexcept

    // Get the buffer to read into.
    void* buf = nullptr;
    size_t buflen = 0;
    std::unique_ptr<IOBuf> ioBuf;

    if (movable) {
      ioBuf = IOBuf::create(readCallback_->maxBufferSize());
      buf = ioBuf->writableBuffer();
      buflen = ioBuf->capacity();
    } else {
      try {
        readCallback_->getReadBuffer(&buf, &buflen);
      } catch (const std::exception& ex) {
        AsyncSocketException aex(
            AsyncSocketException::BAD_ARGS,
            string("ReadCallback::getReadBuffer() "
                   "threw exception: ") +
                ex.what());
        failRead(aex);
        return;
      } catch (...) {
        AsyncSocketException aex(
            AsyncSocketException::BAD_ARGS,
            string("ReadCallback::getReadBuffer() "
                   "threw non-exception type"));
        failRead(aex);
        return;
      }
      if (buf == nullptr || buflen == 0) {
        AsyncSocketException aex(
            AsyncSocketException::INVALID_STATE,
            string("ReadCallback::getReadBuffer() "
                   "returned empty buffer"));
        failRead(aex);
        return;
      }
    }

    // Perform the read
#if _WIN32
    // On Windows you can't call read on a socket, so call recv instead.
    ssize_t bytesRead =
        folly::fileutil_detail::wrapNoInt(recv_internal, fd_, buf, buflen);
#else
    ssize_t bytesRead = folly::readNoInt(fd_.toFd(), buf, buflen);
#endif

    if (bytesRead > 0) {
      if (movable) {
        ioBuf->append(std::size_t(bytesRead));
        readCallback_->readBufferAvailable(std::move(ioBuf));
      } else {
        readCallback_->readDataAvailable(size_t(bytesRead));
      }
      // Fall through and continue around the loop if the read
      // completely filled the available buffer.
      // Note that readCallback_ may have been uninstalled or changed inside
      // readDataAvailable().
      if (static_cast<size_t>(bytesRead) < buflen) {
        return;
      }
    } else if (bytesRead < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
      // No more data to read right now.
      return;
    } else if (bytesRead < 0) {
      AsyncSocketException ex(
          AsyncSocketException::INVALID_STATE, "read failed", errno);
      failRead(ex);
      return;
    } else {
      assert(bytesRead == 0);
      // EOF

      unregisterHandler();
      AsyncReader::ReadCallback* callback = readCallback_;
      readCallback_ = nullptr;
      callback->readEOF();
      return;
    }
    // Max reads per loop?
  }
}
Esempio n. 2
0
void AsyncPipeReader::handlerReady(uint16_t events) noexcept {
  DestructorGuard dg(this);
  CHECK(events & EventHandler::READ);

  VLOG(5) << "AsyncPipeReader::handlerReady() this=" << this << ", fd=" << fd_;
  assert(readCallback_ != nullptr);

  while (readCallback_) {
    // Get the buffer to read into.
    void* buf = nullptr;
    size_t buflen = 0;
    try {
      readCallback_->getReadBuffer(&buf, &buflen);
    } catch (const std::exception& ex) {
      AsyncSocketException aex(AsyncSocketException::BAD_ARGS,
                               string("ReadCallback::getReadBuffer() "
                                      "threw exception: ") + ex.what());
      failRead(aex);
      return;
    } catch (...) {
      AsyncSocketException ex(AsyncSocketException::BAD_ARGS,
                              string("ReadCallback::getReadBuffer() "
                                     "threw non-exception type"));
      failRead(ex);
      return;
    }
    if (buf == nullptr || buflen == 0) {
      AsyncSocketException ex(AsyncSocketException::INVALID_STATE,
                              string("ReadCallback::getReadBuffer() "
                                     "returned empty buffer"));
      failRead(ex);
      return;
    }

    // Perform the read
    ssize_t bytesRead = folly::readNoInt(fd_, buf, buflen);
    if (bytesRead > 0) {
      readCallback_->readDataAvailable(bytesRead);
      // Fall through and continue around the loop if the read
      // completely filled the available buffer.
      // Note that readCallback_ may have been uninstalled or changed inside
      // readDataAvailable().
      if (static_cast<size_t>(bytesRead) < buflen) {
        return;
      }
    } else if (bytesRead < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
      // No more data to read right now.
      return;
    } else if (bytesRead < 0) {
      AsyncSocketException ex(AsyncSocketException::INVALID_STATE,
                              "read failed", errno);
      failRead(ex);
      return;
    } else {
      assert(bytesRead == 0);
      // EOF

      unregisterHandler();
      AsyncReader::ReadCallback* callback = readCallback_;
      readCallback_ = nullptr;
      callback->readEOF();
      return;
    }
    // Max reads per loop?
  }
}
Esempio n. 3
0
AbstractFilter::FilterResult SAAFilter::ProcessMessage( AbstractFilter::buffer_type inputData, AbstractFilter::buffer_type outputData, NameValueCollection& transportHeaders, bool asClient )
{
	ManagedBuffer *inputBuffer = inputData.get();
	string groupId = transportHeaders[ MqFilter::MQGROUPID ];
	if( !asClient )
	{
		if ( inputBuffer )
		{
			DEBUG( "Applying SAA filter..." );

			XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* doc = NULL;
			char* outputXslt = NULL;

			try
			{
				const string& payload = inputBuffer->str();
				doc = XmlUtil::DeserializeFromString( payload );
				XSLTFilter xsltFilter;
				xsltFilter.ProcessMessage( doc, reinterpret_cast< unsigned char** >( &outputXslt), transportHeaders, true );
				size_t outputXsltSize = strlen( outputXslt );
				WorkItem< ManagedBuffer > inBuffer( new ManagedBuffer( reinterpret_cast<unsigned char*> ( outputXslt ), ManagedBuffer::Ref, outputXsltSize) );
				WorkItem< ManagedBuffer > outBuffer( new ManagedBuffer() );
				SwiftFormatFilter swiftFormatFilter;
				swiftFormatFilter.ProcessMessage( inBuffer, outBuffer, transportHeaders, false );
				BatchItem item;
				m_BatchStorage->open( groupId, ios_base::out );
				item.setBinPayload( outBuffer.get() );
				item.setBatchId( groupId );
				item.setSequence( 1 );
				*m_BatchStorage << item;
				item.setSequence( 2 );
				item.setLast();
				item.setPayload( payload );
				*m_BatchStorage << item;
			}
			catch( ... )
			{
				if ( doc != NULL )
					doc->release();
				delete outputXslt;
				TRACE( "Unhandled exception while applying SAA filter");
				throw;
			}
			doc->release();
			delete outputXslt;

			return SAAFilter::Completed;
		}
	}
	else
	{
		DEBUG( "Applying SAA filter ..." );

		WorkItem< ManagedBuffer > outputBuffer( new ManagedBuffer() );
		ManagedBuffer* managedOutputData = outputData.get();
		WorkItem< ManagedBuffer > inputBuffer( new ManagedBuffer() );
		ManagedBuffer* managedInputBuffer = inputBuffer.get();

		m_BatchStorage->open( Base64::decode( groupId ), ios_base::in );
		BatchItem batchItem;
		bool backoutCountExceeds = false;
		try
		{
			//get partner message
			*m_BatchStorage >> batchItem;
			managedInputBuffer->copyFrom( batchItem.getPayload() );

			//check backout count
			// TODO: Change backout count expose mechanism to avoid cast
			BatchManager<BatchMQStorage>* castStorage = dynamic_cast< BatchManager<BatchMQStorage> * >( m_BatchStorage.get() );
			if( castStorage != NULL )
			{
				//backout count exceeded, try Abort
				if( castStorage->storage().getCleaningUp() )
				{
					*m_BatchStorage >> batchItem;
					managedOutputData->copyFrom( batchItem.getPayload() );
					// Dont't  rely on filter user to Abort;
					// The commit is save because SAAFilter-server is always the first stage of message processing
					// ( nothing is partial commited when backout count exceeded)
					castStorage->storage().setCleaningUp( false );
					m_BatchStorage->commit();

					AppException aex( "FileAct message moved to dead letter queue because backout count exceeded" );
					aex.setSeverity( EventSeverity::Fatal );
					throw aex;
				}
			}
			else
				TRACE( "Backout counts should be check here..." )

			//check signature in partner message
			SwiftFormatFilter swiftFormatFilter;
			swiftFormatFilter.ProcessMessage( inputBuffer, outputBuffer, transportHeaders, true );
		}
		catch( runtime_error &ex )
Esempio n. 4
0
SPINET Sound_to_SPINET (Sound me, double timeStep, double windowDuration,
                        double minimumFrequencyHz, double maximumFrequencyHz, long nFilters,
                        double excitationErbProportion, double inhibitionErbProportion) {
	try {
		double firstTime, b = 1.02, samplingFrequency = 1 / my dx;

		if (timeStep < my dx) {
			timeStep = my dx;
		}
		if (maximumFrequencyHz > samplingFrequency / 2) {
			maximumFrequencyHz = samplingFrequency / 2;
		}

		long numberOfFrames;
		Sampled_shortTermAnalysis (me, windowDuration, timeStep, &numberOfFrames, &firstTime);
		autoSPINET thee = SPINET_create (my xmin, my xmax, numberOfFrames, timeStep, firstTime,
		                                 minimumFrequencyHz, maximumFrequencyHz, nFilters, excitationErbProportion, inhibitionErbProportion);
		autoSound window = Sound_createGaussian (windowDuration, samplingFrequency);
		autoSound frame = Sound_createSimple (1, windowDuration, samplingFrequency);
		autoNUMvector<double> f (1, nFilters);
		autoNUMvector<double> bw (1, nFilters);
		autoNUMvector<double> aex (1, nFilters);
		autoNUMvector<double> ain (1, nFilters);

		// Cochlear filterbank: gammatone

		for (long i = 1; i <= nFilters; i++) {
			f[i] = NUMerbToHertz (thy y1 + (i - 1) * thy dy);
			bw[i] = 2 * NUMpi * b * (f[i] * (6.23e-6 * f[i] + 93.39e-3) + 28.52);
		}

		autoMelderProgress progress (L"SPINET analysis");

		for (long i = 1; i <= nFilters; i++) {
			double bb = (f[i] / 1000) * exp (- f[i] / 1000); // outer & middle ear and phase locking
			double tgammaMax = (thy gamma - 1) / bw[i]; // Time where gammafunction envelope has maximum
			double gammaMaxAmplitude = pow ( (thy gamma - 1) / (NUMe * bw[i]), (thy gamma - 1)); // tgammaMax
			double timeCorrection = tgammaMax - windowDuration / 2;

			autoSound gammaTone = Sound_createGammaTone (0, 0.1, samplingFrequency,
			                      thy gamma, b, f[i], 0, 0, 0);
			autoSound filtered = Sounds_convolve (me, gammaTone.peek(), kSounds_convolve_scaling_SUM, kSounds_convolve_signalOutsideTimeDomain_ZERO);

			// To energy measure: weigh with broad-band transfer function

			for (long j = 1; j <= numberOfFrames; j++) {
				Sound_into_Sound (filtered.peek(), frame.peek(), Sampled_indexToX (thee.peek(), j) + timeCorrection);
				Sounds_multiply (frame.peek(), window.peek());
				thy y[i][j] = Sound_power (frame.peek()) * bb / gammaMaxAmplitude;
			}
			Melder_progress ( (double) i / nFilters, L"SPINET: filter ", Melder_integer (i), L" from ",
			                   Melder_integer (nFilters), L".");
		}

		// Excitatory and inhibitory area functions

		for (long i = 1; i <= nFilters; i++) {
			for (long k = 1; k <= nFilters; k++) {
				double fr = (f[k] - f[i]) / bw[i];
				aex[i] += fgamma (fr / thy excitationErbProportion, thy gamma);
				ain[i] += fgamma (fr / thy inhibitionErbProportion, thy gamma);
			}
		}

		// On-center off-surround interactions

		for (long j = 1; j <= numberOfFrames; j++)
			for (long i = 1; i <= nFilters; i++) {
				double a = 0;
				for (long k = 1; k <= nFilters; k++) {
					double fr = (f[k] - f[i]) / bw[i];
					double hexsq = fgamma (fr / thy excitationErbProportion, thy gamma);
					double hinsq = fgamma (fr / thy inhibitionErbProportion, thy gamma);
					a += thy y[k][j] * (hexsq / aex[i] - hinsq / ain[i]);
				}
				thy s[i][j] = a > 0 ? a : 0;
			}
		return thee.transfer();
	} catch (MelderError) {
		Melder_throw (me, ":  no SPINET created.");
	}
}