Design query on defining new events on top of an existing set of events for a socket protocol

StackOverflow https://stackoverflow.com/questions/6317684

  •  26-10-2019
  •  | 
  •  

Question

I wrote a java server and client program using JBoss Netty. In order to send some data to the remote client and receive data back from them, I have defined events and handlers for each event. On the wire, each event is just a single byte(opcode) header followed by the message bytes. Initially I had only supported TCP and had defined events like LOG_IN,LOG_OUT,DATA_IN,DATA_OUT etc in my program. For e.g

public static final int LOG_IN = 0x08;
public static final int LOG_OUT = 0x0a;

Then I decided to support UDP also and ended up having events like LOGIN_UDP, LOGIN_TCP, DATA_OUT_TCP or DATA_OUT_UDP etc so that based on the event generated the correct event handler would get the event and write it to the appropriate socket and remote port.

As you can see the first issue I am facing is that I have almost doubled the number of defined events and event handlers on adding UDP. Is there a better way to approach this scenario?

The second(minor) issue I am facing is that events like DATA_OUT make sense when you are writing from server to client, but when receiving the same event at the client side "DATA_OUT" does not make such sense, since it is actually incoming data for the client. For the moment, I have a decoder which will translate DATA_OUT to DATA_IN. Is this the best approach?

Was it helpful?

Solution

You can use factory pattern to create connection on the basis of the type channel i.e. TCP or UDP. Other details will you have to define once in this case

Instead Calling DATA_OUT you can call it as SERVER_OUT same way SERVER_IN

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top