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

Design Question

Hi

My application reads writes/messages from a socket. It contains inbound
and outbound std::queue<Message*> queues.

I have a base Message and derived classes eg LoginMessage (outbound),
LoginReplyMessage (inbound).

My question is when creating an inbound message from the raw data which
of the following approaches is better?

- a: Directly create object of Message type, add onto the inbound
message queue . Then when using it later create a new LoginReplyMessage
from the message data.

- b: Pass the buffer to a 'factory' method which returns a message of
exact type, eg LoginReplyMessage, using a add onto inbound message queue
(upcasting). When processing downcast to appropriate type and use.

Thanks for reading.

Chris
Jul 22 '05 #1
3 1874
"Chris" <ch***@tuxweb.org> wrote...
My application reads writes/messages from a socket. It contains inbound
and outbound std::queue<Message*> queues.

I have a base Message and derived classes eg LoginMessage (outbound),
LoginReplyMessage (inbound).

My question is when creating an inbound message from the raw data which
of the following approaches is better?

- a: Directly create object of Message type, add onto the inbound
message queue . Then when using it later create a new LoginReplyMessage
from the message data.

- b: Pass the buffer to a 'factory' method which returns a message of
exact type, eg LoginReplyMessage, using a add onto inbound message queue
(upcasting). When processing downcast to appropriate type and use.


I don't understand, probably. So, each 'Message' already contains
all the necessary information to either be a LoginMessage or
a LoginReplyMessage, since you can create either of the latter from
it, right? Then why have LoginMessage and LoginReplyMessage at all?

If you can have 'Message' object distinguish which one of two derived
types it is, there is no sense to have derived types. Therefore your
problem doesn't exist.
Jul 22 '05 #2
Victor Bazarov wrote:
"Chris" <ch***@tuxweb.org> wrote...
My application reads writes/messages from a socket. It contains inbound
and outbound std::queue<Message*> queues.

I have a base Message and derived classes eg LoginMessage (outbound),
LoginReplyMessage (inbound).

My question is when creating an inbound message from the raw data which
of the following approaches is better?

- a: Directly create object of Message type, add onto the inbound
message queue . Then when using it later create a new LoginReplyMessage
from the message data.

- b: Pass the buffer to a 'factory' method which returns a message of
exact type, eg LoginReplyMessage, using a add onto inbound message queue
(upcasting). When processing downcast to appropriate type and use.

I don't understand, probably. So, each 'Message' already contains
all the necessary information to either be a LoginMessage or
a LoginReplyMessage, since you can create either of the latter from
it, right? Then why have LoginMessage and LoginReplyMessage at all?

If you can have 'Message' object distinguish which one of two derived
types it is, there is no sense to have derived types. Therefore your
problem doesn't exist.


Each Message contains the information needed to send over the Socket, ie
a std::vector<char> and various functions for manipulating the bits.

Each more specific Message, eg LoginMessage will contain more
properties, eg login_failed_reason.

// sending is fine
LoginMsg l(username, password)
conn.send(l.getPacket());

// MY question is when receiving

// ** 2 Different Alternatives Below
// 1
Message msg = conn.reveive()
int type = msg.getType();

switch(type) {
case LOGIN:
// create login message?
}

// 2
std::vector<char> byteBuffer = conn.recieve();

LoginMessage msg = MsgFactory::CreateBuildMessage(byteBuffer);

Hope that makes it slightly clearer :)
Thanks for the help.
Jul 22 '05 #3
Chris wrote:
// MY question is when receiving // ** 2 Different Alternatives Below
// 1
Message msg = conn.reveive()
int type = msg.getType(); switch(type) {
case LOGIN:
// create login message?
} // 2
std::vector<char> byteBuffer = conn.recieve(); LoginMessage msg = MsgFactory::CreateBuildMessage(byteBuffer);


The problem with (2) is that you are assuming the message is a LoginMessage. (I
guess this is on the server, otherwise the client would do the same for
LoginMessageReply.) I'm not sure why this is guaranteed. For example, (1) does
not attempt to identify the message as a particular type.

From my perspective, (2) is nicer, but should be expressed as

Message *msg = MsgFactory::CreateBuildMessage(buf);

Then, you can use polymorphism to get the correct behavior out of msg; e.g.,

if(msg->error()){
std::cerr << "error: " << msg->tostring() << std::endl;
return 1;
}

yielding

error: username/password not recognized

OTOH, you may need to cast msg to an appropriate type depending on your server
state machine in order to get certain values. For example, if you are in the
"login request" state, you may need

l->username()
l->password()

In this case, I think you can use dynamic_cast<LoginMessage*>() to check whether
the pointer you get back from MsgFactory::CreateBuildMessage() has the right type.

LoginMsg *l = dynamic_cast<LoginMsg*>(MsgFactory::CreateBuildMes sage(buf);

if(l == 0){
// error
}

Another way to do this is to check

msg->type() == MSG_LOGIN_REQ

WLOG regarding your type definitions. This doesn't make much use of the type
system, but the information must already be there in order for the factory to work.

A simpler scheme might be to use specific message poninters to directly access
message data (rather than wrap messages in accessor classes), and simply retain
the Message type for encapsulating generic message operations [error(),
tostring(), packetize(), etc]. This forces you to think carefully about the
structure of the protocol messages since they are all related now by the
placement and encoding of certain fields (e.g., "type" is the first byte,
"error" is the second byte, error strings are ASCII payloads following the error
byte). In any case, this might look like

Message *msg = MsgFactory::CreateBuildMessage(buf);

if(msg->type() == MSG_LOGIN){
LoginMsg *l = msg->payload();

if(login(l->username, l->password) == false){
// error
}
// ...
}

HTH,

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 22 '05 #4

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

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
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...
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)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.