MinaSender.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.transport.network.mina;
22 
23 import org.apache.mina.common.ByteBuffer;
24 import org.apache.mina.common.CloseFuture;
25 import org.apache.mina.common.IoSession;
26 import org.apache.mina.common.WriteFuture;
27 import org.apache.qpid.transport.Sender;
28 import org.apache.qpid.transport.TransportException;
29 
30 
31 /**
32  * MinaSender
33  */
34 
35 public class MinaSender implements Sender<java.nio.ByteBuffer>
36 {
37     private static final int TIMEOUT = 60 1000;
38 
39     private final IoSession session;
40     private WriteFuture lastWrite = null;
41 
42     public MinaSender(IoSession session)
43     {
44         this.session = session;
45     }
46 
47     public void send(java.nio.ByteBuffer buf)
48     {
49         if (session.isClosing())
50         {
51             throw new TransportException("attempted to write to a closed socket");
52         }
53 
54         synchronized (this)
55         {
56             lastWrite = session.write(ByteBuffer.wrap(buf));
57         }
58     }
59 
60     public void flush()
61     {
62         // pass
63     }
64 
65     public synchronized void close()
66     {
67         // MINA will sometimes throw away in-progress writes when you
68         // ask it to close
69         synchronized (this)
70         {
71             if (lastWrite != null)
72             {
73                 lastWrite.join();
74             }
75         }
76         CloseFuture closed = session.close();
77         closed.join();
78     }
79     
80     public void setIdleTimeout(long l)
81     {
82       //noop
83     }
84     
85     public long getIdleTimeout()
86     {
87         return 0;
88     }
89     
90 }