You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Mark <el...@gmail.com> on 2006/09/18 22:18:53 UTC

synchronous responses to client

I am working on a FTP server using MINA, and it appears that the
responses that the FTP server are sending back to the client are
asynchronous.  Based on my understanding of the protocol, the
responses need to be synchronous.  So the client sends a command, and
the server sends a response.
Is there a way to guarantee client-server data synchronization using
MINA?  I have tried subclassing both the StreamIoHandler and
IoHandlerAdapter classes and neither have worked.

Thank you.

RE: synchronous responses to client

Posted by "Kok Hoor (Gmail)" <ko...@gmail.com>.
Hi Mark,

Yes, you're right. If you look at the description for the join method in the
javadocs, it is:

    join - Wait for the asynchronous operation to end.

Regards,
    Kok Hoor

-----Original Message-----
From: Mark [mailto:elihusmails@gmail.com] 
Sent: Thursday, October 12, 2006 9:18 PM
To: mina-dev@directory.apache.org
Subject: Re: synchronous responses to client

I think I may have figured this out, the javadocs are a little confusing.
In WriteFuture the javadocs say:

IoSession session = ...;
 WriteFuture future = session.write(...);  // Wait until the message is
completely written out to the O/S buffer.
 future.join();

I thought the "Wait until the message is completely written out to the O/S
buffer." meant I have to put in some sort of locking/callback/etc in order
to get everything to happen at the right time.  I appears that the join
method actually will wait until everything is written.

Am I right here?

On 10/12/06, Mark <el...@gmail.com> wrote:
> I am finally getting the chance to get back into this.  The only way I 
> see this working is to use the java.util.concurrent.locks.Lock class.
> Here is what I have so far...
>
> public class MyHandler extends IoHandlerAdapter implements 
> IoFutureListener { .....
> private Lock writeLock = new ReentrantLock(); private Condition locked 
> = writeLock.newCondition();
>
> public void operationComplete(IoFuture arg0) {
>         locked.signal();
> }
>
> private void writeData( IoSession session, String msg ) {
>        writeLock.tryLock();
>         WriteFuture future = session.write( msg );
>         future.addListener( this );
>
>         try {
>                 System.out.println("Awaiting lock condition release...");
>                 locked.await();
>                 System.out.println("Lock condition released..");
>         } catch (InterruptedException e) {
>                 e.printStackTrace();
>         }
>
>         writeLock.unlock();
> }
> }
>
> Is there some other way of doing this, because I am running into 
> problems here?  I am stumped...
>
> Thank you.
>
>
> On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
> > True! :)
> >
> > One thing though: if I use a listener like you suggest I think it 
> > will be called from within the SocketIoProcessor's worker thread, 
> > right? If that's the case it's very important not to do any heavy 
> > processing in such a callback otherwise that SocketIoProcessor would 
> > be completely blocked.
> >
> > Please correct me if I'm wrong.
> >
> > /Niklas
> >
> > Trustin Lee wrote:
> > > On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
> > >>
> > >> Instead of blocking on the WriteFuture you may want to implement 
> > >> IoHandler.messageSent(). For every call to session.write() there 
> > >> will be a matching call to messageSent() once the message you 
> > >> wrote has been sent. This avoids blocking the current thread and 
> > >> should make things more scalable.
> > >
> > >
> > > Or you can use IoFutureListener by adding a listener to the 
> > > WriteFuture.  :)
> > >
> > > Trustin
> >
> >
>


Re: synchronous responses to client

Posted by Mark <el...@gmail.com>.
I just read the comment at being another step that needs to be
written.  I later realized that the comment was describing the call to
join().

I am reading through the Wiki now.  I have a great interest in the
project and want to start contributing to it.  Probably documentation
at first...

Thank you.

On 10/12/06, Trustin Lee <tr...@gmail.com> wrote:
> On 10/12/06, Mark <el...@gmail.com> wrote:
> >
> > I thought the "Wait until the message is completely written out to the
> > O/S buffer." meant I have to put in some sort of locking/callback/etc
> > in order to get everything to happen at the right time.
>
>
> What sentence would you add to clarify our documentation?
>
> Thanks for the feedback,
> Trustin
> --
> what we call human nature is actually human habit
> --
> http://gleamynode.net/
> --
> PGP key fingerprints:
> * E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
> * B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6
>
>

Re: synchronous responses to client

Posted by Trustin Lee <tr...@gmail.com>.
On 10/12/06, Mark <el...@gmail.com> wrote:
>
> I thought the "Wait until the message is completely written out to the
> O/S buffer." meant I have to put in some sort of locking/callback/etc
> in order to get everything to happen at the right time.


What sentence would you add to clarify our documentation?

Thanks for the feedback,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6

Re: synchronous responses to client

Posted by Mark <el...@gmail.com>.
I think I may have figured this out, the javadocs are a little
confusing.  In WriteFuture the javadocs say:

IoSession session = ...;
 WriteFuture future = session.write(...);
 // Wait until the message is completely written out to the O/S buffer.
 future.join();

I thought the "Wait until the message is completely written out to the
O/S buffer." meant I have to put in some sort of locking/callback/etc
in order to get everything to happen at the right time.  I appears
that the join method actually will wait until everything is written.

Am I right here?

On 10/12/06, Mark <el...@gmail.com> wrote:
> I am finally getting the chance to get back into this.  The only way I
> see this working is to use the java.util.concurrent.locks.Lock class.
> Here is what I have so far...
>
> public class MyHandler extends IoHandlerAdapter implements IoFutureListener {
> .....
> private Lock writeLock = new ReentrantLock();
> private Condition locked = writeLock.newCondition();
>
> public void operationComplete(IoFuture arg0) {
>         locked.signal();
> }
>
> private void writeData( IoSession session, String msg )
> {
>        writeLock.tryLock();
>         WriteFuture future = session.write( msg );
>         future.addListener( this );
>
>         try {
>                 System.out.println("Awaiting lock condition release...");
>                 locked.await();
>                 System.out.println("Lock condition released..");
>         } catch (InterruptedException e) {
>                 e.printStackTrace();
>         }
>
>         writeLock.unlock();
> }
> }
>
> Is there some other way of doing this, because I am running into
> problems here?  I am stumped...
>
> Thank you.
>
>
> On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
> > True! :)
> >
> > One thing though: if I use a listener like you suggest I think it will
> > be called from within the SocketIoProcessor's worker thread, right? If
> > that's the case it's very important not to do any heavy processing in
> > such a callback otherwise that SocketIoProcessor would be completely
> > blocked.
> >
> > Please correct me if I'm wrong.
> >
> > /Niklas
> >
> > Trustin Lee wrote:
> > > On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
> > >>
> > >> Instead of blocking on the WriteFuture you may want to implement
> > >> IoHandler.messageSent(). For every call to session.write() there will be
> > >> a matching call to messageSent() once the message you wrote has been
> > >> sent. This avoids blocking the current thread and should make things
> > >> more scalable.
> > >
> > >
> > > Or you can use IoFutureListener by adding a listener to the
> > > WriteFuture.  :)
> > >
> > > Trustin
> >
> >
>

Re: synchronous responses to client

Posted by Mark <el...@gmail.com>.
I am finally getting the chance to get back into this.  The only way I
see this working is to use the java.util.concurrent.locks.Lock class.
Here is what I have so far...

public class MyHandler extends IoHandlerAdapter implements IoFutureListener {
.....
private Lock writeLock = new ReentrantLock();
private Condition locked = writeLock.newCondition();

public void operationComplete(IoFuture arg0) {
	locked.signal();
}

private void writeData( IoSession session, String msg )
{
       writeLock.tryLock();
	WriteFuture future = session.write( msg );
	future.addListener( this );
			
	try {
		System.out.println("Awaiting lock condition release...");
		locked.await();
		System.out.println("Lock condition released..");
	} catch (InterruptedException e) {
		e.printStackTrace();
	}

	writeLock.unlock();
}
}

Is there some other way of doing this, because I am running into
problems here?  I am stumped...

Thank you.


On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
> True! :)
>
> One thing though: if I use a listener like you suggest I think it will
> be called from within the SocketIoProcessor's worker thread, right? If
> that's the case it's very important not to do any heavy processing in
> such a callback otherwise that SocketIoProcessor would be completely
> blocked.
>
> Please correct me if I'm wrong.
>
> /Niklas
>
> Trustin Lee wrote:
> > On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
> >>
> >> Instead of blocking on the WriteFuture you may want to implement
> >> IoHandler.messageSent(). For every call to session.write() there will be
> >> a matching call to messageSent() once the message you wrote has been
> >> sent. This avoids blocking the current thread and should make things
> >> more scalable.
> >
> >
> > Or you can use IoFutureListener by adding a listener to the
> > WriteFuture.  :)
> >
> > Trustin
>
>

Re: synchronous responses to client

Posted by Niklas Therning <ni...@trillian.se>.
True! :)

One thing though: if I use a listener like you suggest I think it will 
be called from within the SocketIoProcessor's worker thread, right? If 
that's the case it's very important not to do any heavy processing in 
such a callback otherwise that SocketIoProcessor would be completely 
blocked.

Please correct me if I'm wrong.

/Niklas

Trustin Lee wrote:
> On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
>>
>> Instead of blocking on the WriteFuture you may want to implement
>> IoHandler.messageSent(). For every call to session.write() there will be
>> a matching call to messageSent() once the message you wrote has been
>> sent. This avoids blocking the current thread and should make things
>> more scalable.
> 
> 
> Or you can use IoFutureListener by adding a listener to the 
> WriteFuture.  :)
> 
> Trustin


Re: synchronous responses to client

Posted by Trustin Lee <tr...@gmail.com>.
On 9/19/06, Niklas Therning <ni...@trillian.se> wrote:
>
> Instead of blocking on the WriteFuture you may want to implement
> IoHandler.messageSent(). For every call to session.write() there will be
> a matching call to messageSent() once the message you wrote has been
> sent. This avoids blocking the current thread and should make things
> more scalable.


Or you can use IoFutureListener by adding a listener to the WriteFuture.  :)

Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP key fingerprints:
* E167 E6AF E73A CBCE EE41  4A29 544D DE48 FE95 4E7E
* B693 628E 6047 4F8F CFA4  455E 1C62 A7DC 0255 ECA6

Re: synchronous responses to client

Posted by Niklas Therning <ni...@trillian.se>.
Instead of blocking on the WriteFuture you may want to implement 
IoHandler.messageSent(). For every call to session.write() there will be 
a matching call to messageSent() once the message you wrote has been 
sent. This avoids blocking the current thread and should make things 
more scalable.

/Niklas

Mark wrote:
> thanks.  RTFM strikes again!! :)
> 
> On 9/19/06, Greg Duffy <gd...@gmail.com> wrote:
>> There is definitely a queue, as the IoSession.write(...) method is
>> asynchronous. Quoth the javadoc,
>>
>> http://directory.apache.org/subprojects/mina/apidocs/org/apache/mina/common/IoSession.html#write(java.lang.Object) 
>>
>>
>> As you can see from that link, you may wait for the message to be
>> written by calling join on the WriteFuture returned by
>> IoSession.write(..). It will block until MINA writes the message out.
>>
>> HTH.
>> -Greg
>>
>> On 9/19/06, Mark <el...@gmail.com> wrote:
>> > when I look at the FTP traffic, it appears as though the responses
>> > from my FTP Server do no match up with what the client is sending.
>> > When MINA sends data to a client using IoSession.write(), does that
>> > data get sent immediately, or is it queued in some fashion?
>> >
>> > Thank you.
>> >
>> >
>> > On 9/18/06, Vinod Panicker <vi...@gmail.com> wrote:
>> > > On 9/19/06, Mark <el...@gmail.com> wrote:
>> > > > I am working on a FTP server using MINA, and it appears that the
>> > > > responses that the FTP server are sending back to the client are
>> > > > asynchronous.  Based on my understanding of the protocol, the
>> > > > responses need to be synchronous.  So the client sends a 
>> command, and
>> > > > the server sends a response.
>> > > > Is there a way to guarantee client-server data synchronization 
>> using
>> > > > MINA?  I have tried subclassing both the StreamIoHandler and
>> > > > IoHandlerAdapter classes and neither have worked.
>> > >
>> > > Pardon me, but I don't quite understand. If the server is yours
>> > > (developed by you), I assume that you are responsible for sending the
>> > > responses to the client.
>> > >
>> > > Regards,
>> > > Vinod.
>> > >
>> >
>>


Re: synchronous responses to client

Posted by Mark <el...@gmail.com>.
thanks.  RTFM strikes again!! :)

On 9/19/06, Greg Duffy <gd...@gmail.com> wrote:
> There is definitely a queue, as the IoSession.write(...) method is
> asynchronous. Quoth the javadoc,
>
> http://directory.apache.org/subprojects/mina/apidocs/org/apache/mina/common/IoSession.html#write(java.lang.Object)
>
> As you can see from that link, you may wait for the message to be
> written by calling join on the WriteFuture returned by
> IoSession.write(..). It will block until MINA writes the message out.
>
> HTH.
> -Greg
>
> On 9/19/06, Mark <el...@gmail.com> wrote:
> > when I look at the FTP traffic, it appears as though the responses
> > from my FTP Server do no match up with what the client is sending.
> > When MINA sends data to a client using IoSession.write(), does that
> > data get sent immediately, or is it queued in some fashion?
> >
> > Thank you.
> >
> >
> > On 9/18/06, Vinod Panicker <vi...@gmail.com> wrote:
> > > On 9/19/06, Mark <el...@gmail.com> wrote:
> > > > I am working on a FTP server using MINA, and it appears that the
> > > > responses that the FTP server are sending back to the client are
> > > > asynchronous.  Based on my understanding of the protocol, the
> > > > responses need to be synchronous.  So the client sends a command, and
> > > > the server sends a response.
> > > > Is there a way to guarantee client-server data synchronization using
> > > > MINA?  I have tried subclassing both the StreamIoHandler and
> > > > IoHandlerAdapter classes and neither have worked.
> > >
> > > Pardon me, but I don't quite understand. If the server is yours
> > > (developed by you), I assume that you are responsible for sending the
> > > responses to the client.
> > >
> > > Regards,
> > > Vinod.
> > >
> >
>

Re: synchronous responses to client

Posted by Greg Duffy <gd...@gmail.com>.
There is definitely a queue, as the IoSession.write(...) method is
asynchronous. Quoth the javadoc,

http://directory.apache.org/subprojects/mina/apidocs/org/apache/mina/common/IoSession.html#write(java.lang.Object)

As you can see from that link, you may wait for the message to be
written by calling join on the WriteFuture returned by
IoSession.write(..). It will block until MINA writes the message out.

HTH.
-Greg

On 9/19/06, Mark <el...@gmail.com> wrote:
> when I look at the FTP traffic, it appears as though the responses
> from my FTP Server do no match up with what the client is sending.
> When MINA sends data to a client using IoSession.write(), does that
> data get sent immediately, or is it queued in some fashion?
>
> Thank you.
>
>
> On 9/18/06, Vinod Panicker <vi...@gmail.com> wrote:
> > On 9/19/06, Mark <el...@gmail.com> wrote:
> > > I am working on a FTP server using MINA, and it appears that the
> > > responses that the FTP server are sending back to the client are
> > > asynchronous.  Based on my understanding of the protocol, the
> > > responses need to be synchronous.  So the client sends a command, and
> > > the server sends a response.
> > > Is there a way to guarantee client-server data synchronization using
> > > MINA?  I have tried subclassing both the StreamIoHandler and
> > > IoHandlerAdapter classes and neither have worked.
> >
> > Pardon me, but I don't quite understand. If the server is yours
> > (developed by you), I assume that you are responsible for sending the
> > responses to the client.
> >
> > Regards,
> > Vinod.
> >
>

Re: synchronous responses to client

Posted by Mark <el...@gmail.com>.
when I look at the FTP traffic, it appears as though the responses
from my FTP Server do no match up with what the client is sending.
When MINA sends data to a client using IoSession.write(), does that
data get sent immediately, or is it queued in some fashion?

Thank you.


On 9/18/06, Vinod Panicker <vi...@gmail.com> wrote:
> On 9/19/06, Mark <el...@gmail.com> wrote:
> > I am working on a FTP server using MINA, and it appears that the
> > responses that the FTP server are sending back to the client are
> > asynchronous.  Based on my understanding of the protocol, the
> > responses need to be synchronous.  So the client sends a command, and
> > the server sends a response.
> > Is there a way to guarantee client-server data synchronization using
> > MINA?  I have tried subclassing both the StreamIoHandler and
> > IoHandlerAdapter classes and neither have worked.
>
> Pardon me, but I don't quite understand. If the server is yours
> (developed by you), I assume that you are responsible for sending the
> responses to the client.
>
> Regards,
> Vinod.
>

Re: synchronous responses to client

Posted by Vinod Panicker <vi...@gmail.com>.
On 9/19/06, Mark <el...@gmail.com> wrote:
> I am working on a FTP server using MINA, and it appears that the
> responses that the FTP server are sending back to the client are
> asynchronous.  Based on my understanding of the protocol, the
> responses need to be synchronous.  So the client sends a command, and
> the server sends a response.
> Is there a way to guarantee client-server data synchronization using
> MINA?  I have tried subclassing both the StreamIoHandler and
> IoHandlerAdapter classes and neither have worked.

Pardon me, but I don't quite understand. If the server is yours
(developed by you), I assume that you are responsible for sending the
responses to the client.

Regards,
Vinod.