0

Currently I try to implement a Server/Client - Application and came up with a problem, I cant find a solution for.

Im running a Server, which is waiting for Clients to login, adding them to a cachedThreadPool and starting a new Runnable for each Client, to handle their individual requests etc.

Now there are some answers to these requests, which have to be broadcasted after the server did his processing.

        this.mOos = new ObjectOutputStream(this.mSocket.getOutputStream());
        this.mOos.flush();
        this.mOis = new ObjectInputStream(this.mSocket.getInputStream());

        while(true)
        {
            Object o = this.mOis.readObject();

            if(o !=null && this.mInputList.size() < 20)
            {
                this.mInputList.add(o);
            }

            if(!this.mInputList.isEmpty())
            {
                handleIncomingObject();
            }

            if(!this.mOutputList.isEmpty())
            {                       
                try 
                {
                    this.mOos.writeObject(this.mOutputList.remove(0));
                    this.mOos.flush();
                    this.mOos.reset();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }               
        }

Now what I'm having trouble with is, that when I process a request in:

handleIncomingObject();

which needs to be broadcasted.

My approach is to give a reference to the ExecutorService Object, when I create the runnable, to have access to the threadpool IN the different threads.

As you may see, every client got his own

LinkedList<Object> mInputList
LinkedList<Object> mOutputList

to buffer incoming and outgoing messages.

In handleIncomingObject() I'd like to do something like:

//assume exec as ExecutorService
for(Thread t : exec)
{
        mOutputList.add(new Message(message));
}

Thanks in advance any every help or suggestions.

Greetings, Ben.

Aleksandr Podkutin
  • 2,532
  • 1
  • 20
  • 31
Ben Win
  • 830
  • 8
  • 21

1 Answers1

1

Use a Vector containing all clients and iterate over this Vector to send broadcast messages. You can use a method like:

public static synchronized void broadcast(Message msg) {
    for(Client c : vector) {
        c.mOutputList.add(msg);
    }
}
kaetzacoatl
  • 1,419
  • 1
  • 19
  • 27
  • Tyvm for this suggestion, even though i'm angry about myself when i see how simple this approach is ;-) – Ben Win Jun 15 '14 at 14:00