# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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.
0
Commented:
Something like this (in pseudo code) :

remove old messages from buffer
message grade = buffer size + 1
end while
0
Commented:
>> 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 not sure I completely understand this part, but easiest is to keep the timestamp of the last non-C and non-D grade. If that timestamp is further away than 10 minutes, then the grade becomes a D grade.

Modified pseudo code :

timestamp = now
remove old messages from buffer
message grade = buffer size + 1  (A, B or C)
if grade is A or B then
timestamp = now
else if grade is C then
if now - timestamp greater than 10 minutes then
end if
end if
end while
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
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.
0
Commented:
>> 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.
0
Commented:
Note that this part in the earlier pseudo code :

if now - timestamp greater than 10 minutes then
end if

should have been a grade D of course :

if now - timestamp greater than 10 minutes then
end if
0
Author Commented:
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.
0
Commented:
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
0
Author Commented:
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.
0
Commented:
Happy to be of assistance :)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.