Question

I'm making a basic outline for a server i plan to use in the future. an engine if you will.

Basically, i'm making a client/server set of classes that will be extracted into a jar that i will use later by including them in the class path much like you would with lwjgl.

In my server class, it needs to make a new class object when a client connects to it. example:

Thread t = new Thread(new MainServerThread(clientSocket));

now, originally, this MainServerThread() was made by me, and implemented Runnable. Now, because of how this needs to be, the MainServerThread() type class needs to be made by the programmer, outside of the engine.

My question is, if i make an interface that a programmer can implement, and run that,

  1. can the interface also make the client implement runnable? like, could the interface implement runnable and then pass that on?
  2. can a class that implements my interface, lets say the class is called test, could i have my server use that class as a parameter? (i'm bad at wording my questions, so here is an example)

SERVER ENGINE (the one i'm making):

public class Server{
      public Server(ServerThread st){ //serverthread is the name of my interface
           st.haha();
      }
}

CLIENT CLASS USING MY SERVER:

public class test implements ServerThread{ //all the methods are imported
    public test(){
        new Server(this);
    }

    public void haha(){
        System.out.println("hi");
    }
}

now why didnt i just go out and test this? i did, and it didnt work for me...

So how would I accomplish something to this nature?

EDIT: Thanks for the quick answers guys! I'm trying to figure out now why it wasnt working to begin with... But i'll accept an answer as soon as i can!

Was it helpful?

Solution

1) can the interface also make the client implement runnable? like, could the interface implement runnable and then pass that on?

Yep. You can make an interface inherit the properties of another interface.

public interface IClient extends Runnable

2) can a class that implements my interface, lets say the class is called test, could i have my server use that class as a parameter? (i'm bad at wording my questions, so here is an example)

Yes you could, and the code you provided should work.

But what I think you want to do is..

Remember, the important thing is that you keep the model in mind. That model is the client-server model. That means, the server acts as the switch for all of the clients, but it's the client that requests the connection from the server. It goes something like this.

public class Client implements IClient
{
    public void haha()
    {
         // Prints a value to the server.
         getConnection().writeline("hi");
    }

    public BufferedWriter getConnection()
    {
         // Here you request a connection from the server.
         String ip = "10.20.50.60"; // Example.

         // Get the connection via your own classes.
         BufferedWriter out = new BufferedWriter(getOutputStream(ip));

         return out;

    }

    private OutputStream getOutputStream(String ip)
    {
        // Logic to get the socket stream pointing to the server.
    }
}

And the server will have a method to recieve this data, and decide what to do with it, and do that, it needs to have a slave listening on the stream your client is writing to. The slave listens, obtains data and passes onto the logic. The logic decides what to do, and passes onto a writing class that outputs to the appropriate stream.

OTHER TIPS

1) You can make your interface extend the runnable interface:

public interface MyInterface extends Runnable { ... }

2) Yes you can do new Server(this) as you did in your example.

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