OO network protocol

V

VisionSet

The usual example for a Socket based protocol is an enumeration of int
commands and a huge switch statement.

Yuk!

I'm thinking of a String command that is the name of a class, then loading,
instantiating and executing its single method, which accepts and returns an
Object.

All these methods will want to interact with some global attributes, so I'm
thinking have them as inner classes.

Not sure of the exact symantics but I'm guessing

Object response = Class.forName(OUTER_CLASS + "." +
command).newInstance().execute(commandArg);

[Casts removed for brevity.]

How's that sound?

Any advice wrt this ie improvements!?
 
A

Andrew McDonagh

VisionSet said:
The usual example for a Socket based protocol is an enumeration of int
commands and a huge switch statement.

Yuk!
:)

Yep


I'm thinking of a String command that is the name of a class, then loading,
instantiating and executing its single method, which accepts and returns an
Object.

All these methods will want to interact with some global attributes, so I'm
thinking have them as inner classes.

Not sure of the exact symantics but I'm guessing

Object response = Class.forName(OUTER_CLASS + "." +
command).newInstance().execute(commandArg);

[Casts removed for brevity.]

How's that sound?

Any advice wrt this ie improvements!?

Take a look at the various Behavioral design patterns.

The State and Strategy patterns are useful for this kind of problem.


Andy
 
B

Benji

VisionSet said:
The usual example for a Socket based protocol is an enumeration of int
commands and a huge switch statement.

Well...your number one bad thing that comes from this is that you won't be
able to use any other language but java to implement this protocol. The
other bad thing is that you now have to be careful about letting an attacker
execute arbitrary code on your machine.

My suggestion if you wanted to make this nicer would be to have a hashmap
of "command" -> handler. That way you can have nicer execution without
introducing nastiness related to allowing user input (from the network!)
to direct what code to execute. (Also, it would still be easy to translate
into c code, or something else)
 
R

Roedy Green

The usual example for a Socket based protocol is an enumeration of int
commands and a huge switch statement.

Yuk!

I'm thinking of a String command that is the name of a class, then loading,
instantiating and executing its single method, which accepts and returns an
Object.

The overhead is likely many times higher. check it out.

Another approach is a hashMap of delegate objects each indexed by key
and implementing an interface that does the socket command.

Another approach is to use an enum. You can then use valueOf to
convert from string to enum, then invoke a method common to all enum
constants.

I would be interested to know the relative speed of the four
approaches.
 
P

Patrick May

VisionSet said:
The usual example for a Socket based protocol is an enumeration of
int commands and a huge switch statement.

Yuk!

I'm thinking of a String command that is the name of a class, then
loading, instantiating and executing its single method, which
accepts and returns an Object.

http://www.spe.com/pjm/message-broker

Regards,

Patrick
 
C

Chris Uppal

VisionSet said:
The usual example for a Socket based protocol is an enumeration of int
commands and a huge switch statement.

Yuk!

So use the integer as an index into an array of handler objects. Or if you
want to use human-readable command names in the protocol itself (which you
might, but that should determined by the best design for the protocol, not by
the nature of the receiving application) then use Benji's suggestion.

At some point you have to cross the threshold between from the world of linear
data on-the-wire (or in-the-file) and the world of objects. /How/ you do that
is not particularly important (usually -- though there may be efficiency
issues); the point is to do it as soon as possible (within reason).

-- chris
 
V

VisionSet

So use the integer as an index into an array of handler objects. Or if you
want to use human-readable command names in the protocol itself (which you
might, but that should determined by the best design for the protocol, not by
the nature of the receiving application) then use Benji's suggestion.

Yes that does seem a more rational approach, and you'd think more obvious!
;-/
At some point you have to cross the threshold between from the world of linear
data on-the-wire (or in-the-file) and the world of objects. /How/ you do that
is not particularly important (usually -- though there may be efficiency
issues); the point is to do it as soon as possible (within reason).

mmm, well RMI does it beautifully, just that it doesn't suit all situations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top