• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a good way to do this?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is rather complicated and a bit hard to explain I hope you understand. If not you can ask me
for clarification. (ooh and thank you for trying to read my question because it's a bit long :s)

I'm trying to find a good way to do something in java but I'm not sure if there is a proper way of doing.
There is a big change it isn't That would make me a bit sad but I hope some experienced people here
can figure it out please :p

So what I'm doing is creating a network protocol. It would word by transmitting messages
first a message ID is send (1 byte) and then all the fields are send (just by using a DataOuputStream)
So for example a LoginRequestMessage would have ID 0 and 2 fields a String for the username
and a int for the protocol version.
Now there would be quite a lot of these messages so I would create an abstract class Message
give it an abstract read() method and an abstract write() method (maybe some other once). Then every Message would
extend that base class.
So I want a better method. I want be be able to write at text file like this


0:LoginRequestMessage
{
String:Username
int:ProtocolVersion
}

1:HeartbeatMessage{
...
...
...


Well the point is that a text file defines all the message like this
message id:message name{
fields
}
where fields is a list of "java type:field name"

Then I want to be able to do the following in my code:
Create 1 class called Message be able to create one of these messages by either giving it the id of the message
or the message name.
then just be able to call message.getUsername() or if not possible message.get("Username") (for the protocol version this would become message.getProtocolVersion() or message.get("ProtocolVersion"))
So I basically want to be able to have a getter and setter method
for every field in the message and get the correct java type returned from that, not just Object. And with java type I mean every possible java primitive or Object so also objects from classes I define elsewhere in my code.

Oooh yeah performance is also a bit of an issue because I don't want to sarifice to much of it by not writing every message class.

aaaaaaaahhhhhhh. If java.lang.invoke.MethodHandle was generic most of my problem would be solved. But it just returns Object aaaaaaaaaaaaaaaargh :p
 
reply
    Bookmark Topic Watch Topic
  • New Topic