1

I'm working on a test application which requires connection to server and save the response from it. The response actually is a data packet,so I need to understand what type of packet it is and to execute it.For now I can receive the response, calculate the data packet and now I need a little help with switch method where I want to get the packet type.

Here is the code I'm using :

                    RPCPacket packet=null;
                    switch(RPCPacketType.getPacketTypeByValue(pType)){
                        case ST_OBJECT_TYPE_INFO_START:
                        {
                            packet = new InfoStartRPCPacket(    objectIdentificator,
                                                                RPCPacketType.getPacketTypeByValue(pType),
                                                                RPCOperationType.getByValue(Integer.parseInt(operation)),
                                                                objectId,
                                                                id,
                                                                Integer.parseInt(size),
                                                                hash,
                                                                RPCPacketDataType.getByValue(dataType),
                                                                first );
                            break;
                        }
                        case ST_OBJECT_TYPE_INFO_END:{
                            packet = new InfoEndRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_INFO_ERROR:{
                            packet = new InfoErrorRPCPacket();

                            break;
                        }
                            // Basic packets
                        case ST_OBJECT_TYPE_COLLECTION:{
                            packet = new CollectionRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CATEGORY:{
                            packet = new CategoryRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CARD:{
                            packet = new CardRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MESSAGE:{
                            packet = new MessageRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_GENRE:{
                            packet = new GenreRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_TAG:{
                            packet = new TagRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_USER:{
                            packet = new UserRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CARDSTATS_CATEGORY:{
                            packet = new CardStatsCategoryRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_CONTENT:{
                            packet = new ContentRPCPacket();

                            break;
                        }
                            // Media packets
                        case ST_OBJECT_TYPE_MEDIA_COLLECTION:{
                            packet = new MediaCollectionRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_CATEGORY:{
                            packet = new MediaCategoryRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_CARD:{
                            packet = new MediaCardRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_TAG:{
                            packet = new MediaTagRPCPacket();

                            break;
                        }
                        case ST_OBJECT_TYPE_MEDIA_COLLECTION_BUTTON:{
                            packet = new MediaCollectionButtonRPCPacket();

                            break;
                        }
                            // unknown packet
                        default: {
                            packet=null;

                            break;
                        }
                    }

I need a way to be able to initialize the packet as InfoStartRPCPacket or something different after I check the all switches.Basically I want to be able to do something like this outside switch :

packet.executeInfoStartPacket(/*params*/);  , when executeInfoStartPacket is a method in InfoStartRPCPacket class.

Any suggestions how to do that?

3
  • I don't really understand your question. Haven't you already implemented the switch statement? Commented Sep 8, 2011 at 10:37
  • Yes.I mean, when I run the switch statement and packetType is for example InfoStart I want to be able to set the global packet to InfoStart and execute it as calling executeInfoStart() method from it's own class. Did you understand it? Commented Sep 8, 2011 at 10:39
  • Please don't post tons of code when only a small part of it is relevant to your question! Commented Sep 8, 2011 at 10:43

1 Answer 1

1

You can't really do that and you shouldn't.

Instead you could have an abstract method in your RPCPacket class called process() (possibly giving any necessary context as parameters) and overload that in each specific sub-class.

This way the packet-specific code automatically has access to all the packet-specific fields and methods.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually If I create an Interface and all of my child classes extends RPCPacket and implements Interface,is this will be a solution.And create method process() in Interface?Will this solve my problem?
@Android-Droid: this won't help unless RPCPacket implements this interface as well or you cast to that interface (hoping that all RPCPacket implementations really implement it). But why should you? What's wrong with adding that method to that class? I see no advantage in adding another interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.