3

I'm simulating packets from a source that produces packets at a given packet/second interval. I want to make a stream class that operates like an ostream object, allowing operator<< to be used to output things through it, but with the caveat that each value inserted should be released from the buffer to a file descriptor, in order, at a specified interval.

So, for instance, I might have a socket with file descriptor sockfd and say:

MyBuffer buffer(sockfd, 1000); //Interval of 1000 milliseconds
buffer << 1 << 2 << 3;

and the output would be timed such that it would output

1
<1 second gap>
2
<1 second gap>
3

to the socket. I'm looking at Boost.Iostreams right now, would that be a good solution? Is there some magical phrase I can google that describes this problem that I'm not aware of?

Any help would be appreciated.

Thanks Brad

Bradley Swain
  • 804
  • 5
  • 12
  • Do you want to release *characters* one at a time or *objects inserted into the stream* one at a time? – templatetypedef Feb 17 '11 at 21:32
  • Are insertions happening concurrently while packets are output? If so, what happens when the stream becomes empty for >1sec, and then becomes not empty again? Is the next packet transmitted immediately, or at the next 1sec interval? – Emile Cormier Feb 17 '11 at 21:41
  • @templatetypedef it would be objects – Bradley Swain Feb 17 '11 at 21:46
  • @emile it would be happening concurrently, so I think the only solution would be like templatetypedef suggested, which is a polling thread. I think I was just approaching it wrong-headedly. – Bradley Swain Feb 17 '11 at 21:47
  • 1
    In that case, the queue in template's solution would have to be thread-safe, to avoid race conditions when a packet is input simultaneously while another is output. If you make the producer and consumer threads acquire a mutex while manipulating the queue, that should do the trick. – Emile Cormier Feb 17 '11 at 21:51

2 Answers2

2

One option for doing this that's completely orthogonal to building a custom streams class would be to maintain a queue of strings that's polled by a thread every second. Each time the queue is polled, the thread reads out the first element and sends it across the network.

This doesn't use the streams library, but I think that might be what you want. Internally, most streams just glob together all the input they get into a mass of text, losing the information about which parts of the text correspond to each object you inserted.

EDIT: I should have mentioned this the first time around, but please be sure to use the appropriate synchronization on this queue! You'll probably want to use a mutex to guard access to it, or to use a clever lock-free queue if that doesn't work. Just be sure not to blindly read and write to it from multiple threads.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
2

Should the 1000ms be asynchronous ? If not, you could put a Sleep(1000) in your stream's operator<<. Depending on what you're trying to do, it could suit you.

Calvin1602
  • 9,413
  • 2
  • 44
  • 55