473,324 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Is there a design pattern for this?

me
Hi there

I'm writing a messaging interface that takes a stream of binary data
from a socket, interprets it according to a message specification (field
names/lengths etc), and then acts upon the message.

My original design was to read from the socket until i got a code that
indicates the start of a message, then read MESSAGE_LENGTH bytes, then
pass it to another class for interpretation and actioning, thus getting
a nice seperation between the network interface and the actual data.

The problem is that some fields in the message have Field Extension
bits. In short, this means that if the LSB of the field is 1, then there
are extra bytes inserted in the message immediately afterwards.

So my problem is how do I interpret the message on the fly (in order to
read the correct number of bytes), while still maintaining seperation
between reading of the message bytes and their interpretation?

Cheers

pjw
Jul 22 '05 #1
3 1208
me wrote:
Hi there

I'm writing a messaging interface that takes a stream of binary data
from a socket, interprets it according to a message specification (field
names/lengths etc), and then acts upon the message.

My original design was to read from the socket until i got a code that
indicates the start of a message, then read MESSAGE_LENGTH bytes, then
pass it to another class for interpretation and actioning, thus getting
a nice seperation between the network interface and the actual data.

The problem is that some fields in the message have Field Extension
bits. In short, this means that if the LSB of the field is 1, then there
are extra bytes inserted in the message immediately afterwards.

So my problem is how do I interpret the message on the fly (in order to
read the correct number of bytes), while still maintaining seperation
between reading of the message bytes and their interpretation?

Cheers

pjw


Well, you could make the protocol sane. :-)
This is exactly why most protocols use a tag-length-value approach. That
way you get a nice seperation between the transport layer, which only
needs to be able to find the outer-most length value and the application
layer, which knows about the individual fields carried by the message.

So, in short: every message should carry a fixed header which contains
the number of octets to follow in the body.

If you're not in control of the protocol, then you have my sympathies.

HTH,
Jacques.
Jul 22 '05 #2
me
Jacques Labuschagne wrote:
me wrote:
Hi there

I'm writing a messaging interface that takes a stream of binary data
from a socket, interprets it according to a message specification
(field names/lengths etc), and then acts upon the message.

My original design was to read from the socket until i got a code that
indicates the start of a message, then read MESSAGE_LENGTH bytes, then
pass it to another class for interpretation and actioning, thus
getting a nice seperation between the network interface and the actual
data.

The problem is that some fields in the message have Field Extension
bits. In short, this means that if the LSB of the field is 1, then
there are extra bytes inserted in the message immediately afterwards.

So my problem is how do I interpret the message on the fly (in order
to read the correct number of bytes), while still maintaining
seperation between reading of the message bytes and their interpretation?

Cheers

pjw

Well, you could make the protocol sane. :-)
This is exactly why most protocols use a tag-length-value approach. That
way you get a nice seperation between the transport layer, which only
needs to be able to find the outer-most length value and the application
layer, which knows about the individual fields carried by the message.

So, in short: every message should carry a fixed header which contains
the number of octets to follow in the body.

If you're not in control of the protocol, then you have my sympathies.

HTH,
Jacques.


Indeed. Unfortunately I cant change it.....

One idea I had would be to take data from the socket one byte at a time,
and pass it to the interpretor. The interpretor would then pass back
some kind of indication as to how big the next 'chunk' should be.

I'm somewhat resigned to that fact that its probably going to be
messy...especially as the interpretor and socket handler are in seperate
threads....

cheers
Jul 22 '05 #3
I am just wondering about your design decision, having Interpreter and
Socket in 2 different threads.. I think I would introduce a package manager
that recieved a full package from the Socket, and passed it to the
Interpreter..
If the Interpreter needs to deal with transmission issues( gathering
packets, and checking they seem sane) and interprete the data, it will have
to many responsibilities (for my code anyway..)...

Jesper

"me" <me@me.com> wrote in message news:2v*************@uni-berlin.de...
Jacques Labuschagne wrote:
me wrote:
Hi there

I'm writing a messaging interface that takes a stream of binary data
from a socket, interprets it according to a message specification
(field names/lengths etc), and then acts upon the message.

My original design was to read from the socket until i got a code that
indicates the start of a message, then read MESSAGE_LENGTH bytes, then
pass it to another class for interpretation and actioning, thus
getting a nice seperation between the network interface and the actual
data.

The problem is that some fields in the message have Field Extension
bits. In short, this means that if the LSB of the field is 1, then
there are extra bytes inserted in the message immediately afterwards.

So my problem is how do I interpret the message on the fly (in order
to read the correct number of bytes), while still maintaining
seperation between reading of the message bytes and their interpretation?
Cheers

pjw

Well, you could make the protocol sane. :-)
This is exactly why most protocols use a tag-length-value approach. That
way you get a nice seperation between the transport layer, which only
needs to be able to find the outer-most length value and the application
layer, which knows about the individual fields carried by the message.

So, in short: every message should carry a fixed header which contains
the number of octets to follow in the body.

If you're not in control of the protocol, then you have my sympathies.

HTH,
Jacques.


Indeed. Unfortunately I cant change it.....

One idea I had would be to take data from the socket one byte at a time,
and pass it to the interpretor. The interpretor would then pass back
some kind of indication as to how big the next 'chunk' should be.

I'm somewhat resigned to that fact that its probably going to be
messy...especially as the interpretor and socket handler are in seperate
threads....

cheers

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
1
by: Takayasu Kenduma | last post by:
hi everybody. i am quite new about design patterns and i would like improve my experience in c# applications design. so i choose a common problem as a draft to talk about in the group to find the...
1
by: Nick | last post by:
I have read a few months ago about a policy design pattern but can find nothing on it now. Does this design pattern exist and if so could someone explain what it is or links to a description. ...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
4
by: FluffyCat | last post by:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Command Pattern. Since you all enjoyed the Visitor Pattern so much yesterday, today I have the Command Pattern...
6
by: pitachu | last post by:
Hi, I'm not an expect in .NET, so would anyone know an answer a design pattern for the following? There are many customers that require minor customizations to the program I will be...
16
by: sailor.gu | last post by:
Hi all guys, As an embeded programmer with five year C experience, I did read many great books related with design, coding, test,debug,algorithms, compiler, design, os, pm and others. I...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
3
by: FluffyCat | last post by:
Last month I continued my series of design patterns examples using PHP 5 with the Observer Pattern and the Prototype Pattern. Here now is my 16th example, the Adapter pattern. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.