I have crated an interface like this
public interface NetworkInterface {
public void onReceive();
}
And I have two classes implementing this interface like this..
public class NetworkManager implements NetworkInterface {
private NetworkQueue mNetworkQueue;
private TCPConnection mTCPConnection;
private Worker mWorker;
public NetworkManager(){
mNetworkQueue = new NetworkQueue();
mTCPConnection = new TCPConnection();
mWorker = new Worker();
}
@Override
public void onReceive() {
// TODO Auto-generated method stub
}
}
public class TCPConnection implements NetworkInterface{
private final static String IP = "1.1.1.1";
private final static String PORT = "12001";
private Socket mSocket;
private DataOutputStream mOut;
private InputStream mIn;
public TCPConnection() throws NumberFormatException, UnknownHostException, IOException{
mSocket = new Socket(InetAddress.getByName(IP),Integer.valueOf(PORT));
mOut = new DataOutputStream(mSocket.getOutputStream());
mIn = mSocket.getInputStream();
}
@Override
public void onReceive() {
// TODO Auto-generated method stub
}
}
So TCPConnection is going to import NetworkManager and in the onReceive function of TCPConnection, it will call NetworkManager’s onReceive function. In onReceive function of NetworkManager will add anything passing as a parameter to the work queue.
I was wondering if this is the right way to approach the call back function.
Thanks in advance.
You need to understand how TCP networking actually works before you start designing frameworks for it. I suggest you read the Custom Networking trail of the Java Tutorial.