pelican/examples/example-2/emulator/StreamEmulator.cpp
/*
 * This StreamEmulator sends data using a TCP socket.
 *
 *
 * Configuration
 * -----------------------------------------------------------------------------
 * It is configured by the following XML (populated here with example values)
 *
 * <StreamEmulator>
 *    <connection host="127.0.0.1" port="2001" />
 *    <packet     numSamples="100" />
 *    <data       numPackets="2000" pacetInterval="10" reportInterval="200" />
 * </SteamEmulator>
 *
 * The values of the host and port attributes determine the address on which
 * the TCP socket used to send emulated data should be opened.
 *
 * The numSamples attribute of packet determines the number of 4 byte values
 * are included in each emulated packets data section.
 *
 * The numPackets attribute determines the number of packets to be sent
 * by the emulator binary before it completes. If set to a value of "-1"
 * the emulator will continue sending packets until it is terminated by the
 * user.
 *
 * The packetInterval attribute is the delay between the sending of packets in
 * microseconds.
 *
 * The reportInterval is the interval between benchmark report messages and
 * is specified in numbers of packets.
 *
 * Packet format
 * -----------------------------------------------------------------------------
 * Emulated packets consist of a header and data section. The data section
 * consists of a number of 32 bit samples (qunit32 values), with the number
 * specified in the XML configuration. The header consists of the following:
 *
 *  - The total packet size, in bytes (4 bytes)
 *  - The packet counter. Index of the packet, starting at 0. (4bytes)
 *  - Timer value. Time since the last report log, in milliseconds. (4bytes)
 *  - Number of packets to be send by the emulator (4 bytes)
 *  - Report interval. Number of packets between report logs (4 bytes)
 *
 *  The header size is therefore 20 bytes.
 *
 *  The total size of the packet is therefore:
 *      20 bytes + number of samples x 4 bytes
 *
 * Note on the TCP socket connection
 * ----------------------------------------------------------------------------
 * The TCP socket, on which the emulator sends its data is instantiated in the
 * createDevice() method. If a connection cannot be made with the TCP server
 * immediately this function will block for up to 10 seconds repeatedly trying
 * to connect to the server.
 */
This StreamEmulator sends data using a TCP socket.
Configuration

It is configured by the following XML (populated here with example values)



The values of the host and port attributes determine the address on which
the TCP socket used to send emulated data should be opened.

The numSamples attribute of packet determines the number of 4 byte values
are included in each emulated packets data section.

The numPackets attribute determines the number of packets to be sent
by the emulator binary before it completes. If set to a value of "-1"
the emulator will continue sending packets until it is terminated by the
user.

The packetInterval attribute is the delay between the sending of packets in
microseconds.

The reportInterval is the interval between benchmark report messages and
is specified in numbers of packets.

Packet format

Emulated packets consist of a header and data section. The data section
consists of a number of 32 bit samples (qunit32 values), with the number
specified in the XML configuration. The header consists of the following.

- The total packet size, in bytes (4 bytes)
- The packet counter. Index of the packet, starting at 0. (4bytes)
- Timer value. Time since the last report log, in milliseconds. (4bytes)
- Number of packets to be send by the emulator (4 bytes)
- Report interval. Number of packets between report logs (4 bytes)

The header size is therefore 20 bytes.

The total size of the packet is therefore:
20 bytes + number of samples x 4 bytes

Note on the TCP socket connection

The TCP socket, on which the emulator sends its data is instantiated in the
createDevice() method. If a connection cannot be made with the TCP server
immediately this function will block for up to 10 seconds repeatedly trying
to connect to the server.
This StreamEmulator sends data using a TCP socket . Configuration It is configured by the following XML ( populated here with example values ) The values of the host and port attributes determine the address on which the TCP socket used to send emulated data should be opened . The numSamples attribute of packet determines the number of 4 byte values are included in each emulated packets data section . The numPackets attribute determines the number of packets to be sent by the emulator binary before it completes . If set to a value of " - 1 " the emulator will continue sending packets until it is terminated by the user . The packetInterval attribute is the delay between the sending of packets in microseconds . The reportInterval is the interval between benchmark report messages and is specified in numbers of packets . Packet format Emulated packets consist of a header and data section . The data section consists of a number of 32 bit samples ( qunit32 values ) with the number specified in the XML configuration . The header consists of the following . - The total packet size in bytes ( 4 bytes ) - The packet counter . Index of the packet starting at 0 . ( 4bytes ) - Timer value . Time since the last report log in milliseconds . ( 4bytes ) - Number of packets to be send by the emulator ( 4 bytes ) - Report interval . Number of packets between report logs ( 4 bytes ) The header size is therefore 20 bytes . The total size of the packet is therefore : 20 bytes + number of samples x 4 bytes Note on the TCP socket connection The TCP socket on which the emulator sends its data is instantiated in the createDevice () method . If a connection cannot be made with the TCP server immediately this function will block for up to 10 seconds repeatedly trying to connect to the server .
StreamEmulator::StreamEmulator(const pelican::ConfigNode& config)
: AbstractEmulator(), packetCounter_(0), reportCounter_(0)
{
    host_ = QHostAddress(config.getOption("connection", "host", "127.0.0.1"));
    port_ = config.getOption("connection", "port", "127.0.0.1").toShort();
    numSamples_ = (quint32)config.getOption("packet", "numSamples", "10000").toUInt();
    numPackets_ = config.getOption("data", "numPackets", "2000").toInt();
    packetInterval_ = config.getOption("data", "packetInterval", "0").toUInt();
    reportInterval_ = config.getOption("data", "reportInterval", QString::number(numPackets_)).toInt();
    headerSize_=3*sizeof(quint32)+2*sizeof(qint32);
    packetSize_=numSamples_*sizeof(quint32)+headerSize_;
    cout << "Emulator Settings:" << endl;
    cout << " * host            = " << host_.toString().toStdString() << endl;
    cout << " * port            = " << port_ << endl;
    cout << " * packet samples  = " << numSamples_ << endl;
    cout << " * packet size     = " << packetSize_ << " bytes ";
    cout << "[" <<packetSize_/(1024.0*1024.0) << " MiB]" << endl;
    cout << "   - header        = " << headerSize_ << " bytes" << endl;
    cout << "   - data          = " << packetSize_-headerSize_ << " bytes" << endl;
    cout << " * no. packets     = " << numPackets_ << endl;
    cout << " * report interval = " << reportInterval_ << endl;
    cout << " * packet interval = " << packetInterval_ << endl;
    packet_.resize(packetSize_);
}
StreamEmulator :: StreamEmulator ( const pelican :: ConfigNode & config ) : AbstractEmulator ( ) , packetCounter_ ( 0 ) , reportCounter_ ( 0 ) { host_ = QHostAddress ( config . getOption ( " " , " " , " " ) ) ; port_ = config . getOption ( " " , " " , " " ) . toShort ( ) ; numSamples_ = ( quint32 ) config . getOption ( " " , " " , " " ) . toUInt ( ) ; numPackets_ = config . getOption ( " " , " " , " " ) . toInt ( ) ; packetInterval_ = config . getOption ( " " , " " , " " ) . toUInt ( ) ; reportInterval_ = config . getOption ( " " , " " , QString :: number ( numPackets_ ) ) . toInt ( ) ; headerSize_ = 3 * sizeof ( quint32 ) +2 * sizeof ( qint32 ) ; packetSize_ = numSamples_ * sizeof ( quint32 ) + headerSize_ ; cout << " " << endl ; cout << " " << host_ . toString ( ) . toStdString ( ) << endl ; cout << " " << port_ << endl ; cout << " " << numSamples_ << endl ; cout << " " << packetSize_ << " " ; cout << " " << packetSize_ / ( 1024.0 * 1024.0 ) << " " << endl ; cout << " " << headerSize_ << " " << endl ; cout << " " << packetSize_ - headerSize_ << " " << endl ; cout << " " << numPackets_ << endl ; cout << " " << reportInterval_ << endl ; cout << " " << packetInterval_ << endl ; packet_ . resize ( packetSize_ ) ; }
This StreamEmulator sends data using a TCP socket.
This StreamEmulator sends data using a TCP socket .
// Resize the packet data.