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.