KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > http > ClientConnection


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose, Sebastian Mueller.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.http;
22
23 import java.net.*;
24 import java.util.*;
25 import java.io.*;
26
27 import org.jacorb.util.*;
28 import org.jacorb.orb.*;
29 import org.jacorb.orb.giop.*;
30
31 /**
32  * This class tunnels a GIOP request in HTTP.
33  * @author Sebastian Mueller
34  * @version $Id: ClientConnection.java,v 1.14 2004/05/06 12:40:00 nicolas Exp $
35  */

36
37 public final class ClientConnection
38     extends org.jacorb.orb.giop.ClientConnection
39 {
40     static int counter = 0;
41     int mycounter = counter++;
42     boolean connected = true;
43     Object JavaDoc notifier;
44
45     //HTTP
46
HTTPClient.HTTPResponse rsp;
47     String JavaDoc host=null;
48     int port;
49     
50     private ORB orb = null;
51
52     public ClientConnection( String JavaDoc _host,
53                              int _port,
54                              org.jacorb.orb.factory.SocketFactory factory,
55                              ORB orb )
56     {
57         this.orb = orb;
58         host=_host;
59         port=_port;
60         connection_info=host+":"+port;
61         
62         socket_factory = factory;
63        
64     }
65     protected void abort()
66     throws java.io.EOFException JavaDoc
67     {
68     
69     org.jacorb.util.Debug.output(3,"HTTPClient Connection "+mycounter+" to " +
70                                  connection_info + " aborting...->Ex");
71     
72     Enumeration keys = replies.keys();
73     int lost_replies = 0;
74
75     while(keys.hasMoreElements())
76     {
77         ReplyInputStream client =
78         (ReplyInputStream)replies.get(keys.nextElement());
79         client.cancel( false );
80         lost_replies++;
81     }
82
83     replies.clear();
84     buffers.clear();
85
86     if( lost_replies > 0 )
87         org.jacorb.util.Debug.output(2,"Lost " + lost_replies + " outstanding replies");
88     
89     throw new java.io.EOFException JavaDoc();
90     }
91
92     public synchronized void closeConnection()
93     {
94         connected=false;
95     }
96
97     public boolean connected()
98     {
99         // return connected;
100
return true;
101     }
102     public Hashtable get_buffers(){
103         return buffers;
104     }
105    
106     public Hashtable get_replies(){
107         return replies;
108     }
109
110
111     /* readBuffer is only called by receiveReply
112      */

113     public synchronized byte[] readBuffer()
114     throws IOException
115     {
116     
117         while(repReceptor==null) {
118             try {
119             Thread.sleep(100); //make sure repReceptor is there
120
}catch(InterruptedException JavaDoc iex){}
121         }
122         
123         
124     synchronized(notifier){
125             while(rsp==null){
126                 try{
127                     notifier.wait();
128                 }catch(InterruptedException JavaDoc iex){}
129             }
130     }
131     try{
132             in_stream=new BufferedInputStream(rsp.getInputStream());
133     }catch(Exception JavaDoc e){
134             org.jacorb.util.Debug.output(1,"This is a Client->Server HTTP Connection. ReadBuffer calls can only be done after some send call+receiveReply()");
135     }
136     //in_stream.mark(2048);
137
//int i;
138
//while((i=in_stream.read())>0){
139
// System.out.print(i+" ");
140
//}
141
//in_stream.reset();
142
return super.readBuffer();
143     }
144
145     //TODO!
146
public synchronized void reconnect()
147     throws org.omg.CORBA.COMM_FAILURE JavaDoc
148     {
149     org.jacorb.util.Debug.output(1,"Trying to reconnect to " + connection_info);
150     //send waiting buffers
151

152     }
153
154     public LocateReplyInputStream sendLocateRequest( LocateRequestOutputStream os )
155         throws org.omg.CORBA.COMM_FAILURE JavaDoc
156     {
157     synchronized( writeLock )
158     {
159             LocateReplyInputStream rep = null;
160             try
161         {
162                 rsp = null;
163         notifier=new Object JavaDoc();
164         repReceptor=new ReplyReceptor(this,true);
165         byte buf [] = os.getBufferCopy();
166         rep = new LocateReplyInputStream(this.orb, os.requestId());
167         Integer JavaDoc key = new Integer JavaDoc( os.requestId() );
168         buffers.put( key, os ); //changed in 1.1 from byte[]->CDR
169
replies.put( key, rep );
170         
171         synchronized(notifier){
172                     HTTPClient.HTTPConnection con = new HTTPClient.HTTPConnection("http://"+host+":"+port);
173                     rsp = con.Post("",buf);
174                     java.lang.Thread.yield();
175                     notifier.notifyAll(); //notify replyRepector about ready http-reply object
176
}
177             
178             
179             }
180         catch ( Exception JavaDoc e )
181         {
182         org.jacorb.util.Debug.output(2,e);
183         throw new org.omg.CORBA.COMM_FAILURE JavaDoc
184                     (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
185         }
186         return rep;
187     }
188     
189     }
190
191     /** writes the arguments of method calls to the output stream */
192     
193     public org.omg.CORBA.portable.InputStream JavaDoc sendRequest( org.omg.CORBA.Object JavaDoc o,
194                                RequestOutputStream os )
195         throws org.omg.CORBA.COMM_FAILURE JavaDoc{
196         synchronized( writeLock )
197     {
198             ReplyInputStream rep = null;
199
200         try
201         {
202                 rsp = null;
203             notifier=new Object JavaDoc();
204                 repReceptor=new ReplyReceptor(this,true);
205         byte buf [];
206         int size = -1;
207
208         //if( Environment.clientInterceptMessages())
209
//{
210
// org.jacorb.util.Debug.output(4, " passing buffer to interceptors " );
211
// buf = orb.client_messageIntercept_pre( o, os.getBufferCopy());
212
// size = buf.length;
213
//}
214
//else
215
//{
216
buf = os.getBufferCopy();
217                 size = buf.length;
218         //}
219

220         if( os.response_expected() )
221         {
222             rep = new ReplyInputStream(this.orb, os.requestId());
223             Integer JavaDoc key = new Integer JavaDoc( os.requestId() );
224             buffers.put( key, os );
225             replies.put( key, rep );
226         }
227         
228         synchronized(notifier){
229                     HTTPClient.HTTPConnection con = new HTTPClient.HTTPConnection(new URL("http://"+host+":"+port));
230                     rsp = con.Post("",buf);
231                     java.lang.Thread.yield();
232                     notifier.notifyAll(); //notify replyRepector about ready http-reply object
233
}
234                 
235
236             
237         }
238         catch ( Exception JavaDoc e )
239         {
240         org.jacorb.util.Debug.output(2,e);
241         throw new org.omg.CORBA.COMM_FAILURE JavaDoc
242                     (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
243         }
244         
245     
246             return rep;
247         }
248     }
249
250
251     public void setTimeOut(int timeout)
252     throws SocketException
253     {
254     //do nothing here
255
}
256
257     /*
258      * response handling has to be done by caller
259      */

260     public void writeDirectly(byte[] data,int len) throws IOException{
261     synchronized( writeLock )
262     {
263             org.jacorb.util.Debug.output(2,"Sending request with writeDirectly");
264         try
265         {
266      
267         //create buffer
268

269         notifier= new Object JavaDoc();
270         repReceptor=new ReplyReceptor(this,true);
271         byte buf []=new byte[len];
272         System.arraycopy(data,0,buf,0,len);
273                 int size = len;
274         
275         
276         synchronized(notifier){
277                     HTTPClient.HTTPConnection con = new HTTPClient.HTTPConnection(new URL("http://"+host+":"+port));
278                     rsp = con.Post("",buf);
279                     java.lang.Thread.yield();
280                     notifier.notifyAll(); //notify replyRepector about ready http-reply object
281
}
282                 
283
284         //org.jacorb.util.Debug.output(5,"sendreq",buf,size);
285

286             
287         }
288         catch ( Exception JavaDoc e )
289         {
290         org.jacorb.util.Debug.output(2,e);
291         throw new org.omg.CORBA.COMM_FAILURE JavaDoc
292                     (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
293         }
294         }
295     }
296 }
297
298
299
300
301
302
303
Popular Tags