Hi Everybody,

Thanks for your kind consideration.


I have just started using mina to implement server & client for a pub-sub
protocol (MQTT) over TCP/IP.  This protocol already defines the PINGREQ &
PINGRESP packets to implement heart-beat. Both of these packets are 2 bytes
messages. Client does use long-standing connection and after any problem,
try to reconnect after 30 seconds.


My problem is as follows

1. Client is able to send other bigger packets(30+ bytes) correctly.

2. But it is not able to send PINGREQ, a 2-byte message.

3. PINGREQ was delivered to the server when client had to send another
packet (30+ byte messages).

4. I put ProtocolEncoderOutput.flush() after each write, but it does not
work.


Feel free to shout but I feel that the problem is because of optimization of
the number of TCP packets. This is probably mentioned in
http://mina.apache.org/tutorial-on-protocolcodecfilter-for-mina-2x.htmlalso.
I tried searching the forums but this issue was not mentioned.


My question is “how to force a write of 2-bytes message on a TCP socket and
work around this optimization”. Any help or idea is more than welcome…


Following is information about my environment, overall design of heart-beat
& code snippet of ResponseEncoder.


////////////////////////////////////////////////////////////////////

// My client and server environment are same

Ubuntu 9.10 (32 bit), Sun JVM 1.6.0_18 (32 bit), mina-2.0.0-RC1.


////////////////////////////////////////////////////////////////////

//Design for heart-beat (15 sec) is as following

Server:

1. Server maintains last communication time - System.nanotime()- for each
session.

2. Server closes the connection where last communication time is older than
twice of heart-beat-duration.

3. Server repeats this after each heart-beat cycle.


Client:

1. Client need to send some data on each heart-beat cycle.

2. If no data is sent for a heart-beat cycle, client sends PINGREQ, a 2-byte
message.

3. Client repeats this after each heart-beat cycle.


////////////////////////////////////////////////////////////////////

// Client side ResponseEncoder looks like this


public class ClientResponseEncoder extends ProtocolEncoderAdapter {

    public void encode(IoSession session, Object message,
ProtocolEncoderOutput out) throws Exception {

       //Raw bytes construction

        ………….

        ………….

        IoBuffer buffer = IoBuffer.allocate(capacity, false);

        if (null != rawBytes) {

            for (int i = 0; i < rawBytes.length; i++) {

                buffer.put(rawBytes[i]);

            }

        }

        buffer.flip();

        out.write(buffer);

        out.flush(); //This I added on my own. Program does not work
irrespective of this line.

    }

}


Many thanks for reading such a long mail.



Thanks & Regards,

Ash

Reply via email to