OO network protocol

Discussion in 'Java' started by VisionSet, Nov 13, 2005.

  1. VisionSet

    VisionSet Guest

    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
     
    VisionSet, Nov 13, 2005
    #1
    1. Advertising

  2. VisionSet wrote:
    > 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!?
    >
    > --
    > Mike W
    >
    >


    Take a look at the various Behavioral design patterns.

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


    Andy
     
    Andrew McDonagh, Nov 13, 2005
    #2
    1. Advertising

  3. VisionSet

    Benji Guest

    VisionSet, while chewing on bamboo shoots, wrote:
    > 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)

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
     
    Benji, Nov 13, 2005
    #3
  4. VisionSet

    Roedy Green Guest

    On Sun, 13 Nov 2005 23:26:19 GMT, "VisionSet" <>
    wrote, quoted or indirectly quoted someone who 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.


    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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 14, 2005
    #4
  5. VisionSet

    Patrick May Guest

    "VisionSet" <> writes:
    > 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

    ------------------------------------------------------------------------
    S P Engineering, Inc. | The experts in large scale distributed OO
    | systems design and implementation.
    | (C++, Java, Common Lisp, Jini, CORBA, UML)
     
    Patrick May, Nov 14, 2005
    #5
  6. VisionSet

    Chris Uppal Guest

    VisionSet wrote:

    > 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
     
    Chris Uppal, Nov 14, 2005
    #6
  7. VisionSet

    VisionSet Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:437886d2$0$38045$...

    > 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
     
    VisionSet, Nov 14, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Mimic
    Replies:
    5
    Views:
    433
    John Harrison
    May 7, 2004
  2. DaveC

    Network Protocol?

    DaveC, Jul 9, 2004, in forum: C++
    Replies:
    14
    Views:
    704
    Phlip
    Jul 10, 2004
  3. Laszlo Zsolt Nagy

    Developing a network protocol with Python

    Laszlo Zsolt Nagy, Dec 12, 2005, in forum: Python
    Replies:
    12
    Views:
    1,133
    Irmen de Jong
    Dec 15, 2005
  4. Erik
    Replies:
    7
    Views:
    624
    Joshua Cranmer
    Mar 27, 2008
  5. Cooch

    Python as network protocol

    Cooch, Nov 10, 2009, in forum: Python
    Replies:
    16
    Views:
    454
Loading...

Share This Page