KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > proxies > tcp > TcpWriter


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - 2004 ScalAgent Distributed Technologies
4  * Copyright (C) 2004 - France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): ScalAgent Distributed Technologies
22  * Contributor(s):
23  */

24 package org.objectweb.joram.mom.proxies.tcp;
25
26 import java.io.*;
27 import java.net.*;
28
29 import org.objectweb.joram.mom.proxies.*;
30 import org.objectweb.joram.shared.client.MomExceptionReply;
31
32 import fr.dyade.aaa.util.*;
33
34 import org.objectweb.joram.shared.JoramTracing;
35 import org.objectweb.util.monolog.api.BasicLevel;
36
37 /**
38  * The activity responsible for getting the replies
39  * from the user's proxy and writing them to the
40  * socket.
41  */

42 public class TcpWriter extends Daemon {
43   
44   /**
45    * The TCP connection that started
46    * this writer.
47    */

48   private TcpConnection tcpConnection;
49
50   private IOControl ioctrl;
51
52   private AckedQueue replyQueue;
53
54   /**
55    * Creates a new writer.
56    *
57    * @param sock the socket where to write
58    * @param userConnection the connection
59    * with the user's proxy
60    * @param tcpConnection the TCP connection
61    */

62   public TcpWriter(IOControl ioctrl,
63            AckedQueue replyQueue,
64                    TcpConnection tcpConnection)
65     throws IOException {
66     super("tcpWriter");
67     this.ioctrl = ioctrl;
68     this.replyQueue = replyQueue;
69     this.tcpConnection = tcpConnection;
70     replyQueue.reset();
71   }
72
73   public void run() {
74     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
75       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "TcpWriter.run()");
76     try {
77       while (running) {
78         ProxyMessage msg =
79           (ProxyMessage)replyQueue.get();
80         if ((msg.getObject() instanceof MomExceptionReply) &&
81             (((MomExceptionReply) msg.getObject()).getType() == MomExceptionReply.HBCloseConnection)) {
82           // Exception indicating that the connection
83
// has been closed by the heart beat task.
84
// (see UserAgent)
85
new Thread JavaDoc(new Runnable JavaDoc() {
86               public void run() {
87                 tcpConnection.close();
88               }
89             }).start();
90         } else {
91           ioctrl.send(msg);
92           // No queue.pop() !
93
// Done by the proxy (UserAgent)
94
}
95       }
96     } catch (Exception JavaDoc exc) {
97       if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
98         JoramTracing.dbgProxy.log(
99           BasicLevel.DEBUG, "", exc);
100     }
101   }
102
103   protected void shutdown() {
104     close();
105   }
106     
107   protected void close() {
108     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
109       JoramTracing.dbgProxy.log(
110         BasicLevel.DEBUG,
111         "TcpWriter.close()", new Exception JavaDoc());
112     if (ioctrl != null)
113       ioctrl.close();
114     ioctrl = null;
115   }
116 }
117
Popular Tags