473,385 Members | 1,888 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,385 software developers and data experts.

Object Oriented approach to receiving messages

Hi,

I come from a C background. I am trying to receive messages over a serial
port. The message data structure format is along the lines of

int message_id;
int message_length;
int data[message_length];

There will be different messages received with different IDs and variable
lengths.

If I were to write this in C I would use something like:-
switch (message_id){
case 1:
decode_message();
add_to_msg_processing_queue();
break;
....
}

There will also be a transmit routine to write too.

Now I don't feel that this is very good object oriented approach. Could
anyone suggest a better way to handle this type of message structure in C#?

Thanks
Nov 17 '05 #1
8 1674
You can try using the Factory pattern. Basically, you have a class for
each type of message and that class will be responsible for decoding
and further processing of the message.

The factory then creates appropriate objects, based on the message ids
and then passes the message structure to that object for further
action.

Regards
Senthil

Nov 17 '05 #2
You can try using the Factory pattern. Basically, you have a class for
each type of message and that class will be responsible for decoding
and further processing of the message.

The factory then creates appropriate objects, based on the message ids
and then passes the message structure to that object for further
action.

Regards
Senthil

Nov 17 '05 #3


Brian wrote:
There will be different messages received with different IDs and variable
lengths.


You need to dispatch to code depending on ID, right? If you need to
dispatch on predicates on ID you need to get a bit more clever than this.

Setup an IDictionary from ID's to functions:

class Handlers: Hashtable {
public static Handlers Global = new Handlers();
public delegate void Handler(int id, Stream s);
public Handler Register(int id, Handler handler) {
Handler old = this[id];
this[id] = handler;
return old;
}
public void Dispatch(id id, Stream s) {
((Handler)this[id])(id,s);
}
}

Register all the handlers for different ID's:

Handlers.Global.Register(new Handler((new HandlerForSomeId()).f));
...

Read input, something like:

while ( true ) {
int id = ReadIdFromStream(s);
Handlers.Global.Dispatch(id, s);
}

Now, the handler, can read from the stream and place and objects
constructed from the stream in any queue's relevant to processing.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4


Brian wrote:
There will be different messages received with different IDs and variable
lengths.


You need to dispatch to code depending on ID, right? If you need to
dispatch on predicates on ID you need to get a bit more clever than this.

Setup an IDictionary from ID's to functions:

class Handlers: Hashtable {
public static Handlers Global = new Handlers();
public delegate void Handler(int id, Stream s);
public Handler Register(int id, Handler handler) {
Handler old = this[id];
this[id] = handler;
return old;
}
public void Dispatch(id id, Stream s) {
((Handler)this[id])(id,s);
}
}

Register all the handlers for different ID's:

Handlers.Global.Register(new Handler((new HandlerForSomeId()).f));
...

Read input, something like:

while ( true ) {
int id = ReadIdFromStream(s);
Handlers.Global.Dispatch(id, s);
}

Now, the handler, can read from the stream and place and objects
constructed from the stream in any queue's relevant to processing.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #5
Hi,

I've had to do this sort of thing with OOP in the past. In general, I've
used a class for each message type, and had a heirarchy of classes where
there is a genuine heirarchy in the messaage types...

eg

class NavigationalMsg

abstract class TrackMsg

class AirTrackMsg: TrackMsg

class SurfaceTrackMsg: TrackMsg

class StationaryTrackMsg: TrackMsg

Where to decode the raw messages is an interesting problem. Here are some
ideas...

1. The constructor for each class takes raw data (either binary, or ASCII),
and parses it,
2. The constructor for each class takes the decoded values (eg. position,
color, ...), and a separate parser parses the incoming stream, and constructs
the appropriate message classes,
3. The Factory design pattern, as suggested by Senthil. I have heard of this
being used for this problem, but haven't tried it myself.

You might be surprised how much of an overhead is involved in creating a
class for each message type - it is considerably more work than just using
the inline decoding of each message which you described. C# helps by having
Properties. This extra work is only useful if you find that you are using
these message objects throughout your program. If you are just using them
once, to decode the input stream, then it is probably not worthwhile. On the
other hand, if this is your first Object Oriented design, I would recommend
using the message classes, just for the experience, and to see how it
compares with what you are used to.

Regards,

Javaman

"Brian" wrote:
Hi,

I come from a C background. I am trying to receive messages over a serial
port. The message data structure format is along the lines of

int message_id;
int message_length;
int data[message_length];

There will be different messages received with different IDs and variable
lengths.

If I were to write this in C I would use something like:-
switch (message_id){
case 1:
decode_message();
add_to_msg_processing_queue();
break;
...
}

There will also be a transmit routine to write too.

Now I don't feel that this is very good object oriented approach. Could
anyone suggest a better way to handle this type of message structure in C#?

Thanks

Nov 17 '05 #6
Hi,

I've had to do this sort of thing with OOP in the past. In general, I've
used a class for each message type, and had a heirarchy of classes where
there is a genuine heirarchy in the messaage types...

eg

class NavigationalMsg

abstract class TrackMsg

class AirTrackMsg: TrackMsg

class SurfaceTrackMsg: TrackMsg

class StationaryTrackMsg: TrackMsg

Where to decode the raw messages is an interesting problem. Here are some
ideas...

1. The constructor for each class takes raw data (either binary, or ASCII),
and parses it,
2. The constructor for each class takes the decoded values (eg. position,
color, ...), and a separate parser parses the incoming stream, and constructs
the appropriate message classes,
3. The Factory design pattern, as suggested by Senthil. I have heard of this
being used for this problem, but haven't tried it myself.

You might be surprised how much of an overhead is involved in creating a
class for each message type - it is considerably more work than just using
the inline decoding of each message which you described. C# helps by having
Properties. This extra work is only useful if you find that you are using
these message objects throughout your program. If you are just using them
once, to decode the input stream, then it is probably not worthwhile. On the
other hand, if this is your first Object Oriented design, I would recommend
using the message classes, just for the experience, and to see how it
compares with what you are used to.

Regards,

Javaman

"Brian" wrote:
Hi,

I come from a C background. I am trying to receive messages over a serial
port. The message data structure format is along the lines of

int message_id;
int message_length;
int data[message_length];

There will be different messages received with different IDs and variable
lengths.

If I were to write this in C I would use something like:-
switch (message_id){
case 1:
decode_message();
add_to_msg_processing_queue();
break;
...
}

There will also be a transmit routine to write too.

Now I don't feel that this is very good object oriented approach. Could
anyone suggest a better way to handle this type of message structure in C#?

Thanks

Nov 17 '05 #7
B
There would be two main usages of data coming in.

1. One would be sent to a buffer for processing at a later stage i.e.
displaying of plot data.

2. The other would be dealt with immediately and would cause the GUI to be
updated with values, i.e. text files updated, status bar update, icons
changed etc. This type of message would also update and internal store of the
current data values.

So them messages do have a lifetime but it is limited.

I am not sure if I would want to create an object for every type. I am not
sure of the benefit.

Javaman59 in you suggestion I would pass the raw data into the contructor
which would then parse this raw data. To parse the data do I still not come
to the same situation of a switch (message_type).

I suppose I want to minimise code change when a new message type has to be
added.

Also in your abstract class TrackMsg would that consist of the common fields
and methods? Something like

TrackMsg
{
private int Message_Type;
private int Message_Length;

public GetMessageType();
public GetMessage_Length();

}

Thanks,
B

"Brian" wrote:
Hi,

I come from a C background. I am trying to receive messages over a serial
port. The message data structure format is along the lines of

int message_id;
int message_length;
int data[message_length];

There will be different messages received with different IDs and variable
lengths.

If I were to write this in C I would use something like:-
switch (message_id){
case 1:
decode_message();
add_to_msg_processing_queue();
break;
...
}

There will also be a transmit routine to write too.

Now I don't feel that this is very good object oriented approach. Could
anyone suggest a better way to handle this type of message structure in C#?

Thanks

Nov 17 '05 #8
Hello B,

Without knowing your application, I am cautious about giving specific
advice, but I will call it as I see it from here...

The use of a class for each message type is an overhead, but it is worth it
if the messages have some sort of lifetime, are used in a few different
places, and, if each message type has unique data. (If the different message
types have only a small amount of unique data, then you can make one just
class for messages, which aggregates all the message data). It looks to me as
if your application satisfies all of those conditions. One of the advantages
of using a class for each message type is that after you create them
initially (a day or two's work), they are very stable, and don't need much
maintenance. Also, it is easy to add new message types.
Javaman59 in you suggestion I would pass the raw data into the contructor
which would then parse this raw data. To parse the data do I still not come
to the same situation of a switch (message_type).
That is correct. Your top level message handler must parse enough of the
message to determine the message type, and make a switch to determine which
constructor to call, and then pass the rest of the raw message to the
constructor. (I believe that that the Factory pattern presents an alternative
to this, but I haven't used it yet).

I suppose I want to minimise code change when a new message type has to be
added.
You will achieve that with this object oriented design.

Also in your abstract class TrackMsg would that consist of the common fields
and methods? Something like

TrackMsg
{
private int Message_Type;
private int Message_Length;

public GetMessageType();
public GetMessage_Length();

}


Very good - you are on the way to mastering OO!! Just a couple of comments
on your code. 1. I recommend that you use C# capitalization conventions (see
my example below). 2. Your "Get" methods (which are called "accessors") are
correct, but C# gives a better approach, called "properties", which is just a
simpler syntax for accessors. C# makes it just a bit easier to create a class
for each message. 3. Use an "enum" for the message kinds, rather than an int,
because in C# enums help with strong typing (whereas they don't add much in
C++).

Here's how I'd code this class, with correct capitalization, and with
properties...

using System;

// Use an enum for the message kinds
// Pascal case (ie. leading upper), for the enum, and no underscores.
enum MessageKind
{
SurfaceTrack,
AirTrack
}

// Pascal case (ie. leading upper), for the class
class TrackMsg
{
// camel case (ie. leading lower case), for class members
MessageKind messageKind;
int length;

// The "get" accessor, as a property.
public MessageKind MessageKind
{
get { return messageKind; }
}

public int Length
{
get { return length; }
}
}

Good luck with it!

Javaman

Nov 17 '05 #9

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

Similar topics

6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
2
by: Krimgelas | last post by:
Hello, I have been programming in PHP for a while, but always the classic function oriented approach. I would like to learn how to program in PHP with an object oriented approach, but so far...
2
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only...
0
by: Brian | last post by:
Hi, I come from a C background. I am trying to receive messages over a serial port. The message data structure format is along the lines of int message_id; int message_length; int data; ...
73
by: Water Cooler v2 | last post by:
I am new to PHP, just one day new. But I am otherwise a seasoned programmer on many other languages like C, VB 6, C#, VB.NET, Win32 platform and have some tiny bit experience in MFC, C++, Python. ...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
3
by: notnorwegian | last post by:
i have some confusion over this. sure a class is basically a classification, like for example an animal or flower. and an object/instance of that class is then for example a cat. an object is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.