void fft_vfc_base::processStreamIdChanges()
{
    boost::mutex::scoped_lock lock(_sriMutex);

    LOG_TRACE( fft_vfc_base, "processStreamIDChanges QUEUE: " << _sriQueue.size()  );
    if (  _sriQueue.size() == 0 ) return;
    std::string sid("");

    if ( validGRBlock() ) {

        IStreamList::iterator istream;
        int idx=0;
        std::string sid("");
        int mode=0;
        SRIQueue::iterator item = _sriQueue.begin();

        for ( ; item != _sriQueue.end(); item++ ) {
            idx = 0;
            sid = "";
            mode= item->second.mode;
            sid = item->second.streamID;
            istream = _istreams.begin();
            for ( ; istream != _istreams.end(); idx++, istream++ ) {

                if ( istream->port == item->first ) {
                    LOG_DEBUG( fft_vfc_base,  "  SETTING IN_STREAM ID/STREAM_ID :" << idx << "/" << sid  );
                    istream->sri(true);
                    istream->spe(mode);

                    LOG_DEBUG( fft_vfc_base,  "  SETTING  OUT_STREAM ID/STREAM_ID :" << idx << "/" << sid  );
                    setOutputStreamSRI( idx, item->second );
                }
            }
        }

        _sriQueue.clear();

    } else {
        LOG_WARN( fft_vfc_base, " NEW STREAM ID, NO GNU RADIO BLOCK DEFINED, SRI QUEUE SIZE:" << _sriQueue.size() );
    }

}
void sig_source_f_i::changedSamplingFreq(const std::string &id)
{
    boost::mutex::scoped_lock lock(serviceThreadLock);
    BULKIO::StreamSRI sri = BULKIO::StreamSRI();
    sri.hversion = 1;
    sri.xstart = 0.0;
    if (this->gr_sptr->sampling_freq() > 0.0)
        sri.xdelta = 1/this->gr_sptr->sampling_freq();
    else
        sri.xdelta = 1.0;
    sri.xunits = BULKIO::UNITS_TIME;
    sri.subsize = 0;
    sri.ystart = 0.0;
    sri.ydelta = 0.0;
    sri.yunits = BULKIO::UNITS_NONE;
    sri.mode = 0;
    sri.streamID = CORBA::string_dup(this->naming_service_name.c_str());
    setOutputStreamSRI(sri);
    this->gr_sptr->set_sampling_freq(this->sampling_freq);
}
template <  typename IN_PORT_TYPE, typename OUT_PORT_TYPE > int randomizer_base::_transformerServiceFunction( typename  std::vector< gr_istream< IN_PORT_TYPE > > &istreams ,
    typename  std::vector< gr_ostream< OUT_PORT_TYPE > > &ostreams  )
{
    typedef typename std::vector< gr_istream< IN_PORT_TYPE > >   _IStreamList;
    typedef typename std::vector< gr_ostream< OUT_PORT_TYPE > >  _OStreamList;

    boost::mutex::scoped_lock lock(serviceThreadLock);

    if ( validGRBlock() == false ) {

        // create our processing block, and setup  property notifiers
        createBlock();

        LOG_DEBUG( randomizer_base, " FINISHED BUILDING  GNU RADIO BLOCK");
    }
 
    //process any Stream ID changes this could affect number of io streams
    processStreamIdChanges();

    if ( !validGRBlock() || istreams.size() == 0 || ostreams.size() == 0  ) {
        LOG_WARN( randomizer_base, "NO STREAMS ATTACHED TO BLOCK..." );
        return NOOP;
    }

    _input_ready.resize( istreams.size() );
    _ninput_items_required.resize( istreams.size() );
    _ninput_items.resize( istreams.size() );
    _input_items.resize( istreams.size() );
    _output_items.resize( ostreams.size() );

    //
    // RESOLVE: need to look at forecast strategy, 
    //    1)  see how many read items are necessary for N number of outputs
    //    2)  read input data and see how much output we can produce
    //

    //
    // Grab available data from input streams
    //
    typename _OStreamList::iterator ostream;
    typename _IStreamList::iterator istream = istreams.begin();
    int nitems=0;
    for ( int idx=0 ; istream != istreams.end() && serviceThread->threadRunning() ; idx++, istream++ ) {
        // note this a blocking read that can cause deadlocks
        nitems = istream->read();
    
        if ( istream->overrun() ) {
            LOG_WARN( randomizer_base, " NOT KEEPING UP WITH STREAM ID:" << istream->streamID );
        }

        if ( istream->sriChanged() ) {
            // RESOLVE - need to look at how SRI changes can affect Gnu Radio BLOCK state
            LOG_DEBUG( randomizer_base, "SRI CHANGED, STREAMD IDX/ID: " 
                      << idx << "/" << istream->pkt->streamID );
            setOutputStreamSRI( idx, istream->pkt->SRI );
        }
    }

    LOG_TRACE( randomizer_base, "READ NITEMS: "  << nitems );
    if ( nitems <= 0 && !_istreams[0].eos() ) {
        return NOOP;
    }

    bool eos = false;
    int  nout = 0;
    bool workDone = false;

    while ( nout > -1 && serviceThread->threadRunning() ) {
        eos = false;
        nout = _forecastAndProcess( eos, istreams, ostreams );
        if ( nout > -1  ) {
            workDone = true;

            // we chunked on data so move read pointer..
            istream = istreams.begin();
            for ( ; istream != istreams.end(); istream++ ) {
                int idx=std::distance( istreams.begin(), istream );
                // if we processed data for this stream
                if ( _input_ready[idx] ) {
                    size_t nitems = 0;
                    try {
                        nitems = gr_sptr->nitems_read( idx );
                    } catch(...){}
      
                    if ( nitems > istream->nitems() ) {
                        LOG_WARN( randomizer_base,  "WORK CONSUMED MORE DATA THAN AVAILABLE,  READ/AVAILABLE "
                                 << nitems << "/" << istream->nitems() );
                        nitems = istream->nitems();
                    }
                    istream->consume( nitems );
                    LOG_TRACE( randomizer_base, " CONSUME READ DATA  ITEMS/REMAIN " << nitems << "/" << istream->nitems());
                }
            }
            gr_sptr->reset_read_index();
        }

        // check for not enough data return
        if ( nout == -1 ) {

            // check for  end of stream
            istream = istreams.begin();
            for ( ; istream != istreams.end() ; istream++) {
                if ( istream->eos() ) {
                    eos=true;
                }
            }
            if ( eos ) {
                LOG_TRACE(  randomizer_base, "EOS SEEN, SENDING DOWNSTREAM " );
                _forecastAndProcess( eos, istreams, ostreams);
            }
        }
    }

    if ( eos ) {
        istream = istreams.begin();
        for ( ; istream != istreams.end() ; istream++ ) {
            int idx=std::distance( istreams.begin(), istream );
            LOG_DEBUG( randomizer_base, " CLOSING INPUT STREAM IDX:" << idx );
            istream->close();
        }

        // close remaining output streams
        ostream = ostreams.begin();
        for ( ; eos && ostream != ostreams.end(); ostream++ ) {
            int idx=std::distance( ostreams.begin(), ostream );
            LOG_DEBUG( randomizer_base, " CLOSING OUTPUT STREAM IDX:" << idx );
            ostream->close();
        }
    }

    //
    // set the read pointers of the GNU Radio Block to start at the beginning of the 
    // supplied buffers
    //
    gr_sptr->reset_read_index();

    LOG_TRACE( randomizer_base, " END OF TRANSFORM SERVICE FUNCTION....." << noutput_items );

    if ( nout == -1 && eos == false && !workDone ) {
        return NOOP;
    } else {
        return NORMAL;
    }
}
void copy_octet_base::processStreamIdChanges() {

  boost::mutex::scoped_lock lock(_sriMutex);

  LOG_TRACE( copy_octet_base, "processStreamIDChanges QUEUE: " << _sriQueue.size()  );
  if (  _sriQueue.size() == 0 ) return;
  std::string sid("");

  if ( validGRBlock() ) {
    int max_input =  gr_sptr->get_max_input_streams();
    int n_input = gr_sptr->get_num_input_streams();
    LOG_TRACE( copy_octet_base, " IN_MAX=" << max_input << " N_IN:" << n_input);
    int max_output =  gr_sptr->get_max_output_streams();
    int n_output = gr_sptr->get_num_output_streams(); 
    LOG_TRACE( copy_octet_base, " OUT_MAX=" << max_output << " N_OUT:" << n_output);          

    bool var_istreams = false;
    if ( max_input == -1 && inPorts.size() == 1 ) 
       var_istreams = true;

    bool var_ostreams = false;
    if ( max_output == -1 && outPorts.size() == 1 ) 
       var_ostreams = true;    

    IStreamList::iterator istream;
    int idx=0;
    std::string sid("");
    int mode=0;
    SRIQueue::iterator item = _sriQueue.begin();
    
    for ( ; item != _sriQueue.end(); item++ ) {
       idx = 0;
       sid = "";
       mode= item->second.mode;
       sid = item->second.streamID;
       istream = _istreams.begin();
       for ( ; istream != _istreams.end(); idx++, istream++ ) {
	   if ( !var_istreams && istream->port == item->first ) {
	      break;
	   }
	   else if ( var_istreams && (istream->port == item->first) && (istream->streamID == sid) )  {
	        break;
	  }
       }

       if ( istream == _istreams.end() ) {
          if ( var_istreams )  {
	     if ( gr_sptr ) gr_sptr->add_read_index();
	     _istreams.push_back( gr_istream< BULKIO_dataOctet_In_i >( item->first, sid, gr_sptr, idx, mode ) );
             LOG_TRACE( copy_octet_base, "GR_ISTREAM::ADD  PORT:" << item->first->getName() << " STREAM_ID:" << sid << " NSTREAMS:" << _istreams.size());

	     if ( var_ostreams ) {
	     	RH_UsesPortMap::iterator p_out = outPorts.begin();
		_ostreams.push_back( gr_ostream < BULKIO_dataOctet_Out_i >( dynamic_cast< BULKIO_dataOctet_Out_i *>(p_out->second), sid, gr_sptr, idx ));
	        LOG_TRACE( copy_octet_base, "GR_OSTREAM::ADD  PORT:" << p_out->second->getName() << " STREAM_ID:" << sid << " OSTREAMS:" << _ostreams.size());
             }    
	  }
	  else {
	       LOG_WARN( copy_octet_base, " NEW STREAM ID, MISSING INPUT STREAM DEFINITION"  );
	  }
       }
       else if ( !var_istreams ) {
	    LOG_DEBUG(  copy_octet_base,  "  SETTING GR_OSTREAM ID/STREAM_ID :" << idx << "/" << sid  );
	    istream->sri(true);
	    istream->spe(mode);
       }

       LOG_DEBUG(  copy_octet_base,  "  GR_OSTREAM ID/STREAM_ID :" << idx << "/" << sid  );
       setOutputStreamSRI( idx, item->second );
	        
    }

     _sriQueue.clear();
     
  }
  else {
 	LOG_WARN(  copy_octet_base, " NEW STREAM ID, NO GNU RADIO BLOCK DEFINED, SRI QUEUE SIZE:" << _sriQueue.size() );
  }
      

 LOG_TRACE(  copy_octet_base,  "processStreamID,  GR_ISTREAM MAP SIZE:"  << _istreams.size() );

}