KickJava   Java API By Example, From Geeks To Geeks.

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


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

22 package org.objectweb.joram.mom.proxies.tcp;
23
24 import org.objectweb.joram.mom.proxies.*;
25
26 import java.net.*;
27 import java.io.*;
28
29 import org.objectweb.joram.shared.JoramTracing;
30
31 import org.objectweb.util.monolog.api.BasicLevel;
32
33 import fr.dyade.aaa.agent.AgentId;
34
35 /**
36  * Handles the TCP connection. Starts the
37  * reader and writer threads responsible for
38  * reading the requests and writing the replies.
39  * Calls the <code>UserConnection</code> in order
40  * to invoke the user's proxy and get its replies.
41  *
42  * @see TcpProxyService
43  * @see TcpConnectionListener
44  */

45 public class TcpConnection {
46
47   private IOControl ioctrl;
48
49   private AgentId proxyId;
50
51   private int key;
52
53   private AckedQueue replyQueue;
54
55   /**
56    * The reader thread responsible for
57    * reading the requests (input).
58    */

59   private TcpReader tcpReader;
60
61   /**
62    * The writer thread responsible for
63    * writing the replies (output).
64    */

65   private TcpWriter tcpWriter;
66
67   /**
68    * The TCP proxy service used to
69    * register and unregister this connection.
70    */

71   private TcpProxyService proxyService;
72
73   private boolean closeConnection;
74
75   /**
76    * Creates a new TCP connection.
77    *
78    * @param sock the TCP connection socket
79    * @param proxyService the TCP proxy service
80    */

81   public TcpConnection(
82     IOControl ioctrl,
83     AgentId proxyId,
84     AckedQueue replyQueue,
85     int key,
86     TcpProxyService proxyService,
87     boolean closeConnection) {
88     this.ioctrl = ioctrl;
89     this.proxyId = proxyId;
90     this.replyQueue = replyQueue;
91     this.key = key;
92     this.proxyService = proxyService;
93     this.closeConnection = closeConnection;
94   }
95
96   public final AgentId getProxyId() {
97     return proxyId;
98   }
99
100   public final int getKey() {
101     return key;
102   }
103
104   /**
105    * Starts the connection reader and writer threads.
106    */

107   void start() throws Exception JavaDoc {
108     if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG))
109       JoramTracing.dbgProxy.log(
110         BasicLevel.DEBUG,
111         "TcpConnection.start()");
112     try {
113       tcpWriter = new TcpWriter(
114     ioctrl,
115         replyQueue,
116         this);
117       tcpReader = new TcpReader(
118         ioctrl,
119     proxyId,
120         this,
121         closeConnection);
122       proxyService.registerConnection(this);
123       tcpWriter.start();
124       tcpReader.start();
125     } catch (Exception JavaDoc exc) {
126       close();
127       throw exc;
128     }
129   }
130
131   /**
132    * Stops the connection reader and
133    * writer threads.
134    * Closes the socket.
135    */

136   void close() {
137     if (tcpWriter != null)
138       tcpWriter.stop();
139     if (tcpReader != null)
140       tcpReader.stop();
141     if (ioctrl != null)
142       ioctrl.close();
143     ioctrl = null;
144     proxyService.unregisterConnection(
145       TcpConnection.this);
146   }
147
148 }
149
Popular Tags