Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

OO network protocol

Thread view: 
VisionSet - 13 Nov 2005 23:26 GMT
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!?

--
Mike W
Andrew McDonagh - 13 Nov 2005 23:35 GMT
> 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
[quoted text clipped - 16 lines]
> --
> Mike W

Take a look at the various Behavioral design patterns.

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

Andy
Benji - 13 Nov 2005 23:39 GMT
> The usual example for a Socket based protocol is an enumeration of int
> commands and a huge switch statement.

> Yuk!

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)

Signature

Of making better designs there is no end,
 and much refactoring wearies the body.

Roedy Green - 14 Nov 2005 01:40 GMT
>The usual example for a Socket based protocol is an enumeration of int
>commands and a huge switch statement.
[quoted text clipped - 4 lines]
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Patrick May - 14 Nov 2005 09:36 GMT
> The usual example for a Socket based protocol is an enumeration of
> int commands and a huge switch statement.
[quoted text clipped - 4 lines]
> loading, instantiating and executing its single method, which
> accepts and returns an Object.

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

Regards,

Patrick

------------------------------------------------------------------------
S P Engineering, Inc.    | The experts in large scale distributed OO
                        | systems design and implementation.
         pjm@spe.com    | (C++, Java, Common Lisp, Jini, CORBA, UML)
Chris Uppal - 14 Nov 2005 12:37 GMT
> 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
VisionSet - 14 Nov 2005 19:38 GMT
> 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.

--
Mike W


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.