Simulating streaming data

c, named-pipes, perl, simulation, stream

Solution

Set up a background process that writes the file to a socket at whatever rate you want. In Perl,

use Socket;
socketpair my $A, my $B, AF_UNIX, SOCK_STREAM, PF_UNSPEC;

if (fork() == 0) {
    stream();
    exit;
}

while (<$A>) {
    print;
}

sub stream {
    # output 1024 bytes/sec
    select $B; $| = 1;          # disable output buffering
    open my $fh, '<', '/file/to/stream';
    my $buffer;
    while (my $n = read $fh, $buffer, 1024) {
        sleep 1;
        print $B $buffer;
    }
    close $fh;
}

Problem

I'm trying to write a program that will read from a flat file of data and simulate streaming it so I can test a program that reads streaming data without having to connect and start up the streaming hardware. What are the more realistic ways to accomplish this? I need it to stream at variable speeds depending on the hardware im simulating. My two ideas so far are a program that writes to a named pipe, or a program that writes to a virtual serial port at the rates I need. Is there a better (more realistic) way of simulating streaming data?

Original source

Related problems