Return-Path: X-Original-To: apmail-mina-users-archive@www.apache.org Delivered-To: apmail-mina-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 447F8EE69 for ; Sat, 16 Feb 2013 15:16:25 +0000 (UTC) Received: (qmail 13034 invoked by uid 500); 16 Feb 2013 15:16:25 -0000 Delivered-To: apmail-mina-users-archive@mina.apache.org Received: (qmail 12814 invoked by uid 500); 16 Feb 2013 15:16:23 -0000 Mailing-List: contact users-help@mina.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@mina.apache.org Delivered-To: mailing list users@mina.apache.org Received: (qmail 12787 invoked by uid 99); 16 Feb 2013 15:16:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 16 Feb 2013 15:16:22 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of elecharny@gmail.com designates 74.125.82.170 as permitted sender) Received: from [74.125.82.170] (HELO mail-we0-f170.google.com) (74.125.82.170) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 16 Feb 2013 15:16:14 +0000 Received: by mail-we0-f170.google.com with SMTP id z53so3609022wey.15 for ; Sat, 16 Feb 2013 07:15:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:reply-to:user-agent:mime-version:to :subject:references:in-reply-to:x-enigmail-version:content-type :content-transfer-encoding; bh=oM5VggwE4+CME6oTN9bQuET2huTkIu8Zsxrjh19eAK0=; b=AycDw4beB27lPChd0QK4tM4G1N+sRiMw9gMnfre2+Xzz5rOYObWzqTxpq6vKyotplX S3IDA5DvPWsI8GbOUJp8Z5hQiItXpEFH8+33J2Fw9vM4N07bSt8r4EVd95tlVksfx0y+ VNXMG6dUX2pY17wzE6zrovYHtmm7vCbAI+khOCk0ZWhdaRcqVDYvJE4p6Oo1sqTujqqA HIFngD1c7pCC9ODG/2JT3R3CIYM0P4Ma4XMxMVC0CiF2H1h3OcB+LbrFqL8HOc3DlmvR 1TdgqhlcOCCfjq+Y1/bqU03xqszy2kJ6jqnuMdra2QqmtEIZVsnl+GhKMh3V4reqn+PD qThA== X-Received: by 10.180.100.169 with SMTP id ez9mr11718433wib.3.1361027753764; Sat, 16 Feb 2013 07:15:53 -0800 (PST) Received: from Emmanuels-MacBook-Pro.local (ran75-1-78-192-106-184.fbxo.proxad.net. [78.192.106.184]) by mx.google.com with ESMTPS id fx5sm11255771wib.11.2013.02.16.07.15.52 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 16 Feb 2013 07:15:52 -0800 (PST) Message-ID: <511FA2A8.8030009@gmail.com> Date: Sat, 16 Feb 2013 16:15:52 +0100 From: =?UTF-8?B?RW1tYW51ZWwgTMOpY2hhcm55?= Reply-To: elecharny@apache.org User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: "users@mina.apache.org" Subject: Re: Controlling lock operation inside the session.write() method References: <511EBE7D.6010504@gmail.com> In-Reply-To: X-Enigmail-Version: 1.5 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Virus-Checked: Checked by ClamAV on apache.org Le 2/16/13 2:30 PM, Ricardo Cristian Ramirez a écrit : > On Saturday, February 16, 2013 12:02:21 AM Emmanuel Lécharny wrote: > >> What exactly do you want to do ? Is this interrupting some sending if a >> message is received from the server ? > I am trying to send a message only once to the server but it seems > impossible > in the scope of non-blocking communication and my enable/disable conditions. You have to define a stateful protocol to handle such conditional communications. In other words, and this is totally orthogonal to the blocking or non blocking IO, your client must know if it can send a message, and the server must tell the client about this possibility. One possibility is to have a client blocked untl it receives an ACK from the server. Here is a possible protocol : Initial state ------------- Client : can send Server : can receive Normal state ------------ (1) client : send message, switch to 'wait for ACK' (cannot send an other message until this ACK is received) ... (some delay) ... (2) server : receive the message, send a ACK to the client ... (some delay) ... (3) client : receive the server's ACK, switch to 'can send a message' And now, you can iterate to (1) > > Assume receiver thread takes the processor from sender thread before > sending > is not completed. And, receiver takes a disable message: > > Checking the client state before write() method may cause a missing > message: > Client writes message to the session. Receiver thread takes the processor. > Receiver takes a disable message. When, the sender has the processor again, > it > sends the message but server does not take into account a client's message > after disabling it. However, client thinks that the message has been sent. > if(state==sending_enabled): > session.write(...) The message can be considered as sent by the client when the 'MessageSent' event is fired. This event means the data has been fully flushed into the socket. Now, checking that is totally irrelevant in your scenario : you *can't* - I mean, you really can't - know if the server has received the message, unless the server has sent you an aknowledge. For that, the server has to receive the message and send a response (see the protocol above). Eveything else (the threads reading or wraiting , whatsoever) is just irrelevant. Just consider that when you ave session.write() the message from the client side, then the client has to wait for a response from the server, and it has to be a message, not something like a messageSent event. This has to be present in your high level protocol, whatever the communication layer you are using (TCP, UDP, etc). That also mean that your server must send a message when it has received a full message from the client. This is really simple. > > Checking the client state after write() method may cause duplication: > Client > writes message to the session and waits for the completion. After > completion, > receiver thread takes the processor between two if statement below. > Receiver > takes a disable message. Then, sender thread takes the processor again. > Checks > the state and resends the messages for second time. > FutureWriter fWriter = session.write(...) > fWriter.join() > if(fWriter.isWritten()): > if(state==sending_disabled): > mark this message as not send. try to send again when sending > enabled You are trying to do some post-mortem control. It does not work, period. Do it the other way : send a message when you know you can : - either when the client connects for the first time - or when you have actually received the server's response In any other case, you should be in a non-transmitting state. If you never receive a response from the server, then you should consider either that the conneciton has been brutally closed from the server side, or that the server is buggy. In any case, close the session and inform the user. > > - o - > > On the other hand, interrupting the sending queue after taking a disable > message may cause a loss too. Consider the following code: > messageReceived(message): > if(message==sending_disabled): > list = session.getSendingQueue() > session.cancelSendingQueue() > addToResendList(list) > > Receiver takes a disable message and tries to cancel sending queue. Before > canceling (calling cancelSendingQueue()), sender thread gains the processor > and sends the message. Now, client thinks the message has been sent but > server > has just ignored it. Message is lost. One more thing : if your server may send a 'sending_disable' message at any time, then you have no way to guarantee that this message will be processed corectly on the client side. At best, if the server send you such a message then it should consider that any message it will receive from the client is invalid (just because such a message might have been sent before the server sent its 'disable-sending' message). Or you can consider that the server will accept the client message, assuming that the client has sent it *before* it recived the 'disable-sending' message. That's also a legitimate choice. But again, this is something you should design in your protocol, and the way MINA works is really totally orthogonal. Note that using Blocking IO does not solve any of the issue you have : just because you have written some data in a socket does not mean the server has received those data. In fact, you can *at the same time* have the client and the server sending some data to each other (this is a full duplex communication). The only way to solve this issue is to remap some high level protocol on top of the low mevel protocol to manage such situations. Hope it helps. -- Regards, Cordialement, Emmanuel Lécharny www.iktek.com