1 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 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 notifier; 44 45 HTTPClient.HTTPResponse rsp; 47 String host=null; 48 int port; 49 50 private ORB orb = null; 51 52 public ClientConnection( String _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 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 (); 90 } 91 92 public synchronized void closeConnection() 93 { 94 connected=false; 95 } 96 97 public boolean connected() 98 { 99 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 113 public synchronized byte[] readBuffer() 114 throws IOException 115 { 116 117 while(repReceptor==null) { 118 try { 119 Thread.sleep(100); }catch(InterruptedException iex){} 121 } 122 123 124 synchronized(notifier){ 125 while(rsp==null){ 126 try{ 127 notifier.wait(); 128 }catch(InterruptedException iex){} 129 } 130 } 131 try{ 132 in_stream=new BufferedInputStream(rsp.getInputStream()); 133 }catch(Exception 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 return super.readBuffer(); 143 } 144 145 public synchronized void reconnect() 147 throws org.omg.CORBA.COMM_FAILURE 148 { 149 org.jacorb.util.Debug.output(1,"Trying to reconnect to " + connection_info); 150 152 } 153 154 public LocateReplyInputStream sendLocateRequest( LocateRequestOutputStream os ) 155 throws org.omg.CORBA.COMM_FAILURE 156 { 157 synchronized( writeLock ) 158 { 159 LocateReplyInputStream rep = null; 160 try 161 { 162 rsp = null; 163 notifier=new Object (); 164 repReceptor=new ReplyReceptor(this,true); 165 byte buf [] = os.getBufferCopy(); 166 rep = new LocateReplyInputStream(this.orb, os.requestId()); 167 Integer key = new Integer ( os.requestId() ); 168 buffers.put( key, os ); 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(); } 177 178 179 } 180 catch ( Exception e ) 181 { 182 org.jacorb.util.Debug.output(2,e); 183 throw new org.omg.CORBA.COMM_FAILURE 184 (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE); 185 } 186 return rep; 187 } 188 189 } 190 191 192 193 public org.omg.CORBA.portable.InputStream sendRequest( org.omg.CORBA.Object o, 194 RequestOutputStream os ) 195 throws org.omg.CORBA.COMM_FAILURE { 196 synchronized( writeLock ) 197 { 198 ReplyInputStream rep = null; 199 200 try 201 { 202 rsp = null; 203 notifier=new Object (); 204 repReceptor=new ReplyReceptor(this,true); 205 byte buf []; 206 int size = -1; 207 208 buf = os.getBufferCopy(); 217 size = buf.length; 218 220 if( os.response_expected() ) 221 { 222 rep = new ReplyInputStream(this.orb, os.requestId()); 223 Integer key = new Integer ( 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(); } 234 235 236 237 } 238 catch ( Exception e ) 239 { 240 org.jacorb.util.Debug.output(2,e); 241 throw new org.omg.CORBA.COMM_FAILURE 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 } 256 257 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 269 notifier= new Object (); 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(); } 282 283 284 286 287 } 288 catch ( Exception e ) 289 { 290 org.jacorb.util.Debug.output(2,e); 291 throw new org.omg.CORBA.COMM_FAILURE 292 (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE); 293 } 294 } 295 } 296 } 297 298 299 300 301 302 303 | Popular Tags |