KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 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.IOException JavaDoc;
27
28 import org.objectweb.joram.mom.proxies.CloseConnectionNot;
29 import org.objectweb.joram.mom.proxies.ConnectionManager;
30 import org.objectweb.joram.mom.proxies.FlowControl;
31 import org.objectweb.joram.mom.proxies.MultiCnxSync;
32 import org.objectweb.joram.mom.proxies.ProxyMessage;
33 import org.objectweb.joram.mom.proxies.RequestNot;
34 import org.objectweb.joram.shared.client.AbstractJmsRequest;
35 import org.objectweb.joram.shared.client.ProducerMessages;
36
37 import org.objectweb.joram.shared.JoramTracing;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 import fr.dyade.aaa.util.Daemon;
41 import fr.dyade.aaa.agent.AgentId;
42 import fr.dyade.aaa.agent.Channel;
43
44 /**
45  * The activity responsible for reading the requests from the socket and invoke
46  * the user's proxy.
47  */

48 public class TcpReader extends Daemon {
49
50   /**
51    * The TCP connection that started this reader.
52    */

53   private TcpConnection tcpConnection;
54
55   private IOControl ioctrl;
56
57   private AgentId proxyId;
58
59   private boolean closeConnection;
60
61   /**
62    * Creates a new reader.
63    *
64    * @param sock
65    * the socket to read
66    * @param userConnection
67    * the connection with the user's proxy
68    * @param tcpConnection
69    * the TCP connection
70    */

71   public TcpReader(IOControl ioctrl, AgentId proxyId,
72       TcpConnection tcpConnection, boolean closeConnection) throws IOException JavaDoc {
73     super("tcpReader");
74     this.ioctrl = ioctrl;
75     this.proxyId = proxyId;
76     this.tcpConnection = tcpConnection;
77     this.closeConnection = closeConnection;
78   }
79
80   public void run() {
81     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
82       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "TcpReader.run()");
83     try {
84       while (running) {
85         ProxyMessage msg = ioctrl.receive();
86         canStop = false;
87         if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
88           JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "TcpReader reads msg: "
89               + msg);
90         ConnectionManager.sendToProxy(
91             proxyId,
92             tcpConnection.getKey(),
93             (AbstractJmsRequest)msg.getObject(),
94             msg);
95         canStop = true;
96       }
97     } catch (Throwable JavaDoc error) {
98       canStop = false;
99       if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
100         JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "", error);
101     } finally {
102       canStop = false;
103       if (closeConnection) {
104         Channel.sendTo(proxyId, new CloseConnectionNot(tcpConnection.getKey()));
105       }
106       new Thread JavaDoc(new Runnable JavaDoc() {
107         public void run() {
108           tcpConnection.close();
109         }
110       }).start();
111
112     }
113   }
114
115   protected void shutdown() {
116     close();
117   }
118
119   protected void close() {
120     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
121       JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "TcpReader.close()");
122     if (ioctrl != null)
123       ioctrl.close();
124     ioctrl = null;
125   }
126 }
127
Popular Tags