Re: TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread Emmanuel Lécharny
Le 11/11/13 7:01 PM, jonas85 a écrit :
> Ohh I see! Thank you very much! I think I can make something out of this. The
> solution smells a bit of recursion then, I guess..

Not exactly recursion, by asynchronous, and event driven, yes, for sure.

The thing is that you have to keep some context in the session
attributes in order for your code to work. Your code logic will look
likes something like :

messageReceived() :
process the message
prepare the responses
session.write(first response)
store the responses in the session context

messageSent() :
get the next response to send from the session context
sent it


and depends on MINA to call you as many times as you have responses to send.

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com 



Re: TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread jonas85
Ohh I see! Thank you very much! I think I can make something out of this. The
solution smells a bit of recursion then, I guess..



--
View this message in context: 
http://apache-mina.10907.n7.nabble.com/TcpNoDelay-for-NioSocketAcceptor-not-working-tp40327p40333.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.


Re: TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread jonas85
Okay, I just put a System.out.println() in the messageSent() function (didn't
know about this one until now) to see when the messages really get sent.
None of them were sent while the program was still in the loop. 



--
View this message in context: 
http://apache-mina.10907.n7.nabble.com/TcpNoDelay-for-NioSocketAcceptor-not-working-tp40327p40331.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.


Re: TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread Emmanuel Lécharny
Le 11/11/13 6:35 PM, jonas85 a écrit :
> Hello Emmanuel,
>
> thanks for the response! I am going to try something out with the
> messageSent() function in a bit to see if that fixes it. But I wonder if
> your assumption is right, 
You bet it is ! The thread which writes data into the socket is the very
same that the one which is used by your loop, so unless you quit the
loop, there is no way for the messages to be sent.

The way it works is that it will enqueue all the messages, an when done,
it will now try to get the messages from the queue and write them into
the socket.

Btw, I was wrong assuming that using the future could help : you wll
just block forever. The only solution is to use the messageSent()
event.-- Regards, Cordialement, Emmanuel Lécharny www.iktek.com


Re: TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread jonas85
Hello Emmanuel,

thanks for the response! I am going to try something out with the
messageSent() function in a bit to see if that fixes it. But I wonder if
your assumption is right, because not even the first message is getting sent
immediately. I believe this should happen, if the thread in charge of
sending was doing anything at all?

Instead none of them messages seem to get sent until the loop is over. 



--
View this message in context: 
http://apache-mina.10907.n7.nabble.com/TcpNoDelay-for-NioSocketAcceptor-not-working-tp40327p40330.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.


Re: TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread Emmanuel Lécharny
Le 11/11/13 4:26 PM, jonas85 a écrit :
> Hello,
>
> I think I have a problem that's related to the TcpNoDelay option. I'm
> writing a server, which, in one function, runs through a loop, gets a string
> in each iteration and sends it to the client, basically like the following:
>
> String tempStr;
> IoSession session; 
> for(int i = 0; i < 10; i++)
> {
> tempStr = getResultString(i);
> session.write(tempStr); 
> }
>
> My problem is that the server seems to wait first and does nothing, then
> sends all strings at the same time. I'm currently initializing the server
> like this:
>
> SocketAcceptor acceptor = new NioSocketAcceptor();
> acceptor.getSessionConfig().setTcpNoDelay(true);
>
> Unfortunately, this doesn't change anything. And now I'm clueless and need
> your help!

That's quite simple : the session.write() is not blocking, so your code
is just pushing messages into a queue as fast as it can (ie, way faster
than the thread in charge of sending those messages to your client can do).

If you want to be sure that the server send a new message only when the
previous message has been written into the socket, then do a
session.write() when you receive the messageSent() event for the
previous message.

You can also get the writeFuture return by the session.write() call and
wait for it to be completed before sending a new message, but that would
block a thread for quite a long time.

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com 



TcpNoDelay for NioSocketAcceptor not working?

2013-11-11 Thread jonas85
Hello,

I think I have a problem that's related to the TcpNoDelay option. I'm
writing a server, which, in one function, runs through a loop, gets a string
in each iteration and sends it to the client, basically like the following:

String tempStr;
IoSession session; 
for(int i = 0; i < 10; i++)
{
tempStr = getResultString(i);
session.write(tempStr); 
}

My problem is that the server seems to wait first and does nothing, then
sends all strings at the same time. I'm currently initializing the server
like this:

SocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.getSessionConfig().setTcpNoDelay(true);

Unfortunately, this doesn't change anything. And now I'm clueless and need
your help!

Thanks



--
View this message in context: 
http://apache-mina.10907.n7.nabble.com/TcpNoDelay-for-NioSocketAcceptor-not-working-tp40327.html
Sent from the Apache MINA User Forum mailing list archive at Nabble.com.