Link to home
Start Free TrialLog in
Avatar of davev
davev

asked on

Message Processing Algorithm

I'm developing an application in which I process various message strings sent from a server. One of these messages occurs every 10 seconds or so and I must process this message as follows:

If I get 6 messages within approximately 50 seconds (at 0, 10, 20, 30, 40, 50 seconds or some other 50 second block), then the message frequency is acceptable and gets a grade of A. If for whatever reason there is an interruption in the message stream and I get between 1 and 5 strings in that time frame, then the grade is B. If I receive no messages in that time span then the grade is C and if this continues for 10 minutes then the grade is D and must be rechecked every 10 minutes. I'm using a 55 second time frame to check for the frequency of messages in case the messages are slightly longer than 10 seconds apart.

The problem I'm running into is that if there is a break in the comunication, I know it'll be a grade less than A and will remain as such until I get 6 consecutive strings within that 55 second time frame to get a grade of A again. If I hapeen to have a grade of C or D and all of a sudden get 6 messages in a row, the first 5 still have an grade of B (because this is still less than 6), and get an A only when the 6th message comes in. In addition, if the 6 consecutive message is consistent (that is, at 0 10 20 30 40 50 and continuing at 10 20 30 40 50 60 and so on), then the grade is consistently an A. Only when there is a break in the message stream does the grade fall below A and, as previously mentioned, there must be 6 consecutive messages at any give time in order to get a grade A again.

I know this probably sounds like a junior programmer issue, but I can't seem to get my head around it. I'll gladly double the points to the first person who can provide a workable solution (Scout's honor), and for the record, no this is NOT a homework assignment, even though I'm sure there are those out there who might think it is and choose not to respond because of that thought. Any assistance is genuinely appreciated. TIA
Avatar of Infinity08
Infinity08
Flag of Belgium image

A sliding window approach would suit this very well. The width of your window is 50 seconds (or 55), and you keep all messages received within that window in some buffer. Messages are removed from that buffer as soon as the window slides over them, messages are added as soon as they arrive. The moment a message arrives, the buffer size (+ 1 for the new message) decides the grade.
Something like this (in pseudo code) :

        while receive message
            remove old messages from buffer
            message grade = buffer size + 1
            add message to buffer
        end while
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of davev
davev

ASKER

That sounds so elegantly simple, I can't wait to try it out just to be sure! I assume that if I'm looking for 6 consecutive messages, then buffer size should be 5? I'm assuming this will also handle the case of 0 messages in the time frame too.
>> then buffer size should be 5?

Don't assume a max. buffer size unless you're sure. What if 7 messages are received in the last 50 seconds ? What if 100 are received ?


>> I'm assuming this will also handle the case of 0 messages in the time frame too.

Yes. As long as you clean the old messages from the buffer before you assign the grade, it's all fine.
Note that this part in the earlier pseudo code :

                if now - timestamp greater than 10 minutes then
                    message grade = C
                end if

should have been a grade D of course :

                if now - timestamp greater than 10 minutes then
                    message grade = D
                end if
Avatar of davev

ASKER

This sounds very workable, but unfortunately won't be able to try it out until I get home tonight so I hope you're patient. :) the messages should occur consistently every 10 seconds or so, but you're right, there could be other amounts that I need to contend with in the future. And because I can only try this at home, I want to stress again that this is NOT a homework assignment, but rather an evening and weekends project that I'm enhancing for a previous employer. I appreciate the effort you taking to help me resolve this.
Take your time. Depending on what you'll use to implement this (I assume in C#, and I don't know that language), you can probably optimize the above algorithm here and there. But it should give you a good idea of how to implement it.

Note that the "remove old messages from buffer" part would be something like this :

        for every message in the buffer
            if message timestamp + 50 is smaller than now
                remove message from buffer
            end if
        end for
Avatar of davev

ASKER

Thanks so much for your insight into this. After applying a couple other changes to meet other requirements of the application, the code ran flawlessly. it's great to have people out there who know what they're talking about. Thanks again.
Happy to be of assistance :)